brave.propagation.CurrentTraceContext.ScopeDecorator Java Examples

The following examples show how to use brave.propagation.CurrentTraceContext.ScopeDecorator. 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: CorrelationScopeDecoratorTest.java    From brave with Apache License 2.0 6 votes vote down vote up
void assertNestedUpdatesCoherent(ScopeDecorator decorator) {
  try (Scope s = decorator.decorateScope(contextWithBaggage, mock(Scope.class))) {
    FLUSH_FIELD.baggageField().updateValue(contextWithBaggage, "word");
    try (Scope s1 = decorator.decorateScope(contextWithBaggage, mock(Scope.class))) {
      assertThat(map).containsEntry("flushed", "word");
      FLUSH_FIELD.baggageField().updateValue(contextWithBaggage, "outlook");
      try (Scope s2 = decorator.decorateScope(contextWithBaggage, mock(Scope.class))) {
        assertThat(map).containsEntry("flushed", "outlook");
        FLUSH_FIELD.baggageField().updateValue(contextWithBaggage, "powerpoint");
        try (Scope s3 = decorator.decorateScope(contextWithBaggage, mock(Scope.class))) {
          assertThat(map).containsEntry("flushed", "powerpoint");
          FLUSH_FIELD.baggageField().updateValue(contextWithBaggage, "sharepoint");
          assertThat(map).containsEntry("flushed", "sharepoint");
        }
        assertThat(map).containsEntry("flushed", "powerpoint");
      }
      assertThat(map).containsEntry("flushed", "outlook");
    }
    assertThat(map).containsEntry("flushed", "word");
  }
}
 
Example #2
Source File: CorrelationScopeDecoratorFactoryBeanTest.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void configs() {
  context = new XmlBeans(""
    + "<util:constant id=\"traceId\" static-field=\"brave.baggage.BaggageFields.TRACE_ID\"/>\n"
    + "<bean id=\"correlationDecorator\" class=\"brave.spring.beans.CorrelationScopeDecoratorFactoryBean\">\n"
    + "  <property name=\"builder\">\n"
    + "    <bean class=\"brave.context.log4j12.MDCScopeDecorator\" factory-method=\"newBuilder\"/>\n"
    + "  </property>\n"
    + "  <property name=\"configs\">\n"
    + "    <list>\n"
    + "      <bean class=\"brave.spring.beans.SingleCorrelationFieldFactoryBean\">\n"
    + "        <property name=\"baggageField\" ref=\"traceId\"/>\n"
    + "        <property name=\"name\" value=\"X-B3-TraceId\"/>\n"
    + "      </bean>\n"
    + "    </list>\n"
    + "  </property>\n"
    + "</bean>"
  );

  ScopeDecorator decorator = context.getBean("correlationDecorator", ScopeDecorator.class);
  assertThat(decorator).extracting("field")
    .usingRecursiveComparison()
    .isEqualTo(
      SingleCorrelationField.newBuilder(TRACE_ID).name("X-B3-TraceId").build());
}
 
Example #3
Source File: TraceBaggageConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(CorrelationScopeDecorator.class)
@ConditionalOnBean(CorrelationScopeDecorator.Builder.class)
@ConditionalOnProperty(value = "spring.sleuth.baggage.correlation-enabled",
		matchIfMissing = true)
ScopeDecorator correlationScopeDecorator(
		@Qualifier(WHITELISTED_MDC_KEYS) List<String> whiteListedMDCKeys,
		SleuthBaggageProperties sleuthBaggageProperties,
		@Nullable List<CorrelationScopeCustomizer> correlationScopeCustomizers) {

	Set<String> correlationFields = redirectOldPropertyToNew(WHITELISTED_MDC_KEYS,
			whiteListedMDCKeys, "spring.sleuth.baggage.correlation-fields",
			sleuthBaggageProperties.getCorrelationFields());

	// Add fields from properties
	CorrelationScopeDecorator.Builder builder = MDCScopeDecorator.newBuilder();
	for (String field : correlationFields) {
		builder.add(SingleCorrelationField.newBuilder(BaggageField.create(field))
				.build());
	}

	// handle user overrides
	if (correlationScopeCustomizers != null) {
		for (CorrelationScopeCustomizer customizer : correlationScopeCustomizers) {
			customizer.customize(builder);
		}
	}
	return builder.build();
}
 
Example #4
Source File: CurrentTraceContextTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void ignoresNoopScopeDecorator() {
  ScopeDecorator one = (context, scope) -> scope;

  CurrentTraceContext shouldHaveOnlyOne = newBuilder()
    .addScopeDecorator(ScopeDecorator.NOOP)
    .addScopeDecorator(one).build();

  assertThat(shouldHaveOnlyOne).extracting("scopeDecorators")
    .asInstanceOf(InstanceOfAssertFactories.ARRAY)
    .doesNotContain(ScopeDecorator.NOOP);
}
 
