Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;

import java.io.PrintWriter;
import java.io.StringWriter;
Expand Down Expand Up @@ -42,16 +43,11 @@ public String doLayout(ILoggingEvent event) {
}

private OtelLogRecord buildEnrichedOtelLogRecord(ILoggingEvent event) {
String traceId = "";
String spanId = "";
String traceFlags = "";

ProxyContext proxyContext = ContextManager.getProxyContext();
if (proxyContext != null) {
traceId = proxyContext.getTraceId();
spanId = proxyContext.getSpanId();
traceFlags = proxyContext.getTraceFlags();
}
//Note. context storage is managed by io.vertx.tracing.opentelemetry.VertxContextStorageProvider
SpanContext spanContext = Span.current().getSpanContext();
String traceId = spanContext.getTraceId();
String spanId = spanContext.getSpanId();
String traceFlags = spanContext.getTraceFlags().asHex();

Map<String, Object> attributes = new LinkedHashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.TraceFlags;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
Expand All @@ -36,6 +38,9 @@ class AutoEnrichedOtelJsonLayoutTest {
private MockedStatic<Vertx> vertxMock;
private MockedStatic<ContextManager> contextManagerMock;
private MockedStatic<Span> spanMock;
private Span currentSpan;
private SpanContext spanContext;
private TraceFlags traceFlags;

@BeforeEach
void setUp() {
Expand All @@ -52,9 +57,13 @@ void setUp() {

// Mock Span
spanMock = mockStatic(Span.class);
Span mockSpan = mock(Span.class);
when(mockSpan.isRecording()).thenReturn(false);
spanMock.when(Span::current).thenReturn(mockSpan);
currentSpan = mock(Span.class);
when(currentSpan.isRecording()).thenReturn(false);
spanMock.when(Span::current).thenReturn(currentSpan);
spanContext = mock(SpanContext.class);
when(currentSpan.getSpanContext()).thenReturn(spanContext);
traceFlags = mock(TraceFlags.class);
when(spanContext.getTraceFlags()).thenReturn(traceFlags);
}

@AfterEach
Expand Down Expand Up @@ -257,9 +266,9 @@ void shouldIncludeTraceContextFromProxyContext() throws Exception {
ProxyContext proxyContext = mock(ProxyContext.class);
HttpServerResponse response = mock(HttpServerResponse.class);
when(response.ended()).thenReturn(false);
when(proxyContext.getTraceId()).thenReturn("22510e56eb9b21f6b03dbc038cd8fb71");
when(proxyContext.getSpanId()).thenReturn("8a46c76f1554b00a");
when(proxyContext.getTraceFlags()).thenReturn("01");
when(spanContext.getTraceId()).thenReturn("22510e56eb9b21f6b03dbc038cd8fb71");
when(spanContext.getSpanId()).thenReturn("8a46c76f1554b00a");
when(traceFlags.asHex()).thenReturn("01");
when(proxyContext.getResponse()).thenReturn(response);

contextManagerMock.when(ContextManager::getProxyContext).thenReturn(proxyContext);
Expand Down
Loading