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

Slf4JEventListener support custom key #160

Merged
merged 7 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.assertj:assertj-core'
testImplementation 'org.mockito:mockito-core'
testImplementation 'ch.qos.logback:logback-classic'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we need StaticMDCBinder to init MDC

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is it used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,50 @@ public class Slf4JEventListener implements EventListener {

private static final InternalLogger log = InternalLoggerFactory.getInstance(Slf4JEventListener.class);

private static final String DEFAULT_TRACE_ID_KEY = "traceId";

private static final String DEFAULT_SPAN_ID_KEY = "spanId";

private final String traceIdKey;

private final String spanIdKey;

public Slf4JEventListener() {
this(DEFAULT_TRACE_ID_KEY, DEFAULT_SPAN_ID_KEY);
}

/**
* @param traceIdKey custom traceId Key
* @param spanIdKey custom spanId Key
* @since 1.1.0
*/
public Slf4JEventListener(String traceIdKey, String spanIdKey) {
jonatan-ivanov marked this conversation as resolved.
Show resolved Hide resolved
this.traceIdKey = traceIdKey;
this.spanIdKey = spanIdKey;
}

private void onScopeAttached(EventPublishingContextWrapper.ScopeAttachedEvent event) {
log.trace("Got scope changed event [{}]", event);
Span span = event.getSpan();
if (span != null) {
MDC.put("traceId", span.getSpanContext().getTraceId());
MDC.put("spanId", span.getSpanContext().getSpanId());
MDC.put(traceIdKey, span.getSpanContext().getTraceId());
MDC.put(spanIdKey, span.getSpanContext().getSpanId());
}
}

private void onScopeRestored(EventPublishingContextWrapper.ScopeRestoredEvent event) {
log.trace("Got scope restored event [{}]", event);
Span span = event.getSpan();
if (span != null) {
MDC.put("traceId", span.getSpanContext().getTraceId());
MDC.put("spanId", span.getSpanContext().getSpanId());
MDC.put(traceIdKey, span.getSpanContext().getTraceId());
MDC.put(spanIdKey, span.getSpanContext().getSpanId());
}
}

private void onScopeClosed(EventPublishingContextWrapper.ScopeClosedEvent event) {
log.trace("Got scope closed event [{}]", event);
MDC.remove("traceId");
MDC.remove("spanId");
MDC.remove(traceIdKey);
MDC.remove(spanIdKey);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.micrometer.tracing.BaggageInScope;
import io.micrometer.tracing.Span;
import io.micrometer.tracing.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.extension.trace.propagation.B3Propagator;
import io.opentelemetry.sdk.OpenTelemetrySdk;
Expand All @@ -28,6 +29,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.MDC;

import java.util.Collections;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -60,6 +62,12 @@ class OtelTracingApiTests {
// [Micrometer Tracing component] A Micrometer Tracing listener for setting up MDC
Slf4JEventListener slf4JEventListener = new Slf4JEventListener();

private final String customTraceIdKey = "customTraceId";

private final String customSpanIdKey = "customSpanId";

Slf4JEventListener slf4JEventListenerCustom = new Slf4JEventListener(customTraceIdKey, customSpanIdKey);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we call this customSlf4JEventListener?
Also, is it possible to move these three lines to the new test method you created?


// [Micrometer Tracing component] A Micrometer Tracing listener for setting
// Baggage in MDC. Customizable
// with correlation fields (currently we're setting empty list)
Expand Down Expand Up @@ -209,4 +217,63 @@ void should_work_with_baggage() {
.isEqualTo("value 3");
}

@Test
void testSlf4JEventListener() {
Span newSpan = this.tracer.nextSpan().name("testMDC");
try (Tracer.SpanInScope ws = this.tracer.withSpan(newSpan.start())) {
Context current = Context.current();

EventPublishingContextWrapper.ScopeAttachedEvent scopeAttachedEvent = new EventPublishingContextWrapper.ScopeAttachedEvent(
current);
slf4JEventListener.onEvent(scopeAttachedEvent);
slf4JEventListenerCustom.onEvent(scopeAttachedEvent);

String traceId = MDC.get("traceId");
String customTraceId = MDC.get(customTraceIdKey);

then(traceId).isNotBlank();
then(customTraceId).isNotBlank();
then(traceId).isEqualTo(customTraceId);

String spanId = MDC.get("spanId");
String customSpanId = MDC.get(customSpanIdKey);

then(spanId).isNotBlank();
then(customSpanId).isNotBlank();
then(spanId).isEqualTo(customSpanId);

EventPublishingContextWrapper.ScopeRestoredEvent scopeRestoredEvent = new EventPublishingContextWrapper.ScopeRestoredEvent(
current);
slf4JEventListener.onEvent(scopeRestoredEvent);
slf4JEventListenerCustom.onEvent(scopeRestoredEvent);

traceId = MDC.get("traceId");
customTraceId = MDC.get(customTraceIdKey);

then(traceId).isNotBlank();
then(customTraceId).isNotBlank();
then(traceId).isEqualTo(customTraceId);

spanId = MDC.get("spanId");
customSpanId = MDC.get(customSpanIdKey);

then(spanId).isNotBlank();
then(customSpanId).isNotBlank();
then(spanId).isEqualTo(customSpanId);
}
finally {
newSpan.end();
}

EventPublishingContextWrapper.ScopeClosedEvent scopeClosedEvent = new EventPublishingContextWrapper.ScopeClosedEvent();
slf4JEventListener.onEvent(scopeClosedEvent);
slf4JEventListenerCustom.onEvent(scopeClosedEvent);

then(MDC.get("traceId")).isNull();
then(MDC.get(customTraceIdKey)).isNull();
then(MDC.get("spanId")).isNull();
then(MDC.get(customSpanIdKey)).isNull();

}

}