diff --git a/implementation/cdi/src/main/java/io/smallrye/opentelemetry/implementation/cdi/OpenTelemetryProducer.java b/implementation/cdi/src/main/java/io/smallrye/opentelemetry/implementation/cdi/OpenTelemetryProducer.java index 3350de1b..eebc06b2 100644 --- a/implementation/cdi/src/main/java/io/smallrye/opentelemetry/implementation/cdi/OpenTelemetryProducer.java +++ b/implementation/cdi/src/main/java/io/smallrye/opentelemetry/implementation/cdi/OpenTelemetryProducer.java @@ -1,12 +1,34 @@ package io.smallrye.opentelemetry.implementation.cdi; +import static io.smallrye.opentelemetry.api.OpenTelemetryConfig.INSTRUMENTATION_NAME; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.function.BiConsumer; + +import jakarta.enterprise.context.RequestScoped; +import jakarta.enterprise.inject.Disposes; import jakarta.enterprise.inject.Produces; +import jakarta.enterprise.inject.spi.CDI; import jakarta.inject.Inject; import jakarta.inject.Singleton; import io.opentelemetry.api.OpenTelemetry; +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.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; +import io.opentelemetry.sdk.common.CompletableResultCode; import io.smallrye.opentelemetry.api.OpenTelemetryConfig; @Singleton @@ -30,4 +52,111 @@ public OpenTelemetry getOpenTelemetry() { .build() .getOpenTelemetrySdk(); } + + @Produces + @Singleton + public Tracer getTracer() { + return CDI.current().select(OpenTelemetry.class).get().getTracer(INSTRUMENTATION_NAME); + } + + @Produces + @RequestScoped + public Span getSpan() { + 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 + public Baggage getBaggage() { + 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(); + } + }; + } + + void close(@Disposes final OpenTelemetry openTelemetry) { + OpenTelemetrySdk openTelemetrySdk = (OpenTelemetrySdk) openTelemetry; + List shutdown = new ArrayList<>(); + shutdown.add(openTelemetrySdk.getSdkTracerProvider().shutdown()); + shutdown.add(openTelemetrySdk.getSdkMeterProvider().shutdown()); + shutdown.add(openTelemetrySdk.getSdkLoggerProvider().shutdown()); + CompletableResultCode.ofAll(shutdown).join(10, TimeUnit.SECONDS); + } } diff --git a/testsuite/pom.xml b/testsuite/pom.xml index 563352be..1e76aa96 100644 --- a/testsuite/pom.xml +++ b/testsuite/pom.xml @@ -37,9 +37,7 @@ - tck