|
| 1 | +import 'package:meta/meta.dart'; |
| 2 | + |
| 3 | +import '../../../sentry.dart'; |
| 4 | +import 'metric.dart'; |
| 5 | + |
| 6 | +/// Public API for recording metrics |
| 7 | +abstract base class SentryMetrics { |
| 8 | + void count( |
| 9 | + String name, |
| 10 | + int value, { |
| 11 | + Map<String, SentryAttribute>? attributes, |
| 12 | + Scope? scope, |
| 13 | + }); |
| 14 | + |
| 15 | + void gauge( |
| 16 | + String name, |
| 17 | + num value, { |
| 18 | + String? unit, |
| 19 | + Map<String, SentryAttribute>? attributes, |
| 20 | + Scope? scope, |
| 21 | + }); |
| 22 | + |
| 23 | + void distribution( |
| 24 | + String name, |
| 25 | + num value, { |
| 26 | + String? unit, |
| 27 | + Map<String, SentryAttribute>? attributes, |
| 28 | + Scope? scope, |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +typedef CaptureMetricCallback = Future<void> Function(SentryMetric metric); |
| 33 | +typedef ScopeProvider = Scope Function(); |
| 34 | + |
| 35 | +@internal |
| 36 | +final class DefaultSentryMetrics implements SentryMetrics { |
| 37 | + final bool _isMetricsEnabled; |
| 38 | + final CaptureMetricCallback _captureMetricCallback; |
| 39 | + final ClockProvider _clockProvider; |
| 40 | + final ScopeProvider _defaultScopeProvider; |
| 41 | + |
| 42 | + DefaultSentryMetrics( |
| 43 | + {required bool isMetricsEnabled, |
| 44 | + required CaptureMetricCallback captureMetricCallback, |
| 45 | + required ClockProvider clockProvider, |
| 46 | + required ScopeProvider defaultScopeProvider}) |
| 47 | + : _isMetricsEnabled = isMetricsEnabled, |
| 48 | + _captureMetricCallback = captureMetricCallback, |
| 49 | + _clockProvider = clockProvider, |
| 50 | + _defaultScopeProvider = defaultScopeProvider; |
| 51 | + |
| 52 | + @override |
| 53 | + void count( |
| 54 | + String name, |
| 55 | + int value, { |
| 56 | + Map<String, SentryAttribute>? attributes, |
| 57 | + Scope? scope, |
| 58 | + }) { |
| 59 | + if (!_isMetricsEnabled) return; |
| 60 | + |
| 61 | + final metric = SentryCounterMetric( |
| 62 | + timestamp: _clockProvider(), |
| 63 | + name: name, |
| 64 | + value: value, |
| 65 | + spanId: _activeSpanIdFor(scope), |
| 66 | + traceId: _traceIdFor(scope), |
| 67 | + attributes: attributes ?? {}); |
| 68 | + |
| 69 | + _captureMetricCallback(metric); |
| 70 | + } |
| 71 | + |
| 72 | + @override |
| 73 | + void gauge( |
| 74 | + String name, |
| 75 | + num value, { |
| 76 | + String? unit, |
| 77 | + Map<String, SentryAttribute>? attributes, |
| 78 | + Scope? scope, |
| 79 | + }) { |
| 80 | + if (!_isMetricsEnabled) return; |
| 81 | + |
| 82 | + final metric = SentryGaugeMetric( |
| 83 | + timestamp: _clockProvider(), |
| 84 | + name: name, |
| 85 | + value: value, |
| 86 | + spanId: _activeSpanIdFor(scope), |
| 87 | + traceId: _traceIdFor(scope), |
| 88 | + attributes: attributes ?? {}); |
| 89 | + |
| 90 | + _captureMetricCallback(metric); |
| 91 | + } |
| 92 | + |
| 93 | + @override |
| 94 | + void distribution( |
| 95 | + String name, |
| 96 | + num value, { |
| 97 | + String? unit, |
| 98 | + Map<String, SentryAttribute>? attributes, |
| 99 | + Scope? scope, |
| 100 | + }) { |
| 101 | + if (!_isMetricsEnabled) return; |
| 102 | + |
| 103 | + final metric = SentryDistributionMetric( |
| 104 | + timestamp: _clockProvider(), |
| 105 | + name: name, |
| 106 | + value: value, |
| 107 | + unit: unit, |
| 108 | + spanId: _activeSpanIdFor(scope), |
| 109 | + traceId: _traceIdFor(scope), |
| 110 | + attributes: attributes ?? {}); |
| 111 | + |
| 112 | + _captureMetricCallback(metric); |
| 113 | + } |
| 114 | + |
| 115 | + SentryId _traceIdFor(Scope? scope) => |
| 116 | + (scope ?? _defaultScopeProvider()).propagationContext.traceId; |
| 117 | + |
| 118 | + SpanId? _activeSpanIdFor(Scope? scope) => |
| 119 | + (scope ?? _defaultScopeProvider()).span?.context.spanId; |
| 120 | +} |
| 121 | + |
| 122 | +@internal |
| 123 | +final class NoOpSentryMetrics implements SentryMetrics { |
| 124 | + const NoOpSentryMetrics(); |
| 125 | + |
| 126 | + static const instance = NoOpSentryMetrics(); |
| 127 | + |
| 128 | + @override |
| 129 | + void count(String name, int value, |
| 130 | + {Map<String, SentryAttribute>? attributes, Scope? scope}) {} |
| 131 | + |
| 132 | + @override |
| 133 | + void distribution(String name, num value, |
| 134 | + {String? unit, Map<String, SentryAttribute>? attributes, Scope? scope}) {} |
| 135 | + |
| 136 | + @override |
| 137 | + void gauge(String name, num value, |
| 138 | + {String? unit, Map<String, SentryAttribute>? attributes, Scope? scope}) {} |
| 139 | +} |
0 commit comments