diff --git a/brave-tests/src/main/java/brave/test/ITRemote.java b/brave-tests/src/main/java/brave/test/ITRemote.java index 64f2c42bd6..2836ea2348 100644 --- a/brave-tests/src/main/java/brave/test/ITRemote.java +++ b/brave-tests/src/main/java/brave/test/ITRemote.java @@ -16,6 +16,7 @@ import brave.Tracing; import brave.baggage.BaggageField; import brave.baggage.BaggagePropagation; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import brave.propagation.B3Propagation; import brave.propagation.CurrentTraceContext; import brave.propagation.Propagation; @@ -96,7 +97,7 @@ protected ITRemote() { checkForLeakedScopes = strictScopeDecorator; } propagationFactory = BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(BAGGAGE_FIELD).build(); + .add(SingleBaggageField.remote(BAGGAGE_FIELD)).build(); tracing = tracingBuilder(Sampler.ALWAYS_SAMPLE).build(); } diff --git a/brave-tests/src/main/java/brave/test/propagation/CurrentTraceContextTest.java b/brave-tests/src/main/java/brave/test/propagation/CurrentTraceContextTest.java index a6ec73dea3..938669d0f3 100644 --- a/brave-tests/src/main/java/brave/test/propagation/CurrentTraceContextTest.java +++ b/brave-tests/src/main/java/brave/test/propagation/CurrentTraceContextTest.java @@ -15,7 +15,8 @@ import brave.baggage.BaggageField; import brave.baggage.BaggagePropagation; -import brave.baggage.CorrelationField; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import brave.internal.Nullable; import brave.propagation.B3Propagation; import brave.propagation.CurrentTraceContext; @@ -41,11 +42,11 @@ import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown; public abstract class CurrentTraceContextTest { - protected static final CorrelationField CORRELATION_FIELD = - CorrelationField.create(BaggageField.create("user-id")); + protected static final SingleCorrelationField CORRELATION_FIELD = + SingleCorrelationField.create(BaggageField.create("user-id")); Propagation.Factory baggageFactory = BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(CORRELATION_FIELD.baggageField()).build(); + .add(SingleBaggageField.remote(CORRELATION_FIELD.baggageField())).build(); protected final CurrentTraceContext currentTraceContext; protected final TraceContext context = baggageFactory.decorate( diff --git a/brave/README.md b/brave/README.md index d79f38a0da..c4126a4074 100644 --- a/brave/README.md +++ b/brave/README.md @@ -328,13 +328,15 @@ context. For example, if you have a need to know the a specific request's country code, you can propagate it through the trace as an HTTP header with the same name: ```java +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; + // Configure your baggage field COUNTRY_CODE = BaggageField.create("country-code"); // When you initialize the builder, add the baggage you want to propagate tracingBuilder.propagationFactory( BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(COUNTRY_CODE) + .add(SingleBaggageField.remote(COUNTRY_CODE)) .build() ); @@ -371,29 +373,37 @@ For example, the following will propagate the field "x-vcap-request-id" as-is, b fields "countryCode" and "userId" on the wire as "baggage-country-code" and "baggage-user-id" respectively. ```java +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; + REQUEST_ID = BaggageField.create("x-vcap-request-id"); COUNTRY_CODE = BaggageField.create("countryCode"); USER_ID = BaggageField.create("userId"); tracingBuilder.propagationFactory( BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(REQUEST_ID) - .addRemoteField(COUNTRY_CODE, "baggage-country-code") - .addRemoteField(USER_ID, "baggage-user-id").build()) + .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() ); ``` ### Correlation You can also integrate baggage with other correlated contexts such as logging: ```java +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; + AMZN_TRACE_ID = BaggageField.create("x-amzn-trace-id"); // Allow logging patterns like %X{traceId} %X{x-amzn-trace-id} decorator = MDCScopeDecorator.newBuilder() - .addField(AMZN_TRACE_ID).build(); + .add(SingleCorrelationField.create(AMZN_TRACE_ID)).build() tracingBuilder.propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(AMZN_TRACE_ID) + .add(SingleBaggageField.remote(AMZN_TRACE_ID)) .build()) .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder() .addScopeDecorator(decorator) @@ -406,9 +416,11 @@ override them in the builder as needed. Ex. If your log property is %X{trace-id}, you can do this: ```java -builder.clear(); // traceId is a default field! -builder.addField(CorrelationField.newBuilder(BaggageFields.TRACE_ID) - .name("trace-id").build()); +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; + +scopeBuilder.clear() // TRACE_ID is a default field! + .add(SingleCorrelationField.newBuilder(BaggageFields.TRACE_ID) + .name("trace-id").build()) ``` ### Appropriate usage @@ -428,11 +440,13 @@ Amazon Web Services environment, but not reporting data to X-Ray. To ensure X-Ra correctly, pass-through its tracing header like so. ```java +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; + OTHER_TRACE_ID = BaggageField.create("x-amzn-trace-id"); tracingBuilder.propagationFactory( BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(OTHER_TRACE_ID) + .add(SingleBaggageField.remote(OTHER_TRACE_ID)) .build() ); ``` diff --git a/brave/src/main/java/brave/baggage/BaggageField.java b/brave/src/main/java/brave/baggage/BaggageField.java index bb1f084c63..b9c46a1f59 100644 --- a/brave/src/main/java/brave/baggage/BaggageField.java +++ b/brave/src/main/java/brave/baggage/BaggageField.java @@ -50,30 +50,32 @@ *

Ex. once added to `BaggagePropagation`, you can call below to affect the country code * of the current trace context: *

{@code
- *  COUNTRY_CODE.updateValue("FO");
- *  String countryCode = COUNTRY_CODE.get();
+ * COUNTRY_CODE.updateValue("FO");
+ * String countryCode = COUNTRY_CODE.get();
  * }
* *

Or, if you have a reference to a trace context, it is more efficient to use it explicitly: *

{@code
- *   COUNTRY_CODE.updateValue(span.context(), "FO");
- *  String countryCode = COUNTRY_CODE.get(span.context());
- *  Tags.BAGGAGE_FIELD.tag(COUNTRY_CODE, span);
+ * COUNTRY_CODE.updateValue(span.context(), "FO");
+ * String countryCode = COUNTRY_CODE.get(span.context());
+ * Tags.BAGGAGE_FIELD.tag(COUNTRY_CODE, span);
  * }
* *

Correlation

* - * *

You can also integrate baggage with other correlated contexts such as logging: *

{@code
+ * import brave.baggage.BaggagePropagationConfig.SingleBaggageField;
+ * import brave.baggage.CorrelationScopeConfig.SingleCorrelationField;
+ *
  * AMZN_TRACE_ID = BaggageField.create("x-amzn-trace-id");
  *
  * // Allow logging patterns like %X{traceId} %X{x-amzn-trace-id}
  * decorator = MDCScopeDecorator.newBuilder()
- *                              .addField(AMZN_TRACE_ID).build();
+ *                              .add(SingleCorrelationField.create(AMZN_TRACE_ID)).build()
  *
  * tracingBuilder.propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
- *                                                     .addRemoteField(AMZN_TRACE_ID)
+ *                                                     .add(SingleBaggageField.remote(AMZN_TRACE_ID))
  *                                                     .build())
  *               .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder()
  *                                                                  .addScopeDecorator(decorator)
@@ -101,7 +103,7 @@
  * propagate "arbitrary stuff" with a request.
  *
  * @see BaggagePropagation
- * @see CorrelationField
+ * @see CorrelationScopeConfig
  * @since 5.11
  */
 public final class BaggageField {
@@ -197,7 +199,7 @@ public static List getAll(TraceContextOrSamplingFlags extracted) {
    * made current.
    *
    * @see #getByName(TraceContext, String)
-   * @see CorrelationField#name()
+   * @see CorrelationScopeConfig.SingleCorrelationField#name()
    * @since 5.11
    */
   public final String name() {
diff --git a/brave/src/main/java/brave/baggage/BaggagePropagation.java b/brave/src/main/java/brave/baggage/BaggagePropagation.java
index 86bddc29ab..9ea31605d3 100644
--- a/brave/src/main/java/brave/baggage/BaggagePropagation.java
+++ b/brave/src/main/java/brave/baggage/BaggagePropagation.java
@@ -13,6 +13,7 @@
  */
 package brave.baggage;
 
+import brave.baggage.BaggagePropagationConfig.SingleBaggageField;
 import brave.internal.baggage.BaggageHandler;
 import brave.internal.baggage.BaggageHandlers;
 import brave.internal.baggage.ExtraBaggageFields;
@@ -21,7 +22,6 @@
 import brave.propagation.TraceContext.Extractor;
 import brave.propagation.TraceContext.Injector;
 import brave.propagation.TraceContextOrSamplingFlags;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
@@ -38,13 +38,15 @@
  * 

For example, if you have a need to know the a specific request's country code, you can * propagate it through the trace as HTTP headers. *

{@code
+ * import brave.baggage.BaggagePropagationConfig.SingleBaggageField;
+ *
  * // Configure your baggage field
  * COUNTRY_CODE = BaggageField.create("country-code");
  *
  * // When you initialize the builder, add the baggage you want to propagate
  * tracingBuilder.propagationFactory(
  *   BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
- *                     .addRemoteField(COUNTRY_CODE)
+ *                     .add(SingleBaggageField.remote(COUNTRY_CODE))
  *                     .build()
  * );
  *
@@ -52,32 +54,38 @@
  * Tags.BAGGAGE_FIELD.tag(COUNTRY_CODE, span);
  * }
* - *

Customizing propagtion keys

- * By default, the name used as a propagation key (header) by {@link - * FactoryBuilder#addRemoteField(BaggageField, String...)} is the same as the lowercase variant of - * the field name. You can override this by supplying different key names. Note: they will be - * lower-cased. + *

See {@link BaggageField} for baggage usage examples. + * + *

Customizing propagation keys

+ * {@link SingleBaggageField#remote(BaggageField)} sets the name used as a propagation key (header) + * to the lowercase variant of the field name. You can override this by supplying different key + * names. Note: they will be lower-cased. * *

For example, the following will propagate the field "x-vcap-request-id" as-is, but send the * fields "countryCode" and "userId" on the wire as "baggage-country-code" and "baggage-user-id" * respectively. * *

{@code
+ * import brave.baggage.BaggagePropagationConfig.SingleBaggageField;
+ *
  * REQUEST_ID = BaggageField.create("x-vcap-request-id");
  * COUNTRY_CODE = BaggageField.create("countryCode");
  * USER_ID = BaggageField.create("userId");
  *
  * tracingBuilder.propagationFactory(
  *     BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
- *                       .addRemoteField(REQUEST_ID)
- *                       .addRemoteField(COUNTRY_CODE, "baggage-country-code")
- *                       .addRemoteField(USER_ID, "baggage-user-id").build())
+ *                       .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()
  * );
  * }
* - *

See {@link BaggageField} for usage examples - * * @see BaggageField + * @see BaggagePropagationConfig + * @see BaggagePropagationCustomizer * @see CorrelationScopeDecorator * @since 5.11 */ @@ -91,6 +99,7 @@ public static class FactoryBuilder { // not final to backport ExtraFieldPropagat final Propagation.Factory delegate; final Set allKeyNames = new LinkedHashSet<>(); final Map> fieldToKeyNames = new LinkedHashMap<>(); + final Set configs = new LinkedHashSet<>(); FactoryBuilder(Propagation.Factory delegate) { if (delegate == null) throw new NullPointerException("delegate == null"); @@ -98,75 +107,43 @@ public static class FactoryBuilder { // not final to backport ExtraFieldPropagat } /** - * Returns an immutable copy of the currently configured fields mapped to names for use in - * remote propagation. This allows those who can't create the builder to reconfigure this - * builder. + * Returns an immutable copy of the current {@linkplain #add(BaggagePropagationConfig) + * configuration}. This allows those who can't create the builder to reconfigure this builder. * + * @see #clear() * @since 5.11 */ - public Map> fieldToKeyNames() { - return Collections.unmodifiableMap(new LinkedHashMap<>(fieldToKeyNames)); + public Set configs() { + return Collections.unmodifiableSet(new LinkedHashSet<>(configs)); } /** * Clears all state. This allows those who can't create the builder to reconfigure fields. * - * @see #fieldToKeyNames() + * @see #configs() * @see BaggagePropagationCustomizer * @since 5.11 */ public FactoryBuilder clear() { allKeyNames.clear(); fieldToKeyNames.clear(); + configs.clear(); return this; } - /** - * Adds a {@linkplain BaggageField baggage field}, but does not configure remote propagation. - * - * @throws IllegalArgumentException if the field was already added - * @since 5.11 - */ - public FactoryBuilder addField(BaggageField field) { - if (field == null) throw new NullPointerException("field == null"); - if (fieldToKeyNames.containsKey(field)) { - throw new IllegalArgumentException(field.name + " already added"); + /** @since 5.11 */ + public FactoryBuilder add(BaggagePropagationConfig config) { + if (config == null) throw new NullPointerException("config == null"); + if (!(config instanceof SingleBaggageField)) { + throw new UnsupportedOperationException("dynamic fields not yet supported"); } - fieldToKeyNames.put(field, Collections.emptySet()); - return this; - } - - /** - * Adds a {@linkplain BaggageField baggage field} for remote propagation. - * - *

When {@code keyNames} are not supplied the field is referenced the same in-process as it - * is on the wire. For example, the {@linkplain BaggageField#name() name} "x-vcap-request-id" - * would be set as-is including the prefix. - * - * @param keyNames possibly empty lower-case {@link Propagation#keys() propagation key names}. - * @throws IllegalArgumentException if the field was already added or a key name is already in - * use. - * @since 5.11 - */ - public FactoryBuilder addRemoteField(BaggageField field, String... keyNames) { - if (field == null) throw new NullPointerException("field == null"); - if (keyNames == null) throw new NullPointerException("keyNames == null"); - return addRemoteField(field, Arrays.asList(keyNames)); - } - - /** - * Same as {@link #addRemoteField(BaggageField, String...)}. - * - * @since 5.11 - */ - public FactoryBuilder addRemoteField(BaggageField field, Iterable keyNames) { - if (field == null) throw new NullPointerException("field == null"); - if (keyNames == null) throw new NullPointerException("keyNames == null"); - if (fieldToKeyNames.containsKey(field)) { - throw new IllegalArgumentException(field.name + " already added"); + SingleBaggageField field = (SingleBaggageField) config; + if (fieldToKeyNames.containsKey(field.field)) { + throw new IllegalArgumentException(field.field.name + " already added"); } + configs.add(field); Set lcKeyNames = new LinkedHashSet<>(); - for (String keyName : keyNames) { + for (String keyName : field.keyNames) { String lcName = validateName(keyName).toLowerCase(Locale.ROOT); if (allKeyNames.contains(lcName)) { throw new IllegalArgumentException("Propagation key already in use: " + lcName); @@ -175,12 +152,7 @@ public FactoryBuilder addRemoteField(BaggageField field, Iterable keyNam lcKeyNames.add(lcName); } - if (lcKeyNames.isEmpty()) { // add the default name - allKeyNames.add(field.lcName); - lcKeyNames.add(field.lcName); - } - - fieldToKeyNames.put(field, Collections.unmodifiableSet(lcKeyNames)); + fieldToKeyNames.put(field.field, Collections.unmodifiableSet(lcKeyNames)); return this; } diff --git a/brave/src/main/java/brave/baggage/BaggagePropagationConfig.java b/brave/src/main/java/brave/baggage/BaggagePropagationConfig.java new file mode 100644 index 0000000000..418aac1a2c --- /dev/null +++ b/brave/src/main/java/brave/baggage/BaggagePropagationConfig.java @@ -0,0 +1,172 @@ +/* + * Copyright 2013-2020 The OpenZipkin Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package brave.baggage; + +import brave.propagation.Propagation; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Locale; +import java.util.Set; + +import static brave.baggage.BaggageField.validateName; + +/** + * Holds {@link BaggagePropagation} configuration. + * + *

Field mapping

+ * Your log correlation properties may not be the same as the baggage field names. You can override + * them with the builder as needed. + * + *

Ex. If your log property is %X{trace-id}, you can do this: + *

{@code
+ * import brave.baggage.BaggagePropagationConfig.SingleBaggageField;
+ *
+ * scopeBuilder.clear() // TRACE_ID is a default field!
+ *             .add(SingleBaggageField.newBuilder(BaggageFields.TRACE_ID)
+ *                                        .name("trace-id").build())
+ * }
+ * + *

NoteAt the moment, dynamic fields are not supported. Use {@link + * SingleBaggageField} for each field you need to propagate. + * + * @see BaggagePropagation + * @see BaggageField + * @see BaggagePropagationConfig + * @see BaggagePropagationCustomizer + * @since 5.11 + */ +public class BaggagePropagationConfig { + + /** + * Holds {@link BaggagePropagation} configuration for a {@linkplain BaggageField baggage field}. + * + * @see BaggagePropagation + * @see BaggageField + * @since 5.11 + */ + public static class SingleBaggageField extends BaggagePropagationConfig { + + /** + * Configures this field for only local propagation. This will not be read from or written to + * remote headers. + * + * @see #remote(BaggageField) + * @since 5.11 + */ + public static SingleBaggageField local(BaggageField field) { + return new Builder(field).build(); + } + + /** + * 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(); + } + + /** @since 5.11 */ + public static Builder newBuilder(BaggageField field) { + return new Builder(field); + } + + /** + * Allows decorators to reconfigure correlation of this {@link #field()} + * + * @see BaggagePropagationCustomizer + * @since 5.11 + */ + public Builder toBuilder() { + return new Builder(this); + } + + /** @since 5.11 */ + public static final class Builder { + final BaggageField field; + Set keyNames = new LinkedHashSet<>(); + + Builder(BaggageField field) { + this.field = field; + } + + Builder(SingleBaggageField input) { + this.field = input.field; + this.keyNames = new LinkedHashSet<>(input.keyNames()); + } + + /** + * Configures a {@linkplain Propagation#keys() key name} for remote propagation. + * + * @see SingleBaggageField#keyNames() + * @since 5.11 + */ + public Builder addKeyName(String keyName) { + if (keyName == null) throw new NullPointerException("keyName == null"); + String lcName = validateName(keyName).toLowerCase(Locale.ROOT); + keyNames.add(lcName); + return this; + } + + /** @since 5.11 */ + public SingleBaggageField build() { + return new SingleBaggageField(this); + } + } + + final BaggageField field; + final Set keyNames; + + SingleBaggageField(Builder builder) { // sealed to this package + field = builder.field; + keyNames = builder.keyNames.isEmpty() ? Collections.emptySet() + : Collections.unmodifiableSet(new LinkedHashSet<>(builder.keyNames)); + } + + public BaggageField field() { + return field; + } + + /** + * Returns a possibly empty list of lower-case {@link Propagation#keys() propagation key names}. + * When empty, the field is not propagated remotely. + * + * @since 5.11 + */ + public Set keyNames() { + return keyNames; + } + + @Override public String toString() { + return "SingleBaggageFieldConfig{" + field + "}"; + } + + /** Returns true for any config with the same baggage field. */ + @Override public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof SingleBaggageField)) return false; + return field.equals(((SingleBaggageField) o).field); + } + + /** Returns the same value for any config with the same baggage field. */ + @Override public int hashCode() { + return field.hashCode(); + } + } + + BaggagePropagationConfig() { // sealed + } +} diff --git a/brave/src/main/java/brave/baggage/BaggagePropagationCustomizer.java b/brave/src/main/java/brave/baggage/BaggagePropagationCustomizer.java index a7892e61d6..485af62b90 100644 --- a/brave/src/main/java/brave/baggage/BaggagePropagationCustomizer.java +++ b/brave/src/main/java/brave/baggage/BaggagePropagationCustomizer.java @@ -19,7 +19,7 @@ * This allows configuration plugins to collaborate on building an instance of {@link * BaggagePropagation.Factory}. * - *

For example, a customizer can {@link BaggagePropagation.FactoryBuilder#addRemoteField(BaggageField) + *

For example, a customizer can {@link BaggagePropagation.FactoryBuilder#add(BaggagePropagationConfig) * add a baggage field} without affecting the {@link BaggagePropagation#newFactoryBuilder(Propagation.Factory) * trace propagation format}. * @@ -42,6 +42,7 @@ *

* * @see CorrelationScopeCustomizer + * @see BaggagePropagationConfig * @since 5.11 */ public interface BaggagePropagationCustomizer { diff --git a/brave/src/main/java/brave/baggage/CorrelationField.java b/brave/src/main/java/brave/baggage/CorrelationField.java deleted file mode 100644 index f48d99f2af..0000000000 --- a/brave/src/main/java/brave/baggage/CorrelationField.java +++ /dev/null @@ -1,205 +0,0 @@ -/* - * Copyright 2013-2020 The OpenZipkin Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package brave.baggage; - -import brave.internal.baggage.BaggageContext; -import brave.propagation.CurrentTraceContext; -import brave.propagation.TraceContext; - -/** - * Holds {@link CorrelationScopeDecorator} configuration for a {@linkplain BaggageField baggage - * field}. - * - *

Field mapping

- * Your log correlation properties may not be the same as the baggage field names. You can override - * them with the builder as needed. - * - *

Ex. If your log property is %X{trace-id}, you can do this: - *

{@code
- * scopeBuilder.clear() // TRACE_ID is a default field!
- *             .add(CorrelationField.newBuilder(BaggageFields.TRACE_ID)
- *                                  .name("trace-id").build())
- * }
- * - *

Visibility

- *

By default, field updates only apply during {@linkplain CorrelationScopeDecorator scope - * decoration}. This means values set do not flush immediately to the underlying correlation - * context. Rather, they are scheduled for the next scope operation as a way to control overhead. - * {@link CorrelationField#flushOnUpdate()} overrides this. - * - * @see CorrelationScopeDecorator - * @see BaggageField - * @since 5.11 - */ -public final class CorrelationField { - /** @since 5.11 */ - public static CorrelationField create(BaggageField baggageField) { - return new Builder(baggageField).build(); - } - - /** @since 5.11 */ - public static Builder newBuilder(BaggageField baggageField) { - return new Builder(baggageField); - } - - /** - * Allows decorators to reconfigure correlation of this {@link #baggageField()} - * - * @see CorrelationScopeCustomizer - * @since 5.11 - */ - public Builder toBuilder() { - return new Builder(this); - } - - /** @since 5.11 */ - public static final class Builder { - final BaggageField baggageField; - String name; - boolean dirty, flushOnUpdate; - - Builder(BaggageField baggageField) { - this.baggageField = baggageField; - this.name = baggageField.name(); - } - - Builder(CorrelationField input) { - baggageField = input.baggageField; - name = input.name; - dirty = input.dirty; - flushOnUpdate = input.flushOnUpdate; - } - - /** @see CorrelationField#name() */ - public Builder name(String name) { - this.name = BaggageField.validateName(name); - return this; - } - - /** @see CorrelationField#dirty() */ - public Builder dirty() { - this.dirty = true; - return this; - } - - /** @see CorrelationField#flushOnUpdate() */ - public Builder flushOnUpdate() { - this.flushOnUpdate = true; - return this; - } - - /** @since 5.11 */ - public CorrelationField build() { - return new CorrelationField(this); - } - } - - final BaggageField baggageField; - final String name; - final boolean dirty, flushOnUpdate, readOnly; - - CorrelationField(Builder builder) { // sealed to this package - baggageField = builder.baggageField; - name = builder.name; - dirty = builder.dirty; - flushOnUpdate = builder.flushOnUpdate; - readOnly = baggageField.context instanceof BaggageContext.ReadOnly; - } - - public BaggageField baggageField() { - return baggageField; - } - - /** - * The name to use in the correlation context. This defaults to {@link BaggageField#name()} unless - * overridden by {@link Builder#name(String)}. - * - * @since 5.11 - */ - public String name() { - return name; - } - - /** - * Adds a name in the underlying context which is updated directly. The decorator will overwrite - * any underlying changes when the scope closes. - * - *

This is used when there are a mix of libraries controlling the same correlation field. - * For example, if SLF4J MDC can update the same field name. - * - *

This has a similar performance impact to {@link #flushOnUpdate()}, as it requires tracking - * the field value even if there's no change detected. - * - * @since 5.11 - */ - public boolean dirty() { - return dirty; - } - - /** - * When true, updates made to this name via {@linkplain BaggageField#updateValue(TraceContext, - * String)} flush immediately to the correlation context. - * - *

This is useful for callbacks that have a void return. Ex. - *

{@code
-   * @SendTo(SourceChannels.OUTPUT)
-   * public void timerMessageSource() {
-   *   // Assume BUSINESS_PROCESS is an updatable field
-   *   BUSINESS_PROCESS.updateValue("accounting");
-   *   // Assuming a Log4j context, the expression %{bp} will show "accounting" in businessCode()
-   *   businessCode();
-   * }
-   * }
- * - *

Appropriate Usage

- * This has a significant performance impact as it requires even {@link - * CurrentTraceContext#maybeScope(TraceContext)} to always track values. - * - *

Most fields do not change in the scope of a {@link TraceContext}. For example, standard - * fields such as {@link BaggageFields#SPAN_ID the span ID} and {@linkplain - * BaggageFields#constant(String, String) constants} such as env variables do not need to be - * tracked. Even field value updates do not necessarily need to be flushed to the underlying - * correlation context, as they will apply on the next scope operation. - * - * @since 5.11 - */ - public boolean flushOnUpdate() { - return flushOnUpdate; - } - - /** Returns true if this value is immutable within a {@link TraceContext}. */ - public boolean readOnly() { - return readOnly; - } - - @Override public String toString() { - String baggageName = baggageField.name; - if (baggageName.equals(name)) { - return "CorrelationField{" + name + "}"; - } - return "CorrelationField{" + baggageName + "->" + name + "}"; - } - - /** Returns true for any correlation field with the same baggage field. */ - @Override public boolean equals(Object o) { - if (o == this) return true; - if (!(o instanceof CorrelationField)) return false; - return baggageField.equals(((CorrelationField) o).baggageField); - } - - /** Returns the same value for any correlation field with the same baggage field. */ - @Override public int hashCode() { - return baggageField.hashCode(); - } -} diff --git a/brave/src/main/java/brave/baggage/CorrelationFlushScope.java b/brave/src/main/java/brave/baggage/CorrelationFlushScope.java index 2417a99663..016c5cacfe 100644 --- a/brave/src/main/java/brave/baggage/CorrelationFlushScope.java +++ b/brave/src/main/java/brave/baggage/CorrelationFlushScope.java @@ -22,7 +22,7 @@ import static brave.baggage.CorrelationScopeDecorator.equal; -/** Sets up thread locals needed to support {@link CorrelationField#flushOnUpdate()} */ +/** Sets up thread locals needed to support {@link CorrelationScopeConfig#flushOnUpdate()} */ final class CorrelationFlushScope extends AtomicBoolean implements Scope { final CorrelationUpdateScope updateScope; diff --git a/brave/src/main/java/brave/baggage/CorrelationScopeConfig.java b/brave/src/main/java/brave/baggage/CorrelationScopeConfig.java new file mode 100644 index 0000000000..3365080eb7 --- /dev/null +++ b/brave/src/main/java/brave/baggage/CorrelationScopeConfig.java @@ -0,0 +1,224 @@ +/* + * Copyright 2013-2020 The OpenZipkin Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package brave.baggage; + +import brave.internal.baggage.BaggageContext; +import brave.propagation.CurrentTraceContext; +import brave.propagation.TraceContext; + +/** + * Holds {@link CorrelationScopeDecorator} configuration. + * + *

Field mapping

+ * Your log correlation properties may not be the same as the baggage field names. You can override + * them with the builder as needed. + * + *

Ex. If your log property is %X{trace-id}, you can do this: + *

{@code
+ * import brave.baggage.CorrelationScopeConfig.SingleCorrelationField;
+ *
+ * scopeBuilder.clear() // TRACE_ID is a default field!
+ *             .add(SingleCorrelationField.newBuilder(BaggageFields.TRACE_ID)
+ *                                        .name("trace-id").build())
+ * }
+ * + *

NoteAt the moment, dynamic fields are not supported. Use {@link + * SingleCorrelationField} for each field you need in the correlation context. + * + * @see CorrelationScopeDecorator + * @see BaggageField + * @since 5.11 + */ +public class CorrelationScopeConfig { + + /** + * Holds {@link CorrelationScopeDecorator} configuration for a {@linkplain BaggageField baggage + * field}. + * + *

Visibility

+ *

By default, field updates only apply during {@linkplain CorrelationScopeDecorator scope + * decoration}. This means values set do not flush immediately to the underlying correlation + * context. Rather, they are scheduled for the next scope operation as a way to control overhead. + * {@link SingleCorrelationField#flushOnUpdate()} overrides this. + * + * @see CorrelationScopeDecorator + * @see BaggageField + * @since 5.11 + */ + public static class SingleCorrelationField extends CorrelationScopeConfig { + + /** @since 5.11 */ + public static SingleCorrelationField create(BaggageField baggageField) { + return new Builder(baggageField).build(); + } + + /** @since 5.11 */ + public static Builder newBuilder(BaggageField baggageField) { + return new Builder(baggageField); + } + + /** + * Allows decorators to reconfigure correlation of this {@link #baggageField()} + * + * @see CorrelationScopeCustomizer + * @since 5.11 + */ + public Builder toBuilder() { + return new Builder(this); + } + + /** @since 5.11 */ + public static final class Builder { + final BaggageField baggageField; + String name; + boolean dirty, flushOnUpdate; + + Builder(BaggageField baggageField) { + this.baggageField = baggageField; + this.name = baggageField.name(); + } + + Builder(SingleCorrelationField input) { + baggageField = input.baggageField; + name = input.name; + dirty = input.dirty; + flushOnUpdate = input.flushOnUpdate; + } + + /** @see SingleCorrelationField#name() */ + public Builder name(String name) { + this.name = BaggageField.validateName(name); + return this; + } + + /** @see SingleCorrelationField#dirty() */ + public Builder dirty() { + this.dirty = true; + return this; + } + + /** @see SingleCorrelationField#flushOnUpdate() */ + public Builder flushOnUpdate() { + this.flushOnUpdate = true; + return this; + } + + /** @since 5.11 */ + public SingleCorrelationField build() { + return new SingleCorrelationField(this); + } + } + + final BaggageField baggageField; + final String name; + final boolean dirty, flushOnUpdate, readOnly; + + SingleCorrelationField(Builder builder) { // sealed to this package + baggageField = builder.baggageField; + name = builder.name; + dirty = builder.dirty; + flushOnUpdate = builder.flushOnUpdate; + readOnly = baggageField.context instanceof BaggageContext.ReadOnly; + } + + public BaggageField baggageField() { + return baggageField; + } + + /** + * The name to use in the correlation context. This defaults to {@link BaggageField#name()} + * unless overridden by {@link Builder#name(String)}. + * + * @since 5.11 + */ + public String name() { + return name; + } + + /** + * Adds a name in the underlying context which is updated directly. The decorator will overwrite + * any underlying changes when the scope closes. + * + *

This is used when there are a mix of libraries controlling the same correlation field. + * For example, if SLF4J MDC can update the same field name. + * + *

This has a similar performance impact to {@link #flushOnUpdate()}, as it requires + * tracking the field value even if there's no change detected. + * + * @since 5.11 + */ + public boolean dirty() { + return dirty; + } + + /** + * When true, updates made to this name via {@linkplain BaggageField#updateValue(TraceContext, + * String)} flush immediately to the correlation context. + * + *

This is useful for callbacks that have a void return. Ex. + *

{@code
+     * @SendTo(SourceChannels.OUTPUT)
+     * public void timerMessageSource() {
+     *   // Assume BUSINESS_PROCESS is an updatable field
+     *   BUSINESS_PROCESS.updateValue("accounting");
+     *   // Assuming a Log4j context, the expression %{bp} will show "accounting" in businessCode()
+     *   businessCode();
+     * }
+     * }
+ * + *

Appropriate Usage

+ * This has a significant performance impact as it requires even {@link + * CurrentTraceContext#maybeScope(TraceContext)} to always track values. + * + *

Most fields do not change in the scope of a {@link TraceContext}. For example, standard + * fields such as {@link BaggageFields#SPAN_ID the span ID} and {@linkplain + * BaggageFields#constant(String, String) constants} such as env variables do not need to be + * tracked. Even field value updates do not necessarily need to be flushed to the underlying + * correlation context, as they will apply on the next scope operation. + * + * @since 5.11 + */ + public boolean flushOnUpdate() { + return flushOnUpdate; + } + + /** Returns true if this value is immutable within a {@link TraceContext}. */ + public boolean readOnly() { + return readOnly; + } + + @Override public String toString() { + String baggageName = baggageField.name; + if (baggageName.equals(name)) { + return "SingleCorrelationField{" + name + "}"; + } + return "SingleCorrelationField{" + baggageName + "->" + name + "}"; + } + + /** Returns true for any config with the same baggage field. */ + @Override public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof SingleCorrelationField)) return false; + return baggageField.equals(((SingleCorrelationField) o).baggageField); + } + + /** Returns the same value for any config with the same baggage field. */ + @Override public int hashCode() { + return baggageField.hashCode(); + } + } + + CorrelationScopeConfig() { // sealed + } +} diff --git a/brave/src/main/java/brave/baggage/CorrelationScopeCustomizer.java b/brave/src/main/java/brave/baggage/CorrelationScopeCustomizer.java index a7f9a724ab..1667dd9062 100644 --- a/brave/src/main/java/brave/baggage/CorrelationScopeCustomizer.java +++ b/brave/src/main/java/brave/baggage/CorrelationScopeCustomizer.java @@ -19,7 +19,7 @@ * This allows configuration plugins to collaborate on building an instance of {@link * CorrelationScopeDecorator}. * - *

For example, a customizer can {@linkplain CorrelationScopeDecorator.Builder#addField(CorrelationField) + *

For example, a customizer can {@linkplain CorrelationScopeDecorator.Builder#add(CorrelationScopeConfig) * configure a baggage field} without instantiating the {@link CorrelationScopeDecorator.Builder). * *

This also allows one object to customize both {@linkplain BaggagePropagation baggage} @@ -40,6 +40,7 @@ *

  • Spring Autowired Collections
  • * * + * @see CorrelationScopeConfig * @see BaggageCustomizer * @since 5.11 */ diff --git a/brave/src/main/java/brave/baggage/CorrelationScopeDecorator.java b/brave/src/main/java/brave/baggage/CorrelationScopeDecorator.java index 33bba4ed21..89b2071a3a 100644 --- a/brave/src/main/java/brave/baggage/CorrelationScopeDecorator.java +++ b/brave/src/main/java/brave/baggage/CorrelationScopeDecorator.java @@ -13,6 +13,7 @@ */ package brave.baggage; +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import brave.internal.CorrelationContext; import brave.internal.Nullable; import brave.propagation.CurrentTraceContext.Scope; @@ -29,10 +30,13 @@ * *

    Setup example: *

    {@code
    + * import brave.baggage.CorrelationScopeConfig.SingleCorrelationField;
    + *
      * // Add the field "region", so it can be used as a log expression %X{region}
      * CLOUD_REGION = BaggageFields.constant("region", System.getEnv("CLOUD_REGION"));
    + *
      * decorator = MDCScopeDecorator.newBuilder()
    - *                              .addField(CorrelationField.create(CLOUD_REGION))
    + *                              .add(SingleCorrelationField.create(CLOUD_REGION))
      *                              .build();
      *
      * // Integrate the decorator
    @@ -57,7 +61,9 @@
      * }
      * }
    * - * @see CorrelationField + * @see CorrelationScopeConfig + * @see CorrelationScopeCustomizer + * @see BaggagePropagation * @since 5.11 */ public abstract class CorrelationScopeDecorator implements ScopeDecorator { @@ -67,36 +73,36 @@ public static abstract class Builder { final CorrelationContext context; // Don't allow mixed case of the same name! final Set allNames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); - final Set fields = new LinkedHashSet<>(); + final Set fields = new LinkedHashSet<>(); /** Internal constructor used by subtypes. */ protected Builder(CorrelationContext context) { if (context == null) throw new NullPointerException("context == null"); this.context = context; - addField(CorrelationField.create(BaggageFields.TRACE_ID)); - addField(CorrelationField.create(BaggageFields.SPAN_ID)); + add(SingleCorrelationField.create(BaggageFields.TRACE_ID)); + add(SingleCorrelationField.create(BaggageFields.SPAN_ID)); } /** - * Returns an immutable copy of the currently configured {@linkplain #addField(CorrelationField) - * fields}. This allows those who can't create the builder to reconfigure this builder. + * Returns an immutable copy of the current {@linkplain #add(CorrelationScopeConfig) + * configuration}. This allows those who can't create the builder to reconfigure this builder. * * @see #clear() * @since 5.11 */ - public Set fields() { + public Set configs() { return Collections.unmodifiableSet(new LinkedHashSet<>(fields)); } /** - * Invoke this to clear fields so that you can {@linkplain #addField(CorrelationField) add the + * Invoke this to clear fields so that you can {@linkplain #add(CorrelationScopeConfig) add the * ones you need}. * *

    Defaults may include a field you aren't using, such as {@link BaggageFields#PARENT_ID}. * For best performance, only include the fields you use in your correlation expressions (such * as log formats). * - * @see #fields() + * @see #configs() * @see CorrelationScopeDecorator * @since 5.11 */ @@ -106,13 +112,13 @@ public Builder clear() { return this; } - /** - * Adds a correlation property into the context with its {@link BaggageField#name()}. - * - * @since 5.11 - */ - public Builder addField(CorrelationField field) { - if (field == null) throw new NullPointerException("field == null"); + /** @since 5.11 */ + public Builder add(CorrelationScopeConfig config) { + if (config == null) throw new NullPointerException("config == null"); + if (!(config instanceof SingleCorrelationField)) { + throw new UnsupportedOperationException("dynamic fields not yet supported"); + } + SingleCorrelationField field = (SingleCorrelationField) config; if (fields.contains(field)) { throw new IllegalArgumentException( "Baggage Field already added: " + field.baggageField.name); @@ -130,7 +136,7 @@ public final ScopeDecorator build() { 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 CorrelationField[0])); + return new Multiple(context, fields.toArray(new SingleCorrelationField[0])); } } @@ -141,9 +147,9 @@ public final ScopeDecorator build() { } static final class Single extends CorrelationScopeDecorator { - final CorrelationField field; + final SingleCorrelationField field; - Single(CorrelationContext context, CorrelationField field) { + Single(CorrelationContext context, SingleCorrelationField field) { super(context); this.field = field; } @@ -171,9 +177,9 @@ static final class Single extends CorrelationScopeDecorator { } static final class Multiple extends CorrelationScopeDecorator { - final CorrelationField[] fields; + final SingleCorrelationField[] fields; - Multiple(CorrelationContext context, CorrelationField[] fields) { + Multiple(CorrelationContext context, SingleCorrelationField[] fields) { super(context); this.fields = fields; } @@ -184,7 +190,7 @@ static final class Multiple extends CorrelationScopeDecorator { String[] valuesToRevert = new String[fields.length]; for (int i = 0; i < fields.length; i++) { - CorrelationField field = fields[i]; + SingleCorrelationField field = fields[i]; String valueToRevert = context.getValue(field.name); String currentValue = field.baggageField.getValue(traceContext); diff --git a/brave/src/main/java/brave/baggage/CorrelationUpdateScope.java b/brave/src/main/java/brave/baggage/CorrelationUpdateScope.java index c473f62832..756566effd 100644 --- a/brave/src/main/java/brave/baggage/CorrelationUpdateScope.java +++ b/brave/src/main/java/brave/baggage/CorrelationUpdateScope.java @@ -13,6 +13,7 @@ */ package brave.baggage; +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import brave.internal.CorrelationContext; import brave.internal.Nullable; import brave.propagation.CurrentTraceContext.Scope; @@ -45,14 +46,14 @@ abstract class CorrelationUpdateScope extends AtomicBoolean implements Scope { static final class Single extends CorrelationUpdateScope { final Scope delegate; - final CorrelationField field; + final SingleCorrelationField field; final @Nullable String valueToRevert; boolean shouldRevert; Single( Scope delegate, CorrelationContext context, - CorrelationField field, + SingleCorrelationField field, @Nullable String valueToRevert, boolean shouldRevert ) { @@ -82,14 +83,14 @@ static final class Single extends CorrelationUpdateScope { static final class Multiple extends CorrelationUpdateScope { final Scope delegate; - final CorrelationField[] fields; + final SingleCorrelationField[] fields; final String[] valuesToRevert; int shouldRevert; Multiple( Scope delegate, CorrelationContext context, - CorrelationField[] fields, + SingleCorrelationField[] fields, String[] valuesToRevert, int shouldRevert ) { diff --git a/brave/src/main/java/brave/propagation/ExtraFieldPropagation.java b/brave/src/main/java/brave/propagation/ExtraFieldPropagation.java index 5297156f58..5b37328fe4 100644 --- a/brave/src/main/java/brave/propagation/ExtraFieldPropagation.java +++ b/brave/src/main/java/brave/propagation/ExtraFieldPropagation.java @@ -15,6 +15,7 @@ import brave.baggage.BaggageField; import brave.baggage.BaggagePropagation; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import brave.internal.Nullable; import brave.propagation.TraceContext.Extractor; import brave.propagation.TraceContext.Injector; @@ -69,9 +70,7 @@ this.baggageFactory = BaggagePropagation.newFactoryBuilder(delegate); } - /** - * @deprecated Since 5.11, use {@link BaggagePropagation.FactoryBuilder#addField(BaggageField)} - */ + /** @deprecated Since 5.11, use {@link SingleBaggageField#local(BaggageField)} */ @Deprecated public FactoryBuilder addRedactedField(String fieldName) { fieldName = validateFieldName(fieldName); redactedNames.add(fieldName); @@ -79,20 +78,14 @@ return this; } - /** - * @deprecated Since 5.11, use {@link BaggagePropagation.FactoryBuilder#addRemoteField(BaggageField, - * String...)}. - */ + /** @deprecated Since 5.11, use {@link SingleBaggageField#remote(BaggageField)} */ @Deprecated public FactoryBuilder addField(String fieldName) { fieldName = validateFieldName(fieldName); addKeyName(fieldName, fieldName); return this; } - /** - * @deprecated Since 5.11, use {@link BaggagePropagation.FactoryBuilder#addRemoteField(BaggageField, - * Iterable)} - */ + /** @deprecated Since 5.11, use {@link SingleBaggageField.Builder#addKeyName(String)} */ @Deprecated public FactoryBuilder addPrefixedFields(String prefix, Collection names) { if (prefix == null) throw new NullPointerException("prefix == null"); prefix = validateFieldName(prefix); @@ -116,10 +109,14 @@ public Factory build() { for (Map.Entry> entry : nameToKeyNames.entrySet()) { BaggageField field = BaggageField.create(entry.getKey()); if (redactedNames.contains(field.name())) { - baggageFactory.addField(field); + baggageFactory.add(SingleBaggageField.local(field)); } else { extraKeyNames.addAll(entry.getValue()); - baggageFactory.addRemoteField(field, 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])); diff --git a/brave/src/test/java/brave/TracerTest.java b/brave/src/test/java/brave/TracerTest.java index e501d0ca2b..d70b34c009 100644 --- a/brave/src/test/java/brave/TracerTest.java +++ b/brave/src/test/java/brave/TracerTest.java @@ -17,6 +17,7 @@ import brave.Tracer.SpanInScope; import brave.baggage.BaggageField; import brave.baggage.BaggagePropagation; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import brave.handler.FinishedSpanHandler; import brave.handler.MutableSpan; import brave.propagation.B3Propagation; @@ -55,7 +56,7 @@ public class TracerTest { Propagation.Factory propagationFactory = B3Propagation.FACTORY; CurrentTraceContext currentTraceContext = StrictCurrentTraceContext.create(); Propagation.Factory baggageFactory = BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(BAGGAGE_FIELD).build(); + .add(SingleBaggageField.remote(BAGGAGE_FIELD)).build(); Tracer tracer = Tracing.newBuilder() .spanReporter(new Reporter() { @Override public void report(zipkin2.Span span) { diff --git a/brave/src/test/java/brave/baggage/BaggageFieldTest.java b/brave/src/test/java/brave/baggage/BaggageFieldTest.java index 459c099df3..93bbea0dab 100644 --- a/brave/src/test/java/brave/baggage/BaggageFieldTest.java +++ b/brave/src/test/java/brave/baggage/BaggageFieldTest.java @@ -14,9 +14,10 @@ package brave.baggage; import brave.Tracing; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import brave.internal.baggage.BaggageContext; -import brave.internal.baggage.ExtraBaggageFields; import brave.internal.baggage.ExtraBaggageContext; +import brave.internal.baggage.ExtraBaggageFields; import brave.propagation.B3Propagation; import brave.propagation.CurrentTraceContext.Scope; import brave.propagation.Propagation; @@ -36,8 +37,8 @@ public class BaggageFieldTest { static final BaggageField AMZN_TRACE_ID = BaggageField.create("x-amzn-trace-id"); Propagation.Factory factory = BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(REQUEST_ID, "x-vcap-request-id") - .addRemoteField(AMZN_TRACE_ID).build(); + .add(SingleBaggageField.newBuilder(REQUEST_ID).addKeyName("x-vcap-request-id").build()) + .add(SingleBaggageField.remote(AMZN_TRACE_ID)).build(); Propagation propagation = factory.create(Propagation.KeyFactory.STRING); Extractor> extractor = propagation.extractor(Map::get); diff --git a/brave/src/test/java/brave/baggage/CorrelationScopeDecoratorTest.java b/brave/src/test/java/brave/baggage/CorrelationScopeDecoratorTest.java index cbaa856c1d..485d430fd8 100644 --- a/brave/src/test/java/brave/baggage/CorrelationScopeDecoratorTest.java +++ b/brave/src/test/java/brave/baggage/CorrelationScopeDecoratorTest.java @@ -13,6 +13,8 @@ */ package brave.baggage; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import brave.baggage.CorrelationUpdateScope.Single; import brave.internal.CorrelationContext; import brave.internal.Nullable; @@ -37,23 +39,24 @@ import static org.mockito.Mockito.mock; public class CorrelationScopeDecoratorTest { - static final CorrelationField - TRACE_ID = CorrelationField.newBuilder(BaggageFields.TRACE_ID).name("X-B3-TraceId").build(), - FIELD = CorrelationField.create(BaggageField.create("user-id")), - FIELD_2 = CorrelationField.create(BaggageField.create("country-code")), + static final SingleCorrelationField + TRACE_ID = + SingleCorrelationField.newBuilder(BaggageFields.TRACE_ID).name("X-B3-TraceId").build(), + FIELD = SingleCorrelationField.create(BaggageField.create("user-id")), + FIELD_2 = SingleCorrelationField.create(BaggageField.create("country-code")), DIRTY_FIELD = FIELD.toBuilder().name("dirty").dirty().build(), - LOCAL_FIELD = CorrelationField.create(BaggageField.create("serviceId")), - FLUSH_FIELD = CorrelationField.newBuilder(BaggageField.create("bp")) + LOCAL_FIELD = SingleCorrelationField.create(BaggageField.create("serviceId")), + FLUSH_FIELD = SingleCorrelationField.newBuilder(BaggageField.create("bp")) .name("flushed") .flushOnUpdate() .build(); static final Map map = new LinkedHashMap<>(); Propagation.Factory baggageFactory = BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addField(LOCAL_FIELD.baggageField()) - .addRemoteField(FIELD.baggageField()) - .addRemoteField(FIELD_2.baggageField()) - .addRemoteField(FLUSH_FIELD.baggageField()) + .add(SingleBaggageField.local(LOCAL_FIELD.baggageField())) + .add(SingleBaggageField.remote(FIELD.baggageField())) + .add(SingleBaggageField.remote(FIELD_2.baggageField())) + .add(SingleBaggageField.remote(FLUSH_FIELD.baggageField())) .build(); TraceContext context = baggageFactory.decorate(TraceContext.newBuilder() @@ -66,35 +69,35 @@ public class CorrelationScopeDecoratorTest { ScopeDecorator decorator = new TestBuilder().build(); ScopeDecorator onlyTraceIdDecorator = new TestBuilder() .clear() - .addField(TRACE_ID) + .add(TRACE_ID) .build(); ScopeDecorator onlyScopeDecorator = new TestBuilder() .clear() - .addField(FIELD) + .add(FIELD) .build(); ScopeDecorator withBaggageFieldsDecorator = new TestBuilder() .clear() - .addField(TRACE_ID) - .addField(FIELD) - .addField(LOCAL_FIELD) - .addField(FIELD_2) + .add(TRACE_ID) + .add(FIELD) + .add(LOCAL_FIELD) + .add(FIELD_2) .build(); ScopeDecorator withFlushOnUpdateScopeDecorator = new TestBuilder() - .addField(FIELD) - .addField(LOCAL_FIELD) - .addField(FIELD_2) - .addField(FLUSH_FIELD) + .add(FIELD) + .add(LOCAL_FIELD) + .add(FIELD_2) + .add(FLUSH_FIELD) .build(); ScopeDecorator onlyFlushOnUpdateScopeDecorator = new TestBuilder() .clear() - .addField(FLUSH_FIELD) + .add(FLUSH_FIELD) .build(); ScopeDecorator withDirtyFieldDecorator = new TestBuilder() - .addField(DIRTY_FIELD) + .add(DIRTY_FIELD) .build(); ScopeDecorator onlyDirtyFieldDecorator = new TestBuilder() .clear() - .addField(DIRTY_FIELD) + .add(DIRTY_FIELD) .build(); @Before public void before() { @@ -107,29 +110,29 @@ public class CorrelationScopeDecoratorTest { @Test public void no_dupes() { CorrelationScopeDecorator.Builder builder = - new TestBuilder().addField(FLUSH_FIELD); + new TestBuilder().add(FLUSH_FIELD); - assertThatThrownBy(() -> builder.addField(FLUSH_FIELD)) + assertThatThrownBy(() -> builder.add(FLUSH_FIELD)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Baggage Field already added: bp"); } @Test public void clear_and_add() { CorrelationScopeDecorator.Builder builder = new TestBuilder() - .addField(FIELD) - .addField(FLUSH_FIELD); + .add(FIELD) + .add(FLUSH_FIELD); - Set fields = builder.fields(); + Set fields = builder.configs(); builder.clear(); - fields.forEach(builder::addField); + fields.forEach(builder::add); assertThat(builder) .usingRecursiveComparison() .isEqualTo(new TestBuilder() - .addField(FIELD) - .addField(FLUSH_FIELD)); + .add(FIELD) + .add(FLUSH_FIELD)); } @Test public void doesntDecorateNoop() { @@ -147,7 +150,7 @@ public class CorrelationScopeDecoratorTest { (CorrelationUpdateScope.Multiple) withDirtyFieldDecorator.decorateScope(context, Scope.NOOP); BitSet shouldRevert = BitSet.valueOf(new long[] {scopeMultiple.shouldRevert}); - assertThat(scopeMultiple.fields).extracting(CorrelationField::name) + assertThat(scopeMultiple.fields).extracting(SingleCorrelationField::name) .containsExactly("traceId", "spanId", "dirty"); assertThat(shouldRevert.get(0)).isFalse(); diff --git a/brave/src/test/java/brave/features/opentracing/OpenTracingAdapterTest.java b/brave/src/test/java/brave/features/opentracing/OpenTracingAdapterTest.java index e6e549d3d0..652a5b0b78 100644 --- a/brave/src/test/java/brave/features/opentracing/OpenTracingAdapterTest.java +++ b/brave/src/test/java/brave/features/opentracing/OpenTracingAdapterTest.java @@ -14,9 +14,10 @@ package brave.features.opentracing; import brave.Tracing; -import brave.propagation.B3Propagation; import brave.baggage.BaggageField; import brave.baggage.BaggagePropagation; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; +import brave.propagation.B3Propagation; import brave.propagation.TraceContext; import io.opentracing.propagation.Format; import io.opentracing.propagation.TextMapAdapter; @@ -41,7 +42,7 @@ public class OpenTracingAdapterTest { List spans = new ArrayList<>(); Tracing brave = Tracing.newBuilder() .propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(BAGGAGE_FIELD).build()) + .add(SingleBaggageField.remote(BAGGAGE_FIELD)).build()) .spanReporter(spans::add).build(); BraveTracer opentracing = BraveTracer.wrap(brave); diff --git a/brave/src/test/java/brave/internal/InternalBaggageTest.java b/brave/src/test/java/brave/internal/InternalBaggageTest.java index 1d387d474e..5e6509b009 100644 --- a/brave/src/test/java/brave/internal/InternalBaggageTest.java +++ b/brave/src/test/java/brave/internal/InternalBaggageTest.java @@ -15,6 +15,7 @@ import brave.baggage.BaggageField; import brave.baggage.BaggagePropagation; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import brave.propagation.B3SinglePropagation; import brave.propagation.ExtraFieldPropagation; import brave.propagation.Propagation; @@ -25,9 +26,9 @@ public class InternalBaggageTest { @Test public void allKeyNames_baggagePropagation() { Propagation.Factory factory = BaggagePropagation.newFactoryBuilder(B3SinglePropagation.FACTORY) - .addField(BaggageField.create("redacted")) - .addRemoteField(BaggageField.create("user-id")) - .addRemoteField(BaggageField.create("session-id")).build(); + .add(SingleBaggageField.local(BaggageField.create("redacted"))) + .add(SingleBaggageField.remote(BaggageField.create("user-id"))) + .add(SingleBaggageField.remote(BaggageField.create("session-id"))).build(); assertThat(InternalBaggage.instance.allKeyNames(factory)) .containsExactly("b3", "user-id", "session-id"); } diff --git a/brave/src/test/java/brave/internal/baggage/BaggagePropagationTest.java b/brave/src/test/java/brave/internal/baggage/BaggagePropagationTest.java index 445f3d2e48..0b11d8aeae 100644 --- a/brave/src/test/java/brave/internal/baggage/BaggagePropagationTest.java +++ b/brave/src/test/java/brave/internal/baggage/BaggagePropagationTest.java @@ -15,6 +15,8 @@ import brave.baggage.BaggageField; import brave.baggage.BaggagePropagation; +import brave.baggage.BaggagePropagationConfig; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import brave.propagation.B3Propagation; import brave.propagation.B3SingleFormat; import brave.propagation.B3SinglePropagation; @@ -42,8 +44,8 @@ public class BaggagePropagationTest { "Root=1-67891233-abcdef012345678912345678;Parent=463ac35c9f6413ad;Sampled=1"; String uuid = "f4308d05-2228-4468-80f6-92a8377ba193"; Propagation.Factory factory = newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(vcapRequestId) - .addRemoteField(amznTraceId).build(); + .add(SingleBaggageField.remote(vcapRequestId)) + .add(SingleBaggageField.remote(amznTraceId)).build(); Map carrier = new LinkedHashMap<>(); TraceContext.Injector> injector; @@ -75,26 +77,38 @@ public class BaggagePropagationTest { @Test public void newFactory_sharingRemoteName() { BaggagePropagation.FactoryBuilder builder = newFactoryBuilder(B3Propagation.FACTORY); - builder.addRemoteField(BaggageField.create("userName"), "baggage"); - assertThatThrownBy(() -> builder.addRemoteField(BaggageField.create("userId"), "baggage")) + 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"); } - @Test public void name_clear_and_add() { - BaggagePropagation.FactoryBuilder builder = newFactoryBuilder(B3Propagation.FACTORY); - builder.addRemoteField(vcapRequestId, "request-id", "request_id"); - builder.addRemoteField(amznTraceId).build(); + @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 configs = builder.configs(); - Map> saved = builder.fieldToKeyNames(); builder.clear(); - saved.forEach(builder::addRemoteField); + + configs.forEach(builder::add); assertThat(builder) .usingRecursiveComparison() .isEqualTo(newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(vcapRequestId, "request-id", "request_id") - .addRemoteField(amznTraceId)); + .add(requestIdConfig) + .add(traceIdConfig)); } @Test public void inject_baggage() { @@ -156,9 +170,19 @@ public class BaggagePropagationTest { 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) - .addRemoteField(userId, "baggage-userId", "baggage_userId") - .addRemoteField(sessionId, "baggage-sessionId", "baggage_sessionId") + .add(userIdConfig) + .add(sessionIdConfig) .build(); initialize(); @@ -177,8 +201,8 @@ public class BaggagePropagationTest { BaggageField sessionId = BaggageField.create("sessionId"); factory = newFactoryBuilder(B3Propagation.FACTORY) - .addField(userId) - .addRemoteField(sessionId) + .add(SingleBaggageField.local(userId)) + .add(SingleBaggageField.remote(sessionId)) .build(); initialize(); @@ -198,8 +222,8 @@ public class BaggagePropagationTest { BaggageField sessionId = BaggageField.create("sessionId"); factory = newFactoryBuilder(B3SinglePropagation.FACTORY) - .addField(userId) - .addRemoteField(sessionId) + .add(SingleBaggageField.local(userId)) + .add(SingleBaggageField.remote(sessionId)) .build(); initialize(); @@ -217,9 +241,21 @@ public class BaggagePropagationTest { 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) - .addRemoteField(userId, "userId", "baggage-userId", "baggage_userId") - .addRemoteField(sessionId, "sessionId", "baggage-sessionId", "baggage_sessionId") + .add(userIdConfig) + .add(sessionIdConfig) .build(); initialize(); @@ -241,9 +277,10 @@ public class BaggagePropagationTest { } @Test public void dupesNotOk() { + SingleBaggageField userIdConfig = SingleBaggageField.local(BaggageField.create("userId")); BaggagePropagation.FactoryBuilder builder = newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(BaggageField.create("userId")); - assertThatThrownBy(() -> builder.addRemoteField(BaggageField.create("userId"))) + .add(userIdConfig); + assertThatThrownBy(() -> builder.add(userIdConfig)) .isInstanceOf(IllegalArgumentException.class); } } diff --git a/context/log4j12/src/main/java/brave/context/log4j12/MDCScopeDecorator.java b/context/log4j12/src/main/java/brave/context/log4j12/MDCScopeDecorator.java index ea284527ca..6f17fb9269 100644 --- a/context/log4j12/src/main/java/brave/context/log4j12/MDCScopeDecorator.java +++ b/context/log4j12/src/main/java/brave/context/log4j12/MDCScopeDecorator.java @@ -14,7 +14,7 @@ package brave.context.log4j12; import brave.baggage.BaggageFields; -import brave.baggage.CorrelationField; +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import brave.baggage.CorrelationScopeDecorator; import brave.internal.CorrelationContext; import brave.internal.Nullable; @@ -71,10 +71,10 @@ public static CorrelationScopeDecorator.Builder newBuilder() { @Deprecated public static CurrentTraceContext.ScopeDecorator create() { return new Builder() .clear() - .addField(CorrelationField.create(BaggageFields.TRACE_ID)) - .addField(CorrelationField.create(BaggageFields.PARENT_ID)) - .addField(CorrelationField.create(BaggageFields.SPAN_ID)) - .addField(CorrelationField.create(BaggageFields.SAMPLED)) + .add(SingleCorrelationField.create(BaggageFields.TRACE_ID)) + .add(SingleCorrelationField.create(BaggageFields.PARENT_ID)) + .add(SingleCorrelationField.create(BaggageFields.SPAN_ID)) + .add(SingleCorrelationField.create(BaggageFields.SAMPLED)) .build(); } diff --git a/context/log4j12/src/test/java/brave/context/log4j12/MDCScopeDecoratorTest.java b/context/log4j12/src/test/java/brave/context/log4j12/MDCScopeDecoratorTest.java index 4e2418cf96..c407b93cd6 100644 --- a/context/log4j12/src/test/java/brave/context/log4j12/MDCScopeDecoratorTest.java +++ b/context/log4j12/src/test/java/brave/context/log4j12/MDCScopeDecoratorTest.java @@ -53,7 +53,7 @@ static void assumeMDCWorks() { static class BuilderSupplier implements Supplier { @Override public CurrentTraceContext.Builder get() { return ThreadLocalCurrentTraceContext.newBuilder() - .addScopeDecorator(MDCScopeDecorator.newBuilder().addField(CORRELATION_FIELD).build()); + .addScopeDecorator(MDCScopeDecorator.newBuilder().add(CORRELATION_FIELD).build()); } } diff --git a/context/log4j2/src/main/java/brave/context/log4j2/ThreadContextScopeDecorator.java b/context/log4j2/src/main/java/brave/context/log4j2/ThreadContextScopeDecorator.java index 9f4c38a438..3078717365 100644 --- a/context/log4j2/src/main/java/brave/context/log4j2/ThreadContextScopeDecorator.java +++ b/context/log4j2/src/main/java/brave/context/log4j2/ThreadContextScopeDecorator.java @@ -14,7 +14,7 @@ package brave.context.log4j2; import brave.baggage.BaggageFields; -import brave.baggage.CorrelationField; +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import brave.baggage.CorrelationScopeDecorator; import brave.internal.CorrelationContext; import brave.internal.Nullable; @@ -71,10 +71,10 @@ public static CorrelationScopeDecorator.Builder newBuilder() { @Deprecated public static CurrentTraceContext.ScopeDecorator create() { return new Builder() .clear() - .addField(CorrelationField.create(BaggageFields.TRACE_ID)) - .addField(CorrelationField.create(BaggageFields.PARENT_ID)) - .addField(CorrelationField.create(BaggageFields.SPAN_ID)) - .addField(CorrelationField.create(BaggageFields.SAMPLED)) + .add(SingleCorrelationField.create(BaggageFields.TRACE_ID)) + .add(SingleCorrelationField.create(BaggageFields.PARENT_ID)) + .add(SingleCorrelationField.create(BaggageFields.SPAN_ID)) + .add(SingleCorrelationField.create(BaggageFields.SAMPLED)) .build(); } diff --git a/context/log4j2/src/test/java/brave/context/log4j2/ThreadContextScopeDecoratorTest.java b/context/log4j2/src/test/java/brave/context/log4j2/ThreadContextScopeDecoratorTest.java index 1d85b03dbd..2a65b90928 100644 --- a/context/log4j2/src/test/java/brave/context/log4j2/ThreadContextScopeDecoratorTest.java +++ b/context/log4j2/src/test/java/brave/context/log4j2/ThreadContextScopeDecoratorTest.java @@ -32,7 +32,7 @@ static class BuilderSupplier implements Supplier { @Override public CurrentTraceContext.Builder get() { return ThreadLocalCurrentTraceContext.newBuilder() .addScopeDecorator(ThreadContextScopeDecorator.newBuilder() - .addField(CORRELATION_FIELD).build()); + .add(CORRELATION_FIELD).build()); } } diff --git a/context/slf4j/src/main/java/brave/context/slf4j/MDCScopeDecorator.java b/context/slf4j/src/main/java/brave/context/slf4j/MDCScopeDecorator.java index 6553a093ef..e9d94ed029 100644 --- a/context/slf4j/src/main/java/brave/context/slf4j/MDCScopeDecorator.java +++ b/context/slf4j/src/main/java/brave/context/slf4j/MDCScopeDecorator.java @@ -14,7 +14,7 @@ package brave.context.slf4j; import brave.baggage.BaggageFields; -import brave.baggage.CorrelationField; +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import brave.baggage.CorrelationScopeDecorator; import brave.internal.CorrelationContext; import brave.internal.Nullable; @@ -72,10 +72,10 @@ public static CorrelationScopeDecorator.Builder newBuilder() { @Deprecated public static CurrentTraceContext.ScopeDecorator create() { return new Builder() .clear() - .addField(CorrelationField.create(BaggageFields.TRACE_ID)) - .addField(CorrelationField.create(BaggageFields.PARENT_ID)) - .addField(CorrelationField.create(BaggageFields.SPAN_ID)) - .addField(CorrelationField.create(BaggageFields.SAMPLED)) + .add(SingleCorrelationField.create(BaggageFields.TRACE_ID)) + .add(SingleCorrelationField.create(BaggageFields.PARENT_ID)) + .add(SingleCorrelationField.create(BaggageFields.SPAN_ID)) + .add(SingleCorrelationField.create(BaggageFields.SAMPLED)) .build(); } diff --git a/context/slf4j/src/test/java/brave/context/slf4j/MDCScopeDecoratorTest.java b/context/slf4j/src/test/java/brave/context/slf4j/MDCScopeDecoratorTest.java index 042cc89655..fbd3825313 100644 --- a/context/slf4j/src/test/java/brave/context/slf4j/MDCScopeDecoratorTest.java +++ b/context/slf4j/src/test/java/brave/context/slf4j/MDCScopeDecoratorTest.java @@ -31,7 +31,7 @@ public class MDCScopeDecoratorTest extends CurrentTraceContextTest { static class BuilderSupplier implements Supplier { @Override public CurrentTraceContext.Builder get() { return ThreadLocalCurrentTraceContext.newBuilder() - .addScopeDecorator(MDCScopeDecorator.newBuilder().addField(CORRELATION_FIELD).build()); + .addScopeDecorator(MDCScopeDecorator.newBuilder().add(CORRELATION_FIELD).build()); } } diff --git a/instrumentation/benchmarks/src/main/java/brave/EndToEndBenchmarks.java b/instrumentation/benchmarks/src/main/java/brave/EndToEndBenchmarks.java index 38361cdff3..41fd3e1f71 100644 --- a/instrumentation/benchmarks/src/main/java/brave/EndToEndBenchmarks.java +++ b/instrumentation/benchmarks/src/main/java/brave/EndToEndBenchmarks.java @@ -15,6 +15,7 @@ import brave.baggage.BaggageField; import brave.baggage.BaggagePropagation; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import brave.context.log4j2.ThreadContextScopeDecorator; import brave.handler.FinishedSpanHandler; import brave.handler.MutableSpan; @@ -137,9 +138,14 @@ public static class TracedBaggage extends ForwardingTracingFilter { public TracedBaggage() { super(Tracing.newBuilder() .propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(REQUEST_ID) - .addRemoteField(COUNTRY_CODE, "baggage-country-code") - .addRemoteField(USER_ID, "baggage-user-id").build()) + .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()) .spanReporter(AsyncReporter.create(new NoopSender())) .build()); } diff --git a/instrumentation/benchmarks/src/main/java/brave/TracerBenchmarks.java b/instrumentation/benchmarks/src/main/java/brave/TracerBenchmarks.java index 45408d2d16..0b2f08ace3 100644 --- a/instrumentation/benchmarks/src/main/java/brave/TracerBenchmarks.java +++ b/instrumentation/benchmarks/src/main/java/brave/TracerBenchmarks.java @@ -13,10 +13,11 @@ */ package brave; +import brave.baggage.BaggagePropagation; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import brave.handler.FinishedSpanHandler; import brave.handler.MutableSpan; import brave.propagation.B3Propagation; -import brave.baggage.BaggagePropagation; import brave.propagation.CurrentTraceContext; import brave.propagation.Propagation; import brave.propagation.TraceContext; @@ -52,7 +53,7 @@ @State(Scope.Benchmark) public class TracerBenchmarks { Propagation.Factory baggageFactory = BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(BAGGAGE_FIELD).build(); + .add(SingleBaggageField.remote(BAGGAGE_FIELD)).build(); TraceContext context = TraceContext.newBuilder().traceIdHigh(333L).traceId(444L).spanId(3).sampled(true).build(); diff --git a/instrumentation/benchmarks/src/main/java/brave/baggage/BaggagePropagationBenchmarks.java b/instrumentation/benchmarks/src/main/java/brave/baggage/BaggagePropagationBenchmarks.java index d45cbc1a25..46d84cd764 100644 --- a/instrumentation/benchmarks/src/main/java/brave/baggage/BaggagePropagationBenchmarks.java +++ b/instrumentation/benchmarks/src/main/java/brave/baggage/BaggagePropagationBenchmarks.java @@ -13,6 +13,7 @@ */ package brave.baggage; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import brave.internal.HexCodec; import brave.propagation.B3Propagation; import brave.propagation.Propagation; @@ -44,7 +45,8 @@ public class BaggagePropagationBenchmarks { public static final BaggageField BAGGAGE_FIELD = BaggageField.create("user-id"); static final Propagation.Factory factory = - BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY).addRemoteField(BAGGAGE_FIELD).build(); + BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) + .add(SingleBaggageField.remote(BAGGAGE_FIELD)).build(); static final Propagation propagation = factory.create(Propagation.KeyFactory.STRING); static final Injector> injector = propagation.injector(Map::put); static final Extractor> extractor = propagation.extractor(Map::get); diff --git a/instrumentation/benchmarks/src/main/java/brave/jersey/server/JerseyServerBenchmarks.java b/instrumentation/benchmarks/src/main/java/brave/jersey/server/JerseyServerBenchmarks.java index d7503617f0..ea934ab1eb 100644 --- a/instrumentation/benchmarks/src/main/java/brave/jersey/server/JerseyServerBenchmarks.java +++ b/instrumentation/benchmarks/src/main/java/brave/jersey/server/JerseyServerBenchmarks.java @@ -14,10 +14,11 @@ package brave.jersey.server; import brave.Tracing; +import brave.baggage.BaggagePropagation; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import brave.http.HttpServerBenchmarks; import brave.http.HttpTracing; import brave.propagation.B3Propagation; -import brave.baggage.BaggagePropagation; import brave.sampler.Sampler; import io.undertow.servlet.api.DeploymentInfo; import java.util.Collections; @@ -83,8 +84,7 @@ public static class TracedBaggageApp extends Application { return new LinkedHashSet<>(asList(new Resource(), TracingApplicationEventListener.create( HttpTracing.create(Tracing.newBuilder() .propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(BAGGAGE_FIELD) - .build()) + .add(SingleBaggageField.remote(BAGGAGE_FIELD)).build()) .spanReporter(Reporter.NOOP) .build()) ))); diff --git a/instrumentation/benchmarks/src/main/java/brave/netty/http/NettyHttpServerBenchmarks.java b/instrumentation/benchmarks/src/main/java/brave/netty/http/NettyHttpServerBenchmarks.java index 7bde02badd..437e9a3b42 100644 --- a/instrumentation/benchmarks/src/main/java/brave/netty/http/NettyHttpServerBenchmarks.java +++ b/instrumentation/benchmarks/src/main/java/brave/netty/http/NettyHttpServerBenchmarks.java @@ -15,6 +15,7 @@ import brave.Tracing; import brave.baggage.BaggagePropagation; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import brave.http.HttpServerBenchmarks; import brave.propagation.B3Propagation; import brave.sampler.Sampler; @@ -64,9 +65,11 @@ static class TracingDispatchHandler extends ChannelDuplexHandler { final ChannelDuplexHandler traced = NettyHttpTracing.create( Tracing.newBuilder() .propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(REQUEST_ID) - .addRemoteField(COUNTRY_CODE, "baggage-country-code") - .addRemoteField(USER_ID, "baggage-user-id").build()) + .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()) .spanReporter(Reporter.NOOP) .build() ).serverHandler(); diff --git a/instrumentation/benchmarks/src/main/java/brave/propagation/CurrentTraceContextBenchmarks.java b/instrumentation/benchmarks/src/main/java/brave/propagation/CurrentTraceContextBenchmarks.java index 50c8b3a172..f7c6241ff0 100644 --- a/instrumentation/benchmarks/src/main/java/brave/propagation/CurrentTraceContextBenchmarks.java +++ b/instrumentation/benchmarks/src/main/java/brave/propagation/CurrentTraceContextBenchmarks.java @@ -15,7 +15,8 @@ import brave.baggage.BaggageFields; import brave.baggage.BaggagePropagation; -import brave.baggage.CorrelationField; +import brave.baggage.BaggagePropagationConfig; +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import brave.context.log4j2.ThreadContextScopeDecorator; import brave.propagation.CurrentTraceContext.Scope; import java.util.concurrent.TimeUnit; @@ -45,13 +46,13 @@ public class CurrentTraceContextBenchmarks { static final CurrentTraceContext log4j2OnlyTraceId = ThreadLocalCurrentTraceContext.newBuilder() .addScopeDecorator(ThreadContextScopeDecorator.newBuilder() .clear() - .addField(CorrelationField.create(BaggageFields.TRACE_ID)) + .add(SingleCorrelationField.create(BaggageFields.TRACE_ID)) .build()) .build(); static final CurrentTraceContext log4j2OnlyBaggage = ThreadLocalCurrentTraceContext.newBuilder() .addScopeDecorator(ThreadContextScopeDecorator.newBuilder() .clear() - .addField(CorrelationField.create(BAGGAGE_FIELD)) + .add(SingleCorrelationField.create(BAGGAGE_FIELD)) .build()) .build(); static final CurrentTraceContext log4j2 = ThreadLocalCurrentTraceContext.newBuilder() @@ -60,12 +61,11 @@ public class CurrentTraceContextBenchmarks { static final Propagation.Factory baggageFactory = BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(BAGGAGE_FIELD) - .build(); + .add(BaggagePropagationConfig.SingleBaggageField.remote(BAGGAGE_FIELD)).build(); static final CurrentTraceContext log4j2Baggage = ThreadLocalCurrentTraceContext.newBuilder() .addScopeDecorator(ThreadContextScopeDecorator.newBuilder() - .addField(CorrelationField.create(BAGGAGE_FIELD)).build()) + .add(SingleCorrelationField.create(BAGGAGE_FIELD)).build()) .build(); static final TraceContext context = baggageFactory.decorate(TraceContext.newBuilder() diff --git a/instrumentation/benchmarks/src/main/java/brave/servlet/ServletBenchmarks.java b/instrumentation/benchmarks/src/main/java/brave/servlet/ServletBenchmarks.java index bf6b76bcbc..852ba574a8 100644 --- a/instrumentation/benchmarks/src/main/java/brave/servlet/ServletBenchmarks.java +++ b/instrumentation/benchmarks/src/main/java/brave/servlet/ServletBenchmarks.java @@ -14,9 +14,10 @@ package brave.servlet; import brave.Tracing; +import brave.baggage.BaggagePropagation; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import brave.http.HttpServerBenchmarks; import brave.propagation.B3Propagation; -import brave.baggage.BaggagePropagation; import brave.sampler.Sampler; import io.undertow.servlet.Servlets; import io.undertow.servlet.api.DeploymentInfo; @@ -70,7 +71,7 @@ public static class TracedBaggage extends ForwardingTracingFilter { public TracedBaggage() { super(TracingFilter.create(Tracing.newBuilder() .propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(BAGGAGE_FIELD).build()) + .add(SingleBaggageField.remote(BAGGAGE_FIELD)).build()) .spanReporter(Reporter.NOOP) .build())); } diff --git a/instrumentation/benchmarks/src/main/java/brave/sparkjava/SparkBenchmarks.java b/instrumentation/benchmarks/src/main/java/brave/sparkjava/SparkBenchmarks.java index 3bb8fb4667..ed5a02c08b 100644 --- a/instrumentation/benchmarks/src/main/java/brave/sparkjava/SparkBenchmarks.java +++ b/instrumentation/benchmarks/src/main/java/brave/sparkjava/SparkBenchmarks.java @@ -14,9 +14,10 @@ package brave.sparkjava; import brave.Tracing; +import brave.baggage.BaggagePropagation; +import brave.baggage.BaggagePropagationConfig; import brave.http.HttpServerBenchmarks; import brave.propagation.B3Propagation; -import brave.baggage.BaggagePropagation; import brave.sampler.Sampler; import io.undertow.servlet.api.DeploymentInfo; import io.undertow.servlet.api.FilterInfo; @@ -73,7 +74,7 @@ public static class TracedBaggage implements SparkApplication { SparkTracing sparkTracing = SparkTracing.create( Tracing.newBuilder() .propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) - .addRemoteField(BAGGAGE_FIELD).build()) + .add(BaggagePropagationConfig.SingleBaggageField.remote(BAGGAGE_FIELD)).build()) .spanReporter(Reporter.NOOP).build() ); diff --git a/spring-beans/README.md b/spring-beans/README.md index a933b34497..7432d7eed3 100644 --- a/spring-beans/README.md +++ b/spring-beans/README.md @@ -11,9 +11,9 @@ Bean Factories exist for the following types: * MessagingTracingFactoryBean - for messaging tagging and sampling policy * CurrentTraceContextFactoryBean - to integrate decorators such as correlation. * BaggagePropagationFactoryBean - for propagating baggage fields in process and over headers + * SingleBaggageFieldFactoryBean - configures a single baggage field * CorrelationScopeDecoratorFactoryBean - for scope decorations such as MDC (logging) field correlation -* CorrelationFieldFactoryBean - configures a baggage field for correlation - + * SingleCorrelationFieldFactoryBean - configures a single baggage field for correlation Here are some example beans using the factories in this module: ```xml @@ -61,7 +61,7 @@ with trace headers: - + @@ -79,9 +79,9 @@ Here's an example of adding only the trace ID as the correlation property "X-B3- - + - + diff --git a/spring-beans/src/main/java/brave/spring/beans/BaggagePropagationFactoryBean.java b/spring-beans/src/main/java/brave/spring/beans/BaggagePropagationFactoryBean.java index 87274c1194..2c157cd887 100644 --- a/spring-beans/src/main/java/brave/spring/beans/BaggagePropagationFactoryBean.java +++ b/spring-beans/src/main/java/brave/spring/beans/BaggagePropagationFactoryBean.java @@ -13,8 +13,8 @@ */ package brave.spring.beans; -import brave.baggage.BaggageField; import brave.baggage.BaggagePropagation; +import brave.baggage.BaggagePropagationConfig; import brave.baggage.BaggagePropagationCustomizer; import brave.propagation.B3Propagation; import brave.propagation.Propagation; @@ -24,19 +24,17 @@ /** Spring XML config does not support chained builders. This converts accordingly */ public class BaggagePropagationFactoryBean implements FactoryBean { Propagation.Factory propagationFactory = B3Propagation.FACTORY; - List fields; - List remoteFields; + List configs; List customizers; @Override public Propagation.Factory getObject() { BaggagePropagation.FactoryBuilder builder = BaggagePropagation.newFactoryBuilder(propagationFactory); - if (fields != null) { - for (BaggageField field : fields) builder.addField(field); - } - if (remoteFields != null) { - for (RemoteBaggageField remoteField : remoteFields) - builder.addRemoteField(remoteField.field, remoteField.keyNames); + if (configs != null) { + builder.clear(); + for (BaggagePropagationConfig config : configs) { + builder.add(config); + } } if (customizers != null) { for (BaggagePropagationCustomizer customizer : customizers) customizer.customize(builder); @@ -56,12 +54,8 @@ public void setPropagationFactory(Propagation.Factory propagationFactory) { this.propagationFactory = propagationFactory; } - public void setFields(List fields) { - this.fields = fields; - } - - public void setRemoteFields(List remoteFields) { - this.remoteFields = remoteFields; + public void setConfigs(List configs) { + this.configs = configs; } public void setCustomizers(List customizers) { diff --git a/spring-beans/src/main/java/brave/spring/beans/CorrelationScopeDecoratorFactoryBean.java b/spring-beans/src/main/java/brave/spring/beans/CorrelationScopeDecoratorFactoryBean.java index b68ac51667..f7bf4a08ef 100644 --- a/spring-beans/src/main/java/brave/spring/beans/CorrelationScopeDecoratorFactoryBean.java +++ b/spring-beans/src/main/java/brave/spring/beans/CorrelationScopeDecoratorFactoryBean.java @@ -13,7 +13,7 @@ */ package brave.spring.beans; -import brave.baggage.CorrelationField; +import brave.baggage.CorrelationScopeConfig; import brave.baggage.CorrelationScopeCustomizer; import brave.baggage.CorrelationScopeDecorator; import brave.propagation.CurrentTraceContext.ScopeDecorator; @@ -23,15 +23,15 @@ /** Spring XML config does not support chained builders. This converts accordingly */ public class CorrelationScopeDecoratorFactoryBean implements FactoryBean { CorrelationScopeDecorator.Builder builder; - List fields; + List configs; List customizers; @Override public ScopeDecorator getObject() { if (builder == null) throw new NullPointerException("builder == null"); - if (fields != null) { + if (configs != null) { builder.clear(); - for (CorrelationField field : fields) { - builder.addField(field); + for (CorrelationScopeConfig config : configs) { + builder.add(config); } } if (customizers != null) { @@ -52,8 +52,8 @@ public void setBuilder(CorrelationScopeDecorator.Builder builder) { this.builder = builder; } - public void setFields(List fields) { - this.fields = fields; + public void setConfigs(List configs) { + this.configs = configs; } public void setCustomizers(List customizers) { diff --git a/spring-beans/src/main/java/brave/spring/beans/RemoteBaggageField.java b/spring-beans/src/main/java/brave/spring/beans/SingleBaggageFieldFactoryBean.java similarity index 55% rename from spring-beans/src/main/java/brave/spring/beans/RemoteBaggageField.java rename to spring-beans/src/main/java/brave/spring/beans/SingleBaggageFieldFactoryBean.java index c7f6a980cf..8aac8217ed 100644 --- a/spring-beans/src/main/java/brave/spring/beans/RemoteBaggageField.java +++ b/spring-beans/src/main/java/brave/spring/beans/SingleBaggageFieldFactoryBean.java @@ -14,13 +14,35 @@ package brave.spring.beans; import brave.baggage.BaggageField; +import brave.baggage.BaggagePropagationConfig; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import java.util.Collections; import java.util.List; +import org.springframework.beans.factory.FactoryBean; -public class RemoteBaggageField { +/** Spring XML config does not support chained builders. This converts accordingly */ +public class SingleBaggageFieldFactoryBean implements FactoryBean { BaggageField field; List keyNames = Collections.emptyList(); + @Override public SingleBaggageField getObject() { + SingleBaggageField.Builder builder = SingleBaggageField.newBuilder(field); + if (keyNames != null) { + for (String keyName : keyNames) { + builder.addKeyName(keyName); + } + } + return builder.build(); + } + + @Override public Class getObjectType() { + return BaggagePropagationConfig.class; + } + + @Override public boolean isSingleton() { + return true; + } + public void setField(BaggageField field) { this.field = field; } diff --git a/spring-beans/src/main/java/brave/spring/beans/CorrelationFieldFactoryBean.java b/spring-beans/src/main/java/brave/spring/beans/SingleCorrelationFieldFactoryBean.java similarity index 76% rename from spring-beans/src/main/java/brave/spring/beans/CorrelationFieldFactoryBean.java rename to spring-beans/src/main/java/brave/spring/beans/SingleCorrelationFieldFactoryBean.java index 10e7f91c7a..079df9be3d 100644 --- a/spring-beans/src/main/java/brave/spring/beans/CorrelationFieldFactoryBean.java +++ b/spring-beans/src/main/java/brave/spring/beans/SingleCorrelationFieldFactoryBean.java @@ -14,25 +14,26 @@ package brave.spring.beans; import brave.baggage.BaggageField; -import brave.baggage.CorrelationField; +import brave.baggage.CorrelationScopeConfig; +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import org.springframework.beans.factory.FactoryBean; /** Spring XML config does not support chained builders. This converts accordingly */ -public class CorrelationFieldFactoryBean implements FactoryBean { +public class SingleCorrelationFieldFactoryBean implements FactoryBean { BaggageField baggageField; String name; boolean dirty, flushOnUpdate; - @Override public CorrelationField getObject() { - CorrelationField.Builder builder = CorrelationField.newBuilder(baggageField); + @Override public SingleCorrelationField getObject() { + SingleCorrelationField.Builder builder = SingleCorrelationField.newBuilder(baggageField); if (name != null) builder.name(name); if (dirty) builder.dirty(); if (flushOnUpdate) builder.flushOnUpdate(); return builder.build(); } - @Override public Class getObjectType() { - return CorrelationField.class; + @Override public Class getObjectType() { + return CorrelationScopeConfig.class; } @Override public boolean isSingleton() { diff --git a/spring-beans/src/test/java/brave/spring/beans/BaggagePropagationFactoryBeanTest.java b/spring-beans/src/test/java/brave/spring/beans/BaggagePropagationFactoryBeanTest.java index 74f7da7594..d8a2018762 100755 --- a/spring-beans/src/test/java/brave/spring/beans/BaggagePropagationFactoryBeanTest.java +++ b/spring-beans/src/test/java/brave/spring/beans/BaggagePropagationFactoryBeanTest.java @@ -58,38 +58,15 @@ public class BaggagePropagationFactoryBeanTest { .isEqualTo(B3SinglePropagation.FACTORY); } - @Test public void fields() { + @Test public void configs() { context = new XmlBeans("" + "\n" + " userId\n" + "\n" + "\n" - + " \n" + + " \n" + " \n" - + " \n" - + " \n" - + " \n" - + "\n" - ); - - assertThathandlersWithKeyNames() - .extracting("handler.field") - .usingFieldByFieldElementComparator() - .containsExactly(BaggageField.create("userId")); - assertThathandlersWithKeyNames() - .flatExtracting("keyNames") - .isEmpty(); - } - - @Test public void remoteFields() { - context = new XmlBeans("" - + "\n" - + " userId\n" - + "\n" - + "\n" - + " \n" - + " \n" - + " \n" + + " \n" + " \n" + " \n" + " \n" @@ -112,31 +89,6 @@ public class BaggagePropagationFactoryBeanTest { .containsExactly("baggage_user_id", "baggage-user-id"); } - @Test public void remoteFields_defaultKeyName() { - context = new XmlBeans("" - + "\n" - + " userId\n" - + "\n" - + "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + "" - ); - - assertThathandlersWithKeyNames() - .extracting("handler.field") - .usingFieldByFieldElementComparator() - .containsExactly(BaggageField.create("userId")); - assertThathandlersWithKeyNames() - .flatExtracting("keyNames") - .containsExactly("userid"); - } - @Test public void propagationFactory() { context = new XmlBeans("" + "\n" @@ -146,9 +98,11 @@ public class BaggagePropagationFactoryBeanTest { + " \n" + " \n" + " \n" - + " \n" + + " \n" + " \n" - + " \n" + + " \n" + + " \n" + + " \n" + " \n" + " \n" + "\n" diff --git a/spring-beans/src/test/java/brave/spring/beans/CorrelationScopeDecoratorFactoryBeanTest.java b/spring-beans/src/test/java/brave/spring/beans/CorrelationScopeDecoratorFactoryBeanTest.java index b60625d243..6b9ea26b74 100755 --- a/spring-beans/src/test/java/brave/spring/beans/CorrelationScopeDecoratorFactoryBeanTest.java +++ b/spring-beans/src/test/java/brave/spring/beans/CorrelationScopeDecoratorFactoryBeanTest.java @@ -13,8 +13,7 @@ */ package brave.spring.beans; -import brave.baggage.BaggageFields; -import brave.baggage.CorrelationField; +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import brave.baggage.CorrelationScopeCustomizer; import brave.baggage.CorrelationScopeDecorator; import brave.propagation.CurrentTraceContext.ScopeDecorator; @@ -23,6 +22,8 @@ import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; +import static brave.baggage.BaggageFields.SPAN_ID; +import static brave.baggage.BaggageFields.TRACE_ID; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -68,21 +69,21 @@ public class CorrelationScopeDecoratorFactoryBeanTest { assertThat(context.getBean("correlationDecorator", CorrelationScopeDecorator.class)) .extracting("fields").asInstanceOf(InstanceOfAssertFactories.ARRAY) .containsExactly( - CorrelationField.create(BaggageFields.TRACE_ID), - CorrelationField.create(BaggageFields.SPAN_ID) + SingleCorrelationField.create(TRACE_ID), + SingleCorrelationField.create(SPAN_ID) ); } - @Test public void fields() { + @Test public void configs() { context = new XmlBeans("" + "\n" + "\n" + " \n" + " \n" + " \n" - + " \n" + + " \n" + " \n" - + " \n" + + " \n" + " \n" + " \n" + " \n" @@ -94,7 +95,8 @@ public class CorrelationScopeDecoratorFactoryBeanTest { ScopeDecorator decorator = context.getBean("correlationDecorator", ScopeDecorator.class); assertThat(decorator).extracting("field") .usingRecursiveComparison() - .isEqualTo(CorrelationField.newBuilder(BaggageFields.TRACE_ID).name("X-B3-TraceId").build()); + .isEqualTo( + SingleCorrelationField.newBuilder(TRACE_ID).name("X-B3-TraceId").build()); } public static final CorrelationScopeCustomizer diff --git a/spring-beans/src/test/java/brave/spring/beans/SingleBaggageFieldFactoryBeanTest.java b/spring-beans/src/test/java/brave/spring/beans/SingleBaggageFieldFactoryBeanTest.java new file mode 100644 index 0000000000..ddbff0cbac --- /dev/null +++ b/spring-beans/src/test/java/brave/spring/beans/SingleBaggageFieldFactoryBeanTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2013-2020 The OpenZipkin Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package brave.spring.beans; + +import brave.baggage.BaggageField; +import brave.baggage.BaggagePropagationConfig; +import brave.baggage.BaggagePropagationConfig.SingleBaggageField; +import org.junit.After; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SingleBaggageFieldFactoryBeanTest { + XmlBeans context; + + @After public void close() { + if (context != null) context.close(); + } + + @Test public void leastProperties() { + context = new XmlBeans("" + + "\n" + + " userId\n" + + "\n" + + "\n" + + " \n" + + "\n" + ); + + assertThat(context.getBean("userIdBaggageConfig", BaggagePropagationConfig.class)) + .usingRecursiveComparison() + .isEqualTo(SingleBaggageField.local(BaggageField.create("userId"))); + } + + @Test public void allProperties() { + context = new XmlBeans("" + + "\n" + + " userId\n" + + "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " user-id\n" + + " \n" + + " \n" + + "\n" + ); + + assertThat(context.getBean("userIdBaggageConfig", BaggagePropagationConfig.class)) + .usingRecursiveComparison() + .isEqualTo(SingleBaggageField.newBuilder(BaggageField.create("userId")) + .addKeyName("user-id") + .build()); + } +} diff --git a/spring-beans/src/test/java/brave/spring/beans/CorrelationFieldFactoryBeanTest.java b/spring-beans/src/test/java/brave/spring/beans/SingleCorrelationFieldFactoryBeanTest.java similarity index 70% rename from spring-beans/src/test/java/brave/spring/beans/CorrelationFieldFactoryBeanTest.java rename to spring-beans/src/test/java/brave/spring/beans/SingleCorrelationFieldFactoryBeanTest.java index b17476edaa..cd1aebf981 100644 --- a/spring-beans/src/test/java/brave/spring/beans/CorrelationFieldFactoryBeanTest.java +++ b/spring-beans/src/test/java/brave/spring/beans/SingleCorrelationFieldFactoryBeanTest.java @@ -14,13 +14,14 @@ package brave.spring.beans; import brave.baggage.BaggageFields; -import brave.baggage.CorrelationField; +import brave.baggage.CorrelationScopeConfig; +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import org.junit.After; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -public class CorrelationFieldFactoryBeanTest { +public class SingleCorrelationFieldFactoryBeanTest { XmlBeans context; @After public void close() { @@ -30,20 +31,20 @@ public class CorrelationFieldFactoryBeanTest { @Test public void leastProperties() { context = new XmlBeans("" + "\n" - + "\n" + + "\n" + " \n" + "\n" ); - assertThat(context.getBean("traceIdCorrelation", CorrelationField.class)) + assertThat(context.getBean("traceIdCorrelationConfig", CorrelationScopeConfig.class)) .usingRecursiveComparison() - .isEqualTo(CorrelationField.create(BaggageFields.TRACE_ID)); + .isEqualTo(SingleCorrelationField.create(BaggageFields.TRACE_ID)); } @Test public void allProperties() { context = new XmlBeans("" + "\n" - + "\n" + + "\n" + " \n" + " \n" + " \n" @@ -51,9 +52,9 @@ public class CorrelationFieldFactoryBeanTest { + "\n" ); - assertThat(context.getBean("traceIdCorrelation", CorrelationField.class)) + assertThat(context.getBean("traceIdCorrelationConfig", CorrelationScopeConfig.class)) .usingRecursiveComparison() - .isEqualTo(CorrelationField.newBuilder(BaggageFields.TRACE_ID) + .isEqualTo(SingleCorrelationField.newBuilder(BaggageFields.TRACE_ID) .name("X-B3-TraceId") .dirty() .flushOnUpdate()