Skip to content

Commit

Permalink
Merge 71d79f1 into 9cef931
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer authored Nov 16, 2022
2 parents 9cef931 + 71d79f1 commit 4241258
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 6 deletions.
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(final @Nullable Attributes attributes) {
final @NotNull Map<String, Object> mapWithStringKeys = new HashMap<>();

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
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(final @NotNull String key, final @NotNull Object context) {
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

0 comments on commit 4241258

Please sign in to comment.