Skip to content
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

docs: add section for custom otel sampler #10843

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Changes from all commits
Commits
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 @@ -113,3 +113,72 @@ tracer.startActiveSpan("span name", () => {
```

You can also use any other tracer; all OpenTelemetry spans will be picked up by Sentry automatically.

## Using a Custom Sampler

We recommend using `SentrySampler` as this will ensure the correct subset of traces is sent to Sentry depending on your `tracesSampleRate`, as well as that all other Sentry features like trace propagation work as expected.
If you however need to use your own sampler then make sure to wrap your `SamplingResult` with our `wrapSamplingDecision` method:

```js {filename: custom-sampler.js}
const { wrapSamplingDecision } = require("@sentry/opentelemetry");

// implements Sampler from "@opentelemetry/sdk-trace-node"
class CustomSampler {
shouldSample(
context,
_traceId,
_spanName,
_spanKind,
attributes,
_links
) {
const decision = yourDecisionLogic();

// wrap the result
return wrapSamplingDecision({
decision,
context,
spanAttributes: attributes,
});
}

toString() {
return CustomSampler.name;
}
}

module.exports = CustomSampler;

```

Now use your sampler in your `TraceProvider`:

```js {filename: instrument.js}
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const Sentry = require("@sentry/node");
const {
SentrySpanProcessor,
SentryPropagator,
SentrySampler,
} = require("@sentry/opentelemetry");
const CustomSampler = require("./custom-sampler");

const sentryClient = Sentry.init({
dsn: "__DSN__",
skipOpenTelemetrySetup: true,

// By defining any sample rate,
// tracing intergations will be added by default
tracesSampleRate: 0
});


const provider = new NodeTracerProvider({
sampler: new CustomSampler(sentryClient),
});

// ...rest of your setup

// Validate that the setup is correct
Sentry.validateOpenTelemetrySetup();
```
Loading