Skip to content

Commit

Permalink
Allow events to be emitted with timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
breedx-splk committed Oct 18, 2023
1 parent 57d8334 commit e12f50c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ static EventEmitter getInstance() {
return INSTANCE;
}

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

@Override
public void emit(String eventName, Attributes attributes) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
@ThreadSafe
public interface EventEmitter {

/**
* Emit an event.
*
* @param epochNanos The time at which the event happened, in epoch nanoseconds.
* @param eventName the event name, which acts as a classifier for events. Within a particular
* event domain, event name defines a particular class or type of event.
* @param attributes attributes associated with the event
*/
@SuppressWarnings("InconsistentOverloads")
void emit(long epochNanos, String eventName, Attributes attributes);

/**
* Emit an event.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,14 @@ private SdkEventEmitter(Clock clock, Logger delegateLogger, String eventDomain)

@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(clock.now(), TimeUnit.NANOSECONDS)
.setTimestamp(epochNanos, TimeUnit.NANOSECONDS)
.setAllAttributes(attributes)
.setAttribute(EVENT_DOMAIN, eventDomain)
.setAttribute(EVENT_NAME, eventName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@

package io.opentelemetry.sdk.logs.internal;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.events.EventEmitter;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.logs.ReadWriteLogRecord;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.resources.Resource;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -91,4 +94,24 @@ void emit_NoDomain() {
.put("event.name", "event-name")
.build());
}

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

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

emitter.emit(yesterday, "testing", attributes);

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 e12f50c

Please sign in to comment.