Skip to content

Commit d294a42

Browse files
authored
Add getters/accessors for readable fields in ReadWriteLogRecord. (#6924)
1 parent a084b3a commit d294a42

File tree

4 files changed

+127
-8
lines changed

4 files changed

+127
-8
lines changed
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
11
Comparing source compatibility of opentelemetry-sdk-logs-1.46.0-SNAPSHOT.jar against opentelemetry-sdk-logs-1.45.0.jar
2-
No changes.
2+
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.logs.ReadWriteLogRecord (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++ NEW METHOD: PUBLIC(+) java.lang.Object getAttribute(io.opentelemetry.api.common.AttributeKey<T>)
5+
+++ NEW ANNOTATION: javax.annotation.Nullable
6+
GENERIC TEMPLATES: +++ T:java.lang.Object
7+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.common.Attributes getAttributes()
8+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.common.Value<?> getBodyValue()
9+
+++ NEW ANNOTATION: javax.annotation.Nullable
10+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.common.InstrumentationScopeInfo getInstrumentationScopeInfo()
11+
+++ NEW METHOD: PUBLIC(+) long getObservedTimestampEpochNanos()
12+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.Severity getSeverity()
13+
+++ NEW METHOD: PUBLIC(+) java.lang.String getSeverityText()
14+
+++ NEW ANNOTATION: javax.annotation.Nullable
15+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.trace.SpanContext getSpanContext()
16+
+++ NEW METHOD: PUBLIC(+) long getTimestampEpochNanos()

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

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
import io.opentelemetry.api.common.AttributeKey;
99
import io.opentelemetry.api.common.Attributes;
10+
import io.opentelemetry.api.common.Value;
11+
import io.opentelemetry.api.logs.Severity;
12+
import io.opentelemetry.api.trace.SpanContext;
13+
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
1014
import io.opentelemetry.sdk.logs.data.LogRecordData;
15+
import javax.annotation.Nullable;
1116

1217
/**
1318
* A log record that can be read from and written to.
@@ -47,7 +52,54 @@ default ReadWriteLogRecord setAllAttributes(Attributes attributes) {
4752
/** Return an immutable {@link LogRecordData} instance representing this log record. */
4853
LogRecordData toLogRecordData();
4954

50-
// TODO: add additional log record accessors. Currently, all fields can be accessed indirectly via
51-
// #toLogRecordData() at the expense of additional allocations.
55+
/**
56+
* Returns the value of a given attribute if it exists. This is the equivalent of calling
57+
* getAttributes().get(key)
58+
*/
59+
@Nullable
60+
default <T> T getAttribute(AttributeKey<T> key) {
61+
return toLogRecordData().getAttributes().get(key);
62+
}
63+
64+
/** Returns the instrumentation scope that generated this log. */
65+
default InstrumentationScopeInfo getInstrumentationScopeInfo() {
66+
return toLogRecordData().getInstrumentationScopeInfo();
67+
}
68+
69+
/** Returns the timestamp at which the log record occurred, in epoch nanos. */
70+
default long getTimestampEpochNanos() {
71+
return toLogRecordData().getTimestampEpochNanos();
72+
}
73+
74+
/** Returns the timestamp at which the log record was observed, in epoch nanos. */
75+
default long getObservedTimestampEpochNanos() {
76+
return toLogRecordData().getTimestampEpochNanos();
77+
}
5278

79+
/** Return the span context for this log, or {@link SpanContext#getInvalid()} if unset. */
80+
default SpanContext getSpanContext() {
81+
return toLogRecordData().getSpanContext();
82+
}
83+
84+
/** Returns the severity for this log, or {@link Severity#UNDEFINED_SEVERITY_NUMBER} if unset. */
85+
default Severity getSeverity() {
86+
return toLogRecordData().getSeverity();
87+
}
88+
89+
/** Returns the severity text for this log, or null if unset. */
90+
@Nullable
91+
default String getSeverityText() {
92+
return toLogRecordData().getSeverityText();
93+
}
94+
95+
/** Returns the {@link Value} representation of the log body, of null if unset. */
96+
@Nullable
97+
default Value<?> getBodyValue() {
98+
return toLogRecordData().getBodyValue();
99+
}
100+
101+
/** Returns the attributes for this log, or {@link Attributes#empty()} if unset. */
102+
default Attributes getAttributes() {
103+
return toLogRecordData().getAttributes();
104+
}
53105
}

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,57 @@ public LogRecordData toLogRecordData() {
125125
attributes == null ? 0 : attributes.getTotalAddedValues());
126126
}
127127
}
128+
129+
@Override
130+
public InstrumentationScopeInfo getInstrumentationScopeInfo() {
131+
return instrumentationScopeInfo;
132+
}
133+
134+
@Override
135+
public long getTimestampEpochNanos() {
136+
return timestampEpochNanos;
137+
}
138+
139+
@Override
140+
public long getObservedTimestampEpochNanos() {
141+
return observedTimestampEpochNanos;
142+
}
143+
144+
@Override
145+
public SpanContext getSpanContext() {
146+
return spanContext;
147+
}
148+
149+
@Override
150+
public Severity getSeverity() {
151+
return severity;
152+
}
153+
154+
@Nullable
155+
@Override
156+
public String getSeverityText() {
157+
return severityText;
158+
}
159+
160+
@Nullable
161+
@Override
162+
public Value<?> getBodyValue() {
163+
return body;
164+
}
165+
166+
@Override
167+
public Attributes getAttributes() {
168+
return getImmutableAttributes();
169+
}
170+
171+
@Nullable
172+
@Override
173+
public <T> T getAttribute(AttributeKey<T> key) {
174+
synchronized (lock) {
175+
if (attributes == null || attributes.isEmpty()) {
176+
return null;
177+
}
178+
return attributes.get(key);
179+
}
180+
}
128181
}