Example #5
Source File: CorrelationScopeDecorator.java    From brave with Apache License 2.0 5 votes vote down vote up
/** @return {@link ScopeDecorator#NOOP} if no baggage fields were added. */
public final ScopeDecorator build() {
  int fieldCount = fields.size();
  if (fieldCount == 0) return ScopeDecorator.NOOP;
  if (fieldCount == 1) return new Single(context, fields.iterator().next());
  if (fieldCount > 32) throw new IllegalArgumentException("over 32 baggage fields");
  return new Multiple(context, fields.toArray(new SingleCorrelationField[0]));
}
 
Example #6
Source File: CorrelationScopeDecoratorTest.java    From brave with Apache License 2.0 5 votes vote down vote up
/** When a context is in an unexpected state, save off fields and revert. */
@Test public void decoratesNoop_unconfiguredFields() {
  for (ScopeDecorator decorator : asList(withBaggageFieldsDecorator, onlyScopeDecorator)) {
    map.put(FIELD.name(), "romeo");
    map.put(FIELD_2.name(), "FO");
    map.put(LOCAL_FIELD.name(), "abcd");

    try (Scope scope = decorator.decorateScope(context, Scope.NOOP)) {
      assertThat(scope).isNotSameAs(Scope.NOOP);
    }
  }
  map.clear();
}
 
Example #7
Source File: CorrelationScopeDecoratorFactoryBean.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public ScopeDecorator getObject() {
  if (builder == null) throw new NullPointerException("builder == null");
  if (configs != null) {
    builder.clear();
    for (CorrelationScopeConfig config : configs) {
      builder.add(config);
    }
  }
  if (customizers != null) {
    for (CorrelationScopeCustomizer customizer : customizers) customizer.customize(builder);
  }
  return builder.build();
}
 
Example #8
Source File: CurrentTraceContextFactoryBean.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public CurrentTraceContext getObject() {
  CurrentTraceContext.Builder builder = ThreadLocalCurrentTraceContext.newBuilder();
  if (scopeDecorators != null) {
    for (ScopeDecorator scopeDecorator : scopeDecorators) {
      builder.addScopeDecorator(scopeDecorator);
    }
  }
  if (customizers != null) {
    for (CurrentTraceContextCustomizer customizer : customizers) customizer.customize(builder);
  }
  return builder.build();
}
 
Example #9
Source File: TracingConfiguration.java    From brave-webmvc-example with MIT License 4 votes vote down vote up
/** Allows log patterns to use {@code %{traceId}} {@code %{spanId}} and {@code %{userName}} */
@Bean ScopeDecorator correlationScopeDecorator() {
  return MDCScopeDecorator.newBuilder()
      .add(SingleCorrelationField.create(USER_NAME)).build();
}
 
Example #10
Source File: TracingConfiguration.java    From brave-webmvc-example with MIT License 4 votes vote down vote up
/** Allows log patterns to use {@code %{traceId}} {@code %{spanId}} and {@code %{userName}} */
@Bean ScopeDecorator correlationScopeDecorator() {
  return ThreadContextScopeDecorator.newBuilder()
      .add(SingleCorrelationField.create(USER_NAME)).build();
}
 
Example #11
Source File: JfrScopeDecorator.java    From brave with Apache License 2.0 4 votes vote down vote up
/** @deprecated since 5.11 use {@link #get()} */
@Deprecated public static ScopeDecorator create() {
  return new JfrScopeDecorator();
}
 
Example #12
Source File: StrictScopeDecoratorTest.java    From brave with Apache License 2.0 4 votes vote down vote up
BusinessClass(Tracing tracing, ScopeDecorator decorator, TraceContext context) {
  this.tracing = tracing;
  this.threadLocalSpan = ThreadLocalSpan.create(tracing.tracer());
  this.decorator = decorator;
  this.context = context;
}
 
Example #13
Source File: CorrelationScopeDecoratorFactoryBean.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override public Class<? extends ScopeDecorator> getObjectType() {
  return ScopeDecorator.class;
}
 
Example #14
Source File: CurrentTraceContextFactoryBean.java    From brave with Apache License 2.0 4 votes vote down vote up
public void setScopeDecorators(List<ScopeDecorator> scopeDecorators) {
  this.scopeDecorators = scopeDecorators;
}
 
Example #15
Source File: JfrScopeDecorator.java    From brave with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a singleton that configures {@link BaggageFields#TRACE_ID} and {@link
 * BaggageFields#SPAN_ID}.
 *
 * @since 5.11
 */
public static ScopeDecorator get() {
  return INSTANCE;
}