Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Add MDCScopeManager for correlating logs with trace context #718

Merged
merged 12 commits into from Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -611,6 +611,11 @@ public Builder withScopeManager(ScopeManager scopeManager) {
return this;
}

public Builder withMDcScopeManager() {
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
this.scopeManager = new MDcScopeManager();
return this;
}

public Builder withClock(Clock clock) {
this.clock = clock;
return this;
Expand Down
69 changes: 69 additions & 0 deletions jaeger-core/src/main/java/io/jaegertracing/internal/MDcScope.java
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2018, The Jaeger Authors
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
*
* 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.jaegertracing.internal;

import io.opentracing.Scope;
import io.opentracing.Span;
import org.slf4j.MDC;

public class MDcScope implements Scope {
avimas marked this conversation as resolved.
Show resolved Hide resolved
private static final String TRACE_ID = "traceId";
private static final String SPAN_ID = "spanId";
private static final String SAMPLED = "sampled";
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved

private final Scope wrapped;
private final String originalTraceId;
private final String originalSpanId;
private final String originalSampled;

/**
* mdcScope.
*/
public MDcScope(Scope scope, Span span) {
this.wrapped = scope;
this.originalTraceId = MDC.get(TRACE_ID);
this.originalSpanId = MDC.get(SPAN_ID);
this.originalSampled = MDC.get(SAMPLED);

if (span.context() instanceof JaegerSpanContext) {
putContext((JaegerSpanContext) span.context());
}
}

@Override
public void close() {
wrapped.close();

MDC.remove(TRACE_ID);
MDC.remove(SPAN_ID);
MDC.remove(SAMPLED);

if (originalTraceId != null) {
MDC.put(TRACE_ID, originalTraceId);
}
if (originalSpanId != null) {
MDC.put(SPAN_ID, originalSpanId);
}
if (originalSampled != null) {
MDC.put(SAMPLED, originalSampled);
}
}

protected void putContext(JaegerSpanContext spanContext) {
MDC.put(TRACE_ID, spanContext.getTraceId());
avimas marked this conversation as resolved.
Show resolved Hide resolved
MDC.put(SPAN_ID, String.format("%16x", spanContext.getSpanId()));
MDC.put(SAMPLED, Boolean.toString(spanContext.isSampled()));
}
}
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2018, The Jaeger 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.jaegertracing.internal;

import io.opentracing.Scope;
import io.opentracing.ScopeManager;
import io.opentracing.Span;
import io.opentracing.util.ThreadLocalScopeManager;

public class MDcScopeManager implements ScopeManager {
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
private final ScopeManager wrapped = new ThreadLocalScopeManager();
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved

@Override
public Scope activate(Span span) {
return new MDcScope(wrapped.activate(span), span);
}

@Override
public Span activeSpan() {
return wrapped.activeSpan();
}
}