Java Code Examples for brave.propagation.TraceContext#findExtra()

The following examples show how to use brave.propagation.TraceContext#findExtra() . 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: AWSInjector.java    From zipkin-aws with Apache License 2.0 6 votes vote down vote up
/**
 * This version of propagation contains at least 74 characters corresponding to identifiers and
 * the sampling bit. It will also include extra fields where present.
 *
 * <p>Ex 74 characters: {@code
 * Root=1-67891233-abcdef012345678912345678;Parent=463ac35c9f6413ad;Sampled=1}
 *
 * <p>{@inheritDoc}
 */
@Override
public void inject(TraceContext traceContext, R request) {
  AmznTraceId amznTraceId = traceContext.findExtra(AmznTraceId.class);
  CharSequence customFields = amznTraceId != null ? amznTraceId.customFields : null;
  int customFieldsLength = customFields == null ? 0 : customFields.length();
  // Root=1-67891233-abcdef012345678912345678;Parent=463ac35c9f6413ad;Sampled=1
  char[] result = new char[74 + customFieldsLength];
  System.arraycopy(ROOT, 0, result, 0, 5);
  writeRoot(traceContext, result, 5);
  System.arraycopy(PARENT, 0, result, 40, 8);
  writeHexLong(result, 48, traceContext.spanId());
  System.arraycopy(SAMPLED, 0, result, 64, 9);
  Boolean sampled = traceContext.sampled();
  // Sampled status is same as B3, but ? means downstream decides (like omitting X-B3-Sampled)
  // https://github.com/aws/aws-xray-sdk-go/blob/391885218b556c43ed05a1e736a766d70fc416f1/header/header.go#L50
  result[73] = sampled == null ? '?' : sampled ? '1' : '0';
  for (int i = 0; i < customFieldsLength; i++) {
    result[i + 74] = customFields.charAt(i);
  }
  setter.put(request, AMZN_TRACE_ID_NAME, new String(result));
}
 
Example 2
Source File: BaggagePropagation.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override public void inject(TraceContext context, R request) {
  delegate.inject(context, request);
  BaggageFields extra = context.findExtra(BaggageFields.class);
  if (extra == null) return;
  Map<String, String> values =
      extra.toMapFilteringFieldNames(factory.localFieldNames);
  if (values.isEmpty()) return;

  for (BaggagePropagationConfig config : factory.configs) {
    if (config.baggageCodec == BaggageCodec.NOOP) continue; // local field

    String value = config.baggageCodec.encode(values, context, request);
    if (value == null) continue;

    List<String> keys = config.baggageCodec.injectKeyNames();
    for (int i = 0, length = keys.size(); i < length; i++) {
      setter.put(request, keys.get(i), value);
    }
  }
}
 
Example 3
Source File: ExtraFactoryTest.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void contextsAreIndependent() {
  TraceContext decorated = propagationFactory.decorate(context);
  BasicMapExtra extra1 = decorated.findExtra(BasicMapExtra.class);
  extra1.put("1", "one");

  context2 = propagationFactory.decorate(context2.toBuilder().addExtra(extra1).build());
  BasicMapExtra extra2 = context2.findExtra(BasicMapExtra.class);

  // Instances are not the same
  assertThat(extra1).isNotSameAs(extra2);

  // But have the same values
  assertThat(extra1).isEqualTo(extra2);

  extra1.put("1", "two");
  extra2.put("1", "three");

  // Yet downstream changes don't affect eachother
  assertThat(extra1.get("1")).isEqualTo("two");
  assertThat(extra2.get("1")).isEqualTo("three");
}
 
