Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getters/accessors for readable fields in ReadWriteLogRecord. #6924

Merged
merged 11 commits into from
Dec 19, 2024
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 @@
/** 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);

Check warning on line 61 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java#L61

Added line #L61 was not covered by tests

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be worth adding some unit tests for these default methods? It could be beneficial for possible future classes that do not overwrite them (or overwrite them partially)

}

/** Returns the instrumentation scope that generated this log. */
default InstrumentationScopeInfo getInstrumentationScopeInfo() {
return toLogRecordData().getInstrumentationScopeInfo();

Check warning on line 66 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java#L66

Added line #L66 was not covered by tests
}

/** Returns the timestamp at which the log record occurred, in epoch nanos. */
default long getTimestampEpochNanos() {
return toLogRecordData().getTimestampEpochNanos();

Check warning on line 71 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java#L71

Added line #L71 was not covered by tests
}

/** Returns the timestamp at which the log record was observed, in epoch nanos. */
default long getObservedTimestampEpochNanos() {
return toLogRecordData().getTimestampEpochNanos();

Check warning on line 76 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java#L76

Added line #L76 was not covered by tests
}

/** Return the span context for this log, or {@link SpanContext#getInvalid()} if unset. */
default SpanContext getSpanContext() {
return toLogRecordData().getSpanContext();

Check warning on line 81 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java#L81

Added line #L81 was not covered by tests
}

/** Returns the severity for this log, or {@link Severity#UNDEFINED_SEVERITY_NUMBER} if unset. */
default Severity getSeverity() {
return toLogRecordData().getSeverity();

Check warning on line 86 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java#L86

Added line #L86 was not covered by tests
}

/** Returns the severity text for this log, or null if unset. */
@Nullable
default String getSeverityText() {
return toLogRecordData().getSeverityText();

Check warning on line 92 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java#L92

Added line #L92 was not covered by tests
}

/** Returns the {@link Value} representation of the log body, of null if unset. */
@Nullable
default Value<?> getBodyValue() {
return toLogRecordData().getBodyValue();

Check warning on line 98 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java#L98

Added line #L98 was not covered by tests
}

/** Returns the attributes for this log, or {@link Attributes#empty()} if unset. */
default Attributes getAttributes() {
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved
return toLogRecordData().getAttributes();

Check warning on line 103 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ReadWriteLogRecord.java#L103

Added line #L103 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,57 @@
attributes == null ? 0 : attributes.getTotalAddedValues());
}
}

@Override
public InstrumentationScopeInfo getInstrumentationScopeInfo() {
return instrumentationScopeInfo;

Check warning on line 131 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java#L131

Added line #L131 was not covered by tests
}

@Override
public long getTimestampEpochNanos() {
return timestampEpochNanos;

Check warning on line 136 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java#L136

Added line #L136 was not covered by tests
}

@Override
public long getObservedTimestampEpochNanos() {
return observedTimestampEpochNanos;

Check warning on line 141 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java#L141

Added line #L141 was not covered by tests
}

@Override
public SpanContext getSpanContext() {
return spanContext;

Check warning on line 146 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java#L146

Added line #L146 was not covered by tests
}

@Override
public Severity getSeverity() {
return severity;

Check warning on line 151 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java#L151

Added line #L151 was not covered by tests
}

@Nullable
@Override
public String getSeverityText() {
return severityText;

Check warning on line 157 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java#L157

Added line #L157 was not covered by tests
}

@Nullable
@Override
public Value<?> getBodyValue() {
return body;

Check warning on line 163 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java#L163

Added line #L163 was not covered by tests
}

@Override
public Attributes getAttributes() {
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved
return getImmutableAttributes();
}

@Nullable
@Override
public <T> T getAttribute(AttributeKey<T> key) {
synchronized (lock) {

Check warning on line 174 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java#L174

Added line #L174 was not covered by tests
if (attributes == null || attributes.isEmpty()) {
return null;

Check warning on line 176 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java#L176

Added line #L176 was not covered by tests
}
return attributes.get(key);

Check warning on line 178 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java

View check run for this annotation

Codecov / codecov/patch

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java#L178

Added line #L178 was not covered by tests
}
}
}
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
Loading