GH-935 - Migrate to Micrometer Tracings's local service name.

This commit is contained in:
Oliver Drotbohm
2025-02-20 21:05:07 +01:00
parent 0fd05077a5
commit 421c2f8eff
3 changed files with 54 additions and 30 deletions

View File

@@ -83,12 +83,6 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
@@ -120,6 +114,12 @@
<artifactId>spring-rabbit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2025 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.modulith.observability;
import io.micrometer.tracing.exporter.FinishedSpan;
import io.micrometer.tracing.exporter.SpanFilter;
/**
* {@link SpanFilter} that sets a local service name according to the current module's name.
*
* @author Marcin Grzejszczak
* @since 1.4
*/
public class LocalServiceRenamingSpanFilter implements SpanFilter {
/*
* (non-Javadoc)
* @see io.micrometer.tracing.exporter.SpanFilter#map(io.micrometer.tracing.exporter.FinishedSpan)
*/
@Override
public FinishedSpan map(FinishedSpan span) {
String moduleKey = span.getTags().get(ModulithObservations.LowKeys.MODULE_KEY.asString());
if (moduleKey != null) {
span.setLocalServiceName(moduleKey);
}
return span;
}
}

View File

@@ -15,16 +15,12 @@
*/
package org.springframework.modulith.observability.autoconfigure;
import brave.handler.MutableSpan;
import brave.handler.SpanHandler;
import brave.propagation.TraceContext;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.observation.ObservationFilter;
import io.micrometer.observation.ObservationRegistry;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnThreading;
import org.springframework.boot.autoconfigure.thread.Threading;
@@ -34,6 +30,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.task.support.ContextPropagatingTaskDecorator;
import org.springframework.modulith.observability.LocalServiceRenamingSpanFilter;
import org.springframework.modulith.observability.ModuleEventListener;
import org.springframework.modulith.observability.ModuleObservabilityBeanPostProcessor;
import org.springframework.modulith.observability.ModulePassingObservationFilter;
@@ -76,26 +73,9 @@ class ModuleObservabilityAutoConfiguration {
return new ModulePassingObservationFilter();
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(SpanHandler.class)
static class BraveConfiguration {
/**
* Corresponds to {@link ModulithObservations.LowKeys#MODULE_KEY}
*/
private static final String MODULE_KEY_TAG_NAME = "module.key";
@Bean
SpanHandler localServiceNameRenamingSpanHandler() {
return new SpanHandler() {
@Override public boolean end(TraceContext context, MutableSpan span, Cause cause) {
String tag = span.tag(MODULE_KEY_TAG_NAME);
if (tag != null) {
span.localServiceName(tag);
}
return super.end(context, span, cause);
}
};
}
@Bean
LocalServiceRenamingSpanFilter localServiceRenamingSpanFilter() {
return new LocalServiceRenamingSpanFilter();
}
}