brave.context.slf4j.MDCScopeDecorator Java Examples

The following examples show how to use brave.context.slf4j.MDCScopeDecorator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TracingConfiguration.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
/** Controls aspects of tracing such as the service name that shows up in the UI */
@Bean Tracing tracing(@Value("${spring.application.name}") String serviceName) {
  return Tracing.newBuilder()
      .localServiceName(serviceName)
      .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "user-name"))
      .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder()
          .addScopeDecorator(MDCScopeDecorator.create()) // puts trace IDs into logs
          .build()
      )
      .spanReporter(spanReporter()).build();
}
 
Example #2
Source File: TraceBaggageConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(CorrelationScopeDecorator.class)
@ConditionalOnBean(CorrelationScopeDecorator.Builder.class)
@ConditionalOnProperty(value = "spring.sleuth.baggage.correlation-enabled",
		matchIfMissing = true)
ScopeDecorator correlationScopeDecorator(
		@Qualifier(WHITELISTED_MDC_KEYS) List<String> whiteListedMDCKeys,
		SleuthBaggageProperties sleuthBaggageProperties,
		@Nullable List<CorrelationScopeCustomizer> correlationScopeCustomizers) {

	Set<String> correlationFields = redirectOldPropertyToNew(WHITELISTED_MDC_KEYS,
			whiteListedMDCKeys, "spring.sleuth.baggage.correlation-fields",
			sleuthBaggageProperties.getCorrelationFields());

	// Add fields from properties
	CorrelationScopeDecorator.Builder builder = MDCScopeDecorator.newBuilder();
	for (String field : correlationFields) {
		builder.add(SingleCorrelationField.newBuilder(BaggageField.create(field))
				.build());
	}

	// handle user overrides
	if (correlationScopeCustomizers != null) {
		for (CorrelationScopeCustomizer customizer : correlationScopeCustomizers) {
			customizer.customize(builder);
		}
	}
	return builder.build();
}
 
Example #3
Source File: CatalogTracing.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static HttpTracing createHttpTracing(String serviceName, AsyncReporter<Span> reporter) {
    final Tracing tracing = Tracing
        .newBuilder()
        .localServiceName(serviceName)
        .currentTraceContext(
            ThreadLocalCurrentTraceContext
                .newBuilder()
                .addScopeDecorator(MDCScopeDecorator.create()) 
                .build()
          )
        .spanReporter(reporter)
        .build();
    
    return HttpTracing.create(tracing);
}
 
Example #4
Source File: TracingConfiguration.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Bean
CurrentTraceContext currentTraceContext() {
  return ThreadLocalCurrentTraceContext.newBuilder().addScopeDecorator(MDCScopeDecorator.create()).build();
}
 
Example #5
Source File: AbstractZipkinFactory.java    From dropwizard-zipkin with Apache License 2.0 4 votes vote down vote up
/**
 * Build a new {@link HttpTracing} instance for interfacing with Zipkin
 *
 * @param environment Environment
 * @param reporter reporter
 * @return HttpTracing instance
 */
protected Optional<HttpTracing> buildTracing(
    final Environment environment, final SpanHandler reporter) {

  LOGGER.info(
      "Registering Zipkin service ({}) at <{}:{}>", serviceName, serviceHost, servicePort);

  final Tracing tracing =
      Tracing.newBuilder()
          .currentTraceContext(
              ThreadLocalCurrentTraceContext.newBuilder()
                  .addScopeDecorator(MDCScopeDecorator.get())
                  .build())
          .localIp(serviceHost)
          .localPort(servicePort)
          .addSpanHandler(reporter)
          .localServiceName(serviceName)
          .sampler(getSampler())
          .traceId128Bit(traceId128Bit)
          .build();

  final HttpTracing.Builder httpTracingBuilder = HttpTracing.newBuilder(tracing);
  if (clientParser != null) httpTracingBuilder.clientParser(clientParser);
  if (clientRequestParser != null) httpTracingBuilder.clientRequestParser(clientRequestParser);
  if (clientResponseParser != null) httpTracingBuilder.clientResponseParser(clientResponseParser);
  if (serverRequestParser != null) httpTracingBuilder.serverRequestParser(serverRequestParser);
  if (serverResponseParser != null) httpTracingBuilder.serverResponseParser(serverResponseParser);
  if (serverParser != null) httpTracingBuilder.serverParser(serverParser);
  if (clientSampler != null) httpTracingBuilder.clientSampler(clientSampler);
  if (serverSampler != null) httpTracingBuilder.serverSampler(serverSampler);

  final HttpTracing httpTracing = httpTracingBuilder.build();

  // Register the tracing feature for client and server requests
  environment.jersey().register(TracingApplicationEventListener.create(httpTracing));
  environment
      .lifecycle()
      .manage(
          new Managed() {
            @Override
            public void start() {
              // nothing to start
            }

            @Override
            public void stop() {
              tracing.close();
            }
          });

  return Optional.of(httpTracing);
}
 
Example #6
Source File: TraceHelper.java    From mdw with Apache License 2.0 4 votes vote down vote up
public static CurrentTraceContext getDefaultCurrentTraceContext() {
    return ThreadLocalCurrentTraceContext.newBuilder()
            .addScopeDecorator(MDCScopeDecorator.create())
            .build();
}
 
Example #7
Source File: TraceBaggageConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(MDC.class)
CorrelationScopeDecorator.Builder correlationScopeDecoratorBuilder() {
	return MDCScopeDecorator.newBuilder();
}
 
Example #8
Source File: TracingConfiguration.java    From brave-webmvc-example with MIT License 4 votes vote down vote up
/** Allows log patterns to use {@code %{traceId}} {@code %{spanId}} and {@code %{userName}} */
@Bean ScopeDecorator correlationScopeDecorator() {
  return MDCScopeDecorator.newBuilder()
      .add(SingleCorrelationField.create(USER_NAME)).build();
}