From d116b36c46cd9525379f7700ced03bf29c4e88a9 Mon Sep 17 00:00:00 2001 From: Mac Date: Fri, 13 Dec 2024 22:08:18 +0800 Subject: [PATCH] remove strong dep micrometer in tracing --- .../tracing/DubboObservationRegistry.java | 54 ++++++++----------- .../tracing/metrics/ObservationMeter.java | 18 +++++++ 2 files changed, 41 insertions(+), 31 deletions(-) create mode 100644 dubbo-metrics/dubbo-tracing/src/main/java/org/apache/dubbo/tracing/metrics/ObservationMeter.java diff --git a/dubbo-metrics/dubbo-tracing/src/main/java/org/apache/dubbo/tracing/DubboObservationRegistry.java b/dubbo-metrics/dubbo-tracing/src/main/java/org/apache/dubbo/tracing/DubboObservationRegistry.java index 5e9b7b35b38..8bc46811697 100644 --- a/dubbo-metrics/dubbo-tracing/src/main/java/org/apache/dubbo/tracing/DubboObservationRegistry.java +++ b/dubbo-metrics/dubbo-tracing/src/main/java/org/apache/dubbo/tracing/DubboObservationRegistry.java @@ -16,15 +16,25 @@ */ package org.apache.dubbo.tracing; +import io.micrometer.observation.ObservationHandler; + +import io.micrometer.observation.ObservationRegistry; +import io.micrometer.tracing.Tracer; +import io.micrometer.tracing.handler.DefaultTracingObservationHandler; +import io.micrometer.tracing.handler.PropagatingReceiverTracingObservationHandler; +import io.micrometer.tracing.handler.PropagatingSenderTracingObservationHandler; + +import io.micrometer.tracing.propagation.Propagator; + import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.JsonUtils; import org.apache.dubbo.config.TracingConfig; -import org.apache.dubbo.metrics.MetricsGlobalRegistry; import org.apache.dubbo.metrics.utils.MetricsSupportUtil; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.tracing.handler.DubboClientTracingObservationHandler; import org.apache.dubbo.tracing.handler.DubboServerTracingObservationHandler; +import org.apache.dubbo.tracing.metrics.ObservationMeter; import org.apache.dubbo.tracing.tracer.PropagatorProvider; import org.apache.dubbo.tracing.tracer.PropagatorProviderFactory; import org.apache.dubbo.tracing.tracer.TracerProvider; @@ -34,8 +44,7 @@ public class DubboObservationRegistry { - private static final ErrorTypeAwareLogger logger = - LoggerFactory.getErrorTypeAwareLogger(DubboObservationRegistry.class); + private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(DubboObservationRegistry.class); private final ApplicationModel applicationModel; @@ -48,8 +57,8 @@ public DubboObservationRegistry(ApplicationModel applicationModel, TracingConfig public void initObservationRegistry() { // If get ObservationRegistry.class from external(eg Spring.), use external. - io.micrometer.observation.ObservationRegistry externalObservationRegistry = - applicationModel.getBeanFactory().getBean(io.micrometer.observation.ObservationRegistry.class); + ObservationRegistry externalObservationRegistry = applicationModel.getBeanFactory() + .getBean(ObservationRegistry.class); if (externalObservationRegistry != null) { if (logger.isDebugEnabled()) { logger.debug("ObservationRegistry.class from external is existed."); @@ -63,48 +72,31 @@ public void initObservationRegistry() { TracerProvider tracerProvider = TracerProviderFactory.getProvider(applicationModel, tracingConfig); if (tracerProvider == null) { - logger.warn( - COMMON_NOT_FOUND_TRACER_DEPENDENCY, - "", - "", - "Can not found OpenTelemetry/Brave tracer dependencies, skip init ObservationRegistry."); + logger.warn(COMMON_NOT_FOUND_TRACER_DEPENDENCY, "", "", "Can not found OpenTelemetry/Brave tracer dependencies, skip init ObservationRegistry."); return; } // The real tracer will come from tracer implementation (OTel / Brave) - io.micrometer.tracing.Tracer tracer = tracerProvider.getTracer(); + Tracer tracer = tracerProvider.getTracer(); // The real propagator will come from tracer implementation (OTel / Brave) PropagatorProvider propagatorProvider = PropagatorProviderFactory.getPropagatorProvider(); - io.micrometer.tracing.propagation.Propagator propagator = propagatorProvider != null - ? propagatorProvider.getPropagator() - : io.micrometer.tracing.propagation.Propagator.NOOP; + Propagator propagator = propagatorProvider != null ? propagatorProvider.getPropagator() : Propagator.NOOP; - io.micrometer.observation.ObservationRegistry registry = io.micrometer.observation.ObservationRegistry.create(); + ObservationRegistry registry = ObservationRegistry.create(); registry.observationConfig() // set up a first matching handler that creates spans - it comes from Micrometer Tracing. // set up spans for sending and receiving data over the wire and a default one. - .observationHandler( - new io.micrometer.observation.ObservationHandler.FirstMatchingCompositeObservationHandler( - new io.micrometer.tracing.handler.PropagatingSenderTracingObservationHandler<>( - tracer, propagator), - new io.micrometer.tracing.handler.PropagatingReceiverTracingObservationHandler<>( - tracer, propagator), - new io.micrometer.tracing.handler.DefaultTracingObservationHandler(tracer))) - .observationHandler( - new io.micrometer.observation.ObservationHandler.FirstMatchingCompositeObservationHandler( - new DubboClientTracingObservationHandler<>(tracer), - new DubboServerTracingObservationHandler<>(tracer))); + .observationHandler(new ObservationHandler.FirstMatchingCompositeObservationHandler(new PropagatingSenderTracingObservationHandler<>(tracer, propagator), new PropagatingReceiverTracingObservationHandler<>(tracer, propagator), new DefaultTracingObservationHandler(tracer))) + .observationHandler(new ObservationHandler.FirstMatchingCompositeObservationHandler(new DubboClientTracingObservationHandler<>(tracer), new DubboServerTracingObservationHandler<>(tracer))); if (MetricsSupportUtil.isSupportMetrics()) { - io.micrometer.core.instrument.MeterRegistry meterRegistry = - MetricsGlobalRegistry.getCompositeRegistry(applicationModel); - registry.observationConfig() - .observationHandler(new io.micrometer.core.instrument.observation.DefaultMeterObservationHandler( - meterRegistry)); + ObservationMeter.addMeterRegistry(registry, applicationModel); } applicationModel.getBeanFactory().registerBean(registry); applicationModel.getBeanFactory().registerBean(tracer); applicationModel.getBeanFactory().registerBean(propagator); } + + } diff --git a/dubbo-metrics/dubbo-tracing/src/main/java/org/apache/dubbo/tracing/metrics/ObservationMeter.java b/dubbo-metrics/dubbo-tracing/src/main/java/org/apache/dubbo/tracing/metrics/ObservationMeter.java new file mode 100644 index 00000000000..5992c3b9385 --- /dev/null +++ b/dubbo-metrics/dubbo-tracing/src/main/java/org/apache/dubbo/tracing/metrics/ObservationMeter.java @@ -0,0 +1,18 @@ +package org.apache.dubbo.tracing.metrics; + +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.observation.ObservationRegistry; + +import org.apache.dubbo.metrics.MetricsGlobalRegistry; +import org.apache.dubbo.rpc.model.ApplicationModel; + +public class ObservationMeter { + + public static void addMeterRegistry(ObservationRegistry registry, ApplicationModel applicationModel) { + MeterRegistry meterRegistry = MetricsGlobalRegistry.getCompositeRegistry(applicationModel); + registry.observationConfig() + .observationHandler(new io.micrometer.core.instrument.observation.DefaultMeterObservationHandler( + meterRegistry)); + } + +}