Skip to content

Commit

Permalink
Merge bbfcea0 into 8e854e7
Browse files Browse the repository at this point in the history
  • Loading branch information
lbloder authored Dec 20, 2024
2 parents 8e854e7 + bbfcea0 commit 6ebd7bb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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.sentry.Breadcrumb;
import io.sentry.EventProcessor;
import io.sentry.Hint;
Expand All @@ -17,10 +19,26 @@
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<String, String> 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
Expand Down Expand Up @@ -164,40 +182,47 @@ 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 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 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);
} finally {
otelSpan.end();
}
// Every SentryEvent reported during the execution of the transaction or a span, will have
// trace
// context attached
Sentry.captureException(
new RuntimeException("this exception 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:
// Sentry.close();
// Sentry.close();
}

private static class SomeEventProcessor implements EventProcessor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
});
});
Expand Down Expand Up @@ -155,7 +156,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);
Expand Down

0 comments on commit 6ebd7bb

Please sign in to comment.