getsentry / getsentry/sentry-dotnet

feat: Forcing a Sampling Decision

Open
#4,386 2 comments 0 reactions 0 assignees View on GitHub
.NET Docs Improvement public API
Dominant language
C#
Stars
770
Forks
248
Avg merge
3d 4h
Merged PRs (30d)
49

Description

### Description

We currently don't support a path where the user can (or is meant to), when starting a new transaction via `SentrySdk.StartTransaction()`, force a sampling decision, without involving `TracesSampler` nor `TracesSampleRate`.

See https://docs.sentry.io/platforms/dotnet/configuration/sampling/.
Also, in the docs we mention an API that does not exist:
```CSharp
// TransactionContext(string, string, bool) cannot be resolved
var transactionContext = new TransactionContext("GET /search", "http", true);
```

The existing `ITransactionContext.IsSampled` is intended to propagate sampling decision from upstream nodes (distributed tracing). See https://docs.sentry.io/concepts/key-terms/tracing/distributed-tracing/.

### Note
Consider: #4392
Consider: https://github.com/getsentry/sentry-dotnet/pull/4374#discussion_r2241130180

### Discussion

Hub.cs

```CSharp
if (isForcedByUserCode.HasValue)
{
sampleRate = isForcedByUserCode.GetValueOrDefault() ? 1.0 : 0.0;
isSampled = isForcedByUserCode.GetValueOrDefault();
}
else if (_options.TracesSampler is { } tracesSampler)
{
var samplingContext = new TransactionSamplingContext(
context,
customSamplingContext);

if (tracesSampler(samplingContext) is { } samplerSampleRate)
{
sampleRate = samplerSampleRate;
isSampled = SampleRandHelper.IsSampled(sampleRand, samplerSampleRate);
}
}

if (isSampled == null)
{
sampleRate = _options.TracesSampleRate ?? 0.0;
isSampled = SampleRandHelper.IsSampled(sampleRand, sampleRate.Value);
}

dynamicSamplingContext = dynamicSamplingContext?.WithSampleRate(sampleRate.Value);
```

---

Issue surfaced through #4374

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.