This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.3 by this push:
     new 40201078b0 Fix compile
40201078b0 is described below

commit 40201078b0d457f4a92d5560b6b3bfa2a6eaa5f0
Author: Albumen Kevin <jhq0...@gmail.com>
AuthorDate: Thu Nov 30 14:00:44 2023 +0800

    Fix compile
---
 .../tracing/tracer/otel/OpenTelemetryProvider.java | 24 +++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git 
a/dubbo-metrics/dubbo-tracing/src/main/java/org/apache/dubbo/tracing/tracer/otel/OpenTelemetryProvider.java
 
b/dubbo-metrics/dubbo-tracing/src/main/java/org/apache/dubbo/tracing/tracer/otel/OpenTelemetryProvider.java
index 2eefadf619..9e79f85809 100644
--- 
a/dubbo-metrics/dubbo-tracing/src/main/java/org/apache/dubbo/tracing/tracer/otel/OpenTelemetryProvider.java
+++ 
b/dubbo-metrics/dubbo-tracing/src/main/java/org/apache/dubbo/tracing/tracer/otel/OpenTelemetryProvider.java
@@ -20,6 +20,7 @@ import org.apache.dubbo.common.Version;
 import org.apache.dubbo.common.lang.Nullable;
 import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
 import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.utils.ClassUtils;
 import org.apache.dubbo.common.utils.StringUtils;
 import org.apache.dubbo.config.ApplicationConfig;
 import org.apache.dubbo.config.TracingConfig;
@@ -46,6 +47,7 @@ import io.micrometer.tracing.otel.bridge.OtelTracer;
 import io.micrometer.tracing.otel.bridge.Slf4JBaggageEventListener;
 import io.micrometer.tracing.otel.bridge.Slf4JEventListener;
 import io.micrometer.tracing.otel.propagation.BaggageTextMapPropagator;
+import io.opentelemetry.api.common.AttributeKey;
 
 import static 
org.apache.dubbo.tracing.utils.ObservationConstants.DEFAULT_APPLICATION_NAME;
 
@@ -81,14 +83,30 @@ public class OpenTelemetryProvider implements 
TracerProvider {
         // [Micrometer Tracing component] A Micrometer Tracing wrapper for OTel
         this.otelCurrentTraceContext = createCurrentTraceContext();
 
+        // Due to https://github.com/micrometer-metrics/tracing/issues/343
+        String RESOURCE_ATTRIBUTES_CLASS_NAME = 
"io.opentelemetry.semconv.ResourceAttributes";
+        boolean isLowVersion = !ClassUtils.isPresent(
+                RESOURCE_ATTRIBUTES_CLASS_NAME, 
Thread.currentThread().getContextClassLoader());
+        AttributeKey<String> serviceNameAttributeKey = 
AttributeKey.stringKey("service.name");
+        String SERVICE_NAME = "SERVICE_NAME";
+
+        if (isLowVersion) {
+            RESOURCE_ATTRIBUTES_CLASS_NAME = 
"io.opentelemetry.semconv.resource.attributes.ResourceAttributes";
+        }
+        try {
+            serviceNameAttributeKey = (AttributeKey<String>) 
ClassUtils.resolveClass(
+                            RESOURCE_ATTRIBUTES_CLASS_NAME,
+                            Thread.currentThread().getContextClassLoader())
+                    .getDeclaredField(SERVICE_NAME)
+                    .get(null);
+        } catch (Throwable ignored) {
+        }
         // [OTel component] SdkTracerProvider is an SDK implementation for 
TracerProvider
         io.opentelemetry.sdk.trace.SdkTracerProvider sdkTracerProvider =
                 io.opentelemetry.sdk.trace.SdkTracerProvider.builder()
                         .setSampler(getSampler())
                         
.setResource(io.opentelemetry.sdk.resources.Resource.create(
-                                io.opentelemetry.api.common.Attributes.of(
-                                        
io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME,
-                                        applicationName)))
+                                
io.opentelemetry.api.common.Attributes.of(serviceNameAttributeKey, 
applicationName)))
                         
.addSpanProcessor(io.opentelemetry.sdk.trace.export.BatchSpanProcessor.builder(
                                         new 
CompositeSpanExporter(spanExporters, null, null, null))
                                 .build())

Reply via email to