Line data Source code
1 : // Copyright The OpenTelemetry Authors
2 : // SPDX-License-Identifier: Apache-2.0
3 :
4 : import 'package:dartastic_opentelemetry_api/dartastic_opentelemetry_api.dart';
5 :
6 : import '../data/metric.dart';
7 : import '../data/metric_point.dart';
8 : import '../meter.dart';
9 : import '../storage/gauge_storage.dart';
10 : import 'base_instrument.dart';
11 :
12 : /// Gauge is a synchronous instrument that records non-additive values.
13 : ///
14 : /// A Gauge is used to measure a non-additive value that represents the current
15 : /// state, such as temperature, memory usage, or CPU utilization.
16 : class Gauge<T extends num> implements APIGauge<T>, SDKInstrument {
17 : /// The underlying API Gauge.
18 : final APIGauge<T> _apiGauge;
19 :
20 : /// The Meter that created this Gauge.
21 : final Meter _meter;
22 :
23 : /// Storage for gauge measurements.
24 : final GaugeStorage<T> _storage = GaugeStorage<T>();
25 :
26 : /// Creates a new Gauge instance.
27 6 : Gauge({required APIGauge<T> apiGauge, required Meter meter})
28 : : _apiGauge = apiGauge,
29 : _meter = meter {
30 : // Register this instrument with the meter provider
31 30 : _meter.provider.registerInstrument(_meter.name, this);
32 : }
33 :
34 6 : @override
35 12 : String get name => _apiGauge.name;
36 :
37 3 : @override
38 6 : String? get unit => _apiGauge.unit;
39 :
40 3 : @override
41 6 : String? get description => _apiGauge.description;
42 :
43 4 : @override
44 8 : bool get enabled => _meter.enabled;
45 :
46 6 : @override
47 6 : APIMeter get meter => _meter;
48 :
49 1 : @override
50 : bool get isCounter => false;
51 :
52 1 : @override
53 : bool get isUpDownCounter => false;
54 :
55 1 : @override
56 : bool get isGauge => true;
57 :
58 1 : @override
59 : bool get isHistogram => false;
60 :
61 4 : @override
62 : void record(T value, [Attributes? attributes]) {
63 : // First use the API implementation (no-op by default)
64 8 : _apiGauge.record(value, attributes);
65 :
66 : // Only record if enabled
67 4 : if (!enabled) return;
68 :
69 : // Record the measurement in our storage
70 8 : _storage.record(value, attributes);
71 : }
72 :
73 1 : @override
74 : void recordWithMap(T value, Map<String, Object> attributeMap) {
75 : // Just convert to Attributes and call record
76 : final attributes =
77 2 : attributeMap.isEmpty ? null : attributeMap.toAttributes();
78 1 : record(value, attributes);
79 : }
80 :
81 : /// Gets the current value of the gauge for a specific set of attributes.
82 2 : T getValue(Attributes attributes) {
83 4 : final value = _storage.getValue(attributes);
84 : // Handle the cast to the generic type
85 3 : if (T == int) return value.toInt() as T;
86 2 : if (T == double) return value.toDouble() as T;
87 : return value;
88 : }
89 :
90 : /// Gets the current points for this gauge.
91 : /// This is used by the SDK to collect metrics.
92 3 : List<MetricPoint<T>> collectPoints() {
93 6 : return _storage.collectPoints();
94 : }
95 :
96 3 : @override
97 : List<Metric> collectMetrics() {
98 4 : if (!enabled) return [];
99 :
100 : // Get the points from storage
101 3 : final points = collectPoints();
102 :
103 3 : if (points.isEmpty) return [];
104 :
105 : // Create a metric with the collected points
106 3 : final metric = Metric(
107 3 : name: name,
108 3 : description: description,
109 3 : unit: unit,
110 : type: MetricType.gauge,
111 : points: points,
112 : );
113 :
114 3 : return [metric];
115 : }
116 : }
|