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 23 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
51 changes: 48 additions & 3 deletions sentry-opentelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ application. Please see the module [README](sentry-opentelemetry-agent/README.md

This contains customizations to the OpenTelemetry Java Agent such as registering the
`SentrySpanProcessor` and `SentryPropagator` as well as providing default properties that
enable the `sentry` propagator and disable exporters so our agent doesn't trigger lots of log
warnings due to OTLP server not being there. This can also be used without the agent.
enable the `sentry` propagator.

### `sentry-opentelemetry-bootstrap`

Expand All @@ -39,4 +38,50 @@ you also need this module as a dependency.
Contains `SentrySpanProcessor` and `SentryPropagator` which are used by our Java Agent but can also
be used when manually instrumenting using OpenTelemetry. If you want to use OpenTelemetry without
the agent but still want some configuration convenience, you should rather use the
`sentry-opentelemetry-agentcustomization` module.
`sentry-opentelemetry-agentless` module or the `sentry-opentelemetry-agentless-spring` module if you are using Spring Boot.

### `sentry-opentelemetry-agentless`
Combines all modules and dependencies needed to use Sentry with OpenTelemetry without the agent.

### `sentry-opentelemetry-agentless-spring`
Combines all modules and dependencies needed to use Sentry with OpenTelemetry in SpringBoot without an agent.

## Running without an Agent
If you want to use Sentry with OpenTelemetry without an agent, you can do so by adding the `sentry-opentelemetry-agentless` module as dependencies to your project.

And run your application with the following JVM arguments:
```
-Dotel.java.global-autoconfigure.enabled=true
```
You may also want to set the following environment variables to if you do not use OTEL exporters:
`OTEL_LOGS_EXPORTER=none;OTEL_METRICS_EXPORTER=none;OTEL_TRACES_EXPORTER=none`

Alternatively you can initialize OpenTelemetry programmatically like this:

```java
// Initialize OpenTelemetry by using the AutoConfiguredOpenTelemetrySdk which automatically
// registers the `SentrySpanProcessor` and `SentryPropagator` and others.
// Also, you need to disable the OTEL exporters if you do not use them.
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();
```

And then initialize Sentry as usual:

```java
// Initialize Sentry
Sentry.init(
options -> {
options.setDsn("...");
...
}
)
```
28 changes: 14 additions & 14 deletions sentry-opentelemetry/sentry-opentelemetry-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,24 @@ For more details on configuring Sentry via `sentry.properties` please see the
[docs page](https://docs.sentry.io/platforms/java/configuration/).

As an alternative to the `SENTRY_PROPERTIES_FILE` environment variable you can provide individual
settings as environment variables (e.g. `SENTRY_DSN=...`) or you may initialize `Sentry` inside
your target application. If you do so, please make sure to set the `instrumenter` to `otel`, e.g.
like this:
settings as environment variables (e.g. `SENTRY_DSN=...`).

## Controlling auto initialization of Sentry

By default, if you pass either `SENTRY_DSN` or `SENTRY_PROPERTIES_FILE` as environment variable,
Sentry will automatically be initialized by this agent. To disable this behaviour, you can set
`SENTRY_AUTO_INIT=false` as environment variable. You will then have to initialize Sentry inside
the target application:

```
Sentry.init(
options -> {
options.setDsn("...");
...
options.setInstrumenter(Instrumenter.OTEL);
}
)
```

Using the `otel` instrumenter will ensure `Sentry` instrumentation will be done via OpenTelemetry
and integrations as well as direct interactions with transactions and spans have no effect.

## Controlling auto initialization of Sentry

By default if you pass either `SENTRY_DSN` or `SENTRY_PROPERTIES_FILE` as environment variable,
Sentry will automatically be initialized by this agent. To disable this behaviour, you can set
`SENTRY_AUTO_INIT=false` as environment variable. You will then have to initialize Sentry inside
the target application.

## Debugging

To enable debug logging for Sentry, please provide `SENTRY_DEBUG=true` as environment variable or
Expand All @@ -62,6 +56,7 @@ Example log message:
```
ERROR io.opentelemetry.exporter.internal.grpc.OkHttpGrpcExporter - Failed to export spans. The request could not be executed. Full error message: Failed to connect to localhost/[0:0:0:0:0:0:0:1]:4317
ERROR io.opentelemetry.exporter.internal.grpc.OkHttpGrpcExporter - Failed to export metrics. The request could not be executed. Full error message: Failed to connect to localhost/[0:0:0:0:0:0:0:1]:4317
ERROR io.opentelemetry.exporter.internal.http.HttpExporter - Failed to export logs. The request could not be executed. Full error message: Failed to connect to localhost/[0:0:0:0:0:0:0:1]:4318
```

### Traces
Expand All @@ -73,3 +68,8 @@ see [OpenTelemetry GitHub](https://github.com/open-telemetry/opentelemetry-java/

To turn off exporting of metrics you can set `OTEL_METRICS_EXPORTER=none`
see [OpenTelemetry GitHub](https://github.com/open-telemetry/opentelemetry-java/tree/main/sdk-extensions/autoconfigure#otlp-exporter-span-metric-and-log-exporters)

### Logs

To turn off log exporting, set `OTEL_LOGS_EXPORTER=none`
see [OpenTelemetry GitHub](https://github.com/open-telemetry/opentelemetry-java/tree/main/sdk-extensions/autoconfigure#otlp-exporter-span-metric-and-log-exporters).
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,21 @@

*NOTE: Our OpenTelemetry modules are still experimental. Any feedback is welcome.*

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 and add a `sentry.properties`
configuration file to your project that could look like this:
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`:

```properties
# NOTE: Replace the test DSN below with YOUR OWN DSN to see the events from this app in your Sentry project/dashboard
dsn=https://[email protected]/5428563
traces-sample-rate=1.0
# OTEL configuration
otel.propagators=tracecontext,baggage,sentry
otel.logs.exporter=none
otel.metrics.exporter=none
otel.traces.exporter=none
```

For more details on configuring Sentry via `sentry.properties` please see the
[docs page](https://docs.sentry.io/platforms/java/configuration/).

As an alternative to the `SENTRY_PROPERTIES_FILE` environment variable you can provide individual
settings as environment variables (e.g. `SENTRY_DSN=...`) or you may initialize `Sentry` inside
your target application. If you do so, please make sure to apply OpenTelemetry specific options, e.g.
like this:

```
Sentry.init(
options -> {
options.setDsn("...");
...
OpenTelemetryUtil.applyOpenTelemetryOptions(options, false);
}
)
```

## Getting rid of exporter error messages

In case you are using this module without needing to use any OpenTelemetry exporters you can add
the following environment variables to turn off exporters and stop seeing error messages about
servers not being reachable in the logs.

Example log message:
```
ERROR io.opentelemetry.exporter.internal.grpc.OkHttpGrpcExporter - Failed to export spans. The request could not be executed. Full error message: Failed to connect to localhost/[0:0:0:0:0:0:0:1]:4317
ERROR io.opentelemetry.exporter.internal.grpc.OkHttpGrpcExporter - Failed to export metrics. The request could not be executed. Full error message: Failed to connect to localhost/[0:0:0:0:0:0:0:1]:4317
```

### Traces

To turn off exporting of traces you can set `OTEL_TRACES_EXPORTER=none`
see [OpenTelemetry GitHub](https://github.com/open-telemetry/opentelemetry-java/tree/main/sdk-extensions/autoconfigure#otlp-exporter-span-metric-and-log-exporters)

### Metrics
This module will automatically configure OpenTelemetry and Sentry for you.

To turn off exporting of metrics you can set `OTEL_METRICS_EXPORTER=none`
see [OpenTelemetry GitHub](https://github.com/open-telemetry/opentelemetry-java/tree/main/sdk-extensions/autoconfigure#otlp-exporter-span-metric-and-log-exporters)
With the dependency and configuration in place, just run your SpringBoot application as usual.
60 changes: 32 additions & 28 deletions sentry-opentelemetry/sentry-opentelemetry-agentless/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,42 @@ For more details on configuring Sentry via `sentry.properties` please see the
[docs page](https://docs.sentry.io/platforms/java/configuration/).

As an alternative to the `SENTRY_PROPERTIES_FILE` environment variable you can provide individual
settings as environment variables (e.g. `SENTRY_DSN=...`) or you may initialize `Sentry` inside
your target application. If you do so, please make sure to apply OpenTelemetry specific options, e.g.
like this:
settings as environment variables (e.g. `SENTRY_DSN=...`).

Run your application with the following JVM arguments:
```
Sentry.init(
options -> {
options.setDsn("...");
...
OpenTelemetryUtil.applyOpenTelemetryOptions(options, false);
}
)
-Dotel.java.global-autoconfigure.enabled=true
```

## Getting rid of exporter error messages

In case you are using this module without needing to use any OpenTelemetry exporters you can add
the following environment variables to turn off exporters and stop seeing error messages about
servers not being reachable in the logs.

Example log message:
```
ERROR io.opentelemetry.exporter.internal.grpc.OkHttpGrpcExporter - Failed to export spans. The request could not be executed. Full error message: Failed to connect to localhost/[0:0:0:0:0:0:0:1]:4317
ERROR io.opentelemetry.exporter.internal.grpc.OkHttpGrpcExporter - Failed to export metrics. The request could not be executed. Full error message: Failed to connect to localhost/[0:0:0:0:0:0:0:1]:4317
You may also want to set the following environment variables to if you do not use OTEL exporters:
`OTEL_LOGS_EXPORTER=none;OTEL_METRICS_EXPORTER=none;OTEL_TRACES_EXPORTER=none`

Alternatively you can initialize OpenTelemetry programmatically like this:

```java
// Initialize OpenTelemetry by using the AutoConfiguredOpenTelemetrySdk which automatically
// registers the `SentrySpanProcessor` and `SentryPropagator` and others.
// Also, you need to disable the OTEL exporters if you do not use them.
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();
```

### Traces
If you're not using `sentry.properties` or environment variables you can then initialize Sentry programmatically as usual:

To turn off exporting of traces you can set `OTEL_TRACES_EXPORTER=none`
see [OpenTelemetry GitHub](https://github.com/open-telemetry/opentelemetry-java/tree/main/sdk-extensions/autoconfigure#otlp-exporter-span-metric-and-log-exporters)

### Metrics

To turn off exporting of metrics you can set `OTEL_METRICS_EXPORTER=none`
see [OpenTelemetry GitHub](https://github.com/open-telemetry/opentelemetry-java/tree/main/sdk-extensions/autoconfigure#otlp-exporter-span-metric-and-log-exporters)
```java
// Initialize Sentry
Sentry.init(
options -> {
options.setDsn("...");
...
}
)
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Sentry Sample Console

Sample application showing how to use Sentry manually without any framework integration.
Sample application showing how to use Sentry with OpenTelemetry manually without any framework integration and without java agent.

## How to run?

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
Loading
Loading