Commit b9f07542 authored by 孙 楠's avatar 孙 楠

更新 zipkin 版本

parent 7be1a2da
...@@ -73,10 +73,18 @@ ...@@ -73,10 +73,18 @@
<groupId>io.zipkin.brave</groupId> <groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-spring-rabbit</artifactId> <artifactId>brave-instrumentation-spring-rabbit</artifactId>
</dependency> </dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-context-slf4j</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId> <artifactId>transmittable-thread-local</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
package cn.quantgroup.tech.brave.configuration; package cn.quantgroup.tech.brave.configuration;
import brave.Tracing; import brave.Tracing;
import brave.context.slf4j.MDCScopeDecorator;
import brave.http.HttpTracing; import brave.http.HttpTracing;
import brave.httpclient.TracingHttpClientBuilder; import brave.httpclient.TracingHttpClientBuilder;
import brave.okhttp3.TracingInterceptor; import brave.okhttp3.TracingInterceptor;
import brave.propagation.B3Propagation; import brave.propagation.B3Propagation;
import brave.propagation.ExtraFieldPropagation; import brave.propagation.ExtraFieldPropagation;
import brave.propagation.ThreadLocalCurrentTraceContext;
import brave.spring.web.TracingClientHttpRequestInterceptor; import brave.spring.web.TracingClientHttpRequestInterceptor;
import brave.spring.webmvc.SpanCustomizingAsyncHandlerInterceptor; import brave.spring.webmvc.SpanCustomizingAsyncHandlerInterceptor;
import cn.quantgroup.tech.brave.filter.CustomDelegatingTracingFilter; import cn.quantgroup.tech.brave.filter.CustomDelegatingTracingFilter;
...@@ -13,7 +15,6 @@ import cn.quantgroup.tech.brave.properties.BraveProperties; ...@@ -13,7 +15,6 @@ import cn.quantgroup.tech.brave.properties.BraveProperties;
import cn.quantgroup.tech.brave.properties.ServiceProperties; import cn.quantgroup.tech.brave.properties.ServiceProperties;
import cn.quantgroup.tech.brave.service.*; import cn.quantgroup.tech.brave.service.*;
import cn.quantgroup.tech.brave.service.impl.*; import cn.quantgroup.tech.brave.service.impl.*;
import cn.quantgroup.tech.brave.slf4j.MDCCurrentTraceContext;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.Dispatcher; import okhttp3.Dispatcher;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
...@@ -220,7 +221,7 @@ public class BraveAutoConfiguration { ...@@ -220,7 +221,7 @@ public class BraveAutoConfiguration {
.sampler(brave.sampler.Sampler.create(braveProperties.getSample())) .sampler(brave.sampler.Sampler.create(braveProperties.getSample()))
.localServiceName(serviceProperties.getName()) .localServiceName(serviceProperties.getName())
.propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "user-name")) .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "user-name"))
.currentTraceContext(MDCCurrentTraceContext.create()) .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder().addScopeDecorator(MDCScopeDecorator.get()).build())
.spanReporter(spanReporter()).build(); .spanReporter(spanReporter()).build();
} }
......
package cn.quantgroup.tech.brave.service.impl; package cn.quantgroup.tech.brave.service.impl;
import brave.context.slf4j.MDCScopeDecorator;
import brave.propagation.CurrentTraceContext; import brave.propagation.CurrentTraceContext;
import brave.propagation.ThreadLocalCurrentTraceContext;
import cn.quantgroup.tech.brave.service.ITechExecutorServiceBuilder; import cn.quantgroup.tech.brave.service.ITechExecutorServiceBuilder;
import cn.quantgroup.tech.brave.slf4j.MDCCurrentTraceContext;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
...@@ -19,7 +20,8 @@ public class TechExecutorServiceBuilderTrace implements ITechExecutorServiceBuil ...@@ -19,7 +20,8 @@ public class TechExecutorServiceBuilderTrace implements ITechExecutorServiceBuil
@Override @Override
public ExecutorService buildExecutorService(ExecutorService executorService) { public ExecutorService buildExecutorService(ExecutorService executorService) {
log.info("构建ExecutorServiceTrace"); log.info("构建ExecutorServiceTrace");
CurrentTraceContext mdcCurrentTraceContext = MDCCurrentTraceContext.create(); CurrentTraceContext mdcCurrentTraceContext = ThreadLocalCurrentTraceContext
.newBuilder().addScopeDecorator(MDCScopeDecorator.get()).build();
return mdcCurrentTraceContext.executorService(executorService); return mdcCurrentTraceContext.executorService(executorService);
} }
...@@ -32,7 +34,8 @@ public class TechExecutorServiceBuilderTrace implements ITechExecutorServiceBuil ...@@ -32,7 +34,8 @@ public class TechExecutorServiceBuilderTrace implements ITechExecutorServiceBuil
@Override @Override
public Runnable wrap(Runnable task) { public Runnable wrap(Runnable task) {
log.info("wrap Runnable 4 support trace"); log.info("wrap Runnable 4 support trace");
CurrentTraceContext mdcCurrentTraceContext = MDCCurrentTraceContext.create(); CurrentTraceContext mdcCurrentTraceContext = ThreadLocalCurrentTraceContext
.newBuilder().addScopeDecorator(MDCScopeDecorator.get()).build();
return mdcCurrentTraceContext.wrap(task); return mdcCurrentTraceContext.wrap(task);
} }
} }
package cn.quantgroup.tech.brave.slf4j;
import brave.internal.codec.HexCodec;
import brave.internal.Nullable;
import brave.propagation.CurrentTraceContext;
import brave.propagation.TraceContext;
import org.slf4j.MDC;
import static brave.internal.codec.HexCodec.lowerHexEqualsTraceId;
import static brave.internal.codec.HexCodec.lowerHexEqualsUnsignedLong;
/**
* Adds {@linkplain MDC} properties TRACE_ID, PARENT_ID and SPAN_ID when a {@link
* brave.Tracer#currentSpan() span is current}. These can be used in log correlation.
*/
public final class MDCCurrentTraceContext extends CurrentTraceContext {
private static final String TRACE_ID = "X-B3-TraceId";
private static final String SPAN_ID = "X-B3-SpanId";
private static final String PARENT_ID = "X-B3-ParentId";
private static final String EXPORTABLE = "X-Span-Export";
public static MDCCurrentTraceContext create() {
return create(CurrentTraceContext.Default.inheritable());
}
public static MDCCurrentTraceContext create(CurrentTraceContext delegate) {
return new MDCCurrentTraceContext(delegate);
}
final CurrentTraceContext delegate;
MDCCurrentTraceContext(CurrentTraceContext delegate) {
if (delegate == null) throw new NullPointerException("delegate == null");
this.delegate = delegate;
}
@Override
public TraceContext get() {
return delegate.get();
}
@Override
public Scope newScope(@Nullable TraceContext currentSpan) {
return newScope(currentSpan, MDC.get(TRACE_ID), MDC.get(SPAN_ID), MDC.get(EXPORTABLE));
}
@Override
public Scope maybeScope(@Nullable TraceContext currentSpan) {
String previousTraceId = MDC.get(TRACE_ID);
String previousSpanId = MDC.get(SPAN_ID);
String sampled = MDC.get(EXPORTABLE);
if (currentSpan == null) {
if (previousTraceId == null) {
return Scope.NOOP;
}
return newScope(null, previousTraceId, previousSpanId, sampled);
}
if (lowerHexEqualsTraceId(previousTraceId, currentSpan)
&& lowerHexEqualsUnsignedLong(previousSpanId, currentSpan.spanId())) {
return Scope.NOOP;
}
return newScope(currentSpan, previousTraceId, previousSpanId, sampled);
}
// all input parameters are nullable
Scope newScope(TraceContext currentSpan, String previousTraceId, String previousSpanId, String sampled) {
String previousParentId = MDC.get(PARENT_ID);
if (currentSpan != null) {
maybeReplaceTraceContext(currentSpan, previousTraceId, previousParentId, previousSpanId, sampled);
} else {
MDC.remove(TRACE_ID);
MDC.remove(PARENT_ID);
MDC.remove(SPAN_ID);
MDC.remove(EXPORTABLE);
}
Scope scope = delegate.newScope(currentSpan);
class MDCCurrentTraceContextScope implements Scope {
@Override
public void close() {
scope.close();
replace(TRACE_ID, previousTraceId);
replace(PARENT_ID, previousParentId);
replace(SPAN_ID, previousSpanId);
//true = 采样了. false = 未采样. null = 决定不了...等会再说
replace(EXPORTABLE, sampled);
}
}
return new MDCCurrentTraceContextScope();
}
void maybeReplaceTraceContext(
TraceContext currentSpan,
String previousTraceId,
@Nullable String previousParentId,
String previousSpanId,
@Nullable String sampled
) {
MDC.put(EXPORTABLE, String.valueOf(currentSpan.sampled()));
boolean sameTraceId = lowerHexEqualsTraceId(previousTraceId, currentSpan);
if (!sameTraceId) {
MDC.put(TRACE_ID, currentSpan.traceIdString());
}
long parentId = currentSpan.parentIdAsLong();
if (parentId == 0L) {
MDC.remove(PARENT_ID);
} else {
boolean sameParentId = lowerHexEqualsUnsignedLong(previousParentId, parentId);
if (!sameParentId) MDC.put(PARENT_ID, HexCodec.toLowerHex(parentId));
}
boolean sameSpanId = lowerHexEqualsUnsignedLong(previousSpanId, currentSpan.spanId());
if (!sameSpanId) MDC.put(SPAN_ID, HexCodec.toLowerHex(currentSpan.spanId()));
}
static void replace(String key, @Nullable String value) {
if (value != null) {
MDC.put(key, value);
} else {
MDC.remove(key);
}
}
}
...@@ -106,31 +106,6 @@ ...@@ -106,31 +106,6 @@
</dependency> </dependency>
<!-- zipkin --> <!-- zipkin -->
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter</artifactId>
<version>${zipkin-reporter2.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.zipkin2</groupId>
<artifactId>zipkin</artifactId>
<version>${zipkin.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave</artifactId>
<version>${brave.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-spring-beans</artifactId>
<version>${brave.version}</version>
</dependency>
<dependency> <dependency>
<groupId>io.zipkin.reporter2</groupId> <groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-sender-kafka11</artifactId> <artifactId>zipkin-sender-kafka11</artifactId>
...@@ -138,33 +113,21 @@ ...@@ -138,33 +113,21 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.zipkin.brave</groupId> <groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-spring-web</artifactId> <artifactId>brave-bom</artifactId>
<version>${brave.version}</version> <version>${brave.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.zipkin.brave</groupId> <groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-spring-webmvc</artifactId> <artifactId>brave-context-slf4j</artifactId>
<version>${brave.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-httpclient</artifactId>
<version>${brave.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-mysql</artifactId>
<version>${brave.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-okhttp3</artifactId>
<version>${brave.version}</version> <version>${brave.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.zipkin.brave</groupId> <groupId>com.squareup.okhttp3</groupId>
<artifactId>brave-instrumentation-spring-rabbit</artifactId> <artifactId>okhttp</artifactId>
<version>${brave.version}</version> <version>${okhttp.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment