Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MicroProfile Telemetry compatibility #38707

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does.

@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
Loading