-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement MDC auto-instrumentation for log4j2
- Loading branch information
Mateusz Rzeszutek
committed
Sep 16, 2020
1 parent
e323d3c
commit 9d9b54d
Showing
7 changed files
with
97 additions
and
37 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
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
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
66 changes: 66 additions & 0 deletions
66
...a/io/opentelemetry/instrumentation/auto/log4j/v2_7/SpanDecoratingContextDataInjector.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,66 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.auto.log4j.v2_7; | ||
|
||
import static io.opentelemetry.instrumentation.api.log.LoggingContextConstants.SAMPLED; | ||
import static io.opentelemetry.instrumentation.api.log.LoggingContextConstants.SPAN_ID; | ||
import static io.opentelemetry.instrumentation.api.log.LoggingContextConstants.TRACE_ID; | ||
|
||
import io.opentelemetry.trace.SpanContext; | ||
import io.opentelemetry.trace.TracingContextUtils; | ||
import java.util.List; | ||
import org.apache.logging.log4j.core.ContextDataInjector; | ||
import org.apache.logging.log4j.core.config.Property; | ||
import org.apache.logging.log4j.util.ReadOnlyStringMap; | ||
import org.apache.logging.log4j.util.SortedArrayStringMap; | ||
import org.apache.logging.log4j.util.StringMap; | ||
|
||
public final class SpanDecoratingContextDataInjector implements ContextDataInjector { | ||
private final ContextDataInjector delegate; | ||
|
||
public SpanDecoratingContextDataInjector(ContextDataInjector delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public StringMap injectContextData(List<Property> list, StringMap stringMap) { | ||
StringMap contextData = delegate.injectContextData(list, stringMap); | ||
|
||
if (contextData.containsKey(TRACE_ID)) { | ||
// Assume already instrumented event if traceId is present. | ||
return contextData; | ||
} | ||
|
||
SpanContext currentContext = TracingContextUtils.getCurrentSpan().getContext(); | ||
if (!currentContext.isValid()) { | ||
return contextData; | ||
} | ||
|
||
StringMap newContextData = new SortedArrayStringMap(contextData); | ||
newContextData.putValue(TRACE_ID, currentContext.getTraceIdAsHexString()); | ||
newContextData.putValue(SPAN_ID, currentContext.getSpanIdAsHexString()); | ||
if (currentContext.isSampled()) { | ||
newContextData.putValue(SAMPLED, "true"); | ||
} | ||
return newContextData; | ||
} | ||
|
||
@Override | ||
public ReadOnlyStringMap rawContextData() { | ||
return delegate.rawContextData(); | ||
} | ||
} |
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