From 008a9e3c37d05ef4d90e109b22c1f2ecd87c29e0 Mon Sep 17 00:00:00 2001 From: Adis Pezo Date: Tue, 19 Sep 2023 01:56:47 +0200 Subject: [PATCH] implemented local and tag baggageFields --- .../tracing/BaggageTagSpanHandler.java | 41 +++++++++++++++++++ .../tracing/BraveAutoConfiguration.java | 18 +++++--- .../tracing/TracingProperties.java | 27 ++++++++++++ .../BaggagePropagationIntegrationTests.java | 10 +++++ .../tracing/BraveAutoConfigurationTests.java | 17 ++++++++ 5 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggageTagSpanHandler.java diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggageTagSpanHandler.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggageTagSpanHandler.java new file mode 100644 index 0000000000..54c36be07a --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggageTagSpanHandler.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012-2023 the original author or 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 + * + * https://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 org.springframework.boot.actuate.autoconfigure.tracing; + +import brave.Tags; +import brave.baggage.BaggageField; +import brave.handler.MutableSpan; +import brave.handler.SpanHandler; +import brave.propagation.TraceContext; + +public class BaggageTagSpanHandler extends SpanHandler { + + final BaggageField[] fieldsToTag; + + BaggageTagSpanHandler(BaggageField[] fieldsToTag) { + this.fieldsToTag = fieldsToTag; + } + + @Override + public boolean end(TraceContext context, MutableSpan span, Cause cause) { + for (BaggageField field : this.fieldsToTag) { + Tags.BAGGAGE_FIELD.tag(field, context, span); + } + return true; + } + +} diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfiguration.java index a278235008..a67b2804ee 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfiguration.java @@ -16,7 +16,6 @@ package org.springframework.boot.actuate.autoconfigure.tracing; -import java.util.ArrayList; import java.util.List; import brave.CurrentSpanCustomizer; @@ -246,18 +245,27 @@ public class BraveAutoConfiguration { } @Bean - @Order(0) + @Order(1) BaggagePropagationCustomizer localFieldsBaggagePropagationCustomizer() { return (builder) -> { - final List localFields = new ArrayList<>( - this.tracingProperties.getBaggage().getCorrelation().getFields()); - localFields.removeAll(this.tracingProperties.getBaggage().getRemoteFields()); + final List localFields = this.tracingProperties.getBaggage().getLocalFields(); for (final String localFieldName : localFields) { builder.add(BaggagePropagationConfig.SingleBaggageField.local(BaggageField.create(localFieldName))); } }; } + @Bean + @Order(2) + SpanHandler baggageTagSpanHandler() { + final List tagFields = this.tracingProperties.getBaggage().getTagFields(); + + if (tagFields.isEmpty()) { + return SpanHandler.NOOP; // Brave ignores these + } + return new BaggageTagSpanHandler(tagFields.stream().map(BaggageField::create).toArray(BaggageField[]::new)); + } + @Bean @ConditionalOnMissingBean Factory propagationFactory(BaggagePropagation.FactoryBuilder factoryBuilder) { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/TracingProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/TracingProperties.java index 6f39e3c126..6483aa0892 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/TracingProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/TracingProperties.java @@ -116,6 +116,17 @@ public class TracingProperties { */ private List remoteFields = new ArrayList<>(); + /** + * List of fields that should be accessible within the JVM process but not + * propagated over the wire. + */ + private List localFields = new ArrayList<>(); + + /** + * List of fields that should automatically become tags. + */ + private List tagFields = new ArrayList<>(); + public boolean isEnabled() { return this.enabled; } @@ -136,10 +147,26 @@ public class TracingProperties { return this.remoteFields; } + public List getLocalFields() { + return this.localFields; + } + + public List getTagFields() { + return this.tagFields; + } + public void setRemoteFields(List remoteFields) { this.remoteFields = remoteFields; } + public void setLocalFields(List localFields) { + this.localFields = localFields; + } + + public void setTagFields(List tagFields) { + this.tagFields = tagFields; + } + public static class Correlation { /** diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggagePropagationIntegrationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggagePropagationIntegrationTests.java index 77c997cd19..de32f48b4a 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggagePropagationIntegrationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggagePropagationIntegrationTests.java @@ -227,6 +227,16 @@ class BaggagePropagationIntegrationTests { "management.tracing.baggage.remote-fields=x-vcap-request-id,country-code,bp", "management.tracing.baggage.correlation.fields=country-code,bp"); } + }, + + BRAVE_LOCAL_FIELDS { + @Override + public ApplicationContextRunner get() { + return new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(BraveAutoConfiguration.class)) + .withPropertyValues("management.tracing.baggage.local-fields=country-code,bp", + "management.tracing.baggage.correlation.fields=country-code,bp"); + } } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java index 372166ed25..c462167df2 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java @@ -26,6 +26,7 @@ import brave.Span; import brave.SpanCustomizer; import brave.Tracer; import brave.Tracing; +import brave.baggage.BaggageField; import brave.baggage.BaggagePropagation; import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import brave.handler.SpanHandler; @@ -341,6 +342,22 @@ class BraveAutoConfigurationTests { }); } + @Test + void shouldCreateTagHandler() { + this.contextRunner.withPropertyValues("management.tracing.baggage.tag-fields=country-code,bp") + .run((context) -> assertThat(context.getBean(BaggageTagSpanHandler.class)).extracting("fieldsToTag") + .asInstanceOf(InstanceOfAssertFactories.array(BaggageField[].class)) + .extracting(BaggageField::name) + .containsOnly("country-code", "bp")); + } + + @Test + void noopOnNoTagFields() { + this.contextRunner.withPropertyValues("management.tracing.baggage.tag-fields=") + .run((context) -> assertThat(context.getBean("baggageTagSpanHandler", SpanHandler.class)) + .isSameAs(SpanHandler.NOOP)); + } + private void injectToMap(Map map, String key, String value) { map.put(key, value); }