Skip to content

[WIP] fix console samples #3979

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

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fae85e1
update readmes for noagent samples, add otel properties to applicatio…
lbloder Dec 10, 2024
4fa4327
update readme for console - noagent
lbloder Dec 10, 2024
44d5b42
update opentelemetry and optentelemetry agent readme
lbloder Dec 10, 2024
3d8511b
bind transaction to scope so that the message is attached to the trace
lbloder Dec 10, 2024
cb5dec2
initialize otel using AutoConfiguredOpenTelemetrySdk to bind SPI prov…
lbloder Dec 10, 2024
4ab84f2
Format code
getsentry-bot Dec 10, 2024
2286edd
format, make transaction current, so that it is sent to sentry
lbloder Dec 10, 2024
cb09dcc
Merge branch 'fix/fix-console-samples' of github.com:getsentry/sentry…
lbloder Dec 10, 2024
1c79edf
fix import
lbloder Dec 16, 2024
92e1fde
Merge branch '8.x.x' into feat/update-otel-sample-readme
lbloder Dec 17, 2024
eb50dd9
Merge branch 'feat/update-otel-sample-readme' into fix/fix-console-sa…
lbloder Dec 17, 2024
8e9ce93
add java options and new agentless module to readme
lbloder Dec 17, 2024
2ffbf93
Merge branch 'feat/update-otel-sample-readme' into fix/fix-console-sa…
lbloder Dec 20, 2024
8e854e7
Merge branch '8.x.x' into feat/update-otel-sample-readme
lbloder Dec 20, 2024
bbfcea0
Merge branch 'feat/update-otel-sample-readme' into fix/fix-console-sa…
lbloder Dec 20, 2024
b1a5b9e
Merge branch '8.x.x' into feat/update-otel-sample-readme
lbloder Dec 20, 2024
c7c68dd
Merge branch '8.x.x' into feat/update-otel-sample-readme
lbloder Jan 7, 2025
0926957
update readmes
lbloder Jan 7, 2025
ca4cb47
Merge branch 'feat/update-otel-sample-readme' into fix/fix-console-sa…
lbloder Jan 7, 2025
ef4ff97
fix typos, add comment about attaching transaction to scope, use shor…
lbloder Jan 13, 2025
1836553
Format code
getsentry-bot Jan 13, 2025
848737d
Merge branch 'main' into feat/update-otel-sample-readme
lbloder Jan 13, 2025
4eed72a
Merge branch 'feat/update-otel-sample-readme' into fix/fix-console-sa…
lbloder Jan 13, 2025
f7d558d
Merge branch 'main' into fix/fix-console-samples
lbloder Mar 28, 2025
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 @@ -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`:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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();

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 @@ -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:
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")) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Makes the code do what the comment says :)

return 0.1;
} else {
return 0.5;
return 1.0;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

set to 1.0 for all transactions except for /products to make it easier for people trying this sample

}
});
});
Expand Down Expand Up @@ -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);
Comment on lines +161 to +163
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Binding the transaction to scope is required here for the sample to work as expected as later in the code we have a line Sentry.captureMessage("this message is connected to the outerSpan"); which only works as described if the transaction is on the scope IIRC.

// 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);
Expand Down
Loading