|
17 | 17 | package org.apache.dubbo.spring.boot.observability.autoconfigure.otel; |
18 | 18 |
|
19 | 19 | import org.apache.dubbo.common.Version; |
| 20 | +import org.apache.dubbo.common.utils.ClassUtils; |
20 | 21 | import org.apache.dubbo.config.ApplicationConfig; |
21 | 22 | import org.apache.dubbo.rpc.model.ModuleModel; |
22 | 23 | import org.apache.dubbo.spring.boot.autoconfigure.DubboConfigurationProperties; |
|
28 | 29 | import java.util.List; |
29 | 30 | import java.util.stream.Collectors; |
30 | 31 |
|
| 32 | +import io.opentelemetry.api.common.AttributeKey; |
31 | 33 | import org.springframework.beans.factory.ObjectProvider; |
32 | 34 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
33 | 35 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
@@ -93,13 +95,29 @@ io.opentelemetry.sdk.trace.SdkTracerProvider otelSdkTracerProvider( |
93 | 95 | .getApplication() |
94 | 96 | .map(ApplicationConfig::getName) |
95 | 97 | .orElse(DEFAULT_APPLICATION_NAME); |
| 98 | + // Due to https://github.com/micrometer-metrics/tracing/issues/343 |
| 99 | + String RESOURCE_ATTRIBUTES_CLASS_NAME = "io.opentelemetry.semconv.ResourceAttributes"; |
| 100 | + boolean isLowVersion = !ClassUtils.isPresent( |
| 101 | + RESOURCE_ATTRIBUTES_CLASS_NAME, Thread.currentThread().getContextClassLoader()); |
| 102 | + AttributeKey<String> attributeKey = null; |
| 103 | + String SERVICE_NAME = "SERVICE_NAME"; |
| 104 | + |
| 105 | + if (isLowVersion) { |
| 106 | + RESOURCE_ATTRIBUTES_CLASS_NAME = "io.opentelemetry.semconv.resource.attributes.ResourceAttributes"; |
| 107 | + } |
| 108 | + try { |
| 109 | + attributeKey = (AttributeKey<String>) ClassUtils.resolveClass( |
| 110 | + RESOURCE_ATTRIBUTES_CLASS_NAME, |
| 111 | + Thread.currentThread().getContextClassLoader()) |
| 112 | + .getDeclaredField(SERVICE_NAME) |
| 113 | + .get(null); |
| 114 | + } catch (Throwable ignored) { |
| 115 | + } |
96 | 116 | io.opentelemetry.sdk.trace.SdkTracerProviderBuilder builder = |
97 | 117 | io.opentelemetry.sdk.trace.SdkTracerProvider.builder() |
98 | 118 | .setSampler(sampler) |
99 | 119 | .setResource(io.opentelemetry.sdk.resources.Resource.create( |
100 | | - io.opentelemetry.api.common.Attributes.of( |
101 | | - io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME, |
102 | | - applicationName))); |
| 120 | + io.opentelemetry.api.common.Attributes.of(attributeKey, applicationName))); |
103 | 121 | spanProcessors.orderedStream().forEach(builder::addSpanProcessor); |
104 | 122 | return builder.build(); |
105 | 123 | } |
|
0 commit comments