Skip to content

Commit 9e1e9c6

Browse files
authored
Stabilize log record event name (#7277)
1 parent 6547279 commit 9e1e9c6

File tree

24 files changed

+133
-57
lines changed

24 files changed

+133
-57
lines changed

api/all/src/main/java/io/opentelemetry/api/logs/LogRecordBuilder.java

+10
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ default LogRecordBuilder setAttribute(String key, int value) {
198198
return setAttribute(key, (long) value);
199199
}
200200

201+
/**
202+
* Sets the event name, which identifies the class / type of the Event.
203+
*
204+
* <p>This name should uniquely identify the event structure (both attributes and body). A log
205+
* record with a non-empty event name is an Event.
206+
*/
207+
default LogRecordBuilder setEventName(String eventName) {
208+
return this;
209+
}
210+
201211
/** Emit the log record. */
202212
void emit();
203213
}

api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedLogRecordBuilder.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ default ExtendedLogRecordBuilder setBody(Value<?> body) {
6161
return this;
6262
}
6363

64+
/**
65+
* Sets the event name, which identifies the class / type of the Event.
66+
*
67+
* <p>This name should uniquely identify the event structure (both attributes and body). A log
68+
* record with a non-empty event name is an Event.
69+
*/
70+
@Override
71+
ExtendedLogRecordBuilder setEventName(String eventName);
72+
6473
/**
6574
* {@inheritDoc}
6675
*
@@ -124,14 +133,6 @@ default ExtendedLogRecordBuilder setAllAttributes(ExtendedAttributes attributes)
124133
*/
125134
<T> ExtendedLogRecordBuilder setAttribute(ExtendedAttributeKey<T> key, T value);
126135

127-
/**
128-
* Sets the event name, which identifies the class / type of the Event.
129-
*
130-
* <p>This name should uniquely identify the event structure (both attributes and body). A log
131-
* record with a non-empty event name is an Event.
132-
*/
133-
ExtendedLogRecordBuilder setEventName(String eventName);
134-
135136
/** Set standard {@code exception.*} attributes based on the {@code throwable}. */
136137
ExtendedLogRecordBuilder setException(Throwable throwable);
137138
}

