Skip to content

Commit 1081ca3

Browse files
committed
Update
1 parent 1a48756 commit 1081ca3

14 files changed

Lines changed: 186 additions & 108 deletions

packages/dart/lib/src/hub.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'client_reports/discard_reason.dart';
99
import 'profiling.dart';
1010
import 'sentry_tracer.dart';
1111
import 'sentry_traces_sampler.dart';
12-
import 'telemetry/metric/sentry_metric.dart';
12+
import 'telemetry/metric/metric.dart';
1313
import 'transport/data_category.dart';
1414

1515
/// Configures the scope through the callback.

packages/dart/lib/src/hub_adapter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'scope.dart';
1111
import 'sentry.dart';
1212
import 'sentry_client.dart';
1313
import 'sentry_options.dart';
14-
import 'telemetry/metric/sentry_metric.dart';
14+
import 'telemetry/metric/metric.dart';
1515
import 'tracing.dart';
1616

1717
/// Hub adapter to make Integrations testable

packages/dart/lib/src/noop_hub.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import 'protocol/sentry_feedback.dart';
1010
import 'scope.dart';
1111
import 'sentry_client.dart';
1212
import 'sentry_options.dart';
13-
import 'telemetry/metric/sentry_metric.dart';
13+
import 'telemetry/metric/metric.dart';
1414
import 'tracing.dart';
1515

1616
class NoOpHub implements Hub {

packages/dart/lib/src/sentry.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import 'sentry_attachment/sentry_attachment.dart';
2323
import 'sentry_client.dart';
2424
import 'sentry_options.dart';
2525
import 'sentry_run_zoned_guarded.dart';
26-
import 'telemetry/metric/sentry_metrics.dart';
26+
import 'telemetry/metric/metrics.dart';
27+
import 'telemetry/metric/metrics_setup_integration.dart';
2728
import 'telemetry/processing/processor_integration.dart';
2829
import 'tracing.dart';
2930
import 'transport/data_category.dart';
@@ -111,6 +112,7 @@ class Sentry {
111112
options.addIntegration(LoadDartDebugImagesIntegration());
112113
}
113114

115+
options.addIntegration(MetricsSetupIntegration());
114116
options.addIntegration(FeatureFlagsIntegration());
115117
options.addIntegration(LogsEnricherIntegration());
116118
options.addIntegration(DefaultTelemetryProcessorIntegration());

packages/dart/lib/src/sentry_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import 'sentry_exception_factory.dart';
1818
import 'sentry_options.dart';
1919
import 'sentry_stack_trace_factory.dart';
2020
import 'sentry_trace_context_header.dart';
21-
import 'telemetry/metric/sentry_metric.dart';
21+
import 'telemetry/metric/metric.dart';
2222
import 'transport/client_report_transport.dart';
2323
import 'transport/data_category.dart';
2424
import 'transport/http_transport.dart';

packages/dart/lib/src/sentry_options.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import 'noop_client.dart';
1212
import 'platform/platform.dart';
1313
import 'sentry_exception_factory.dart';
1414
import 'sentry_stack_trace_factory.dart';
15-
import 'telemetry/metric/sentry_metric.dart';
16-
import 'telemetry/metric/sentry_metrics.dart';
15+
import 'telemetry/metric/metric.dart';
16+
import 'telemetry/metric/metrics.dart';
1717
import 'telemetry/processing/processor.dart';
1818
import 'transport/noop_transport.dart';
1919
import 'version.dart';
@@ -563,7 +563,8 @@ class SentryOptions {
563563

564564
late final SentryLogger logger = SentryLogger(clock);
565565

566-
late final metrics = SentryMetrics(HubAdapter(), clock);
566+
@internal
567+
SentryMetrics metrics = NoOpSentryMetrics.instance;
567568

568569
@internal
569570
TelemetryProcessor telemetryProcessor = NoOpTelemetryProcessor();

packages/dart/lib/src/telemetry/metric/sentry_metric.dart renamed to packages/dart/lib/src/telemetry/metric/metric.dart

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@ import 'metric_type.dart';
55

66
/// Base sealed class for all Sentry metrics
77
sealed class SentryMetric {
8-
final DateTime timestamp;
98
final SentryMetricType type;
10-
final String name;
11-
final num value;
12-
final SentryId traceId;
13-
final SpanId? spanId;
14-
final String? unit;
15-
final Map<String, SentryAttribute> attributes;
169

17-
const SentryMetric({
10+
DateTime timestamp;
11+
String name;
12+
num value;
13+
SentryId traceId;
14+
SpanId? spanId;
15+
String? unit;
16+
Map<String, SentryAttribute> attributes;
17+
18+
SentryMetric({
1819
required this.timestamp,
1920
required this.type,
2021
required this.name,
2122
required this.value,
2223
required this.traceId,
2324
this.spanId,
2425
this.unit,
25-
this.attributes = const {},
26-
});
26+
Map<String, SentryAttribute>? attributes,
27+
}) : attributes = attributes ?? {};
2728

2829
@internal
2930
Map<String, dynamic> toJson() {
@@ -41,9 +42,9 @@ sealed class SentryMetric {
4142
}
4243
}
4344

44-
/// Counter metric - increments counts (only increases)
45+
/// Counter metric - increments counts
4546
final class SentryCounterMetric extends SentryMetric {
46-
const SentryCounterMetric({
47+
SentryCounterMetric({
4748
required super.timestamp,
4849
required super.name,
4950
required super.value,
@@ -56,7 +57,7 @@ final class SentryCounterMetric extends SentryMetric {
5657

5758
/// Gauge metric - tracks values that can go up or down
5859
final class SentryGaugeMetric extends SentryMetric {
59-
const SentryGaugeMetric({
60+
SentryGaugeMetric({
6061
required super.timestamp,
6162
required super.name,
6263
required super.value,
@@ -69,7 +70,7 @@ final class SentryGaugeMetric extends SentryMetric {
6970

7071
/// Distribution metric - tracks statistical distribution of values
7172
final class SentryDistributionMetric extends SentryMetric {
72-
const SentryDistributionMetric({
73+
SentryDistributionMetric({
7374
required super.timestamp,
7475
required super.name,
7576
required super.value,
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import '../../../sentry.dart';
2+
import 'metrics.dart';
3+
4+
class MetricsSetupIntegration extends Integration<SentryOptions> {
5+
static const integrationName = 'MetricsSetup';
6+
7+
@override
8+
void call(Hub hub, SentryOptions options) {
9+
options.metrics = DefaultSentryMetrics(
10+
isMetricsEnabled: options.enableMetrics,
11+
captureMetricCallback: hub.captureMetric,
12+
clockProvider: options.clock,
13+
defaultScopeProvider: () => hub.scope);
14+
15+
options.sdk.addIntegration(integrationName);
16+
}
17+
}

packages/dart/lib/src/telemetry/metric/sentry_metrics.dart

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)