brave.baggage.BaggagePropagationConfig.SingleBaggageField Java Examples

The following examples show how to use brave.baggage.BaggagePropagationConfig.SingleBaggageField. 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: SingleBaggageFieldFactoryBeanTest.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void allProperties() {
  context = new XmlBeans(""
    + "<bean id=\"userId\" class=\"brave.baggage.BaggageField\" factory-method=\"create\">\n"
    + "  <constructor-arg><value>userId</value></constructor-arg>\n"
    + "</bean>\n"
    + "<bean id=\"userIdBaggageConfig\" class=\"brave.spring.beans.SingleBaggageFieldFactoryBean\">\n"
    + "  <property name=\"field\" ref=\"userId\"/>\n"
    + "  <property name=\"keyNames\">\n"
    + "    <list>\n"
    + "      <value>user-id</value>\n"
    + "    </list>\n"
    + "  </property>\n"
    + "</bean>\n"
  );

  assertThat(context.getBean("userIdBaggageConfig", BaggagePropagationConfig.class))
    .usingRecursiveComparison()
    .isEqualTo(SingleBaggageField.newBuilder(BaggageField.create("userId"))
      .addKeyName("user-id")
      .build());
}
 
Example #2
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 #3
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 #4
Source File: BaggagePropagation.java    From brave with Apache License 2.0 6 votes vote down vote up
Factory(FactoryBuilder factoryBuilder) {
  this.delegateFactory = factoryBuilder.delegate;
  this.delegate = delegateFactory.get();

  // Don't add another "extra" if there are only local fields
  List<String> extractKeyNames = Lists.ensureImmutable(factoryBuilder.extractKeyNames);
  this.extra = !extractKeyNames.isEmpty() ? new Extra(extractKeyNames) : null;

  // Associate baggage fields with any remote propagation keys
  this.configs = factoryBuilder.configs.toArray(new BaggagePropagationConfig[0]);

  List<BaggageField> fields = new ArrayList<>();
  Set<String> localFieldNames = new LinkedHashSet<>();
  int maxDynamicFields = 0;
  for (BaggagePropagationConfig config : factoryBuilder.configs) {
    maxDynamicFields += config.maxDynamicFields;
    if (config instanceof SingleBaggageField) {
      BaggageField field = ((SingleBaggageField) config).field;
      fields.add(field);
      if (config.baggageCodec == BaggageCodec.NOOP) localFieldNames.add(field.name());
    }
  }
  this.baggageFactory = BaggageFields.newFactory(fields, maxDynamicFields);
  this.localFieldNames = localFieldNames.toArray(new String[0]);
}
 
Example #5
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 #6
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 #7
Source File: ExtraFieldPropagation.java    From brave with Apache License 2.0 6 votes vote down vote up
/** Returns a wrapper of the delegate if there are no fields to propagate. */
public Factory build() {
  Set<String> extraKeyNames = new LinkedHashSet<>();
  for (Map.Entry<String, Set<String>> entry : nameToKeyNames.entrySet()) {
    BaggageField field = BaggageField.create(entry.getKey());
    if (redactedNames.contains(field.name())) {
      baggageFactory.add(SingleBaggageField.local(field));
    } else {
      extraKeyNames.addAll(entry.getValue());
      SingleBaggageField.Builder builder = SingleBaggageField.newBuilder(field);
      for (String keyName : entry.getValue()) {
        builder.addKeyName(keyName);
      }
      baggageFactory.add(builder.build());
    }
  }
  return new Factory(baggageFactory.build(), extraKeyNames.toArray(new String[0]));
}
 
Example #8
Source File: BaggagePropagationTest.java    From brave with Apache License 2.0 6 votes vote down vote up
/** Redaction prevents named fields from being written downstream. */
@Test public void inject_no_key_names() {
  BaggageField userId = BaggageField.create("userId");
  BaggageField sessionId = BaggageField.create("sessionId");

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

  userId.updateValue(context, "bob");
  sessionId.updateValue(context, "12345");

  injector.inject(context, request);

  assertThat(request)
    .doesNotContainKey("userid")
    .containsEntry("sessionid", "12345");
}
 
Example #9
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 #10
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 #11
Source File: BaggagePropagationTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void inject_field_multiple_key_names() {
  BaggageField userId = BaggageField.create("userId");
  BaggageField sessionId = BaggageField.create("sessionId");

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

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

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

  userId.updateValue(context, "bob");
  sessionId.updateValue(context, "12345");

  injector.inject(context, request);

  // NOTE: the labels are downcased
  assertThat(request).containsOnly(
    entry("b3", B3SingleFormat.writeB3SingleFormat(context)),
    entry("userid", "bob"),
    entry("sessionid", "12345"),
    entry("baggage-userid", "bob"),
    entry("baggage-sessionid", "12345"),
    entry("baggage_userid", "bob"),
    entry("baggage_sessionid", "12345")
  );
}
 
Example #12
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 #13
Source File: BaggagePropagationTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void allKeyNames_baggagePropagation() {
  Propagation.Factory factory = BaggagePropagation.newFactoryBuilder(B3SinglePropagation.FACTORY)
    .add(SingleBaggageField.local(BaggageField.create("redacted"))) // local shouldn't return
    .add(SingleBaggageField.remote(BaggageField.create("user-id")))
    .add(SingleBaggageField.remote(BaggageField.create("session-id"))).build();

  assertThat(BaggagePropagation.allKeyNames(factory.get()))
    .containsExactly("b3", "user-id", "session-id");
}
 
