Skip to content

Commit

Permalink
add addevent overload
Browse files Browse the repository at this point in the history
  • Loading branch information
samvaity committed Jun 16, 2021
1 parent 1a92fbe commit c46413e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,20 @@ public Context getSharedSpanBuilder(String spanName, Context context) {
*/
@Override
public void addEvent(String eventName, Map<String, Object> traceEventAttributes, OffsetDateTime timestamp) {
addEvent(eventName, traceEventAttributes, timestamp, Context.NONE);
}

/**
* {@inheritDoc}
*/
@Override
public void addEvent(String eventName, Map<String, Object> traceEventAttributes, OffsetDateTime timestamp,
Context context) {
Objects.requireNonNull(eventName, "'eventName' cannot be null.");

Span currentSpan = Span.current();
Span currentSpan = getOrDefault(context, PARENT_SPAN_KEY, null, Span.class);
if (currentSpan == null) {
logger.info("Failed to find a starting span to associate the %s with.", eventName);
logger.info("Failed to find a starting span to associate the event %s with.", eventName);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public void startSpanOverloadNullPointerException() {
@Test
public void addLinkTest() {
// Arrange
SpanBuilder span = tracer.spanBuilder("parent-span");
SpanBuilder span = tracer.spanBuilder(PARENT_SPAN_KEY);
Span toLinkSpan = tracer.spanBuilder("new test span").startSpan();

Context spanContext = new Context(
Expand All @@ -317,7 +317,7 @@ public void addLinkTest() {
@Test
public void addLinkNoSpanContextTest() {
// Arrange
SpanBuilder span = tracer.spanBuilder("parent-span");
SpanBuilder span = tracer.spanBuilder(PARENT_SPAN_KEY);

// Act
openTelemetryTracer.addLink(new Context(SPAN_BUILDER_KEY, span));
Expand All @@ -331,7 +331,7 @@ public void addLinkNoSpanContextTest() {
@Test
public void addLinkNoSpanToLinkTest() {
// Arrange
SpanBuilder span = tracer.spanBuilder("parent-span");
SpanBuilder span = tracer.spanBuilder(PARENT_SPAN_KEY);

// Act
openTelemetryTracer.addLink(Context.NONE);
Expand Down Expand Up @@ -489,10 +489,11 @@ public void addEventWithNonNullEventName() {
final String eventName = "event-0";

// Act
openTelemetryTracer.addEvent(eventName, null, null);
openTelemetryTracer.addEvent(eventName, null, null, tracingContext);

// Assert
final ReadableSpan recordEventsSpan = (ReadableSpan) tracingContext.getData(PARENT_SPAN_KEY).get();
assertEquals(PARENT_SPAN_KEY, recordEventsSpan.getName());
List<EventData> eventData = recordEventsSpan.toSpanData().getEvents();
assertNotNull(eventData);
assertEquals(1, eventData.size());
Expand All @@ -513,10 +514,11 @@ public void addEventWithAttributes() {
}};

// Act
openTelemetryTracer.addEvent(eventName, input, null);
openTelemetryTracer.addEvent(eventName, input, null, tracingContext);

// Assert
final ReadableSpan recordEventsSpan = (ReadableSpan) tracingContext.getData(PARENT_SPAN_KEY).get();
assertEquals(PARENT_SPAN_KEY, recordEventsSpan.getName());
List<EventData> eventData = recordEventsSpan.toSpanData().getEvents();
assertNotNull(eventData);
assertEquals(1, eventData.size());
Expand All @@ -541,10 +543,11 @@ public void addEventWithTimeSpecification() {
OffsetDateTime eventTime = OffsetDateTime.parse("2021-01-01T18:35:24.00Z");

// Act
openTelemetryTracer.addEvent(eventName, null, eventTime);
openTelemetryTracer.addEvent(eventName, null, eventTime, tracingContext);

// Assert
final ReadableSpan recordEventsSpan = (ReadableSpan) tracingContext.getData(PARENT_SPAN_KEY).get();
assertEquals(PARENT_SPAN_KEY, recordEventsSpan.getName());
List<EventData> eventData = recordEventsSpan.toSpanData().getEvents();
assertNotNull(eventData);
assertEquals(1, eventData.size());
Expand All @@ -560,7 +563,7 @@ public void addEventAfterSpanEnd() {

// Act
parentSpan.end();
openTelemetryTracer.addEvent(eventName, null, null);
openTelemetryTracer.addEvent(eventName, null, null, tracingContext);

// Assert
final ReadableSpan recordEventsSpan = (ReadableSpan) tracingContext.getData(PARENT_SPAN_KEY).get();
Expand All @@ -570,6 +573,44 @@ public void addEventAfterSpanEnd() {
assertEquals(0, eventData.size());
}

@Test
public void addEventWithChildSpan() {
// Arrange
tracingContext = openTelemetryTracer.start("child-span-1", tracingContext);
final String eventName = "event-0";
OffsetDateTime eventTime = OffsetDateTime.parse("2021-01-01T18:35:24.00Z");

// Act
openTelemetryTracer.addEvent(eventName, null, eventTime, tracingContext);

// Assert
final ReadableSpan recordEventsSpan = (ReadableSpan) tracingContext.getData(PARENT_SPAN_KEY).get();
assertEquals("child-span-1", recordEventsSpan.getName());
List<EventData> eventData = recordEventsSpan.toSpanData().getEvents();
assertNotNull(eventData);
assertEquals(1, eventData.size());
assertEquals(eventName, eventData.get(0).getName());
assertEquals(eventTime,
OffsetDateTime.ofInstant(Instant.ofEpochMilli(eventData.get(0).getEpochNanos() / 1000000), ZoneOffset.UTC));
}

@Test
public void addEventWithoutContext() {
// Arrange
final String eventName = "event-0";
OffsetDateTime eventTime = OffsetDateTime.parse("2021-01-01T18:35:24.00Z");

// Act
openTelemetryTracer.addEvent(eventName, null, eventTime);

// Assert
final ReadableSpan recordEventsSpan = (ReadableSpan) tracingContext.getData(PARENT_SPAN_KEY).get();
assertEquals(PARENT_SPAN_KEY, recordEventsSpan.getName());
List<EventData> eventData = recordEventsSpan.toSpanData().getEvents();
assertNotNull(eventData);
assertEquals(0, eventData.size());
}

private static void assertSpanWithExplicitParent(Context updatedContext, String parentSpanId) {
assertNotNull(updatedContext.getData(PARENT_SPAN_KEY).get());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,22 @@ default Context getSharedSpanBuilder(String spanName, Context context) {
*/
default void addEvent(String name, Map<String, Object> attributes, OffsetDateTime timestamp) {
}

/**
* Adds an event to the current span with the provided {@code timestamp} and {@code attributes}.
* <p>This API does not provide any normalization if provided timestamps are out of range of the current
* span timeline</p>
* <p>Supported attribute values include String, double, boolean, long, String [], double [], long [].
* Any other Object value type and null values will be silently ignored.</p>
*
* @param name the name of the event.
* @param attributes the additional attributes to be set for the event.
* @param timestamp The instant, in UTC, at which the event will be associated to the span.
* @param context the call metadata containing information of the span to which the event should be associated with.
* @throws NullPointerException if {@code eventName} is {@code null}.
*/
default void addEvent(String name, Map<String, Object> attributes, OffsetDateTime timestamp,
Context context) {

}
}

0 comments on commit c46413e

Please sign in to comment.