Line data Source code
1 : // Copyright The OpenTelemetry Authors
2 : // SPDX-License-Identifier: Apache-2.0
3 :
4 : import 'dart:async';
5 :
6 : import 'data/metric_data.dart';
7 :
8 : /// MetricExporter is responsible for sending metrics to a backend.
9 : abstract class MetricExporter {
10 : /// Export a batch of metrics to the backend.
11 : ///
12 : /// Returns true if the export was successful, false otherwise.
13 : Future<bool> export(MetricData data);
14 :
15 : /// Force flush any pending metrics.
16 : ///
17 : /// Returns true if the flush was successful, false otherwise.
18 : Future<bool> forceFlush();
19 :
20 : /// Shutdown the exporter.
21 : ///
22 : /// This should cleanup any resources and perform final exports.
23 : /// Returns true if the shutdown was successful, false otherwise.
24 : Future<bool> shutdown();
25 : }
26 :
27 : /// ConsoleMetricExporter is a simple exporter that prints metrics to the console.
28 : class ConsoleMetricExporter implements MetricExporter {
29 : /// Whether the exporter has been shut down.
30 : bool _isShutdown = false;
31 :
32 0 : @override
33 : Future<bool> export(MetricData data) async {
34 0 : if (_isShutdown) {
35 0 : print('ConsoleMetricExporter: Cannot export after shutdown');
36 : return false;
37 : }
38 :
39 0 : print('ConsoleMetricExporter: Exporting ${data.metrics.length} metrics:');
40 0 : for (final metric in data.metrics) {
41 0 : print(
42 0 : ' - ${metric.name} (${metric.unit ?? "no unit"}): ${metric.description ?? ""}');
43 0 : for (final point in metric.points) {
44 0 : final value = point.valueAsString;
45 0 : print(' - Value: $value, Attributes: ${point.attributes}');
46 0 : if (point.hasExemplars) {
47 0 : print(' Exemplars: ${point.exemplars?.length}');
48 : }
49 : }
50 : }
51 :
52 : return true;
53 : }
54 :
55 0 : @override
56 : Future<bool> forceFlush() async {
57 : // No-op for console exporter
58 : return true;
59 : }
60 :
61 0 : @override
62 : Future<bool> shutdown() async {
63 0 : _isShutdown = true;
64 : return true;
65 : }
66 : }
|