Skip to content

Stabilize log record event name #7277

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

Merged
merged 3 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ default LogRecordBuilder setAttribute(String key, int value) {
return setAttribute(key, (long) value);
}

/**
* Sets the event name, which identifies the class / type of the Event.
*
* <p>This name should uniquely identify the event structure (both attributes and body). A log
* record with a non-empty event name is an Event.
*/
default LogRecordBuilder setEventName(String eventName) {
return this;
}

/** Emit the log record. */
void emit();
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ default ExtendedLogRecordBuilder setBody(Value<?> body) {
return this;
}

/**
* Sets the event name, which identifies the class / type of the Event.
*
* <p>This name should uniquely identify the event structure (both attributes and body). A log
* record with a non-empty event name is an Event.
*/
@Override
ExtendedLogRecordBuilder setEventName(String eventName);

/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -123,14 +132,6 @@ default ExtendedLogRecordBuilder setAllAttributes(ExtendedAttributes attributes)
*/
<T> ExtendedLogRecordBuilder setAttribute(ExtendedAttributeKey<T> key, T value);

/**
* Sets the event name, which identifies the class / type of the Event.
*
* <p>This name should uniquely identify the event structure (both attributes and body). A log
* record with a non-empty event name is an Event.
*/
ExtendedLogRecordBuilder setEventName(String eventName);