Example 4
Source File: ExtraFactoryTest.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void contextsHaveIndependentValue() {
  TraceContext decorated = propagationFactory.decorate(context);
  BasicMapExtra extra1 = decorated.findExtra(BasicMapExtra.class);
  extra1.put("1", "two");

  // we have the same span ID, so we should couple our extra state
  assertThat(propagationFactory.decorate(decorated.toBuilder().build()).extra())
      .isEqualTo(decorated.extra());

  // we no longer have the same span ID, so we should decouple our extra state
  context2 = propagationFactory.decorate(context2.toBuilder().addExtra(extra1).build());
  BasicMapExtra extra2 = context2.findExtra(BasicMapExtra.class);

  // we have different instances of extra
  assertThat(extra1).isNotSameAs(extra2);

  // however, the values inside are the same until a write occurs
  assertThat(extra1).isEqualTo(extra2);

  // check that the change is present, but the other contexts are the same
  String beforeUpdate = extra1.get("1");
  extra1.put("1", "2");

  assertThat(extra1.get("1")).isNotEqualTo(beforeUpdate); // copy-on-write
  assertThat(extra2.get("1")).isEqualTo(beforeUpdate);
}
 
Example 5
Source File: ExtraFactoryTest.java    From brave with Apache License 2.0 6 votes vote down vote up
/**
 * This scenario is possible, albeit rare. {@code tracer.nextSpan(extracted} } is called when
 * there is an implicit parent. For example, you have a trace in progress when extracting extra
 * from an incoming message. Another example is where there is a span in scope due to a leak such
 * as from using {@link CurrentTraceContext.Default#inheritable()}.
 *
 * <p>Extracted extra should merge into the current extra state instead creating multiple
 * entries in {@link TraceContext#extra()}.
 */
@Test public void decorate_extractedExtra_plus_parent_merge() {
  TraceContext decorated = propagationFactory.decorate(context);
  BasicMapExtra extra1 = decorated.findExtra(BasicMapExtra.class);

  BasicMapExtra extracted = factory.create();
  extra1.put("1", "one");
  extracted.put("1", "two");
  extracted.put("2", "three");

  context2 = propagationFactory.decorate(
      context2.toBuilder().addExtra(extra1).addExtra(extracted).build());
  BasicMapExtra extra2 = context2.findExtra(BasicMapExtra.class);
  assertThat(context2.extra()).containsExactly(extra2); // merged

  assertThat(extra2.get("1")).isEqualTo("two"); // extracted should win!
  assertThat(extra2.get("2")).isEqualTo("three");

  assertExtraClaimed(context2);
}
 
Example 6
Source File: ExtraFactoryTest.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void decorate_extractedExtra_plus_emptyParent() {
  TraceContext decorated = propagationFactory.decorate(context);
  BasicMapExtra extra1 = decorated.findExtra(BasicMapExtra.class);

  BasicMapExtra extracted = factory.create();
  extracted.put("2", "three");

  context2 = propagationFactory.decorate(
      context2.toBuilder().addExtra(extra1).addExtra(extracted).build());
  BasicMapExtra extra2 = context2.findExtra(BasicMapExtra.class);
  assertThat(context2.extra()).containsExactly(extra2); // merged

  assertThat(extra2.get("2")).isEqualTo("three");

  assertExtraClaimed(context2);
}
 
Example 7
Source File: ExtraFactoryTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void decorate_parent() {
  TraceContext decorated = propagationFactory.decorate(context);
  BasicMapExtra extra1 = decorated.findExtra(BasicMapExtra.class);
  extra1.put("1", "one");

  context2 = propagationFactory.decorate(context2.toBuilder().addExtra(extra1).build());
  BasicMapExtra extra2 = context2.findExtra(BasicMapExtra.class);
  assertThat(context2.extra()).containsExactly(extra2); // didn't duplicate

  assertThat(extra2.get("1")).isEqualTo("one");

  assertExtraClaimed(context2);
}
 
Example 8
Source File: GrpcPropagation.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void inject(TraceContext context, R request) {
  if (request instanceof GrpcClientRequest) {
    byte[] serialized = TraceContextBinaryFormat.toBytes(context);
    Metadata metadata = ((GrpcClientRequest) request).headers;
    metadata.removeAll(GRPC_TRACE_BIN);
    metadata.put(GRPC_TRACE_BIN, serialized);
    TagsBin tags = context.findExtra(TagsBin.class);
    if (tags != null) {
      metadata.removeAll(GRPC_TAGS_BIN);
      metadata.put(GRPC_TAGS_BIN, tags);
    }
  }
  delegate.inject(context, request);
}