Skip to content

Commit

Permalink
Add Map<String, AnyValue<?>> payload helper overload
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg committed Dec 1, 2023
1 parent 75c13bb commit e1459c6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.extension.incubator.logs.AnyValue;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/** The EventBuilder is used to {@link #emit()} events. */
Expand All @@ -23,6 +24,16 @@ public interface EventBuilder {
*/
EventBuilder setPayload(AnyValue<?> payload);

/**
* Set the {@code payload}.
*
* <p>The {@code payload} is expected to match the schema of other events with the same {@code
* eventName}.
*/
default EventBuilder setPayload(Map<String, AnyValue<?>> payload) {
return setPayload(AnyValue.of(payload));
}

/**
* Set the epoch {@code timestamp}, using the timestamp and unit.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.api.events;

import io.opentelemetry.extension.incubator.logs.AnyValue;
import java.util.Map;
import javax.annotation.concurrent.ThreadSafe;

/**
Expand Down Expand Up @@ -42,6 +43,18 @@ public interface EventEmitter {
*/
void emit(String eventName, AnyValue<?> payload);

/**
* Emit an event.
*
* @param eventName the event name, which defines the class or type of event. Events names SHOULD
* include a namespace to avoid collisions with other event names.
* @param payload the eventPayload, which is expected to match the schema of other events with the
* same {@code eventName}.
*/
default void emit(String eventName, Map<String, AnyValue<?>> payload) {
emit(eventName, AnyValue.of(payload));
}

/**
* Return a {@link EventBuilder} to emit an event.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void noopEventEmitterProvider_doesNotThrow() {
provider
.eventEmitterBuilder("scope-name")
.build()
.emit("namespace.event-name", AnyValue.of("")))
.emit("namespace.event-name", AnyValue.of("payload")))
.doesNotThrowAnyException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ void emit() {
"namespace.event-name",
AnyValue.of(Collections.singletonMap("key1", AnyValue.of("value1")))))
.doesNotThrowAnyException();

assertThatCode(
() ->
DefaultEventEmitter.getInstance()
.emit(
"namespace.event-name",
Collections.singletonMap("key1", AnyValue.of("value1"))))
.doesNotThrowAnyException();
}

@Test
Expand All @@ -36,8 +44,8 @@ void builder() {
() ->
emitter
.builder("namespace.myEvent")
.setPayload(
AnyValue.of(Collections.singletonMap("key1", AnyValue.of("value1"))))
.setPayload(AnyValue.of("payload"))
.setPayload(Collections.singletonMap("key1", AnyValue.of("value1")))
.setTimestamp(123456L, TimeUnit.NANOSECONDS)
.setTimestamp(Instant.now())
.setContext(Context.current())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,8 @@ private static void testLogRecordExporter(LogRecordExporter logRecordExporter) {
.emit();
eventEmitter.emit(
"namespace.event-name",
io.opentelemetry.extension.incubator.logs.AnyValue.of(
Collections.singletonMap(
"key", io.opentelemetry.extension.incubator.logs.AnyValue.of("value"))));
Collections.singletonMap(
"key", io.opentelemetry.extension.incubator.logs.AnyValue.of("value")));
}

// Closing triggers flush of processor
Expand Down

0 comments on commit e1459c6

Please sign in to comment.