Skip to content

feat: Forcing a Sampling Decision #4386

@Flash0ver

Description

@Flash0ver

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:

// 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: #4374 (comment)

Discussion

Hub.cs
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETPull requests that update .net codeDocsImprovementpublic APIAdditions/modifications to, or removals from, the public API surface area.

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions