zipkin2.reporter.brave.ZipkinSpanHandler Java Examples

The following examples show how to use zipkin2.reporter.brave.ZipkinSpanHandler. 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: ZipkinSpanHandlerFactoryBeanTest.java    From zipkin-reporter-java with Apache License 2.0 6 votes vote down vote up
@Test public void errorTag() {
  context = new XmlBeans(""
      + "<bean id=\"zipkinSpanHandler\" class=\"zipkin2.reporter.beans.ZipkinSpanHandlerFactoryBean\">\n"
      + "  <property name=\"spanReporter\">\n"
      + "    <util:constant static-field=\"zipkin2.reporter.Reporter.CONSOLE\"/>\n"
      + "  </property>\n"
      + "  <property name=\"errorTag\">\n"
      + "    <util:constant static-field=\"" + getClass().getName() + ".ERROR_TAG\"/>\n"
      + "  </property>\n"
      + "</bean>"
  );

  assertThat(context.getBean("zipkinSpanHandler", ZipkinSpanHandler.class))
      .extracting("spanReporter.errorTag")
      .isSameAs(ERROR_TAG);
}
 
Example #2
Source File: ZipkinAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
/** Returns one handler for as many reporters as exist. */
@Bean
SpanHandler zipkinSpanHandler(@Nullable List<Reporter<Span>> spanReporters,
		@Nullable Tag<Throwable> errorTag) {
	if (spanReporters == null) {
		return SpanHandler.NOOP;
	}

	LinkedHashSet<Reporter<Span>> reporters = new LinkedHashSet<>(spanReporters);
	reporters.remove(Reporter.NOOP);
	if (spanReporters.isEmpty()) {
		return SpanHandler.NOOP;
	}

	Reporter<Span> spanReporter = reporters.size() == 1 ? reporters.iterator().next()
			: new CompositeSpanReporter(reporters.toArray(new Reporter[0]));

	ZipkinSpanHandler.Builder builder = ZipkinSpanHandler.newBuilder(spanReporter);
	if (errorTag != null) {
		builder.errorTag(errorTag);
	}
	return builder.build();
}
 
Example #3
Source File: ZipkinAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
@Test
void span_handler_comparator() {
	SpanHandler handler1 = mock(SpanHandler.class);
	SpanHandler handler2 = mock(SpanHandler.class);
	ZipkinSpanHandler zipkin1 = mock(ZipkinSpanHandler.class);
	ZipkinSpanHandler zipkin2 = mock(ZipkinSpanHandler.class);

	ArrayList<SpanHandler> spanHandlers = new ArrayList<>();
	spanHandlers.add(handler1);
	spanHandlers.add(zipkin1);
	spanHandlers.add(handler2);
	spanHandlers.add(zipkin2);

	spanHandlers.sort(SPAN_HANDLER_COMPARATOR);

	assertThat(spanHandlers).containsExactly(handler1, handler2, zipkin1, zipkin2);
}
 
Example #4
Source File: ConsoleZipkinFactory.java    From dropwizard-zipkin with Apache License 2.0 5 votes vote down vote up
/**
 * Build a new {@link HttpTracing} instance for interfacing with Zipkin
 *
 * @param environment Environment
 * @return HttpTracing instance
 */
@Override
public Optional<HttpTracing> build(final Environment environment) {
  if (!isEnabled()) {
    LOGGER.warn("Zipkin tracing is disabled");
    return Optional.empty();
  }

  LOGGER.info("Sending spans to console");
  return buildTracing(environment, ZipkinSpanHandler.create(Reporter.CONSOLE));
}
 
Example #5
Source File: ReportingZipkinFactory.java    From dropwizard-zipkin with Apache License 2.0 5 votes vote down vote up
protected Optional<HttpTracing> buildTracing(final Environment environment, Sender sender) {
  final AsyncReporter<Span> reporter =
      AsyncReporter.builder(sender)
          .metrics(new DropwizardReporterMetrics(environment.metrics()))
          .messageTimeout(reportTimeout.toNanoseconds(), TimeUnit.NANOSECONDS)
          .build();

  environment.lifecycle().manage(new ReporterManager(reporter, sender));

  return buildTracing(environment, ZipkinSpanHandler.create(reporter));
}
 
