Skip to content

Commit

Permalink
Add getters/accessors for readable fields in ReadWriteLogRecord. (#6924)
Browse files Browse the repository at this point in the history
  • Loading branch information
breedx-splk authored Dec 19, 2024
1 parent a084b3a commit d294a42
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 8 deletions.
16 changes: 15 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt
Original file line number Diff line number Diff line change
@@ -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.
*** 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<T>)
+++ 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()
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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> T getAttribute(AttributeKey<T> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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> T getAttribute(AttributeKey<T> key) {
synchronized (lock) {
if (attributes == null || attributes.isEmpty()) {
return null;
}
return attributes.get(key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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() {
Expand Down

0 comments on commit d294a42

Please sign in to comment.