Java Code Examples for brave.propagation.TraceContext.Injector#inject()

The following examples show how to use brave.propagation.TraceContext.Injector#inject() . 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: BraveTracer.java    From brave-opentracing with Apache License 2.0 6 votes vote down vote up
/**
 * Injects the underlying context using B3 encoding by default.
 */
@Override public <C> void inject(SpanContext spanContext, Format<C> format, C carrier) {
  BraveSpanContext braveContext = ((BraveSpanContext) spanContext);
  if (carrier instanceof BinaryInject) {
    BinaryCodec.INSTANCE.inject(braveContext.unwrap(), (BinaryInject) carrier);
    return;
  }
  if (!(carrier instanceof TextMapInject)) {
    throw new UnsupportedOperationException(carrier + " not instanceof TextMapInject");
  }
  Kind kind = braveContext.kind;
  Injector<TextMapInject> injector = null;
  if (Kind.CLIENT.equals(kind)) {
    injector = formatToClientInjector.get(format);
  } else if (Kind.PRODUCER.equals(kind)) {
    injector = formatToProducerInjector.get(format);
  } else if (Kind.CONSUMER.equals(kind)) {
    injector = formatToConsumerInjector.get(format);
  }
  if (injector == null) injector = formatToInjector.get(format);
  if (injector == null) {
    throw new UnsupportedOperationException(format + " not in " + formatToInjector.keySet());
  }
  injector.inject(braveContext.unwrap(), (TextMapInject) carrier);
}
 
Example 2
Source File: BaggageInSingleHeaderTest.java    From brave with Apache License 2.0 6 votes vote down vote up
/** This shows that we can encode arbitrary fields into a single header. */
@Test public void encodes_arbitrary_fields() {
  TraceContext context = factory.decorate(TraceContext.newBuilder().traceId(1).spanId(2).build());
  field1.updateValue(context, "1");
  field2.updateValue(context, "2");
  field3.updateValue(context, "3");

  Injector<Map<String, String>> injector = factory.get().injector(Map::put);
  Map<String, String> headers = new LinkedHashMap<>();
  injector.inject(context, headers);

  assertThat(headers).containsOnly(
      entry("b3", "0000000000000001-0000000000000002"),
      entry("one", "1"), // has its own header config which is still serialized
      entry("baggage", "one=1,three=3") // excluding the blacklist field including the dynamic one
  );
}
 
Example 3
Source File: BraveTracer.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override public <C> void inject(SpanContext spanContext, Format<C> format, C carrier) {
  if (!(carrier instanceof TextMapInject)) {
    throw new UnsupportedOperationException(carrier + " not instanceof TextMapInject");
  }
  BraveSpanContext braveContext = ((BraveSpanContext) spanContext);
  Kind kind = braveContext.kind;
  Injector<TextMapInject> injector;
  if (Kind.CLIENT.equals(kind)) {
    injector = clientInjector;
  } else if (Kind.PRODUCER.equals(kind)) {
    injector = producerInjector;
  } else if (Kind.CONSUMER.equals(kind)) {
    injector = consumerInjector;
  } else {
    injector = this.injector;
  }
  injector.inject(braveContext.context, (TextMapInject) carrier);
}
 
Example 4
Source File: InjectorFactoryTest.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void oneFunction_injects_remote() {
  when(remoteSetter.spanKind()).thenReturn(Kind.CLIENT);
  RemoteInjector<Request> remoteInjector =
      (RemoteInjector<Request>) oneFunction.newInjector(remoteSetter);

  for (Kind kind : injectableKinds) {
    when(remoteSetter.spanKind()).thenReturn(kind);

    Injector<Request> nextRemoteInjector = oneFunction.newInjector(remoteSetter);
    assertThat(nextRemoteInjector).isEqualTo(remoteInjector);
    assertThat(nextRemoteInjector).hasSameHashCodeAs(remoteInjector);
    assertThat(nextRemoteInjector).hasToString(remoteInjector.toString());

    assertThat(nextRemoteInjector).extracting("injectorFunction")
        .isEqualTo(remoteInjector.injectorFunction);

    nextRemoteInjector.inject(context, request);
    assertThat(oneCount.getAndSet(0)).isOne();
  }
}
 
Example 5
Source File: InjectorFactoryTest.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void twoFunctions_injects_remote() {
  when(remoteSetter.spanKind()).thenReturn(Kind.CLIENT);
  RemoteInjector<Request> remoteInjector =
      (RemoteInjector<Request>) twoFunctions.newInjector(remoteSetter);

  for (Kind kind : injectableKinds) {
    when(remoteSetter.spanKind()).thenReturn(kind);

    Injector<Request> nextRemoteInjector = twoFunctions.newInjector(remoteSetter);
    assertThat(nextRemoteInjector).isEqualTo(remoteInjector);
    assertThat(nextRemoteInjector).hasSameHashCodeAs(remoteInjector);
    assertThat(nextRemoteInjector).hasToString(remoteInjector.toString());

    assertThat(nextRemoteInjector).extracting("injectorFunction")
        .isEqualTo(remoteInjector.injectorFunction);

    nextRemoteInjector.inject(context, request);
    assertThat(oneCount.getAndSet(0)).isOne();
    assertThat(twoCount.getAndSet(0)).isOne();
  }
}
 
Example 6
Source File: B3PropagationTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void producerUsesB3SingleNoParent_deferred() {
  // This injector won't know the type it is injecting until the call to inject()
  Injector<ProducerRequest> injector = Propagation.B3_STRING.injector(ProducerRequest::header);

  ProducerRequest request = new ProducerRequest();
  injector.inject(context, request);

  assertThat(request.headers)
    .hasSize(1)
    .containsEntry("b3", "0000000000000001-0000000000000003");
}
 
Example 7
Source File: B3PropagationTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void producerUsesB3SingleNoParent() {
  // This injector needs no instanceof checks during inject()
  Injector<ProducerRequest> injector = Propagation.B3_STRING.injector(new ProducerSetter());

  ProducerRequest request = new ProducerRequest();
  injector.inject(context, request);

  assertThat(request.headers)
    .hasSize(1)
    .containsEntry("b3", "0000000000000001-0000000000000003");
}
 
Example 8
Source File: InjectorFactoryTest.java    From brave with Apache License 2.0 5 votes vote down vote up
void kindBasedFunctions_injects_remote(Kind kind, AtomicInteger kindCallCount) {
  when(remoteSetter.spanKind()).thenReturn(kind);

  Injector<Request> nextRemoteInjector = kindBasedFunctions.newInjector(remoteSetter);
  nextRemoteInjector.inject(context, request);

  for (AtomicInteger callCount : allCounts) {
    if (callCount.equals(kindCallCount)) {
      assertThat(callCount.getAndSet(0)).isOne();
    } else {
      assertThat(callCount.getAndSet(0)).isZero();
    }
  }
}
 
Example 9
Source File: HttpClientHandler.java    From brave with Apache License 2.0 5 votes vote down vote up
/**
 * @since 4.4
 * @deprecated Since 5.7, use {@link #handleSend(HttpClientRequest)}.
 */
@Deprecated public <C> Span handleSend(Injector<C> injector, C carrier, Req request, Span span) {
  if (request == null) throw new NullPointerException("carrier == null");
  if (span == null) throw new NullPointerException("span == null");

  injector.inject(span.context(), carrier);
  HttpClientRequest clientRequest;
  if (request instanceof HttpClientRequest) {
    clientRequest = (HttpClientRequest) request;
  } else {
    clientRequest = new HttpClientAdapters.FromRequestAdapter<>(adapter, request);
  }

  return handleStart(clientRequest, span);
}