Skip to content

Commit

Permalink
Change EventBuilder put to setBody
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Dec 4, 2024
1 parent ee5a5b7 commit 6556acf
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void emit() {}
private static class NoopEventBuilder implements EventBuilder {

@Override
public EventBuilder put(String key, Value<?> value) {
public EventBuilder setBody(Value<?> body) {
return this;
}

Expand Down
101 changes: 1 addition & 100 deletions api/all/src/main/java/io/opentelemetry/api/logs/EventBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,115 +5,16 @@

package io.opentelemetry.api.logs;

import static java.util.stream.Collectors.toList;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.Value;
import io.opentelemetry.context.Context;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

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

/** Put the given {@code key} and {@code value} in the payload. */
default EventBuilder put(String key, String value) {
return put(key, Value.of(value));
}

/** Put the given {@code key} and {@code value} in the payload. */
default EventBuilder put(String key, long value) {
return put(key, Value.of(value));
}

/** Put the given {@code key} and {@code value} in the payload. */
default EventBuilder put(String key, double value) {
return put(key, Value.of(value));
}

/** Put the given {@code key} and {@code value} in the payload. */
default EventBuilder put(String key, boolean value) {
return put(key, Value.of(value));
}

/** Put the given {@code key} and {@code value} in the payload. */
default EventBuilder put(String key, String... value) {
List<Value<?>> values = new ArrayList<>(value.length);
for (String val : value) {
values.add(Value.of(val));
}
return put(key, Value.of(values));
}

/** Put the given {@code key} and {@code value} in the payload. */
default EventBuilder put(String key, long... value) {
List<Value<?>> values = new ArrayList<>(value.length);
for (long val : value) {
values.add(Value.of(val));
}
return put(key, Value.of(values));
}

/** Put the given {@code key} and {@code value} in the payload. */
default EventBuilder put(String key, double... value) {
List<Value<?>> values = new ArrayList<>(value.length);
for (double val : value) {
values.add(Value.of(val));
}
return put(key, Value.of(values));
}

/** Put the given {@code key} and {@code value} in the payload. */
default EventBuilder put(String key, boolean... value) {
List<Value<?>> values = new ArrayList<>(value.length);
for (boolean val : value) {
values.add(Value.of(val));
}
return put(key, Value.of(values));
}

/**
* Put the given key and value in the payload.
*
* <p>NOTE: The key value pair is NOT added to the event attributes. Setting event attributes is
* less common than adding entries to the event payload. Use {@link #setAttributes(Attributes)} if
* intending the data to be set in attributes instead of the payload.
*/
@SuppressWarnings("unchecked")
default <T> EventBuilder put(AttributeKey<T> key, T value) {
switch (key.getType()) {
case STRING:
return put(key.getKey(), (String) value);
case BOOLEAN:
return put(key.getKey(), (boolean) value);
case LONG:
return put(key.getKey(), (long) value);
case DOUBLE:
return put(key.getKey(), (double) value);
case STRING_ARRAY:
return put(
key.getKey(),
Value.of(((List<String>) value).stream().map(Value::of).collect(toList())));
case BOOLEAN_ARRAY:
return put(
key.getKey(),
Value.of(((List<Boolean>) value).stream().map(Value::of).collect(toList())));
case LONG_ARRAY:
return put(
key.getKey(), Value.of(((List<Long>) value).stream().map(Value::of).collect(toList())));
case DOUBLE_ARRAY:
return put(
key.getKey(),
Value.of(((List<Double>) value).stream().map(Value::of).collect(toList())));
}
return this;
}

/** Put the given {@code key} and {@code value} in the payload. */
EventBuilder put(String key, Value<?> value);
EventBuilder setBody(Value<?> body);

/**
* Set the epoch {@code timestamp}, using the timestamp and unit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void emit() {}
private static class NoopEventBuilder implements EventBuilder {

@Override
public EventBuilder put(String key, Value<?> value) {
public EventBuilder setBody(Value<?> body) {
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.Clock;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

class SdkEventBuilder implements EventBuilder {

private static final AttributeKey<String> EVENT_NAME = AttributeKey.stringKey("event.name");

private final Map<String, Value<?>> payload = new HashMap<>();
private final Clock clock;
private final LogRecordBuilder logRecordBuilder;
private final String eventName;
Expand All @@ -35,8 +32,8 @@ class SdkEventBuilder implements EventBuilder {
}

@Override
public EventBuilder put(String key, Value<?> value) {
payload.put(key, value);
public EventBuilder setBody(Value<?> body) {
this.logRecordBuilder.setBody(body);
return this;
}

Expand Down Expand Up @@ -74,9 +71,6 @@ public EventBuilder setAttributes(Attributes attributes) {

@Override
public void emit() {
if (!payload.isEmpty()) {
logRecordBuilder.setBody(Value.of(payload));
}
if (!hasTimestamp) {
logRecordBuilder.setTimestamp(clock.now(), TimeUnit.NANOSECONDS);
}
Expand Down

0 comments on commit 6556acf

Please sign in to comment.