-
Notifications
You must be signed in to change notification settings - Fork 801
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow events to be emitted with timestamp (#5928)
Co-authored-by: Jack Berg <jberg@newrelic.com>
- Loading branch information
1 parent
b03ec3a
commit 83993e0
Showing
8 changed files
with
220 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
api/events/src/main/java/io/opentelemetry/api/events/EventBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.api.events; | ||
|
||
import java.time.Instant; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** The EventBuilder is used to {@link #emit()} events. */ | ||
public interface EventBuilder { | ||
|
||
/** | ||
* Set the epoch {@code timestamp} for the event, using the timestamp and unit. | ||
* | ||
* <p>The {@code timestamp} is the time at which the event occurred. If unset, it will be set to | ||
* the current time when {@link #emit()} is called. | ||
*/ | ||
EventBuilder setTimestamp(long timestamp, TimeUnit unit); | ||
|
||
/** | ||
* Set the epoch {@code timestamp} for the event, using the instant. | ||
* | ||
* <p>The {@code timestamp} is the time at which the event occurred. If unset, it will be set to | ||
* the current time when {@link #emit()} is called. | ||
*/ | ||
EventBuilder setTimestamp(Instant instant); | ||
|
||
/** Emit an event. */ | ||
void emit(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
sdk/logs/src/main/java/io/opentelemetry/sdk/logs/internal/SdkEventBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.logs.internal; | ||
|
||
import io.opentelemetry.api.events.EventBuilder; | ||
import io.opentelemetry.api.logs.LogRecordBuilder; | ||
import java.time.Instant; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
class SdkEventBuilder implements EventBuilder { | ||
private final LogRecordBuilder logRecordBuilder; | ||
private final String eventDomain; | ||
private final String eventName; | ||
|
||
SdkEventBuilder(LogRecordBuilder logRecordBuilder, String eventDomain, String eventName) { | ||
this.logRecordBuilder = logRecordBuilder; | ||
this.eventDomain = eventDomain; | ||
this.eventName = eventName; | ||
} | ||
|
||
@Override | ||
public EventBuilder setTimestamp(long timestamp, TimeUnit unit) { | ||
this.logRecordBuilder.setTimestamp(timestamp, unit); | ||
return this; | ||
} | ||
|
||
@Override | ||
public EventBuilder setTimestamp(Instant instant) { | ||
this.logRecordBuilder.setTimestamp(instant); | ||
return this; | ||
} | ||
|
||
@Override | ||
public void emit() { | ||
SdkEventEmitterProvider.addEventNameAndDomain(logRecordBuilder, eventDomain, eventName); | ||
logRecordBuilder.emit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
sdk/logs/src/test/java/io/opentelemetry/sdk/logs/internal/SdkEventBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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.logs.LogRecordBuilder; | ||
import java.time.Instant; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class SdkEventBuilderTest { | ||
|
||
@Test | ||
void emit() { | ||
String eventDomain = "mydomain"; | ||
String eventName = "banana"; | ||
|
||
LogRecordBuilder logRecordBuilder = mock(LogRecordBuilder.class); | ||
when(logRecordBuilder.setTimestamp(anyLong(), any())).thenReturn(logRecordBuilder); | ||
when(logRecordBuilder.setAttribute(any(), any())).thenReturn(logRecordBuilder); | ||
|
||
Instant instant = Instant.now(); | ||
new SdkEventBuilder(logRecordBuilder, eventDomain, eventName) | ||
.setTimestamp(123456L, TimeUnit.NANOSECONDS) | ||
.setTimestamp(instant) | ||
.emit(); | ||
verify(logRecordBuilder).setAttribute(stringKey("event.domain"), eventDomain); | ||
verify(logRecordBuilder).setAttribute(stringKey("event.name"), eventName); | ||
verify(logRecordBuilder).setTimestamp(123456L, TimeUnit.NANOSECONDS); | ||
verify(logRecordBuilder).setTimestamp(instant); | ||
verify(logRecordBuilder).emit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters