Line data Source code
1 : // Copyright The OpenTelemetry Authors
2 : // SPDX-License-Identifier: Apache-2.0
3 :
4 : import '../../../dartastic_opentelemetry.dart';
5 :
6 : /// BaseInstrument is the base class for all metric instruments.
7 : ///
8 : /// It provides common functionality for collecting metrics from instruments.
9 : abstract class SDKInstrument {
10 : /// The name of the instrument
11 : String get name;
12 :
13 : /// The description of the instrument
14 : String? get description;
15 :
16 : /// The unit of the instrument
17 : String? get unit;
18 :
19 : /// Whether the instrument is enabled
20 : bool isEnabled();
21 :
22 : /// The meter that created this instrument
23 : APIMeter get meter;
24 :
25 : /// Collects metrics from this instrument
26 : ///
27 : /// This is called by metric readers to gather the current metrics
28 : List<Metric> collectMetrics();
29 : }
30 :
31 : /// An SDK asynchronous instrument that can receive observations from a
32 : /// batch callback registered with [Meter.registerBatchCallback].
33 : ///
34 : /// A batch callback runs once per collection, before the instruments it
35 : /// covers collect. Its observations are queued here and drained by the
36 : /// instrument's own `collect()`, after the instrument's single-instrument
37 : /// callbacks, so both sources land in the same collection cycle.
38 : abstract class SDKObservableInstrument
39 : implements SDKInstrument, APIObservableInstrument {
40 : /// Queues a measurement observed by a batch callback for the next
41 : /// collection. The value is converted to the instrument's numeric type.
42 : void observeFromBatch(num value, Attributes? attributes);
43 : }
44 :
45 : /// Converts a [num] to the instrument's numeric type [T].
46 : ///
47 : /// Observable callbacks and batch callbacks report [num] values, while the
48 : /// instrument's storage is typed; an `int` instrument truncates a double.
49 10 : T castNum<T extends num>(num value) {
50 19 : if (T == int) return value.toInt() as T;
51 16 : if (T == double) return value.toDouble() as T;
52 : return value as T;
53 : }
|