Add getters/accessors for readable fields in ReadWriteLogRecord. (#6924)
This commit is contained in:
parent
a084b3ae0a
commit
d294a42afb
|
@ -1,2 +1,16 @@
|
||||||
Comparing source compatibility of opentelemetry-sdk-logs-1.46.0-SNAPSHOT.jar against opentelemetry-sdk-logs-1.45.0.jar
|
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()
|
||||||
|
|
|
@ -7,7 +7,12 @@ package io.opentelemetry.sdk.logs;
|
||||||
|
|
||||||
import io.opentelemetry.api.common.AttributeKey;
|
import io.opentelemetry.api.common.AttributeKey;
|
||||||
import io.opentelemetry.api.common.Attributes;
|
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 io.opentelemetry.sdk.logs.data.LogRecordData;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A log record that can be read from and written to.
|
* A log record that can be read from and written to.
|
||||||
|
@ -47,7 +52,54 @@ public interface ReadWriteLogRecord {
|
||||||
/** Return an immutable {@link LogRecordData} instance representing this log record. */
|
/** Return an immutable {@link LogRecordData} instance representing this log record. */
|
||||||
LogRecordData toLogRecordData();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,4 +125,57 @@ class SdkReadWriteLogRecord implements ReadWriteLogRecord {
|
||||||
attributes == null ? 0 : attributes.getTotalAddedValues());
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ class ReadWriteLogRecordTest {
|
||||||
|
|
||||||
logRecord.setAllAttributes(newAttributes);
|
logRecord.setAllAttributes(newAttributes);
|
||||||
|
|
||||||
Attributes result = logRecord.toLogRecordData().getAttributes();
|
Attributes result = logRecord.getAttributes();
|
||||||
assertThat(result.get(stringKey("foo"))).isEqualTo("bar");
|
assertThat(result.get(stringKey("foo"))).isEqualTo("bar");
|
||||||
assertThat(result.get(stringKey("bar"))).isEqualTo("buzz");
|
assertThat(result.get(stringKey("bar"))).isEqualTo("buzz");
|
||||||
assertThat(result.get(stringKey("untouched"))).isEqualTo("yes");
|
assertThat(result.get(stringKey("untouched"))).isEqualTo("yes");
|
||||||
|
@ -35,17 +35,17 @@ class ReadWriteLogRecordTest {
|
||||||
@Test
|
@Test
|
||||||
void addAllHandlesNull() {
|
void addAllHandlesNull() {
|
||||||
SdkReadWriteLogRecord logRecord = buildLogRecord();
|
SdkReadWriteLogRecord logRecord = buildLogRecord();
|
||||||
Attributes originalAttributes = logRecord.toLogRecordData().getAttributes();
|
Attributes originalAttributes = logRecord.getAttributes();
|
||||||
ReadWriteLogRecord result = logRecord.setAllAttributes(null);
|
ReadWriteLogRecord result = logRecord.setAllAttributes(null);
|
||||||
assertThat(result.toLogRecordData().getAttributes()).isEqualTo(originalAttributes);
|
assertThat(result.getAttributes()).isEqualTo(originalAttributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void allHandlesEmpty() {
|
void allHandlesEmpty() {
|
||||||
SdkReadWriteLogRecord logRecord = buildLogRecord();
|
SdkReadWriteLogRecord logRecord = buildLogRecord();
|
||||||
Attributes originalAttributes = logRecord.toLogRecordData().getAttributes();
|
Attributes originalAttributes = logRecord.getAttributes();
|
||||||
ReadWriteLogRecord result = logRecord.setAllAttributes(Attributes.empty());
|
ReadWriteLogRecord result = logRecord.setAllAttributes(Attributes.empty());
|
||||||
assertThat(result.toLogRecordData().getAttributes()).isEqualTo(originalAttributes);
|
assertThat(result.getAttributes()).isEqualTo(originalAttributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
SdkReadWriteLogRecord buildLogRecord() {
|
SdkReadWriteLogRecord buildLogRecord() {
|
||||||
|
|
Loading…
Reference in New Issue