LCOV - code coverage report
Current view: top level - src/metrics - metric_exporter.dart (source / functions) Coverage Total Hit
Test: lcov.info Lines: 86.7 % 15 13
Test Date: 2025-11-15 13:23:01 Functions: - 0 0

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

Generated by: LCOV version 2.0-1