/** Set standard {@code exception.*} attributes based on the {@code throwable}. */
ExtendedLogRecordBuilder setException(Throwable throwable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ void buildAndEmit() {
() ->
getLogger()
.logRecordBuilder()
// TODO (trask) once event name stabilizes
// .setEventName("event name")
.setEventName("event name")
.setTimestamp(100, TimeUnit.SECONDS)
.setTimestamp(Instant.now())
.setObservedTimestamp(100, TimeUnit.SECONDS)
Expand Down
4 changes: 3 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-api.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Comparing source compatibility of opentelemetry-api-1.50.0-SNAPSHOT.jar against opentelemetry-api-1.49.0.jar
No changes.
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.logs.LogRecordBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setEventName(java.lang.String)
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
Comparing source compatibility of opentelemetry-sdk-logs-1.50.0-SNAPSHOT.jar against opentelemetry-sdk-logs-1.49.0.jar
No changes.
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.logs.data.LogRecordData (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) java.lang.String getEventName()
+++ NEW ANNOTATION: javax.annotation.Nullable
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.logs.ReadWriteLogRecord (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) java.lang.String getEventName()
+++ NEW ANNOTATION: javax.annotation.Nullable
10 changes: 10 additions & 0 deletions docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
Comparing source compatibility of opentelemetry-sdk-testing-1.50.0-SNAPSHOT.jar against opentelemetry-sdk-testing-1.49.0.jar
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.LogRecordDataAssert (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.LogRecordDataAssert hasEventName(java.lang.String)
**** MODIFIED CLASS: PUBLIC ABSTRACT io.opentelemetry.sdk.testing.logs.TestLogRecordData (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++* NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String getEventName()
+++ NEW ANNOTATION: javax.annotation.Nullable
**** MODIFIED CLASS: PUBLIC ABSTRACT STATIC io.opentelemetry.sdk.testing.logs.TestLogRecordData$Builder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++* NEW METHOD: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.testing.logs.TestLogRecordData$Builder setEventName(java.lang.String)
+++ NEW CLASS: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.testing.metrics.TestMetricData (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW SUPERCLASS: java.lang.Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.opentelemetry.proto.logs.v1.internal.LogRecord;
import io.opentelemetry.proto.logs.v1.internal.SeverityNumber;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.logs.data.internal.ExtendedLogRecordData;
import java.io.IOException;
import javax.annotation.Nullable;

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

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

private final long timeUnixNano;
private final long observedTimeUnixNano;
Expand Down Expand Up @@ -82,9 +80,7 @@ static LogMarshaler create(LogRecordData logRecordData) {
spanContext.getTraceFlags(),
spanContext.getTraceId().equals(INVALID_TRACE_ID) ? null : spanContext.getTraceId(),
spanContext.getSpanId().equals(INVALID_SPAN_ID) ? null : spanContext.getSpanId(),
logRecordData instanceof ExtendedLogRecordData
? MarshalerUtil.toBytes(((ExtendedLogRecordData) logRecordData).getEventName())
: EMPTY_BYTES);
MarshalerUtil.toBytes(logRecordData.getEventName()));
}

private LogMarshaler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.opentelemetry.exporter.internal.otlp.IncubatingUtil;
import io.opentelemetry.proto.logs.v1.internal.LogRecord;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.logs.data.internal.ExtendedLogRecordData;
import java.io.IOException;

/** See {@link LogMarshaler}. */
Expand Down Expand Up @@ -79,10 +78,7 @@ public void writeTo(Serializer output, LogRecordData log, MarshalerContext conte
if (!spanContext.getSpanId().equals(INVALID_SPAN_ID)) {
output.serializeSpanId(LogRecord.SPAN_ID, spanContext.getSpanId(), context);
}
if (log instanceof ExtendedLogRecordData) {
output.serializeStringWithContext(
LogRecord.EVENT_NAME, ((ExtendedLogRecordData) log).getEventName(), context);
}
output.serializeStringWithContext(LogRecord.EVENT_NAME, log.getEventName(), context);
}

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

if (log instanceof ExtendedLogRecordData) {
size +=
StatelessMarshalerUtil.sizeStringWithContext(
LogRecord.EVENT_NAME, ((ExtendedLogRecordData) log).getEventName(), context);
}
size +=
StatelessMarshalerUtil.sizeStringWithContext(
LogRecord.EVENT_NAME, log.getEventName(), context);

return size;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
final class ExtendedSdkLogRecordBuilder extends SdkLogRecordBuilder
implements ExtendedLogRecordBuilder {

@Nullable private String eventName;
@Nullable private ExtendedAttributesMap extendedAttributes;

ExtendedSdkLogRecordBuilder(
Expand All @@ -33,7 +32,7 @@

@Override
public ExtendedSdkLogRecordBuilder setEventName(String eventName) {
this.eventName = eventName;
super.setEventName(eventName);

Check warning on line 35 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L35 was not covered by tests
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,19 @@ static ExtendedSdkLogRecordData create(
severity,
severityText,
totalAttributeCount,
eventName,
attributes,
body);
body,
eventName);
}

@Override
@Nullable
public abstract Value<?> getBodyValue();

@Override
@Nullable
public abstract String getEventName();

@Override
@SuppressWarnings("deprecation") // Implementation of deprecated method
public io.opentelemetry.sdk.logs.data.Body getBody() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
class ExtendedSdkReadWriteLogRecord extends SdkReadWriteLogRecord
implements ExtendedReadWriteLogRecord {

@Nullable private final String eventName;
private final Object lock = new Object();

@GuardedBy("lock")
Expand Down Expand Up @@ -55,8 +54,8 @@ private ExtendedSdkReadWriteLogRecord(
severity,
severityText,
body,
null);
this.eventName = eventName;
null,
eventName);
this.extendedAttributes = extendedAttributes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@
/** Return an immutable {@link LogRecordData} instance representing this log record. */
LogRecordData toLogRecordData();

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

Check warning on line 58 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#L58

Added line #L58 was not covered by tests
}

/**
* Returns the value of a given attribute if it exists. This is the equivalent of calling {@code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SdkLogRecordBuilder implements LogRecordBuilder {
protected Severity severity = Severity.UNDEFINED_SEVERITY_NUMBER;
@Nullable protected String severityText;
@Nullable protected Value<?> body;
@Nullable protected String eventName;
@Nullable private AttributesMap attributes;

SdkLogRecordBuilder(
Expand All @@ -39,6 +40,12 @@ class SdkLogRecordBuilder implements LogRecordBuilder {
this.instrumentationScopeInfo = instrumentationScopeInfo;
}

@Override
public SdkLogRecordBuilder setEventName(String eventName) {
this.eventName = eventName;
return this;
}

@Override
public SdkLogRecordBuilder setTimestamp(long timestamp, TimeUnit unit) {
this.timestampEpochNanos = unit.toNanos(timestamp);
Expand Down Expand Up @@ -132,6 +139,7 @@ public void emit() {
severity,
severityText,
body,
attributes));
attributes,
eventName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ static SdkLogRecordData create(
@Nullable String severityText,
@Nullable Value<?> body,
Attributes attributes,
int totalAttributeCount) {
int totalAttributeCount,
@Nullable String eventName) {
return new AutoValue_SdkLogRecordData(
resource,
instrumentationScopeInfo,
Expand All @@ -44,13 +45,18 @@ static SdkLogRecordData create(
severityText,
attributes,
totalAttributeCount,
body);
body,
eventName);
}

@Override
@Nullable
public abstract Value<?> getBodyValue();

@Override
@Nullable
public abstract String getEventName();

@Override
@SuppressWarnings("deprecation") // Implementation of deprecated method
public io.opentelemetry.sdk.logs.data.Body getBody() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
protected final Severity severity;
@Nullable protected final String severityText;
@Nullable protected final Value<?> body;
@Nullable protected String eventName;
private final Object lock = new Object();

@GuardedBy("lock")
Expand All @@ -46,7 +47,8 @@
Severity severity,
@Nullable String severityText,
@Nullable Value<?> body,
@Nullable AttributesMap attributes) {
@Nullable AttributesMap attributes,
@Nullable String eventName) {
this.logLimits = logLimits;
this.resource = resource;
this.instrumentationScopeInfo = instrumentationScopeInfo;
Expand All @@ -56,6 +58,7 @@
this.severity = severity;
this.severityText = severityText;
this.body = body;
this.eventName = eventName;
this.attributes = attributes;
}

Expand All @@ -70,7 +73,8 @@
Severity severity,
@Nullable String severityText,
@Nullable Value<?> body,
@Nullable AttributesMap attributes) {
@Nullable AttributesMap attributes,
@Nullable String eventName) {
return new SdkReadWriteLogRecord(
logLimits,
resource,
Expand All @@ -81,7 +85,8 @@
severity,
severityText,
body,
attributes);
attributes,
eventName);
}

@Override
Expand Down Expand Up @@ -122,7 +127,8 @@
severityText,
body,
getImmutableAttributes(),
attributes == null ? 0 : attributes.getTotalAddedValues());
attributes == null ? 0 : attributes.getTotalAddedValues(),
eventName);
}
}

Expand Down Expand Up @@ -168,6 +174,12 @@
return getImmutableAttributes();
}

@Override
@Nullable
public String getEventName() {
return eventName;

Check warning on line 180 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#L180

Added line #L180 was not covered by tests
}

@Nullable
@Override
public <T> T getAttribute(AttributeKey<T> key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,10 @@
* LogLimits#getMaxNumberOfAttributes()}.
*/
int getTotalAttributeCount();

/** Returns the event name, or {@code null} if none is set. */
@Nullable
default String getEventName() {
return null;

Check warning on line 89 in sdk/logs/src/main/java/io/opentelemetry/sdk/logs/data/LogRecordData.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L89 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.incubator.common.ExtendedAttributes;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import javax.annotation.Nullable;

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

@Nullable
String getEventName();

/** Returns the attributes for this log, or {@link ExtendedAttributes#empty()} if unset. */
ExtendedAttributes getExtendedAttributes();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ SdkReadWriteLogRecord buildLogRecord() {
Severity.DEBUG,
"buggin",
body,
initialAttributes);
initialAttributes,
"my.event.name");
}
}
Loading
Loading