From da666521ba9a6333cdd5b4c6b4f93c0ac787b527 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Fri, 9 Feb 2024 11:33:26 +0000 Subject: [PATCH] MicroProfile Telemetry compatibility --- .../runtime/tracing/cdi/TracerProducer.java | 94 ++++++++++++++++++- .../OpenTelemetryClassicThreadContext.java | 35 +++++++ ...boss.resteasy.spi.concurrent.ThreadContext | 1 + 3 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/intrumentation/resteasy/OpenTelemetryClassicThreadContext.java create mode 100644 extensions/opentelemetry/runtime/src/main/resources/META-INF/services/org.jboss.resteasy.spi.concurrent.ThreadContext diff --git a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/cdi/TracerProducer.java b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/cdi/TracerProducer.java index b17c611cfd6e5..6c44fb6edbca3 100644 --- a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/cdi/TracerProducer.java +++ b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/cdi/TracerProducer.java @@ -2,6 +2,10 @@ import static io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig.INSTRUMENTATION_NAME; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.function.BiConsumer; + import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.RequestScoped; import jakarta.enterprise.inject.Produces; @@ -9,7 +13,13 @@ import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.baggage.Baggage; +import io.opentelemetry.api.baggage.BaggageBuilder; +import io.opentelemetry.api.baggage.BaggageEntry; +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanContext; +import io.opentelemetry.api.trace.StatusCode; import io.opentelemetry.api.trace.Tracer; import io.quarkus.arc.DefaultBean; import io.quarkus.opentelemetry.runtime.tracing.DelayedAttributes; @@ -33,13 +43,93 @@ public Tracer getTracer() { @RequestScoped @DefaultBean public Span getSpan() { - return Span.current(); + return new Span() { + @Override + public Span setAttribute(final AttributeKey key, final T value) { + return Span.current().setAttribute(key, value); + } + + @Override + public Span addEvent(final String name, final Attributes attributes) { + return Span.current().addEvent(name, attributes); + } + + @Override + + public Span addEvent( + final String name, + final Attributes attributes, + final long timestamp, + final TimeUnit unit) { + return Span.current().addEvent(name, attributes, timestamp, unit); + } + + @Override + public Span setStatus(final StatusCode statusCode, final String description) { + return Span.current().setStatus(statusCode, description); + } + + @Override + public Span recordException(final Throwable exception, final Attributes additionalAttributes) { + return Span.current().recordException(exception, additionalAttributes); + } + + @Override + public Span updateName(final String name) { + return Span.current().updateName(name); + } + + @Override + public void end() { + Span.current().end(); + } + + @Override + public void end(final long timestamp, final TimeUnit unit) { + Span.current().end(timestamp, unit); + } + + @Override + public SpanContext getSpanContext() { + return Span.current().getSpanContext(); + } + + @Override + public boolean isRecording() { + return Span.current().isRecording(); + } + }; } @Produces @RequestScoped @DefaultBean public Baggage getBaggage() { - return Baggage.current(); + return new Baggage() { + @Override + public int size() { + return Baggage.current().size(); + } + + @Override + public void forEach(final BiConsumer consumer) { + Baggage.current().forEach(consumer); + } + + @Override + public Map asMap() { + return Baggage.current().asMap(); + } + + @Override + public String getEntryValue(final String entryKey) { + return Baggage.current().getEntryValue(entryKey); + } + + @Override + public BaggageBuilder toBuilder() { + return Baggage.current().toBuilder(); + } + }; } } diff --git a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/intrumentation/resteasy/OpenTelemetryClassicThreadContext.java b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/intrumentation/resteasy/OpenTelemetryClassicThreadContext.java new file mode 100644 index 0000000000000..f2f0112822ea5 --- /dev/null +++ b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/intrumentation/resteasy/OpenTelemetryClassicThreadContext.java @@ -0,0 +1,35 @@ +package io.quarkus.opentelemetry.runtime.tracing.intrumentation.resteasy; + +import java.util.HashMap; +import java.util.Map; + +import jakarta.ws.rs.ext.Provider; + +import org.jboss.resteasy.spi.concurrent.ThreadContext; + +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; + +@Provider +public class OpenTelemetryClassicThreadContext implements ThreadContext> { + @Override + public Map capture() { + Map context = new HashMap<>(); + context.put("context", Context.current()); + return context; + } + + @Override + public void push(final Map context) { + Context current = (Context) context.get("context"); + Scope scope = current.makeCurrent(); + context.put("scope", scope); + } + + @Override + public void reset(final Map context) { + Scope scope = (Scope) context.get("scope"); + scope.close(); + context.clear(); + } +} \ No newline at end of file diff --git a/extensions/opentelemetry/runtime/src/main/resources/META-INF/services/org.jboss.resteasy.spi.concurrent.ThreadContext b/extensions/opentelemetry/runtime/src/main/resources/META-INF/services/org.jboss.resteasy.spi.concurrent.ThreadContext new file mode 100644 index 0000000000000..c2b7e3c4bf1e7 --- /dev/null +++ b/extensions/opentelemetry/runtime/src/main/resources/META-INF/services/org.jboss.resteasy.spi.concurrent.ThreadContext @@ -0,0 +1 @@ +io.quarkus.opentelemetry.runtime.tracing.intrumentation.resteasy.OpenTelemetryClassicThreadContext