From 3d8511bf4e3cc413813f17420bb9e7f87d7e396f Mon Sep 17 00:00:00 2001 From: Lukas Bloder Date: Tue, 10 Dec 2024 16:27:16 +0100 Subject: [PATCH 1/2] bind transaction to scope so that the message is attached to the trace --- .../src/main/java/io/sentry/samples/console/Main.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/sentry-samples/sentry-samples-console/src/main/java/io/sentry/samples/console/Main.java b/sentry-samples/sentry-samples-console/src/main/java/io/sentry/samples/console/Main.java index a0c0d5dd9e..e12ab5a0db 100644 --- a/sentry-samples/sentry-samples-console/src/main/java/io/sentry/samples/console/Main.java +++ b/sentry-samples/sentry-samples-console/src/main/java/io/sentry/samples/console/Main.java @@ -86,10 +86,10 @@ public static void main(String[] args) throws InterruptedException { options.setTracesSampler( context -> { // only 10% of transactions with "/product" prefix will be collected - if (!context.getTransactionContext().getName().startsWith("/products")) { + if (context.getTransactionContext().getName().startsWith("/products")) { return 0.1; } else { - return 0.5; + return 1.0; } }); }); @@ -155,7 +155,9 @@ public static void main(String[] args) throws InterruptedException { // // Transactions collect execution time of the piece of code that's executed between the start // and finish of transaction. - ITransaction transaction = Sentry.startTransaction("transaction name", "op"); + final TransactionOptions options = new TransactionOptions(); + options.setBindToScope(true); + ITransaction transaction = Sentry.startTransaction("transaction name", "op", options); // Transactions can contain one or more Spans ISpan outerSpan = transaction.startChild("child"); Thread.sleep(100); From cb5dec263cfadc3d08a401aaf373dd84f0b0dc9a Mon Sep 17 00:00:00 2001 From: Lukas Bloder Date: Tue, 10 Dec 2024 16:28:37 +0100 Subject: [PATCH 2/2] initialize otel using AutoConfiguredOpenTelemetrySdk to bind SPI provided classes --- .../java/io/sentry/samples/console/Main.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sentry-samples/sentry-samples-console-opentelemetry-noagent/src/main/java/io/sentry/samples/console/Main.java b/sentry-samples/sentry-samples-console-opentelemetry-noagent/src/main/java/io/sentry/samples/console/Main.java index c27aad737b..deed2f6ec4 100644 --- a/sentry-samples/sentry-samples-console-opentelemetry-noagent/src/main/java/io/sentry/samples/console/Main.java +++ b/sentry-samples/sentry-samples-console-opentelemetry-noagent/src/main/java/io/sentry/samples/console/Main.java @@ -4,6 +4,9 @@ import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.StatusCode; import io.opentelemetry.context.Scope; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; import io.sentry.Breadcrumb; import io.sentry.EventProcessor; import io.sentry.Hint; @@ -17,10 +20,23 @@ import io.sentry.protocol.Message; import io.sentry.protocol.User; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; public class Main { public static void main(String[] args) throws InterruptedException { + final OpenTelemetrySdk sdk = AutoConfiguredOpenTelemetrySdk.builder() + .addPropertiesSupplier(() -> { + final Map properties = new HashMap<>(); + properties.put("otel.logs.exporter", "none"); + properties.put("otel.metrics.exporter", "none"); + properties.put("otel.traces.exporter", "none"); + return properties; + }) + .build().getOpenTelemetrySdk(); + GlobalOpenTelemetry.set(sdk); + Sentry.init( options -> { // NOTE: Replace the test DSN below with YOUR OWN DSN to see the events from this app in