Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow events to be emitted with timestamp #5928

Merged
merged 9 commits into from
Nov 9, 2023
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);
jack-berg marked this conversation as resolved.
Show resolved Hide resolved

/**
* Emit an event.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ void emit() {
.emit("event-name", Attributes.builder().put("key1", "value1").build()))
.doesNotThrowAnyException();
}

@Test
void emitWithTimestamp() {
EventEmitter emitter = DefaultEventEmitter.getInstance();
Attributes attributes = Attributes.builder().put("key1", "value1").build();
assertThatCode(() -> emitter.emit(System.nanoTime(), "event-name", attributes));
}
}
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());
}
}
Loading