brave.propagation.B3Propagation Java Examples

The following examples show how to use brave.propagation.B3Propagation. 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: ZipkinTraceFactory.java    From pampas with Apache License 2.0 6 votes vote down vote up
@Override
public Tracer getTracer() {
    Sender sender = OkHttpSender.create("http://192.168.20.131:9411/api/v2/spans");

    Reporter spanReporter = AsyncReporter.create(sender);
    // If you want to support baggage, indicate the fields you'd like to
    // whitelist, in this case "country-code" and "user-id". On the wire,
    // they will be prefixed like "baggage-country-code"
    Propagation.Factory propagationFactory = ExtraFieldPropagation.newFactoryBuilder(B3Propagation.FACTORY)
            .addPrefixedFields("baggage-", Arrays.asList("country-code", "user-id"))
            .build();
    Tracing braveTracing = Tracing.newBuilder()
            .localServiceName("gateway")
            .propagationFactory(propagationFactory)
            .spanReporter(spanReporter)
            .build();
    Tracer tracer = BraveTracer.create(braveTracing);

    return tracer;
}
 
Example #2
Source File: ITRemote.java    From brave with Apache License 2.0 6 votes vote down vote up
protected ITRemote() {
  CurrentTraceContext.Builder currentTraceContextBuilder = currentTraceContextBuilder();
  if (currentTraceContextBuilder instanceof StrictCurrentTraceContext.Builder) {
    currentTraceContext = currentTraceContextBuilder.build();
    checkForLeakedScopes = (Closeable) currentTraceContext;
  } else {
    StrictScopeDecorator strictScopeDecorator = StrictScopeDecorator.create();
    currentTraceContext = currentTraceContextBuilder
      .addScopeDecorator(strictScopeDecorator).build();
    checkForLeakedScopes = strictScopeDecorator;
  }
  propagationFactory = BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
    .add(SingleBaggageField.newBuilder(BAGGAGE_FIELD)
      .addKeyName(BAGGAGE_FIELD_KEY)
      .build()).build();
  tracing = tracingBuilder(Sampler.ALWAYS_SAMPLE).build();
}
 
Example #3
Source File: TracerTest.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void join_createsChildWhenUnsupportedByPropagation() {
  tracer = Tracing.newBuilder()
    .propagationFactory(new Propagation.Factory() {
      @Deprecated public <K> Propagation<K> create(Propagation.KeyFactory<K> keyFactory) {
        return B3Propagation.FACTORY.create(keyFactory);
      }
    })
    .addSpanHandler(spans).build().tracer();

  TraceContext fromIncomingRequest = tracer.newTrace().context();

  TraceContext shouldBeChild = tracer.joinSpan(fromIncomingRequest).context();
  assertThat(shouldBeChild.shared())
    .isFalse();
  assertThat(shouldBeChild.parentId())
    .isEqualTo(fromIncomingRequest.spanId());
}
 
Example #4
Source File: BaggagePropagationTest.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void clear_and_add() {
  SingleBaggageField requestIdConfig = SingleBaggageField.newBuilder(vcapRequestId)
    .addKeyName("request-id")
    .addKeyName("request_id")
    .build();

  SingleBaggageField traceIdConfig = SingleBaggageField.remote(amznTraceId);
  BaggagePropagation.FactoryBuilder builder = newFactoryBuilder(B3Propagation.FACTORY)
    .add(requestIdConfig)
    .add(traceIdConfig);

  Set<BaggagePropagationConfig> configs = builder.configs();

  builder.clear();

  configs.forEach(builder::add);

  assertThat(builder)
    .usingRecursiveComparison()
    .isEqualTo(newFactoryBuilder(B3Propagation.FACTORY)
      .add(requestIdConfig)
      .add(traceIdConfig));
}
 
