Skip to content

Commit

Permalink
Simplify OTEL integration to reduce deps (#1160)
Browse files Browse the repository at this point in the history
  • Loading branch information
artur-ciocanu authored Nov 5, 2024
1 parent be05a47 commit 3dc96d7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import io.dapr.spring.messaging.observation.DaprMessagingSenderContext;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.propagation.TextMapSetter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanNameAware;
Expand All @@ -33,7 +31,6 @@

import javax.annotation.Nullable;

import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -59,9 +56,6 @@ public class DaprMessagingTemplate<T> implements DaprMessagingOperations<T>, App
@Nullable
private String beanName;

@Nullable
private OpenTelemetry openTelemetry;

@Nullable
private ObservationRegistry observationRegistry;

Expand Down Expand Up @@ -109,8 +103,6 @@ public void afterSingletonsInstantiated() {

observationRegistry = applicationContext.getBeanProvider(ObservationRegistry.class)
.getIfUnique(() -> observationRegistry);
this.openTelemetry = this.applicationContext.getBeanProvider(OpenTelemetry.class)
.getIfUnique(() -> this.openTelemetry);
observationConvention = applicationContext.getBeanProvider(DaprMessagingObservationConvention.class)
.getIfUnique(() -> observationConvention);
}
Expand Down Expand Up @@ -140,10 +132,7 @@ private Mono<Void> doSendAsync(String topic, T message) {
}

private boolean canUseObservation() {
return observationEnabled
&& observationRegistry != null
&& openTelemetry != null
&& beanName != null;
return observationEnabled && observationRegistry != null && beanName != null;
}

private Mono<Void> publishEvent(String pubsubName, String topic, T message) {
Expand All @@ -154,31 +143,25 @@ private Mono<Void> publishEventWithObservation(String pubsubName, String topic,
DaprMessagingSenderContext senderContext = DaprMessagingSenderContext.newContext(topic, this.beanName);
Observation observation = createObservation(senderContext);

return observation.observe(() ->
publishEvent(pubsubName, topic, message)
.contextWrite(getReactorContext())
.doOnError(err -> {
LOGGER.error("Failed to send msg to '{}' topic", topic, err);

observation.error(err);
observation.stop();
})
.doOnSuccess(ignore -> {
LOGGER.trace("Sent msg to '{}' topic", topic);
observation.start();

observation.stop();
})
);
}
return publishEvent(pubsubName, topic, message)
.contextWrite(getReactorContext(senderContext))
.doOnError(err -> {
LOGGER.error("Failed to send msg to '{}' topic", topic, err);

private Context getReactorContext() {
Map<String, String> map = new HashMap<>();
TextMapSetter<Map<String, String>> setter = (carrier, key, value) -> map.put(key, value);
io.opentelemetry.context.Context otelContext = io.opentelemetry.context.Context.current();
observation.error(err);
observation.stop();
})
.doOnSuccess(ignore -> {
LOGGER.trace("Sent msg to '{}' topic", topic);

openTelemetry.getPropagators().getTextMapPropagator().inject(otelContext, map, setter);
observation.stop();
});
}

return Context.of(map);
private Context getReactorContext(DaprMessagingSenderContext senderContext) {
return Context.of(senderContext.properties());
}

private Observation createObservation(DaprMessagingSenderContext senderContext) {
Expand Down
10 changes: 0 additions & 10 deletions dapr-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,6 @@
<optional>true</optional>
</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 -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down

0 comments on commit 3dc96d7

Please sign in to comment.