sdk/logs/src/test/java/io/opentelemetry/sdk/logs/ReadWriteLogRecordTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void addAllAttributes() {
2626

2727
logRecord.setAllAttributes(newAttributes);
2828

29-
Attributes result = logRecord.toLogRecordData().getAttributes();
29+
Attributes result = logRecord.getAttributes();
3030
assertThat(result.get(stringKey("foo"))).isEqualTo("bar");
3131
assertThat(result.get(stringKey("bar"))).isEqualTo("buzz");
3232
assertThat(result.get(stringKey("untouched"))).isEqualTo("yes");
@@ -35,17 +35,17 @@ void addAllAttributes() {
3535
@Test
3636
void addAllHandlesNull() {
3737
SdkReadWriteLogRecord logRecord = buildLogRecord();
38-
Attributes originalAttributes = logRecord.toLogRecordData().getAttributes();
38+
Attributes originalAttributes = logRecord.getAttributes();
3939
ReadWriteLogRecord result = logRecord.setAllAttributes(null);
40-
assertThat(result.toLogRecordData().getAttributes()).isEqualTo(originalAttributes);
40+
assertThat(result.getAttributes()).isEqualTo(originalAttributes);
4141
}
4242

4343
@Test
4444
void allHandlesEmpty() {
4545
SdkReadWriteLogRecord logRecord = buildLogRecord();
46-
Attributes originalAttributes = logRecord.toLogRecordData().getAttributes();
46+
Attributes originalAttributes = logRecord.getAttributes();
4747
ReadWriteLogRecord result = logRecord.setAllAttributes(Attributes.empty());
48-
assertThat(result.toLogRecordData().getAttributes()).isEqualTo(originalAttributes);
48+
assertThat(result.getAttributes()).isEqualTo(originalAttributes);
4949
}
5050

5151
SdkReadWriteLogRecord buildLogRecord() {

0 commit comments

Comments
 (0)