Simplify OTEL integration to reduce deps (#1160)

This commit is contained in:
artur-ciocanu 2024-11-05 19:49:04 +02:00 committed by GitHub
parent be05a47fd8
commit 3dc96d7d73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 43 deletions

View File

@ -20,8 +20,6 @@ import io.dapr.spring.messaging.observation.DaprMessagingObservationDocumentatio
import io.dapr.spring.messaging.observation.DaprMessagingSenderContext; import io.dapr.spring.messaging.observation.DaprMessagingSenderContext;
import io.micrometer.observation.Observation; import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.ObservationRegistry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.propagation.TextMapSetter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.BeanNameAware;
@ -33,7 +31,6 @@ import reactor.util.context.Context;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
@ -59,9 +56,6 @@ public class DaprMessagingTemplate<T> implements DaprMessagingOperations<T>, App
@Nullable @Nullable
private String beanName; private String beanName;
@Nullable
private OpenTelemetry openTelemetry;
@Nullable @Nullable
private ObservationRegistry observationRegistry; private ObservationRegistry observationRegistry;
@ -109,8 +103,6 @@ public class DaprMessagingTemplate<T> implements DaprMessagingOperations<T>, App
observationRegistry = applicationContext.getBeanProvider(ObservationRegistry.class) observationRegistry = applicationContext.getBeanProvider(ObservationRegistry.class)
.getIfUnique(() -> observationRegistry); .getIfUnique(() -> observationRegistry);
this.openTelemetry = this.applicationContext.getBeanProvider(OpenTelemetry.class)
.getIfUnique(() -> this.openTelemetry);
observationConvention = applicationContext.getBeanProvider(DaprMessagingObservationConvention.class) observationConvention = applicationContext.getBeanProvider(DaprMessagingObservationConvention.class)
.getIfUnique(() -> observationConvention); .getIfUnique(() -> observationConvention);
} }
@ -140,10 +132,7 @@ public class DaprMessagingTemplate<T> implements DaprMessagingOperations<T>, App
} }
private boolean canUseObservation() { private boolean canUseObservation() {
return observationEnabled return observationEnabled && observationRegistry != null && beanName != null;
&& observationRegistry != null
&& openTelemetry != null
&& beanName != null;
} }
private Mono<Void> publishEvent(String pubsubName, String topic, T message) { private Mono<Void> publishEvent(String pubsubName, String topic, T message) {
@ -154,9 +143,10 @@ public class DaprMessagingTemplate<T> implements DaprMessagingOperations<T>, App
DaprMessagingSenderContext senderContext = DaprMessagingSenderContext.newContext(topic, this.beanName); DaprMessagingSenderContext senderContext = DaprMessagingSenderContext.newContext(topic, this.beanName);
Observation observation = createObservation(senderContext); Observation observation = createObservation(senderContext);
return observation.observe(() -> observation.start();
publishEvent(pubsubName, topic, message)
.contextWrite(getReactorContext()) return publishEvent(pubsubName, topic, message)
.contextWrite(getReactorContext(senderContext))
.doOnError(err -> { .doOnError(err -> {
LOGGER.error("Failed to send msg to '{}' topic", topic, err); LOGGER.error("Failed to send msg to '{}' topic", topic, err);
@ -167,18 +157,11 @@ public class DaprMessagingTemplate<T> implements DaprMessagingOperations<T>, App
LOGGER.trace("Sent msg to '{}' topic", topic); LOGGER.trace("Sent msg to '{}' topic", topic);
observation.stop(); observation.stop();
}) });
);
} }
private Context getReactorContext() { private Context getReactorContext(DaprMessagingSenderContext senderContext) {
Map<String, String> map = new HashMap<>(); return Context.of(senderContext.properties());
TextMapSetter<Map<String, String>> setter = (carrier, key, value) -> map.put(key, value);
io.opentelemetry.context.Context otelContext = io.opentelemetry.context.Context.current();
openTelemetry.getPropagators().getTextMapPropagator().inject(otelContext, map, setter);
return Context.of(map);
} }
private Observation createObservation(DaprMessagingSenderContext senderContext) { private Observation createObservation(DaprMessagingSenderContext senderContext) {

View File

@ -75,16 +75,6 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<!-- OTEL dependencies -->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-context</artifactId>
</dependency>
<!-- Test dependencies --> <!-- Test dependencies -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>