api/testing-internal/src/main/java/io/opentelemetry/api/testing/internal/AbstractDefaultLoggerTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ void buildAndEmit() {
5151
() ->
5252
getLogger()
5353
.logRecordBuilder()
54-
// TODO (trask) once event name stabilizes
55-
// .setEventName("event name")
54+
.setEventName("event name")
5655
.setTimestamp(100, TimeUnit.SECONDS)
5756
.setTimestamp(Instant.now())
5857
.setObservedTimestamp(100, TimeUnit.SECONDS)
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Comparing source compatibility of opentelemetry-api-1.50.0-SNAPSHOT.jar against opentelemetry-api-1.49.0.jar
2-
No changes.
2+
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.logs.LogRecordBuilder (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setEventName(java.lang.String)
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
Comparing source compatibility of opentelemetry-sdk-logs-1.50.0-SNAPSHOT.jar against opentelemetry-sdk-logs-1.49.0.jar
2-
No changes.
2+
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.logs.data.LogRecordData (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++ NEW METHOD: PUBLIC(+) java.lang.String getEventName()
5+
+++ NEW ANNOTATION: javax.annotation.Nullable
6+
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.logs.ReadWriteLogRecord (not serializable)
7+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
8+
+++ NEW METHOD: PUBLIC(+) java.lang.String getEventName()
9+
+++ NEW ANNOTATION: javax.annotation.Nullable

docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
Comparing source compatibility of opentelemetry-sdk-testing-1.50.0-SNAPSHOT.jar against opentelemetry-sdk-testing-1.49.0.jar
2+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.LogRecordDataAssert (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.LogRecordDataAssert hasEventName(java.lang.String)
5+
**** MODIFIED CLASS: PUBLIC ABSTRACT io.opentelemetry.sdk.testing.logs.TestLogRecordData (not serializable)
6+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
7+
+++* NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String getEventName()
8+
+++ NEW ANNOTATION: javax.annotation.Nullable
9+
**** MODIFIED CLASS: PUBLIC ABSTRACT STATIC io.opentelemetry.sdk.testing.logs.TestLogRecordData$Builder (not serializable)
10+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
11+
+++* NEW METHOD: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.testing.logs.TestLogRecordData$Builder setEventName(java.lang.String)
212
+++ NEW CLASS: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.testing.metrics.TestMetricData (not serializable)
313
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
414
+++ NEW SUPERCLASS: java.lang.Object

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/logs/LogMarshaler.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import io.opentelemetry.proto.logs.v1.internal.LogRecord;
2121
import io.opentelemetry.proto.logs.v1.internal.SeverityNumber;
2222
import io.opentelemetry.sdk.logs.data.LogRecordData;
23-
import io.opentelemetry.sdk.logs.data.internal.ExtendedLogRecordData;
2423
import java.io.IOException;
2524
import javax.annotation.Nullable;
2625

@@ -40,7 +39,6 @@ final class LogMarshaler extends MarshalerWithSize {
4039

4140
private static final String INVALID_TRACE_ID = TraceId.getInvalid();
4241
private static final String INVALID_SPAN_ID = SpanId.getInvalid();
43-
private static final byte[] EMPTY_BYTES = new byte[0];
4442

4543
private final long timeUnixNano;
4644
private final long observedTimeUnixNano;
@@ -82,9 +80,7 @@ static LogMarshaler create(LogRecordData logRecordData) {
8280
spanContext.getTraceFlags(),
8381
spanContext.getTraceId().equals(INVALID_TRACE_ID) ? null : spanContext.getTraceId(),
8482
spanContext.getSpanId().equals(INVALID_SPAN_ID) ? null : spanContext.getSpanId(),
85-
logRecordData instanceof ExtendedLogRecordData
86-
? MarshalerUtil.toBytes(((ExtendedLogRecordData) logRecordData).getEventName())
87-
: EMPTY_BYTES);
83+
MarshalerUtil.toBytes(logRecordData.getEventName()));
8884
}
8985

9086
private LogMarshaler(

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/logs/LogStatelessMarshaler.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import io.opentelemetry.exporter.internal.otlp.IncubatingUtil;
2121
import io.opentelemetry.proto.logs.v1.internal.LogRecord;
2222
import io.opentelemetry.sdk.logs.data.LogRecordData;
23-
import io.opentelemetry.sdk.logs.data.internal.ExtendedLogRecordData;
2423
import java.io.IOException;
2524

2625
/** See {@link LogMarshaler}. */
@@ -79,10 +78,7 @@ public void writeTo(Serializer output, LogRecordData log, MarshalerContext conte
7978
if (!spanContext.getSpanId().equals(INVALID_SPAN_ID)) {
8079
output.serializeSpanId(LogRecord.SPAN_ID, spanContext.getSpanId(), context);
8180
}
82-
if (log instanceof ExtendedLogRecordData) {
83-
output.serializeStringWithContext(
84-
LogRecord.EVENT_NAME, ((ExtendedLogRecordData) log).getEventName(), context);
85-
}
81+
output.serializeStringWithContext(LogRecord.EVENT_NAME, log.getEventName(), context);
8682
}
8783

8884
@Override
@@ -130,11 +126,9 @@ public int getBinarySerializedSize(LogRecordData log, MarshalerContext context)
130126
size += MarshalerUtil.sizeSpanId(LogRecord.SPAN_ID, spanContext.getSpanId());
131127
}
132128

133-
if (log instanceof ExtendedLogRecordData) {
134-
size +=
135-
StatelessMarshalerUtil.sizeStringWithContext(
136-
LogRecord.EVENT_NAME, ((ExtendedLogRecordData) log).getEventName(), context);
137-
}
129+
size +=
130+
StatelessMarshalerUtil.sizeStringWithContext(
131+
LogRecord.EVENT_NAME, log.getEventName(), context);
138132

139133
return size;
140134
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
final class ExtendedSdkLogRecordBuilder extends SdkLogRecordBuilder
2424
implements ExtendedLogRecordBuilder {
2525

26-
@Nullable private String eventName;
2726
@Nullable private ExtendedAttributesMap extendedAttributes;
2827

2928
ExtendedSdkLogRecordBuilder(
@@ -33,7 +32,7 @@ final class ExtendedSdkLogRecordBuilder extends SdkLogRecordBuilder
3332

3433
@Override
3534
public ExtendedSdkLogRecordBuilder setEventName(String eventName) {
36-
this.eventName = eventName;
35+
super.setEventName(eventName);
3736
return this;
3837
}
3938

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,19 @@ static ExtendedSdkLogRecordData create(
4444
severity,
4545
severityText,
4646
totalAttributeCount,
47-
eventName,
4847
attributes,
49-
body);
48+
body,
49+
eventName);
5050
}
5151

5252
@Override
5353
@Nullable
5454
public abstract Value<?> getBodyValue();
5555

56+
@Override
57+
@Nullable
58+
public abstract String getEventName();
59+
5660
@Override
5761
@SuppressWarnings("deprecation") // Implementation of deprecated method
5862
public io.opentelemetry.sdk.logs.data.Body getBody() {

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
class ExtendedSdkReadWriteLogRecord extends SdkReadWriteLogRecord
2626
implements ExtendedReadWriteLogRecord {
2727

28-
@Nullable private final String eventName;
2928
private final Object lock = new Object();
3029

3130
@GuardedBy("lock")
@@ -55,8 +54,8 @@ private ExtendedSdkReadWriteLogRecord(
5554
severity,
5655
severityText,
5756
body,
58-
null);
59-
this.eventName = eventName;
57+
null,
58+
eventName);
6059
this.extendedAttributes = extendedAttributes;
6160
}
6261

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ default ReadWriteLogRecord setAllAttributes(Attributes attributes) {
5252
/** Return an immutable {@link LogRecordData} instance representing this log record. */
5353
LogRecordData toLogRecordData();
5454

55-
// TODO (trask) once event name stabilizes, add getEventName()
55+
/** Returns the log record event name, or {@code null} if none is set. */
56+
@Nullable
57+
default String getEventName() {
58+
return null;
59+
}
5660

5761
/**
5862
* Returns the value of a given attribute if it exists. This is the equivalent of calling {@code

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class SdkLogRecordBuilder implements LogRecordBuilder {
3030
protected Severity severity = Severity.UNDEFINED_SEVERITY_NUMBER;
3131
@Nullable protected String severityText;
3232
@Nullable protected Value<?> body;
33+
@Nullable protected String eventName;
3334
@Nullable private AttributesMap attributes;
3435

3536
SdkLogRecordBuilder(
@@ -39,6 +40,12 @@ class SdkLogRecordBuilder implements LogRecordBuilder {
3940
this.instrumentationScopeInfo = instrumentationScopeInfo;
4041
}
4142

43+
@Override
44+
public SdkLogRecordBuilder setEventName(String eventName) {
45+
this.eventName = eventName;
46+
return this;
47+
}
48+
4249
@Override
4350
public SdkLogRecordBuilder setTimestamp(long timestamp, TimeUnit unit) {
4451
this.timestampEpochNanos = unit.toNanos(timestamp);
@@ -132,6 +139,7 @@ public void emit() {
132139
severity,
133140
severityText,
134141
body,
135-
attributes));
142+
attributes,
143+
eventName));
136144
}
137145
}

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ static SdkLogRecordData create(
3333
@Nullable String severityText,
3434
@Nullable Value<?> body,
3535
Attributes attributes,
36-
int totalAttributeCount) {
36+
int totalAttributeCount,
37+
@Nullable String eventName) {
3738
return new AutoValue_SdkLogRecordData(
3839
resource,
3940
instrumentationScopeInfo,
@@ -44,13 +45,18 @@ static SdkLogRecordData create(
4445
severityText,
4546
attributes,
4647
totalAttributeCount,
47-
body);
48+
body,
49+
eventName);
4850
}
4951

5052
@Override
5153
@Nullable
5254
public abstract Value<?> getBodyValue();
5355

56+
@Override
57+
@Nullable
58+
public abstract String getEventName();
59+
5460
@Override
5561
@SuppressWarnings("deprecation") // Implementation of deprecated method
5662
public io.opentelemetry.sdk.logs.data.Body getBody() {

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class SdkReadWriteLogRecord implements ReadWriteLogRecord {
3030
protected final Severity severity;
3131
@Nullable protected final String severityText;
3232
@Nullable protected final Value<?> body;
33+
@Nullable protected String eventName;
3334
private final Object lock = new Object();
3435

3536
@GuardedBy("lock")
@@ -46,7 +47,8 @@ protected SdkReadWriteLogRecord(
4647
Severity severity,
4748
@Nullable String severityText,
4849
@Nullable Value<?> body,
49-
@Nullable AttributesMap attributes) {
50+
@Nullable AttributesMap attributes,
51+
@Nullable String eventName) {
5052
this.logLimits = logLimits;
5153
this.resource = resource;
5254
this.instrumentationScopeInfo = instrumentationScopeInfo;
@@ -56,6 +58,7 @@ protected SdkReadWriteLogRecord(
5658
this.severity = severity;
5759
this.severityText = severityText;
5860
this.body = body;
61+
this.eventName = eventName;
5962
this.attributes = attributes;
6063
}
6164

@@ -70,7 +73,8 @@ static SdkReadWriteLogRecord create(
7073
Severity severity,
7174
@Nullable String severityText,
7275
@Nullable Value<?> body,
73-
@Nullable AttributesMap attributes) {
76+
@Nullable AttributesMap attributes,
77+
@Nullable String eventName) {
7478
return new SdkReadWriteLogRecord(
7579
logLimits,
7680
resource,
@@ -81,7 +85,8 @@ static SdkReadWriteLogRecord create(
8185
severity,
8286
severityText,
8387
body,
84-
attributes);
88+
attributes,
89+
eventName);
8590
}
8691

8792
@Override
@@ -122,7 +127,8 @@ public LogRecordData toLogRecordData() {
122127
severityText,
123128
body,
124129
getImmutableAttributes(),
125-
attributes == null ? 0 : attributes.getTotalAddedValues());
130+
attributes == null ? 0 : attributes.getTotalAddedValues(),
131+
eventName);
126132
}
127133
}
128134

@@ -168,6 +174,12 @@ public Attributes getAttributes() {
168174
return getImmutableAttributes();
169175
}
170176

177+
@Override
178+
@Nullable
179+
public String getEventName() {
180+
return eventName;
181+
}
182+
171183
@Nullable
172184
@Override
173185
public <T> T getAttribute(AttributeKey<T> key) {

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/data/LogRecordData.java

+6
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,10 @@ default Value<?> getBodyValue() {
8282
* LogLimits#getMaxNumberOfAttributes()}.
8383
*/
8484
int getTotalAttributeCount();
85+
86+
/** Returns the event name, or {@code null} if none is set. */
87+
@Nullable
88+
default String getEventName() {
89+
return null;
90+
}
8591
}

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/data/internal/ExtendedLogRecordData.java

-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import io.opentelemetry.api.common.Attributes;
99
import io.opentelemetry.api.incubator.common.ExtendedAttributes;
1010
import io.opentelemetry.sdk.logs.data.LogRecordData;
11-
import javax.annotation.Nullable;
1211

1312
/**
1413
* This class is internal and experimental. Its APIs are unstable and can change at any time. Its
@@ -17,9 +16,6 @@
1716
*/
1817
public interface ExtendedLogRecordData extends LogRecordData {
1918

20-
@Nullable
21-
String getEventName();
22-
2319
/** Returns the attributes for this log, or {@link ExtendedAttributes#empty()} if unset. */
2420
ExtendedAttributes getExtendedAttributes();
2521

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ SdkReadWriteLogRecord buildLogRecord() {
6868
Severity.DEBUG,
6969
"buggin",
7070
body,
71-
initialAttributes);
71+
initialAttributes,
72+
"my.event.name");
7273
}
7374
}

0 commit comments

Comments
 (0)