Example #5
Source File: BaggagePropagationTest.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void extract_no_overridden_key_names() {
  BaggageField userId = BaggageField.create("userId");
  BaggageField sessionId = BaggageField.create("sessionId");

  factory = newFactoryBuilder(B3Propagation.FACTORY)
    .add(SingleBaggageField.local(userId))
    .add(SingleBaggageField.remote(sessionId))
    .build();
  initialize();

  injector.inject(context, request);
  request.put("userid", "bob");
  request.put("sessionid", "12345");

  context = extractor.extract(request).context();

  assertThat(userId.getValue(context)).isNull();
  assertThat(sessionId.getValue(context)).isEqualTo("12345");
}
 
Example #6
Source File: AWSPropagationTest.java    From zipkin-aws with Apache License 2.0 6 votes vote down vote up
TraceContext contextWithPassThrough() {
  extractor = BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
      .add(BaggagePropagationConfig.SingleBaggageField.remote(AWSPropagation.FIELD_AMZN_TRACE_ID))
      .build()
      .get()
      .extractor(Map::get);

  TraceContextOrSamplingFlags extracted = extractor.extract(carrier);

  // sanity check
  assertThat(extracted.samplingFlags()).isEqualTo(SamplingFlags.EMPTY);
  assertThat(extracted.extra()).isNotEmpty();

  // Make a context that wasn't from AWSPropagation
  return TraceContext.newBuilder()
      .traceId(1L)
      .spanId(2L)
      .sampled(true)
      .addExtra(extracted.extra().get(0))
      .build();
}
 
Example #7
Source File: EndToEndBenchmarks.java    From brave with Apache License 2.0 6 votes vote down vote up
public TracedBaggage() {
  super(Tracing.newBuilder()
    .propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
      .add(SingleBaggageField.remote(REQUEST_ID))
      .add(SingleBaggageField.newBuilder(COUNTRY_CODE)
        .addKeyName("baggage-country-code")
        .build())
      .add(SingleBaggageField.newBuilder(USER_ID)
        .addKeyName("baggage-user-id")
        .build())
      .build())
    .addSpanHandler(new SpanHandler() {
      // intentionally not NOOP to ensure spans report
    })
    .build());
}
 
Example #8
Source File: ServletBenchmarks.java    From brave with Apache License 2.0 5 votes vote down vote up
public TracedBaggage() {
  super(TracingFilter.create(Tracing.newBuilder()
    .propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
      .add(SingleBaggageField.remote(BAGGAGE_FIELD)).build())
    .spanReporter(Reporter.NOOP)
    .build()));
}
 
Example #9
Source File: TracingJMSConsumerTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void receive_retains_baggage_properties() throws Exception {
  ActiveMQTextMessage message = new ActiveMQTextMessage();
  B3Propagation.B3_STRING.injector(SETTER).inject(parent, message);
  message.setStringProperty(BAGGAGE_FIELD_KEY, "");

  receive(message);

  assertThat(message.getProperties())
    .containsEntry(BAGGAGE_FIELD_KEY, "");

  testSpanHandler.takeRemoteSpan(CONSUMER);
}
 
Example #10
Source File: TracingMessageListenerTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void retains_baggage_headers() throws Exception {
  ActiveMQTextMessage message = new ActiveMQTextMessage();
  B3Propagation.B3_STRING.injector(SETTER).inject(parent, message);
  message.setStringProperty(BAGGAGE_FIELD_KEY, "");

  onMessageConsumed(message);

  assertThat(message.getProperties())
    .hasSize(1) // clears b3
    .containsEntry(BAGGAGE_FIELD_KEY, "");

  testSpanHandler.takeRemoteSpan(CONSUMER);
  testSpanHandler.takeLocalSpan();
}
 
Example #11
Source File: TracingMessageListenerTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void listener_continues_parent_trace() {
  tracingMessageListener =
    new TracingMessageListener(delegate, jmsTracing, false);

  ActiveMQTextMessage message = new ActiveMQTextMessage();
  B3Propagation.B3_STRING.injector(SETTER).inject(parent, message);

  onMessageConsumed(message);

  // clearing headers ensures later work doesn't try to use the old parent
  assertNoProperties(message);

  assertChildOf(testSpanHandler.takeLocalSpan(), parent);
}
 
