Skip to content

Commit 476d7f1

Browse files
committed
also set trace id and span id for logs and metrics
1 parent aaab229 commit 476d7f1

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

sentry-opentelemetry/sentry-opentelemetry-otlp/src/main/java/io/sentry/opentelemetry/otlp/OpenTelemetryOtlpEventProcessor.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import io.sentry.ScopesAdapter;
1010
import io.sentry.SentryEvent;
1111
import io.sentry.SentryLevel;
12+
import io.sentry.SentryLogEvent;
13+
import io.sentry.SentryMetricsEvent;
1214
import io.sentry.SpanContext;
1315
import io.sentry.protocol.SentryId;
1416
import org.jetbrains.annotations.NotNull;
@@ -39,8 +41,8 @@ public OpenTelemetryOtlpEventProcessor() {
3941
new SpanContext(
4042
new SentryId(traceId),
4143
new io.sentry.SpanId(spanId),
42-
"opentelemetry", // TODO probably no way to get span name
43-
null, // TODO where to get parent id from?
44+
"opentelemetry",
45+
null,
4446
null);
4547

4648
event.getContexts().setTrace(spanContext);
@@ -68,6 +70,52 @@ public OpenTelemetryOtlpEventProcessor() {
6870
return event;
6971
}
7072

73+
@Override
74+
public @Nullable SentryLogEvent process(@NotNull SentryLogEvent event) {
75+
@NotNull final Span otelSpan = Span.current();
76+
@NotNull final String traceId = otelSpan.getSpanContext().getTraceId();
77+
@NotNull final String spanId = otelSpan.getSpanContext().getSpanId();
78+
79+
if (TraceId.isValid(traceId) && SpanId.isValid(spanId)) {
80+
event.setTraceId(new SentryId(traceId));
81+
event.setSpanId(new io.sentry.SpanId(spanId));
82+
} else {
83+
scopes
84+
.getOptions()
85+
.getLogger()
86+
.log(
87+
SentryLevel.DEBUG,
88+
"Not linking Sentry event to any transaction created via OpenTelemetry as traceId %s or spanId %s are invalid.",
89+
traceId,
90+
spanId);
91+
}
92+
93+
return event;
94+
}
95+
96+
@Override
97+
public @Nullable SentryMetricsEvent process(@NotNull SentryMetricsEvent event, @NotNull Hint hint) {
98+
@NotNull final Span otelSpan = Span.current();
99+
@NotNull final String traceId = otelSpan.getSpanContext().getTraceId();
100+
@NotNull final String spanId = otelSpan.getSpanContext().getSpanId();
101+
102+
if (TraceId.isValid(traceId) && SpanId.isValid(spanId)) {
103+
event.setTraceId(new SentryId(traceId));
104+
event.setSpanId(new io.sentry.SpanId(spanId));
105+
} else {
106+
scopes
107+
.getOptions()
108+
.getLogger()
109+
.log(
110+
SentryLevel.DEBUG,
111+
"Not linking Sentry event to any transaction created via OpenTelemetry as traceId %s or spanId %s are invalid.",
112+
traceId,
113+
spanId);
114+
}
115+
116+
return event;
117+
}
118+
71119
@Override
72120
public @Nullable Long getOrder() {
73121
return 6000L;

0 commit comments

Comments
 (0)