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

Add contexts to SentryTracer and add otel context to it #2361

Merged
merged 6 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -145,8 +145,8 @@ tasks {
attributes.put("Premain-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
attributes.put("Can-Redefine-Classes", "true")
attributes.put("Can-Retransform-Classes", "true")
attributes.put("Implementation-Vendor", "Demo")
attributes.put("Implementation-Version", "demo-${project.version}-otel-${Config.Libs.otelJavaagentVersion}")
attributes.put("Implementation-Vendor", "Sentry")
attributes.put("Implementation-Version", "sentry-${project.version}-otel-${Config.Libs.otelJavaagentVersion}")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry.opentelemetry;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
Expand Down Expand Up @@ -176,16 +177,15 @@ private void updateTransactionWithOtelData(
spanDescription.getDescription(), spanDescription.getTransactionNameSource());

final @NotNull Map<String, Object> otelContext = toOtelContext(otelSpan);
System.out.println(otelContext);
// TODO set otel context on transaction
sentryTransaction.setContext("otel", otelContext);
}

private @NotNull Map<String, Object> toOtelContext(final @NotNull ReadableSpan otelSpan) {
final @NotNull SpanData spanData = otelSpan.toSpanData();
final @NotNull Map<String, Object> context = new HashMap<>();

context.put("attributes", spanData.getAttributes().asMap());
context.put("resource", spanData.getResource().getAttributes().asMap());
context.put("attributes", toMapWithStringKeys(spanData.getAttributes()));
context.put("resource", toMapWithStringKeys(spanData.getResource().getAttributes()));

return context;
}
Expand Down Expand Up @@ -234,4 +234,19 @@ private SpanStatus mapOtelStatus(final @NotNull ReadableSpan otelSpan) {
private boolean hasSentryBeenInitialized() {
return Sentry.isEnabled();
}

private @NotNull Map<String, Object> toMapWithStringKeys(@Nullable Attributes attributes) {
Copy link
Member Author

@adinauer adinauer Nov 15, 2022

Choose a reason for hiding this comment

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

JsonObjectSerializer only serializes Maps with String keys.

adinauer marked this conversation as resolved.
Show resolved Hide resolved
@NotNull Map<String, Object> mapWithStringKeys = new HashMap<>();
adinauer marked this conversation as resolved.
Show resolved Hide resolved

if (attributes != null) {
attributes.forEach(
(key, value) -> {
if (key != null) {
mapWithStringKeys.put(key.getKey(), value);
}
});
}

return mapWithStringKeys;
}
}
6 changes: 6 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ public abstract interface class io/sentry/ISpan {
}

public abstract interface class io/sentry/ITransaction : io/sentry/ISpan {
public abstract fun getContexts ()Lio/sentry/protocol/Contexts;
public abstract fun getEventId ()Lio/sentry/protocol/SentryId;
public abstract fun getLatestActiveSpan ()Lio/sentry/Span;
public abstract fun getName ()Ljava/lang/String;
Expand All @@ -525,6 +526,7 @@ public abstract interface class io/sentry/ITransaction : io/sentry/ISpan {
public abstract fun isProfileSampled ()Ljava/lang/Boolean;
public abstract fun isSampled ()Ljava/lang/Boolean;
public abstract fun scheduleFinish ()V
public abstract fun setContext (Ljava/lang/String;Ljava/lang/Object;)V
public abstract fun setName (Ljava/lang/String;)V
public abstract fun setName (Ljava/lang/String;Lio/sentry/protocol/TransactionNameSource;)V
}
Expand Down Expand Up @@ -765,6 +767,7 @@ public final class io/sentry/NoOpTransaction : io/sentry/ITransaction {
public fun finish ()V
public fun finish (Lio/sentry/SpanStatus;)V
public fun finish (Lio/sentry/SpanStatus;Ljava/util/Date;)V
public fun getContexts ()Lio/sentry/protocol/Contexts;
public fun getData (Ljava/lang/String;)Ljava/lang/Object;
public fun getDescription ()Ljava/lang/String;
public fun getEventId ()Lio/sentry/protocol/SentryId;
Expand All @@ -783,6 +786,7 @@ public final class io/sentry/NoOpTransaction : io/sentry/ITransaction {
public fun isProfileSampled ()Ljava/lang/Boolean;
public fun isSampled ()Ljava/lang/Boolean;
public fun scheduleFinish ()V
public fun setContext (Ljava/lang/String;Ljava/lang/Object;)V
public fun setData (Ljava/lang/String;Ljava/lang/Object;)V
public fun setDescription (Ljava/lang/String;)V
public fun setMeasurement (Ljava/lang/String;Ljava/lang/Number;)V
Expand Down Expand Up @@ -1564,6 +1568,7 @@ public final class io/sentry/SentryTracer : io/sentry/ITransaction {
public fun finish (Lio/sentry/SpanStatus;)V
public fun finish (Lio/sentry/SpanStatus;Ljava/util/Date;)V
public fun getChildren ()Ljava/util/List;
public fun getContexts ()Lio/sentry/protocol/Contexts;
public fun getData ()Ljava/util/Map;
public fun getData (Ljava/lang/String;)Ljava/lang/Object;
public fun getDescription ()Ljava/lang/String;
Expand All @@ -1585,6 +1590,7 @@ public final class io/sentry/SentryTracer : io/sentry/ITransaction {
public fun isProfileSampled ()Ljava/lang/Boolean;
public fun isSampled ()Ljava/lang/Boolean;
public fun scheduleFinish ()V
public fun setContext (Ljava/lang/String;Ljava/lang/Object;)V
public fun setData (Ljava/lang/String;Ljava/lang/Object;)V
public fun setDescription (Ljava/lang/String;)V
public fun setMeasurement (Ljava/lang/String;Ljava/lang/Number;)V
Expand Down
8 changes: 8 additions & 0 deletions sentry/src/main/java/io/sentry/ITransaction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry;

import io.sentry.protocol.Contexts;
import io.sentry.protocol.SentryId;
import io.sentry.protocol.TransactionNameSource;
import java.util.List;
Expand Down Expand Up @@ -77,4 +78,11 @@ public interface ITransaction extends ISpan {

/** Schedules when transaction should be automatically finished. */
void scheduleFinish();

@ApiStatus.Internal
adinauer marked this conversation as resolved.
Show resolved Hide resolved
void setContext(@NotNull String key, @NotNull Object context);

@ApiStatus.Internal
@NotNull
Contexts getContexts();
}
11 changes: 11 additions & 0 deletions sentry/src/main/java/io/sentry/NoOpTransaction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry;

import io.sentry.protocol.Contexts;
import io.sentry.protocol.SentryId;
import io.sentry.protocol.TransactionNameSource;
import java.util.Collections;
Expand Down Expand Up @@ -177,4 +178,14 @@ public void setMeasurement(@NotNull String name, @NotNull Number value) {}
@Override
public void setMeasurement(
@NotNull String name, @NotNull Number value, @NotNull MeasurementUnit unit) {}

@ApiStatus.Internal
@Override
public void setContext(@NotNull String key, @NotNull Object context) {}

@ApiStatus.Internal
@Override
public @NotNull Contexts getContexts() {
return new Contexts();
}
}
14 changes: 14 additions & 0 deletions sentry/src/main/java/io/sentry/SentryTracer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry;

import io.sentry.protocol.Contexts;
import io.sentry.protocol.MeasurementValue;
import io.sentry.protocol.SentryId;
import io.sentry.protocol.SentryTransaction;
Expand Down Expand Up @@ -78,6 +79,7 @@ public final class SentryTracer implements ITransaction {
private @NotNull TransactionNameSource transactionNameSource;
private final @NotNull Map<String, MeasurementValue> measurements;
private final @NotNull Instrumenter instrumenter;
private final @NotNull Contexts contexts = new Contexts();

public SentryTracer(final @NotNull TransactionContext context, final @NotNull IHub hub) {
this(context, hub, null);
Expand Down Expand Up @@ -661,6 +663,18 @@ Map<String, MeasurementValue> getMeasurements() {
return measurements;
}

@ApiStatus.Internal
@Override
public void setContext(@NotNull String key, @NotNull Object context) {
adinauer marked this conversation as resolved.
Show resolved Hide resolved
contexts.put(key, context);
}

@ApiStatus.Internal
@Override
public @NotNull Contexts getContexts() {
return contexts;
}

private static final class FinishStatus {
static final FinishStatus NOT_FINISHED = FinishStatus.notFinished();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public SentryTransaction(final @NotNull SentryTracer sentryTracer) {
}
}
final Contexts contexts = this.getContexts();

contexts.putAll(sentryTracer.getContexts());

final SpanContext tracerContext = sentryTracer.getSpanContext();
// tags must be placed on the root of the transaction instead of contexts.trace.tags
contexts.setTrace(
Expand Down
25 changes: 25 additions & 0 deletions sentry/src/test/java/io/sentry/SentryTracerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,31 @@ class SentryTracerTest {
)
}

@Test
fun `when transaction is finished, context is set`() {
val tracer = fixture.getSut()
val otelContext = mapOf(
"attributes" to mapOf(
"db.connection_string" to "hsqldb:mem:",
"db.statement" to "CREATE TABLE person ( id INTEGER IDENTITY PRIMARY KEY, firstName VARCHAR(?) NOT NULL, lastName VARCHAR(?) NOT NULL )"
),
"resource" to mapOf(
"process.runtime.version" to "17.0.4.1+1",
"telemetry.auto.version" to "sentry-6.7.0-otel-1.19.2"
)
)
tracer.setContext("otel", otelContext)
tracer.finish()

verify(fixture.hub).captureTransaction(
check {
assertEquals(otelContext, it.contexts["otel"])
},
anyOrNull<TraceContext>(),
anyOrNull()
)
}

@Test
fun `returns sentry-trace header`() {
val tracer = fixture.getSut()
Expand Down