Example #14
Source File: BaggagePropagationTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void allKeyNames_baggagePropagation_noRemote() {
  Propagation.Factory factory = BaggagePropagation.newFactoryBuilder(B3SinglePropagation.FACTORY)
      .add(SingleBaggageField.local(BaggageField.create("redacted"))) // local shouldn't return
      .add(SingleBaggageField.local(BaggageField.create("user-id")))
      .add(SingleBaggageField.local(BaggageField.create("session-id"))).build();

  assertThat(BaggagePropagation.allKeyNames(factory.get()))
      .containsExactly("b3");
}
 
Example #15
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 #16
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 #17
Source File: SingleBaggageFieldFactoryBean.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public SingleBaggageField getObject() {
  SingleBaggageField.Builder builder = SingleBaggageField.newBuilder(field);
  if (keyNames != null) {
    for (String keyName : keyNames) {
      builder.addKeyName(keyName);
    }
  }
  return builder.build();
}
 
Example #18
Source File: SingleBaggageFieldFactoryBeanTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void leastProperties() {
  context = new XmlBeans(""
    + "<bean id=\"userId\" class=\"brave.baggage.BaggageField\" factory-method=\"create\">\n"
    + "  <constructor-arg><value>userId</value></constructor-arg>\n"
    + "</bean>\n"
    + "<bean id=\"userIdBaggageConfig\" class=\"brave.spring.beans.SingleBaggageFieldFactoryBean\">\n"
    + "  <property name=\"field\" ref=\"userId\"/>\n"
    + "</bean>\n"
  );

  assertThat(context.getBean("userIdBaggageConfig", BaggagePropagationConfig.class))
    .usingRecursiveComparison()
    .isEqualTo(SingleBaggageField.local(BaggageField.create("userId")));
}
 
Example #19
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 #20
Source File: OpenTracing0_33_BraveSpanTest.java    From brave-opentracing with Apache License 2.0 5 votes vote down vote up
void init(Tracing.Builder tracingBuilder) {
  if (brave != null) brave.close();
  brave = tracingBuilder
      .localServiceName("tracer")
      .currentTraceContext(currentTraceContext)
      .addSpanHandler(spans)
      .propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
          .add(SingleBaggageField.remote(BaggageField.create("client-id")))
          .build()).build();
  tracer = BraveTracer.create(brave);
}
 
Example #21
Source File: BaggagePropagationConfig.java    From brave with Apache License 2.0 5 votes vote down vote up
SingleBaggageField(Builder builder) { // sealed to this package
  super(builder.keyNames.isEmpty()
      ? BaggageCodec.NOOP
      : SingleFieldBaggageCodec.single(builder.field, builder.keyNames), 0);
  field = builder.field;
  keyNames = builder.keyNames.isEmpty() ? Collections.emptySet()
      : Collections.unmodifiableSet(new LinkedHashSet<>(builder.keyNames));
}
 
Example #22
Source File: TraceBaggageConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Bean
BaggagePropagationCustomizer countryCodeBaggageConfig() {
	return fb -> fb.add(
			SingleBaggageField.newBuilder(BaggageField.create("country-code"))
					.addKeyName("baggage-country-code")
					.addKeyName("baggage_country-code").build());
}
 
Example #23
Source File: BaggagePropagationConfig.java    From brave with Apache License 2.0 4 votes vote down vote up
/** @since 5.11 */
public SingleBaggageField build() {
  return new SingleBaggageField(this);
}
 
Example #24
Source File: BaggagePropagationConfig.java    From brave with Apache License 2.0 4 votes vote down vote up
Builder(SingleBaggageField input) {
  this.field = input.field;
  this.keyNames = new ArrayList<>(input.keyNames());
}
 
Example #25
Source File: TracingConfiguration.java    From brave-webmvc-example with MIT License 4 votes vote down vote up
/** Configures propagation for {@link #USER_NAME}, using the remote header "user_name" */
@Bean Propagation.Factory propagationFactory() {
  return BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
      .add(SingleBaggageField.newBuilder(USER_NAME).addKeyName("user_name").build())
      .build();
}
 
Example #26
Source File: TracingConfiguration.java    From brave-webmvc-example with MIT License 4 votes vote down vote up
/** Configures propagation for {@link #USER_NAME}, using the remote header "user_name" */
@Bean Propagation.Factory propagationFactory() {
  return BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
      .add(SingleBaggageField.newBuilder(USER_NAME).addKeyName("user_name").build())
      .build();
}
 
Example #27
Source File: TraceAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
@Bean
BaggagePropagationCustomizer requestId() {
	return fb -> fb.add(
			SingleBaggageField.remote(BaggageField.create("x-vcap-request-id")));
}
 
Example #28
Source File: TraceAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
@Bean
BaggagePropagationCustomizer countryCode() {
	return fb -> fb
			.add(SingleBaggageField.remote(BaggageField.create("country-code")));
}
 
Example #29
Source File: MultipleHopsIntegrationTests.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
@Bean
BaggagePropagationConfig notInProperties() {
	return SingleBaggageField.remote(BaggageField.create("bar"));
}
 
Example #30
Source File: BaggagePropagationConfig.java    From brave with Apache License 2.0 2 votes vote down vote up
/**
 * Configures this field for remote propagation using its lower-case {@link BaggageField#name()}
 * as the only {@linkplain #keyNames() propagation key name}.
 *
 * @see #local(BaggageField)
 * @see #newBuilder(BaggageField) to use different propagation key names.
 */
public static SingleBaggageField remote(BaggageField field) {
  return new Builder(field).addKeyName(field.lcName).build();
}