diff --git a/sentry-opentelemetry/sentry-opentelemetry-agentless-spring/README.md b/sentry-opentelemetry/sentry-opentelemetry-agentless-spring/README.md index 9e4bd992fb..14ed008ff4 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-agentless-spring/README.md +++ b/sentry-opentelemetry/sentry-opentelemetry-agentless-spring/README.md @@ -3,6 +3,9 @@ This module allows the use of Sentry with OpenTelemetry in SpringBoot without an agent by using the OpenTelemetry Spring Boot Starter. For guidance on when to use this module instead of the agent, please have a look at the [OpenTelemetry Spring Boot Starter documentation](https://opentelemetry.io/docs/zero-code/java/spring-boot-starter/). +This module allows the use of Sentry with OpenTelemetry in SpringBoot without an agent by using the OpenTelemetry Spring Boot Starter. +For guidance on when to use this module instead of the agent, please have a look at the [OpenTelemetry Spring Boot Starter documentation](https://opentelemetry.io/docs/zero-code/java/spring-boot-starter/). + ## How to use it Add the latest `sentry-opentelemetry-agentless-spring` module as a dependency to your Sentry enabled [SpringBoot](https://docs.sentry.io/platforms/java/guides/spring-boot/) application and add the following to your `application.properties`: 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..3cb4293b14 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,7 @@ import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.StatusCode; import io.opentelemetry.context.Scope; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; import io.sentry.Breadcrumb; import io.sentry.EventProcessor; import io.sentry.Hint; @@ -17,10 +18,25 @@ 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 { + + AutoConfiguredOpenTelemetrySdk.builder() + .setResultAsGlobal() + .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(); + Sentry.init( options -> { // NOTE: Replace the test DSN below with YOUR OWN DSN to see the events from this app in @@ -163,37 +179,45 @@ 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. + // Transactions need to be bound to scope in order to have `Messages` or `Exceptions` linked to + // them ITransaction transaction = Sentry.startTransaction("transaction name", "op"); - // Transactions can contain one or more Spans - ISpan outerSpan = transaction.startChild("child"); - Thread.sleep(100); - // Spans create a tree structure. Each span can have one ore more spans inside. - ISpan innerSpan = outerSpan.startChild("jdbc", "select * from product where id = :id"); - innerSpan.setStatus(SpanStatus.OK); - Thread.sleep(300); - // Finish the span and mark the end time of the span execution. - // Note: finishing spans does not send them to Sentry - innerSpan.finish(); - try (ISentryLifecycleToken outerScope = outerSpan.makeCurrent()) { - Span otelSpan = - GlobalOpenTelemetry.get() - .getTracer("demoTracer", "1.0.0") - .spanBuilder("otelSpan") - .startSpan(); - try (Scope innerScope = otelSpan.makeCurrent()) { - otelSpan.setAttribute("otel-attribute", "attribute-value"); - Thread.sleep(150); - otelSpan.setStatus(StatusCode.OK); + try (ISentryLifecycleToken transactionScope = transaction.makeCurrent()) { + // Transactions can contain one or more Spans + ISpan outerSpan = transaction.startChild("child"); + Thread.sleep(100); + // Spans create a tree structure. Each span can have one or more spans inside. + ISpan innerSpan = outerSpan.startChild("jdbc", "select * from product where id = :id"); + innerSpan.setStatus(SpanStatus.OK); + Thread.sleep(300); + // Finish the span and mark the end time of the span execution. + // Note: finishing spans does not send them to Sentry + innerSpan.finish(); + try (ISentryLifecycleToken outerScope = outerSpan.makeCurrent()) { + Span otelSpan = + GlobalOpenTelemetry.get() + .getTracer("demoTracer", "1.0.0") + .spanBuilder("otelSpan") + .startSpan(); + try (Scope innerScope = otelSpan.makeCurrent()) { + otelSpan.setAttribute("otel-attribute", "attribute-value"); + Thread.sleep(150); + otelSpan.setStatus(StatusCode.OK); + } finally { + otelSpan.end(); + } + // Every SentryEvent reported during the execution of the transaction or a span, will have + // trace + // context attached + Sentry.captureMessage("this message is connected to the outerSpan"); + } finally { - otelSpan.end(); + outerSpan.finish(SpanStatus.OK); } + } finally { + // marks transaction as finished and sends it together with all child spans to Sentry + transaction.finish(); } - // Every SentryEvent reported during the execution of the transaction or a span, will have trace - // context attached - Sentry.captureMessage("this message is connected to the outerSpan"); - outerSpan.finish(); - // marks transaction as finished and sends it together with all child spans to Sentry - transaction.finish(); // All events that have not been sent yet are being flushed on JVM exit. Events can be also // flushed manually: 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..d17c683aec 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 @@ -9,6 +9,7 @@ import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.SpanStatus; +import io.sentry.TransactionOptions; import io.sentry.protocol.Message; import io.sentry.protocol.User; import java.util.Collections; @@ -86,10 +87,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,11 +156,15 @@ 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"); + // Transactions need to be bound to scope in order to have `Messages` or `Exceptions` linked to + // them + 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); - // Spans create a tree structure. Each span can have one ore more spans inside. + // Spans create a tree structure. Each span can have one or more spans inside. ISpan innerSpan = outerSpan.startChild("jdbc", "select * from product where id = :id"); innerSpan.setStatus(SpanStatus.OK); Thread.sleep(300);