Example #12
Source File: TracingMessageListenerTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void continues_parent_trace() {
  ActiveMQTextMessage message = new ActiveMQTextMessage();
  B3Propagation.B3_STRING.injector(SETTER).inject(parent, message);

  onMessageConsumed(message);

  // clearing headers ensures later work doesn't try to use the old parent
  assertNoProperties(message);

  MutableSpan consumerSpan = testSpanHandler.takeRemoteSpan(CONSUMER);
  MutableSpan listenerSpan = testSpanHandler.takeLocalSpan();

  assertChildOf(consumerSpan, parent);
  assertChildOf(listenerSpan, consumerSpan);
}
 
Example #13
Source File: BaggagePropagationTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void dupesNotOk() {
  SingleBaggageField userIdConfig = SingleBaggageField.local(BaggageField.create("userId"));
  BaggagePropagation.FactoryBuilder builder = newFactoryBuilder(B3Propagation.FACTORY)
    .add(userIdConfig);
  assertThatThrownBy(() -> builder.add(userIdConfig))
    .isInstanceOf(IllegalArgumentException.class);
}
 
Example #14
Source File: JerseyServerBenchmarks.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public Set<Object> getSingletons() {
  return new LinkedHashSet<>(asList(new Resource(), TracingApplicationEventListener.create(
    HttpTracing.create(Tracing.newBuilder()
      .propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
        .add(SingleBaggageField.remote(BAGGAGE_FIELD)).build())
      .spanReporter(Reporter.NOOP)
      .build())
  )));
}
 
Example #15
Source File: BaggagePropagationTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void extract_field_multiple_key_names() {
  // switch to case insensitive as this example is about http :P
  request = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

  BaggageField userId = BaggageField.create("userId");
  BaggageField sessionId = BaggageField.create("sessionId");

  SingleBaggageField userIdConfig = SingleBaggageField.newBuilder(userId)
    .addKeyName("baggage-userId")
    .addKeyName("baggage_userId")
    .build();

  SingleBaggageField sessionIdConfig = SingleBaggageField.newBuilder(sessionId)
    .addKeyName("baggage-sessionId")
    .addKeyName("baggage_sessionId")
    .build();

  factory = newFactoryBuilder(B3Propagation.FACTORY)
    .add(userIdConfig)
    .add(sessionIdConfig)
    .build();
  initialize();

  injector.inject(context, request);
  request.put("baggage-userId", "bob");
  request.put("baggage-sessionId", "12345");

  context = extractor.extract(request).context();

  assertThat(userId.getValue(context)).isEqualTo("bob");
  assertThat(sessionId.getValue(context)).isEqualTo("12345");
}
 
Example #16
Source File: BaggagePropagationTest.java    From brave with Apache License 2.0 5 votes vote down vote up
/** Less overhead and distraction for the edge case of correlation-only. */
@Test public void extract_baggage_onlyOneExtraWhenNothingRemote() {
  Propagation.Factory factory = newFactoryBuilder(B3Propagation.FACTORY)
      .add(SingleBaggageField.local(vcapRequestId))
      .add(SingleBaggageField.local(amznTraceId)).build();
  extractor = factory.get().extractor(Map::get);

  TraceContextOrSamplingFlags extracted = extractor.extract(request);
  assertThat(extracted.extra())
      .hasSize(1)
      .noneMatch(Extra.class::isInstance);
}
 
Example #17
Source File: BaggagePropagationFactoryBeanTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void propagationFactory_default() {
  context = new XmlBeans(""
    + "<bean id=\"propagationFactory\" class=\"brave.spring.beans.BaggagePropagationFactoryBean\"/>\n"
  );

  assertThat(context.getBean("propagationFactory", Propagation.Factory.class))
    .isEqualTo(B3Propagation.FACTORY);
}
 
