Skip to content

Commit dd11124

Browse files
committed
Refactor Event API to reflect spec changes
1 parent 42cc36b commit dd11124

File tree

13 files changed

+299
-118
lines changed

13 files changed

+299
-118
lines changed

api/incubator/src/main/java/io/opentelemetry/api/incubator/events/DefaultEventLogger.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package io.opentelemetry.api.incubator.events;
77

88
import io.opentelemetry.api.common.Attributes;
9+
import io.opentelemetry.api.incubator.logs.AnyValue;
10+
import io.opentelemetry.api.logs.Severity;
11+
import io.opentelemetry.context.Context;
912
import java.time.Instant;
1013
import java.util.concurrent.TimeUnit;
1114

@@ -20,17 +23,19 @@ static EventLogger getInstance() {
2023
}
2124

2225
@Override
23-
public void emit(String eventName, Attributes attributes) {}
24-
25-
@Override
26-
public EventBuilder builder(String eventName, Attributes attributes) {
26+
public EventBuilder builder(String eventName) {
2727
return NoOpEventBuilder.INSTANCE;
2828
}
2929

3030
private static class NoOpEventBuilder implements EventBuilder {
3131

3232
public static final EventBuilder INSTANCE = new NoOpEventBuilder();
3333

34+
@Override
35+
public EventBuilder put(String key, AnyValue<?> value) {
36+
return this;
37+
}
38+
3439
@Override
3540
public EventBuilder setTimestamp(long timestamp, TimeUnit unit) {
3641
return this;
@@ -41,6 +46,21 @@ public EventBuilder setTimestamp(Instant instant) {
4146
return this;
4247
}
4348

49+
@Override
50+
public EventBuilder setContext(Context context) {
51+
return this;
52+
}
53+
54+
@Override
55+
public EventBuilder setSeverity(Severity severity) {
56+
return this;
57+
}
58+
59+
@Override
60+
public EventBuilder setAttributes(Attributes attributes) {
61+
return this;
62+
}
63+
4464
@Override
4565
public void emit() {}
4666
}

api/incubator/src/main/java/io/opentelemetry/api/incubator/events/EventBuilder.java

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,107 @@
55

66
package io.opentelemetry.api.incubator.events;
77

8+
import io.opentelemetry.api.common.Attributes;
9+
import io.opentelemetry.api.incubator.logs.AnyValue;
10+
import io.opentelemetry.api.logs.Severity;
11+
import io.opentelemetry.context.Context;
812
import java.time.Instant;
13+
import java.util.ArrayList;
14+
import java.util.List;
915
import java.util.concurrent.TimeUnit;
1016

1117
/** The EventBuilder is used to {@link #emit()} events. */
1218
public interface EventBuilder {
1319

20+
/** Put the given {@code key} and {@code value} in the payload. */
21+
default EventBuilder put(String key, String value) {
22+
return put(key, AnyValue.of(value));
23+
}
24+
25+
/** Put the given {@code key} and {@code value} in the payload. */
26+
default EventBuilder put(String key, long value) {
27+
return put(key, AnyValue.of(value));
28+
}
29+
30+
/** Put the given {@code key} and {@code value} in the payload. */
31+
default EventBuilder put(String key, double value) {
32+
return put(key, AnyValue.of(value));
33+
}
34+
35+
/** Put the given {@code key} and {@code value} in the payload. */
36+
default EventBuilder put(String key, boolean value) {
37+
return put(key, AnyValue.of(value));
38+
}
39+
40+
/** Put the given {@code key} and {@code value} in the payload. */
41+
default EventBuilder put(String key, String... value) {
42+
List<AnyValue<?>> values = new ArrayList<>(value.length);
43+
for (String val : value) {
44+
values.add(AnyValue.of(val));
45+
}
46+
return put(key, AnyValue.of(values));
47+
}
48+
49+
/** Put the given {@code key} and {@code value} in the payload. */
50+
default EventBuilder put(String key, long... value) {
51+
List<AnyValue<?>> values = new ArrayList<>(value.length);
52+
for (long val : value) {
53+
values.add(AnyValue.of(val));
54+
}
55+
return put(key, AnyValue.of(values));
56+
}
57+
58+
/** Put the given {@code key} and {@code value} in the payload. */
59+
default EventBuilder put(String key, double... value) {
60+
List<AnyValue<?>> values = new ArrayList<>(value.length);
61+
for (double val : value) {
62+
values.add(AnyValue.of(val));
63+
}
64+
return put(key, AnyValue.of(values));
65+
}
66+
67+
/** Put the given {@code key} and {@code value} in the payload. */
68+
default EventBuilder put(String key, boolean... value) {
69+
List<AnyValue<?>> values = new ArrayList<>(value.length);
70+
for (boolean val : value) {
71+
values.add(AnyValue.of(val));
72+
}
73+
return put(key, AnyValue.of(values));
74+
}
75+
76+
/** Put the given {@code key} and {@code value} in the payload. */
77+
EventBuilder put(String key, AnyValue<?> value);
78+
1479
/**
15-
* Set the epoch {@code timestamp} for the event, using the timestamp and unit.
80+
* Set the epoch {@code timestamp}, using the timestamp and unit.
1681
*
1782
* <p>The {@code timestamp} is the time at which the event occurred. If unset, it will be set to
1883
* the current time when {@link #emit()} is called.
1984
*/
2085
EventBuilder setTimestamp(long timestamp, TimeUnit unit);
2186

2287
/**
23-
* Set the epoch {@code timestamp} for the event, using the instant.
88+
* Set the epoch {@code timestamp}, using the instant.
2489
*
2590
* <p>The {@code timestamp} is the time at which the event occurred. If unset, it will be set to
2691
* the current time when {@link #emit()} is called.
2792
*/
2893
EventBuilder setTimestamp(Instant instant);
2994

95+
/** Set the context. */
96+
EventBuilder setContext(Context context);
97+
98+
/** Set the severity. */
99+
EventBuilder setSeverity(Severity severity);
100+
101+
/**
102+
* Set the attributes.
103+
*
104+
* <p>Event {@link io.opentelemetry.api.common.Attributes} provide additional details about the
105+
* Event which are not part of the well-defined {@link AnyValue} {@code payload}.
106+
*/
107+
EventBuilder setAttributes(Attributes attributes);
108+
30109
/** Emit an event. */
31110
void emit();
32111
}

api/incubator/src/main/java/io/opentelemetry/api/incubator/events/EventLogger.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package io.opentelemetry.api.incubator.events;
77

8-
import io.opentelemetry.api.common.Attributes;
98
import javax.annotation.concurrent.ThreadSafe;
109

1110
/**
@@ -20,10 +19,10 @@
2019
* .build();
2120
*
2221
* void doWork() {
23-
* eventLogger.emit("my-namespace.my-event", Attributes.builder()
22+
* eventLogger.builder("my-namespace.my-event")
2423
* .put("key1", "value1")
2524
* .put("key2", "value2")
26-
* .build())
25+
* .emit();
2726
* // do work
2827
* }
2928
* }
@@ -32,18 +31,6 @@
3231
@ThreadSafe
3332
public interface EventLogger {
3433

35-
/**
36-
* Emit an event.
37-
*
38-
* @param eventName the event name, which identifies the class or type of event. Event with the
39-
* same name are structurally similar to one another. Event names are subject to the same
40-
* naming rules as attribute names. Notably, they are namespaced to avoid collisions. See <a
41-
* href="https://opentelemetry.io/docs/specs/semconv/general/events/">event.name semantic
42-
* conventions</a> for more details.
43-
* @param attributes attributes associated with the event
44-
*/
45-
void emit(String eventName, Attributes attributes);
46-
4734
/**
4835
* Return a {@link EventBuilder} to emit an event.
4936
*
@@ -52,7 +39,6 @@ public interface EventLogger {
5239
* naming rules as attribute names. Notably, they are namespaced to avoid collisions. See <a
5340
* href="https://opentelemetry.io/docs/specs/semconv/general/events/">event.name semantic
5441
* conventions</a> for more details.
55-
* @param attributes attributes associated with the event
5642
*/
57-
EventBuilder builder(String eventName, Attributes attributes);
43+
EventBuilder builder(String eventName);
5844
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ static AnyValue<List<AnyValue<?>>> of(AnyValue<?>... value) {
6262
return AnyValueArray.create(value);
6363
}
6464

65+
/** Returns an {@link AnyValue} for the list of {@link AnyValue} values. */
66+
static AnyValue<List<AnyValue<?>>> of(List<AnyValue<?>> value) {
67+
return AnyValueArray.create(value);
68+
}
69+
6570
/**
6671
* Returns an {@link AnyValue} for the array of {@link KeyAnyValue} values. {@link
6772
* KeyAnyValue#getKey()} values should not repeat - duplicates may be dropped.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ static AnyValue<List<AnyValue<?>>> create(AnyValue<?>... value) {
2828
return new AnyValueArray(Collections.unmodifiableList(list));
2929
}
3030

31+
static AnyValue<List<AnyValue<?>>> create(List<AnyValue<?>> value) {
32+
return new AnyValueArray(Collections.unmodifiableList(value));
33+
}
34+
3135
@Override
3236
public AnyValueType getType() {
3337
return AnyValueType.ARRAY;

api/incubator/src/test/java/io/opentelemetry/api/incubator/events/DefaultEventLoggerProviderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import static org.assertj.core.api.Assertions.assertThat;
99
import static org.assertj.core.api.Assertions.assertThatCode;
1010

11-
import io.opentelemetry.api.common.Attributes;
1211
import org.junit.jupiter.api.Test;
1312

1413
class DefaultEventLoggerProviderTest {
@@ -33,7 +32,8 @@ void noopEventLoggerProvider_doesNotThrow() {
3332
provider
3433
.eventLoggerBuilder("scope-name")
3534
.build()
36-
.emit("event-name", Attributes.empty()))
35+
.builder("namespace.event-name")
36+
.emit())
3737
.doesNotThrowAnyException();
3838
}
3939
}

api/incubator/src/test/java/io/opentelemetry/api/incubator/events/DefaultEventLoggerTest.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,27 @@
88
import static org.assertj.core.api.Assertions.assertThatCode;
99

1010
import io.opentelemetry.api.common.Attributes;
11+
import io.opentelemetry.api.logs.Severity;
12+
import io.opentelemetry.context.Context;
1113
import java.time.Instant;
1214
import java.util.concurrent.TimeUnit;
1315
import org.junit.jupiter.api.Test;
1416

1517
class DefaultEventLoggerTest {
1618

17-
@Test
18-
void emit() {
19-
assertThatCode(() -> DefaultEventLogger.getInstance().emit("event-name", Attributes.empty()))
20-
.doesNotThrowAnyException();
21-
assertThatCode(
22-
() ->
23-
DefaultEventLogger.getInstance()
24-
.emit(
25-
"event-domain.event-name",
26-
Attributes.builder().put("key1", "value1").build()))
27-
.doesNotThrowAnyException();
28-
}
29-
3019
@Test
3120
void builder() {
32-
Attributes attributes = Attributes.builder().put("key1", "value1").build();
3321
EventLogger eventLogger = DefaultEventLogger.getInstance();
3422
assertThatCode(
3523
() ->
3624
eventLogger
37-
.builder("com.example.MyEvent", attributes)
25+
.builder("namespace.myEvent")
26+
.put("key", "value")
3827
.setTimestamp(123456L, TimeUnit.NANOSECONDS)
3928
.setTimestamp(Instant.now())
29+
.setContext(Context.current())
30+
.setSeverity(Severity.DEBUG)
31+
.setAttributes(Attributes.empty())
4032
.emit())
4133
.doesNotThrowAnyException();
4234
}

integration-tests/otlp/src/main/java/io/opentelemetry/integrationtest/OtlpExporterIntegrationTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ private static void testLogRecordExporter(LogRecordExporter logRecordExporter) {
568568
.setSeverityText("DEBUG")
569569
.setContext(Context.current())
570570
.emit();
571-
eventLogger.emit("event-name", Attributes.builder().put("key", "value").build());
571+
eventLogger.builder("namespace.event-name").put("key", "value").emit();
572572
}
573573

574574
// Closing triggers flush of processor
@@ -708,17 +708,18 @@ private static void testLogRecordExporter(LogRecordExporter logRecordExporter) {
708708

709709
// LogRecord via EventLogger.emit(String, Attributes)
710710
io.opentelemetry.proto.logs.v1.LogRecord protoLog2 = ilLogs.getLogRecords(1);
711-
assertThat(protoLog2.getBody().getStringValue()).isEmpty();
712-
assertThat(protoLog2.getAttributesList())
711+
assertThat(protoLog2.getBody().getKvlistValue().getValuesList())
713712
.containsExactlyInAnyOrder(
714-
KeyValue.newBuilder()
715-
.setKey("event.name")
716-
.setValue(AnyValue.newBuilder().setStringValue("event-name").build())
717-
.build(),
718713
KeyValue.newBuilder()
719714
.setKey("key")
720715
.setValue(AnyValue.newBuilder().setStringValue("value").build())
721716
.build());
717+
assertThat(protoLog2.getAttributesList())
718+
.containsExactlyInAnyOrder(
719+
KeyValue.newBuilder()
720+
.setKey("event.name")
721+
.setValue(AnyValue.newBuilder().setStringValue("namespace.event-name").build())
722+
.build());
722723
assertThat(protoLog2.getSeverityText()).isEmpty();
723724
assertThat(TraceId.fromBytes(protoLog2.getTraceId().toByteArray()))
724725
.isEqualTo(spanContext.getTraceId());

sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/FullConfigTest.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import io.opentelemetry.proto.collector.trace.v1.TraceServiceGrpc;
3939
import io.opentelemetry.proto.common.v1.AnyValue;
4040
import io.opentelemetry.proto.common.v1.KeyValue;
41+
import io.opentelemetry.proto.logs.v1.SeverityNumber;
4142
import io.opentelemetry.proto.metrics.v1.Metric;
4243
import io.opentelemetry.sdk.OpenTelemetrySdk;
4344
import java.util.ArrayList;
@@ -207,7 +208,8 @@ void configures() throws Exception {
207208
logger.logRecordBuilder().setBody("info log message").setSeverity(Severity.INFO).emit();
208209

209210
EventLogger eventLogger = GlobalEventLoggerProvider.get().eventLoggerBuilder("test").build();
210-
eventLogger.emit("test-name", Attributes.builder().put("cow", "moo").build());
211+
eventLogger.builder("namespace.test-name").put("cow", "moo").emit();
212+
;
211213

212214
openTelemetrySdk.getSdkTracerProvider().forceFlush().join(10, TimeUnit.SECONDS);
213215
openTelemetrySdk.getSdkLoggerProvider().forceFlush().join(10, TimeUnit.SECONDS);
@@ -329,17 +331,23 @@ void configures() throws Exception {
329331
assertThat(logRecord.getSeverityNumberValue())
330332
.isEqualTo(Severity.INFO.getSeverityNumber());
331333
},
332-
logRecord ->
333-
assertThat(logRecord.getAttributesList())
334-
.containsExactlyInAnyOrder(
335-
KeyValue.newBuilder()
336-
.setKey("event.name")
337-
.setValue(AnyValue.newBuilder().setStringValue("test-name").build())
338-
.build(),
339-
KeyValue.newBuilder()
340-
.setKey("cow")
341-
.setValue(AnyValue.newBuilder().setStringValue("moo").build())
342-
.build()));
334+
logRecord -> {
335+
assertThat(logRecord.getBody().getKvlistValue().getValuesList())
336+
.containsExactlyInAnyOrder(
337+
KeyValue.newBuilder()
338+
.setKey("cow")
339+
.setValue(AnyValue.newBuilder().setStringValue("moo").build())
340+
.build());
341+
assertThat(logRecord.getSeverityNumber())
342+
.isEqualTo(SeverityNumber.SEVERITY_NUMBER_INFO);
343+
assertThat(logRecord.getAttributesList())
344+
.containsExactlyInAnyOrder(
345+
KeyValue.newBuilder()
346+
.setKey("event.name")
347+
.setValue(
348+
AnyValue.newBuilder().setStringValue("namespace.test-name").build())
349+
.build());
350+
});
343351
}
344352

345353
private static List<KeyValue> getFirstDataPointLabels(Metric metric) {

0 commit comments

Comments
 (0)