Skip to content

Commit

Permalink
add EventBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
breedx-splk committed Oct 24, 2023
1 parent db38619 commit a93c3b0
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,42 @@ public void emit(long epochNanos, String eventName, Attributes attributes) {}

@Override
public void emit(String eventName, Attributes attributes) {}

@Override
public EventBuilder builder() {
return NoOpEventBuilder.INSTANCE;
}

@Override
public EventBuilder builder(String eventName) {
return NoOpEventBuilder.INSTANCE;
}

@Override
public EventBuilder builder(String eventName, Attributes attributes) {
return NoOpEventBuilder.INSTANCE;
}

private static class NoOpEventBuilder implements EventBuilder {

public static final EventBuilder INSTANCE = new NoOpEventBuilder();

@Override
public EventBuilder setEventName(String eventName) {
return this;
}

@Override
public EventBuilder setAttributes(Attributes attributes) {
return this;
}

@Override
public EventBuilder setTimestamp(long epochNanos) {
return this;
}

@Override
public void emit() {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.events;

import io.opentelemetry.api.common.Attributes;

/** The EventBuilder is used to emit() events. */
public interface EventBuilder {

/** Set the name of the event. */
EventBuilder setEventName(String eventName);

/** Set the attributes to attach to the event. */
EventBuilder setAttributes(Attributes attributes);

/** Sets the timestamp for the event. */
EventBuilder setTimestamp(long epochNanos);

/** Emit an event. */
void emit();
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ public interface EventEmitter {
* @param attributes attributes associated with the event
*/
void emit(String eventName, Attributes attributes);

EventBuilder builder();

EventBuilder builder(String eventName);

EventBuilder builder(String eventName, Attributes attributes);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.logs.internal;

import static io.opentelemetry.sdk.logs.internal.SdkEventEmitterProvider.EVENT_DOMAIN;
import static io.opentelemetry.sdk.logs.internal.SdkEventEmitterProvider.EVENT_NAME;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.events.EventBuilder;
import io.opentelemetry.api.logs.Logger;
import io.opentelemetry.sdk.common.Clock;
import java.util.concurrent.TimeUnit;

class SdkEventBuilder implements EventBuilder {
private static final String DEFAULT_EVENT_NAME = "unknown-event";
private final Clock clock;
private final Logger delegateLogger;
private final String eventDomain;

private String eventName = DEFAULT_EVENT_NAME;
private Attributes attributes = Attributes.empty();
private long epochNanos = Long.MIN_VALUE;

SdkEventBuilder(Clock clock, Logger delegateLogger, String eventDomain) {
this.clock = clock;
this.delegateLogger = delegateLogger;
this.eventDomain = eventDomain;
}

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

@Override
public EventBuilder setAttributes(Attributes attributes) {
this.attributes = attributes;
return this;
}

@Override
public EventBuilder setTimestamp(long epochNanos) {
this.epochNanos = epochNanos;
return this;
}

@Override
public void emit() {
long timestamp = epochNanos == Long.MIN_VALUE ? clock.now() : epochNanos;
delegateLogger
.logRecordBuilder()
.setTimestamp(timestamp, TimeUnit.NANOSECONDS)
.setAllAttributes(attributes)
.setAttribute(EVENT_DOMAIN, eventDomain)
.setAttribute(EVENT_NAME, eventName)
.emit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.events.EventBuilder;
import io.opentelemetry.api.events.EventEmitter;
import io.opentelemetry.api.events.EventEmitterBuilder;
import io.opentelemetry.api.events.EventEmitterProvider;
import io.opentelemetry.api.logs.Logger;
import io.opentelemetry.api.logs.LoggerBuilder;
import io.opentelemetry.api.logs.LoggerProvider;
import io.opentelemetry.sdk.common.Clock;
import java.util.concurrent.TimeUnit;

/**
* SDK implementation for {@link EventEmitterProvider}.
Expand All @@ -24,7 +24,10 @@
*/
public final class SdkEventEmitterProvider implements EventEmitterProvider {

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

static final String DEFAULT_EVENT_DOMAIN = "unknown";

private final LoggerProvider delegateLoggerProvider;
private final Clock clock;
Expand Down Expand Up @@ -98,9 +101,6 @@ public EventEmitter build() {

private static class SdkEventEmitter implements EventEmitter {

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

private final Clock clock;
private final Logger delegateLogger;
private final String eventDomain;
Expand All @@ -111,19 +111,32 @@ private SdkEventEmitter(Clock clock, Logger delegateLogger, String eventDomain)
this.eventDomain = eventDomain;
}

@Override
public EventBuilder builder() {
return new SdkEventBuilder(clock, delegateLogger, eventDomain);
}

@Override
public EventBuilder builder(String eventName) {
return builder().setEventName(eventName);
}

@Override
public EventBuilder builder(String eventName, Attributes attributes) {
return builder().setEventName(eventName).setAttributes(attributes);
}

@Override
public void emit(String eventName, Attributes attributes) {
emit(clock.now(), eventName, attributes);
}

@Override
public void emit(long epochNanos, String eventName, Attributes attributes) {
delegateLogger
.logRecordBuilder()
.setTimestamp(epochNanos, TimeUnit.NANOSECONDS)
.setAllAttributes(attributes)
.setAttribute(EVENT_DOMAIN, eventDomain)
.setAttribute(EVENT_NAME, eventName)
new SdkEventBuilder(clock, delegateLogger, eventDomain)
.setTimestamp(epochNanos)
.setAttributes(attributes)
.setEventName(eventName)
.emit();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.logs.internal;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.logs.LogRecordBuilder;
import io.opentelemetry.api.logs.Logger;
import io.opentelemetry.sdk.common.Clock;
import org.junit.jupiter.api.Test;

class SdkEventBuilderTest {

@Test
void emit() {
String eventDomain = "mydomain";
String eventName = "banana";
Attributes attributes = Attributes.of(stringKey("foo"), "bar");

Logger logger = mock(Logger.class);
LogRecordBuilder logRecordBuilder = mock(LogRecordBuilder.class);
when(logger.logRecordBuilder()).thenReturn(logRecordBuilder);
when(logRecordBuilder.setTimestamp(anyLong(), any())).thenReturn(logRecordBuilder);
when(logRecordBuilder.setAttribute(any(), any())).thenReturn(logRecordBuilder);
when(logRecordBuilder.setAllAttributes(any())).thenReturn(logRecordBuilder);

new SdkEventBuilder(Clock.getDefault(), logger, eventDomain)
.setEventName(eventName)
.setTimestamp(123456L)
.setAttributes(attributes)
.emit();
verify(logRecordBuilder).setAllAttributes(attributes);
verify(logRecordBuilder).setAttribute(stringKey("event.domain"), eventDomain);
verify(logRecordBuilder).setAttribute(stringKey("event.name"), eventName);
verify(logRecordBuilder).emit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,23 @@ void emit_withTimestamp() {
.put("event.name", "testing")
.build());
}

@Test
void builder() {
long yesterday = System.nanoTime() - TimeUnit.DAYS.toNanos(1);
Attributes attributes = Attributes.of(stringKey("foo"), "bar");

EventEmitter emitter = eventEmitterProvider.eventEmitterBuilder("test-scope").build();

emitter.builder("testing", attributes).setTimestamp(yesterday).emit();
assertThat(seenLog.get().toLogRecordData())
.hasResource(RESOURCE)
.hasInstrumentationScope(InstrumentationScopeInfo.create("test-scope"))
.hasTimestamp(yesterday)
.hasAttributes(
attributes.toBuilder()
.put("event.domain", "unknown")
.put("event.name", "testing")
.build());
}
}

0 comments on commit a93c3b0

Please sign in to comment.