Example #18
Source File: BaggagePropagationTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void newFactory_sharingRemoteName() {
  BaggagePropagation.FactoryBuilder builder = newFactoryBuilder(B3Propagation.FACTORY);
  SingleBaggageField userName =
    SingleBaggageField.newBuilder(BaggageField.create("userName")).addKeyName("baggage").build();
  SingleBaggageField userId =
    SingleBaggageField.newBuilder(BaggageField.create("userId")).addKeyName("baggage").build();
  builder.add(userName);
  assertThatThrownBy(() -> builder.add(userId))
    .isInstanceOf(IllegalArgumentException.class)
    .hasMessage("Propagation key already in use: baggage");
}
 
Example #19
Source File: ExtraFieldPropagationFactoryBeanTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void propagationFactory_default() {
  context = new XmlBeans(""
    + "<bean id=\"propagationFactory\" class=\"brave.spring.beans.ExtraFieldPropagationFactoryBean\"/>"
  );

  assertThat(context.getBean("propagationFactory", Propagation.Factory.class))
    .extracting("delegate")
    .isEqualTo(B3Propagation.FACTORY);
}
 
Example #20
Source File: ImplementationsJFRTest.java    From java-jfr-tracer with Apache License 2.0 5 votes vote down vote up
@Test
public void brave() throws IOException {

	Factory propagationFactory = ExtraFieldPropagation.newFactoryBuilder(B3Propagation.FACTORY)
			.addPrefixedFields("baggage-", Arrays.asList("country-code", "user-id")).build();

	Tracing braveTracing = Tracing.newBuilder().localServiceName("my-service")
			.propagationFactory(propagationFactory).build();
	innerTest(BraveTracer.create(braveTracing));
}
 
Example #21
Source File: XCloudTraceContextExtractorTest.java    From zipkin-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractXCloudTraceContext_noSpanId() {
  String xCloudTraceContext = "8fd836bcfe241ee19a057679a77ba317";
  XCloudTraceContextExtractor extractor =
      new XCloudTraceContextExtractor<>(
          B3Propagation.FACTORY.get(),
          (request, key) -> xCloudTraceContext);

  TraceContextOrSamplingFlags context = extractor.extract(new Object());
  assertThat(context.traceIdContext().traceId()).isEqualTo(-7348336952112078057L);
  assertThat(context.traceIdContext().traceIdHigh()).isEqualTo(-8081649345970823455L);
  assertThat(context.traceIdContext().sampled()).isNull();
}
 
Example #22
Source File: ImplementationsJFRTest.java    From java-jfr-tracer with Apache License 2.0 5 votes vote down vote up
@Test
public void brave() throws IOException {

	Factory propagationFactory = ExtraFieldPropagation.newFactoryBuilder(B3Propagation.FACTORY)
			.addPrefixedFields("baggage-", Arrays.asList("country-code", "user-id")).build();

	Tracing braveTracing = Tracing.newBuilder().localServiceName("my-service")
			.propagationFactory(propagationFactory).build();
	innerTest(BraveTracer.create(braveTracing));
}
 
Example #23
Source File: ZipkinConfig.java    From j360-boot-app-all with Apache License 2.0 5 votes vote down vote up
/** Controls aspects of tracing such as the service name that shows up in the UI */
@Bean
Tracing tracing() {
    return Tracing.newBuilder()
            .localServiceName("j360-miniprogram")
            .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "web"))
            .build();
}
 
Example #24
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 #25
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("${zipkin.service:brave-webmvc-example}") String serviceName) {
    return Tracing.newBuilder()
        .localServiceName(serviceName)
        .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "user-name"))
        .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder()
            .addScopeDecorator(ThreadContextScopeDecorator.create()) // puts trace IDs into logs
            .build()
        )
        .spanReporter(spanReporter()).build();
}
 
