Skip to content

Commit

Permalink
docs: add section for custom otel sampler (#10843)
Browse files Browse the repository at this point in the history
  • Loading branch information
chargome committed Jul 25, 2024
1 parent 4e5cd81 commit c200357
Showing 1 changed file with 69 additions and 0 deletions.
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();
```

0 comments on commit c200357

Please sign in to comment.