-
Notifications
You must be signed in to change notification settings - Fork 858
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db38619
commit a93c3b0
Showing
7 changed files
with
219 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
24 changes: 24 additions & 0 deletions
24
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,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(); | ||
} |
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
62 changes: 62 additions & 0 deletions
62
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,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(); | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
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,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(); | ||
} | ||
} |
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