Example #26
Source File: XCloudTraceContextExtractorTest.java    From zipkin-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractXCloudTraceContext_traceTrue() {
  String xCloudTraceContext = "8fd836bcfe241ee19a057679a77ba317/4981115762139876185;o=1";
  XCloudTraceContextExtractor extractor =
      new XCloudTraceContextExtractor<>(
          B3Propagation.FACTORY.get(),
          (request, key) -> xCloudTraceContext);

  TraceContextOrSamplingFlags context = extractor.extract(new Object());
  assertThat(context.context().traceId()).isEqualTo(-7348336952112078057L);
  assertThat(context.context().traceIdHigh()).isEqualTo(-8081649345970823455L);
  assertThat(context.context().spanId()).isEqualTo(4981115762139876185L);
  assertThat(context.context().sampled()).isTrue();
}
 
Example #27
Source File: XCloudTraceContextExtractorTest.java    From zipkin-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractXCloudTraceContext_spanIdZero() {
  String xCloudTraceContext = "8fd836bcfe241ee19a057679a77ba317/0;o=1";
  XCloudTraceContextExtractor extractor =
      new XCloudTraceContextExtractor<>(
          B3Propagation.FACTORY.get(),
          (request, key) -> xCloudTraceContext);

  TraceContextOrSamplingFlags context = extractor.extract(new Object());
  assertThat(context.traceIdContext().traceId()).isEqualTo(-7348336952112078057L);
  assertThat(context.traceIdContext().traceIdHigh()).isEqualTo(-8081649345970823455L);
  assertThat(context.traceIdContext().sampled()).isTrue();
}
 
Example #28
Source File: XCloudTraceContextExtractorTest.java    From zipkin-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractXCloudTraceContext_traceFalse() {
  String xCloudTraceContext = "8fd836bcfe241ee19a057679a77ba317/4981115762139876185;o=0";
  XCloudTraceContextExtractor extractor =
      new XCloudTraceContextExtractor<>(
          B3Propagation.FACTORY.get(),
          (request, key) -> xCloudTraceContext);

  TraceContextOrSamplingFlags context = extractor.extract(new Object());
  assertThat(context.context().sampled()).isFalse();
}
 
Example #29
Source File: XCloudTraceContextExtractorTest.java    From zipkin-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractXCloudTraceContext_missingTraceTrueValue() {
  String xCloudTraceContext = "8fd836bcfe241ee19a057679a77ba317/4981115762139876185;o=";
  XCloudTraceContextExtractor extractor =
      new XCloudTraceContextExtractor<>(
          B3Propagation.FACTORY.get(),
          (request, key) -> xCloudTraceContext);

  TraceContextOrSamplingFlags context = extractor.extract(new Object());
  assertThat(context.context().traceId()).isEqualTo(-7348336952112078057L);
  assertThat(context.context().traceIdHigh()).isEqualTo(-8081649345970823455L);
  assertThat(context.context().spanId()).isEqualTo(4981115762139876185L);
  assertThat(context.context().sampled()).isNull();
}
 
Example #30
Source File: XCloudTraceContextExtractorTest.java    From zipkin-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractXCloudTraceContext_noTraceTrue() {
  String xCloudTraceContext = "8fd836bcfe241ee19a057679a77ba317/4981115762139876185";
  XCloudTraceContextExtractor extractor =
      new XCloudTraceContextExtractor<>(
          B3Propagation.FACTORY.get(),
          (request, key) -> xCloudTraceContext);

  TraceContextOrSamplingFlags context = extractor.extract(new Object());
  assertThat(context.context().traceId()).isEqualTo(-7348336952112078057L);
  assertThat(context.context().traceIdHigh()).isEqualTo(-8081649345970823455L);
  assertThat(context.context().spanId()).isEqualTo(4981115762139876185L);
  assertThat(context.context().sampled()).isNull();
}