-
Notifications
You must be signed in to change notification settings - Fork 36
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
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
945837d
Slf4JEventListener support custom key
ShenFeng312 e264ca7
Polish codestyle
ShenFeng312 56932e3
Fix OtelTracingApiTests test cases && polish codestyle
ShenFeng312 3f9456d
Polish OtelTracingApiTests
ShenFeng312 8bfd679
Polish OtelTracingApiTests
ShenFeng312 b075d57
Polish OtelTracingApiTests
ShenFeng312 20fec8f
Fix OtelTracingApiTests codestyle
ShenFeng312 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we call this |
||
|
||
// [Micrometer Tracing component] A Micrometer Tracing listener for setting | ||
// Baggage in MDC. Customizable | ||
// with correlation fields (currently we're setting empty list) | ||
|
@@ -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(); | ||
|
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary?
There was a problem hiding this comment.
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 initMDC
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is it used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here https://github.com/qos-ch/slf4j/blob/v_1.7.36/slf4j-api/src/main/java/org/slf4j/MDC.java#L97