Skip to content

Commit

Permalink
Merge pull request #38707 from radcortez/mp-telemetry
Browse files Browse the repository at this point in the history
MicroProfile Telemetry compatibility
  • Loading branch information
brunobat authored Feb 15, 2024
2 parents 3782467 + da66652 commit 8ec03a3
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

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;
import jakarta.inject.Singleton;

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;
Expand All @@ -33,13 +43,93 @@ public Tracer getTracer() {
@RequestScoped
@DefaultBean
public Span getSpan() {
return Span.current();
return new Span() {
@Override
public <T> Span setAttribute(final AttributeKey<T> 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<? super String, ? super BaggageEntry> consumer) {
Baggage.current().forEach(consumer);
}

@Override
public Map<String, BaggageEntry> 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();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -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<Map<String, Object>> {
@Override
public Map<String, Object> capture() {
Map<String, Object> context = new HashMap<>();
context.put("context", Context.current());
return context;
}

@Override
public void push(final Map<String, Object> context) {
Context current = (Context) context.get("context");
Scope scope = current.makeCurrent();
context.put("scope", scope);
}

@Override
public void reset(final Map<String, Object> context) {
Scope scope = (Scope) context.get("scope");
scope.close();
context.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.opentelemetry.runtime.tracing.intrumentation.resteasy.OpenTelemetryClassicThreadContext

0 comments on commit 8ec03a3

Please sign in to comment.