Example #6
Source File: ZipkinSpanHandlerFactoryBeanTest.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
@Test public void spanReporter() {
  context = new XmlBeans(""
      + "<bean id=\"zipkinSpanHandler\" class=\"zipkin2.reporter.beans.ZipkinSpanHandlerFactoryBean\">\n"
      + "  <property name=\"spanReporter\">\n"
      + "    <util:constant static-field=\"zipkin2.reporter.Reporter.CONSOLE\"/>\n"
      + "  </property>\n"
      + "</bean>"
  );

  assertThat(context.getBean("zipkinSpanHandler", ZipkinSpanHandler.class))
      .extracting("spanReporter.delegate")
      .isEqualTo(Reporter.CONSOLE);
}
 
Example #7
Source File: ZipkinSpanHandlerFactoryBeanTest.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
@Test public void alwaysReportSpans() {
  context = new XmlBeans(""
      + "<bean id=\"zipkinSpanHandler\" class=\"zipkin2.reporter.beans.ZipkinSpanHandlerFactoryBean\">\n"
      + "  <property name=\"spanReporter\">\n"
      + "    <util:constant static-field=\"zipkin2.reporter.Reporter.CONSOLE\"/>\n"
      + "  </property>\n"
      + "  <property name=\"alwaysReportSpans\" value=\"true\"/>\n"
      + "</bean>"
  );

  assertThat(context.getBean("zipkinSpanHandler", ZipkinSpanHandler.class))
      .extracting("alwaysReportSpans")
      .isEqualTo(true);
}
 
Example #8
Source File: ZipkinSpanHandlerFactoryBean.java    From zipkin-reporter-java with Apache License 2.0 4 votes vote down vote up
@Override protected SpanHandler createInstance() {
  ZipkinSpanHandler.Builder builder = ZipkinSpanHandler.newBuilder(spanReporter);
  if (errorTag != null) builder.errorTag(errorTag);
  if (alwaysReportSpans != null) builder.alwaysReportSpans(alwaysReportSpans);
  return builder.build();
}
 
Example #9
Source File: Tracing.java    From brave with Apache License 2.0 4 votes vote down vote up
Default(Builder builder) {
  this.clock = builder.clock != null ? builder.clock : Platform.get().clock();
  this.errorParser = builder.errorParser;
  this.propagationFactory = builder.propagationFactory;
  this.stringPropagation = builder.propagationFactory.get();
  this.currentTraceContext = builder.currentTraceContext;
  this.sampler = builder.sampler;
  this.noop = new AtomicBoolean();

  MutableSpan defaultSpan = new MutableSpan(builder.defaultSpan); // safe copy

  Set<SpanHandler> spanHandlers = new LinkedHashSet<>(builder.spanHandlers);
  // When present, the Zipkin handler is invoked after the user-supplied ones.
  if (builder.zipkinSpanReporter != null) {
    spanHandlers.add(
        ZipkinSpanHandler.newBuilder((Reporter<zipkin2.Span>) builder.zipkinSpanReporter)
            .errorTag(errorParser)
            .alwaysReportSpans(builder.alwaysReportSpans)
            .build());
  }
  if (spanHandlers.isEmpty()) spanHandlers.add(new LogSpanHandler());
  if (builder.trackOrphans) {
    spanHandlers.add(OrphanTracker.newBuilder().defaultSpan(defaultSpan).clock(clock).build());
  }

  // Make sure any exceptions caused by span handlers don't crash callers
  SpanHandler spanHandler =
    NoopAwareSpanHandler.create(spanHandlers.toArray(new SpanHandler[0]), noop);

  boolean alwaysSampleLocal = builder.alwaysSampleLocal;
  for (SpanHandler handler : spanHandlers) {
    if (handler instanceof FinishedSpanHandler) {
      // Handle deprecated FinishedSpanHandler.alwaysSampleLocal
      if (((FinishedSpanHandler)handler).alwaysSampleLocal()) alwaysSampleLocal = true;
    }
  }

  this.tracer = new Tracer(
    builder.clock,
    builder.propagationFactory,
    spanHandler,
    new PendingSpans(defaultSpan, clock, spanHandler, noop),
    builder.sampler,
    builder.currentTraceContext,
    builder.traceId128Bit || propagationFactory.requires128BitTraceId(),
    builder.supportsJoin && propagationFactory.supportsJoin(),
    alwaysSampleLocal,
    noop
  );
  // assign current IFF there's no instance already current
  CURRENT.compareAndSet(null, this);
}