-
-
Notifications
You must be signed in to change notification settings - Fork 281
feat(span-first): Add traceLifecycle option, dsc to span and sampling to startSpan
#3429
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
Changes from all commits
021260a
9159d28
aa1cbc3
c5288e5
967c93f
73ded7b
f1c0e97
8dc4a53
6581088
74057a6
61c9219
baba206
37b7f7a
1e737da
0f9bae8
b425edd
621a0de
c5ede36
e997927
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,93 @@ | ||
| import 'package:meta/meta.dart'; | ||
|
|
||
| import 'tracing.dart'; | ||
| import 'sentry_options.dart'; | ||
| import '../sentry.dart'; | ||
| import 'telemetry/span/sentry_span_sampling_context.dart'; | ||
| import 'utils/internal_logger.dart'; | ||
|
|
||
| /// Context used by [TracesSamplerCallback] to determine if transaction | ||
| /// is going to be sampled. | ||
| /// | ||
| /// Note: This class currently supports both static (transaction-based) and | ||
| /// streaming (span-based) modes for backwards compatibility. The dual-mode | ||
| /// design with placeholder values and runtime checks is a temporary solution. | ||
| /// Once the legacy transaction API is removed, this class should be simplified | ||
| /// to only support the streaming mode with [SentrySpanSamplingContextV2]. | ||
| @immutable | ||
| class SentrySamplingContext { | ||
| final SentryTransactionContext _transactionContext; | ||
| final SentrySpanSamplingContextV2 _spanContext; | ||
| final Map<String, dynamic> _customSamplingContext; | ||
| final SentryTraceLifecycle _traceLifecycle; | ||
|
|
||
| SentrySamplingContext(this._transactionContext, this._customSamplingContext); | ||
| // TODO: Remove these placeholders once legacy transaction API is removed. | ||
|
|
||
| /// The Transaction context | ||
| SentryTransactionContext get transactionContext => _transactionContext; | ||
| /// Placeholder for streaming mode where transaction context is not used. | ||
| static final _unusedTransactionContext = | ||
| SentryTransactionContext('unused', 'unused'); | ||
|
|
||
| /// Placeholder for static mode where span context is not used. | ||
| static final _unusedSpanContext = SentrySpanSamplingContextV2('unused', {}); | ||
|
Comment on lines
+24
to
+29
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can't remove/change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LMK if there is a better option here
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's fine, it's a straight forward solution as long as we have to support both. |
||
|
|
||
| SentrySamplingContext( | ||
| this._transactionContext, this._spanContext, this._traceLifecycle, | ||
| {Map<String, dynamic>? customSamplingContext}) | ||
| : _customSamplingContext = customSamplingContext ?? {}; | ||
|
|
||
| /// Creates a sampling context for SpanV2 (streaming mode). | ||
| /// | ||
| /// In streaming mode, the transaction context is not used - only the | ||
| /// span context matters for sampling decisions. | ||
| SentrySamplingContext.forSpanV2(SentrySpanSamplingContextV2 spanContext) | ||
| : _transactionContext = _unusedTransactionContext, | ||
| _spanContext = spanContext, | ||
| _traceLifecycle = SentryTraceLifecycle.streaming, | ||
| _customSamplingContext = {}; | ||
|
|
||
| /// Creates a sampling context for legacy transactions (static mode). | ||
| /// | ||
| /// In static mode, the span context is not used - only the transaction | ||
| /// context matters for sampling decisions. | ||
| SentrySamplingContext.forTransaction( | ||
| SentryTransactionContext transactionContext, { | ||
| Map<String, dynamic>? customSamplingContext, | ||
| }) : _transactionContext = transactionContext, | ||
| _spanContext = _unusedSpanContext, | ||
| _traceLifecycle = SentryTraceLifecycle.static, | ||
| _customSamplingContext = customSamplingContext ?? {}; | ||
|
|
||
| /// The Transaction context. | ||
| /// | ||
| /// Throws [StateError] if accessed in streaming mode. | ||
| /// | ||
| /// TODO: Remove this getter once legacy transaction API is removed. | ||
| /// This runtime check is a temporary solution for backwards compatibility. | ||
| SentryTransactionContext get transactionContext { | ||
| if (_traceLifecycle != SentryTraceLifecycle.static) { | ||
| internalLogger | ||
| .error('transactionContext is only available in static mode. ' | ||
| 'Use spanContext for streaming mode.'); | ||
| } | ||
| return _transactionContext; | ||
| } | ||
|
|
||
| /// The Span V2 sampling context. | ||
| /// | ||
| /// Throws [StateError] if accessed in static mode. | ||
| /// | ||
| /// TODO: Remove the runtime check once legacy transaction API is removed. | ||
| /// This runtime check is a temporary solution for backwards compatibility. | ||
| SentrySpanSamplingContextV2 get spanContext { | ||
| if (_traceLifecycle != SentryTraceLifecycle.streaming) { | ||
| internalLogger.error('spanContext is only available in streaming mode. ' | ||
| 'Use transactionContext for static mode.'); | ||
| } | ||
| return _spanContext; | ||
| } | ||
|
|
||
| /// The given sampling context | ||
| Map<String, dynamic> get customSamplingContext => | ||
| Map.unmodifiable(_customSamplingContext); | ||
|
|
||
| /// The trace lifecycle mode for this sampling context. | ||
| SentryTraceLifecycle get traceLifecycle => _traceLifecycle; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /// Controls how trace data is collected and transmitted to Sentry. | ||
| enum SentryTraceLifecycle { | ||
| /// Spans are sent individually as they complete. | ||
| /// | ||
| /// Each span is buffered and transmitted independently without waiting for the entire trace to finish. | ||
| streaming, | ||
|
|
||
| /// Spans are buffered and sent as a complete transaction. | ||
| /// | ||
| /// All spans in a trace are collected and transmitted together when the | ||
| /// root span ends, matching the traditional transaction model. | ||
| static, | ||
| } | ||
|
|
||
| // TODO(next-pr): Guard the APIs that are not supported for the different lifecycle modes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx for the explanation why this is here 👍