diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt b/docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt index 3cee338a2c6..dca61614aaf 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt @@ -1,2 +1,16 @@ Comparing source compatibility of opentelemetry-sdk-logs-1.46.0-SNAPSHOT.jar against opentelemetry-sdk-logs-1.45.0.jar -No changes. \ No newline at end of file +*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.logs.ReadWriteLogRecord (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + +++ NEW METHOD: PUBLIC(+) java.lang.Object getAttribute(io.opentelemetry.api.common.AttributeKey) + +++ NEW ANNOTATION: javax.annotation.Nullable + GENERIC TEMPLATES: +++ T:java.lang.Object + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.common.Attributes getAttributes() + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.common.Value getBodyValue() + +++ NEW ANNOTATION: javax.annotation.Nullable + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.common.InstrumentationScopeInfo getInstrumentationScopeInfo() + +++ NEW METHOD: PUBLIC(+) long getObservedTimestampEpochNanos() + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.Severity getSeverity() + +++ NEW METHOD: PUBLIC(+) java.lang.String getSeverityText() + +++ NEW ANNOTATION: javax.annotation.Nullable + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.trace.SpanContext getSpanContext() + +++ NEW METHOD: PUBLIC(+) long getTimestampEpochNanos() diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java index d99267d9acd..6ad913c1029 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java @@ -7,7 +7,12 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.common.Value; +import io.opentelemetry.api.logs.Severity; +import io.opentelemetry.api.trace.SpanContext; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.logs.data.LogRecordData; +import javax.annotation.Nullable; /** * A log record that can be read from and written to. @@ -47,7 +52,54 @@ default ReadWriteLogRecord setAllAttributes(Attributes attributes) { /** Return an immutable {@link LogRecordData} instance representing this log record. */ LogRecordData toLogRecordData(); - // TODO: add additional log record accessors. Currently, all fields can be accessed indirectly via - // #toLogRecordData() at the expense of additional allocations. + /** + * Returns the value of a given attribute if it exists. This is the equivalent of calling + * getAttributes().get(key) + */ + @Nullable + default T getAttribute(AttributeKey key) { + return toLogRecordData().getAttributes().get(key); + } + + /** Returns the instrumentation scope that generated this log. */ + default InstrumentationScopeInfo getInstrumentationScopeInfo() { + return toLogRecordData().getInstrumentationScopeInfo(); + } + + /** Returns the timestamp at which the log record occurred, in epoch nanos. */ + default long getTimestampEpochNanos() { + return toLogRecordData().getTimestampEpochNanos(); + } + + /** Returns the timestamp at which the log record was observed, in epoch nanos. */ + default long getObservedTimestampEpochNanos() { + return toLogRecordData().getTimestampEpochNanos(); + } + /** Return the span context for this log, or {@link SpanContext#getInvalid()} if unset. */ + default SpanContext getSpanContext() { + return toLogRecordData().getSpanContext(); + } + + /** Returns the severity for this log, or {@link Severity#UNDEFINED_SEVERITY_NUMBER} if unset. */ + default Severity getSeverity() { + return toLogRecordData().getSeverity(); + } + + /** Returns the severity text for this log, or null if unset. */ + @Nullable + default String getSeverityText() { + return toLogRecordData().getSeverityText(); + } + + /** Returns the {@link Value} representation of the log body, of null if unset. */ + @Nullable + default Value getBodyValue() { + return toLogRecordData().getBodyValue(); + } + + /** Returns the attributes for this log, or {@link Attributes#empty()} if unset. */ + default Attributes getAttributes() { + return toLogRecordData().getAttributes(); + } } diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java index 881f1d124ee..e6c68ce6ec4 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java @@ -125,4 +125,57 @@ public LogRecordData toLogRecordData() { attributes == null ? 0 : attributes.getTotalAddedValues()); } } + + @Override + public InstrumentationScopeInfo getInstrumentationScopeInfo() { + return instrumentationScopeInfo; + } + + @Override + public long getTimestampEpochNanos() { + return timestampEpochNanos; + } + + @Override + public long getObservedTimestampEpochNanos() { + return observedTimestampEpochNanos; + } + + @Override + public SpanContext getSpanContext() { + return spanContext; + } + + @Override + public Severity getSeverity() { + return severity; + } + + @Nullable + @Override + public String getSeverityText() { + return severityText; + } + + @Nullable + @Override + public Value getBodyValue() { + return body; + } + + @Override + public Attributes getAttributes() { + return getImmutableAttributes(); + } + + @Nullable + @Override + public T getAttribute(AttributeKey key) { + synchronized (lock) { + if (attributes == null || attributes.isEmpty()) { + return null; + } + return attributes.get(key); + } + } } diff --git a/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/ReadWriteLogRecordTest.java b/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/ReadWriteLogRecordTest.java index e671372c515..7a444817d40 100644 --- a/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/ReadWriteLogRecordTest.java +++ b/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/ReadWriteLogRecordTest.java @@ -26,7 +26,7 @@ void addAllAttributes() { logRecord.setAllAttributes(newAttributes); - Attributes result = logRecord.toLogRecordData().getAttributes(); + Attributes result = logRecord.getAttributes(); assertThat(result.get(stringKey("foo"))).isEqualTo("bar"); assertThat(result.get(stringKey("bar"))).isEqualTo("buzz"); assertThat(result.get(stringKey("untouched"))).isEqualTo("yes"); @@ -35,17 +35,17 @@ void addAllAttributes() { @Test void addAllHandlesNull() { SdkReadWriteLogRecord logRecord = buildLogRecord(); - Attributes originalAttributes = logRecord.toLogRecordData().getAttributes(); + Attributes originalAttributes = logRecord.getAttributes(); ReadWriteLogRecord result = logRecord.setAllAttributes(null); - assertThat(result.toLogRecordData().getAttributes()).isEqualTo(originalAttributes); + assertThat(result.getAttributes()).isEqualTo(originalAttributes); } @Test void allHandlesEmpty() { SdkReadWriteLogRecord logRecord = buildLogRecord(); - Attributes originalAttributes = logRecord.toLogRecordData().getAttributes(); + Attributes originalAttributes = logRecord.getAttributes(); ReadWriteLogRecord result = logRecord.setAllAttributes(Attributes.empty()); - assertThat(result.toLogRecordData().getAttributes()).isEqualTo(originalAttributes); + assertThat(result.getAttributes()).isEqualTo(originalAttributes); } SdkReadWriteLogRecord buildLogRecord() {