LCOV - code coverage report
Current view: top level - src/metrics/observe - observable_result.dart (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 9 9
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 'package:dartastic_opentelemetry_api/dartastic_opentelemetry_api.dart';
       5              : 
       6              : /// Implementation of the APIObservableResult interface for asynchronous instruments.
       7              : ///
       8              : /// ObservableResult is used by asynchronous instruments to collect measurements
       9              : /// during observation callbacks. When a callback is invoked, it receives an
      10              : /// ObservableResult that it can use to record observations.
      11              : ///
      12              : /// More information:
      13              : /// https://opentelemetry.io/docs/specs/otel/metrics/api/#asynchronous-instruments
      14              : class ObservableResult<T extends num> implements APIObservableResult<T> {
      15              :   /// The list of measurements recorded during this observation.
      16              :   final List<Measurement<T>> _measurements = [];
      17              : 
      18              :   /// Records an observation with this result.
      19              :   ///
      20              :   /// This method records a measurement with the specified value and optional
      21              :   /// attributes. The measurement will be associated with the current timestamp.
      22              :   ///
      23              :   /// @param value The observed value to record
      24              :   /// @param attributes Optional attributes to associate with this observation
      25            5 :   @override
      26              :   void observe(T value, [Attributes? attributes]) {
      27              :     // Make sure we have a valid OTelFactory
      28              :     if (OTelFactory.otelFactory == null) {
      29            1 :       if (OTelLog.isWarn()) {
      30            1 :         OTelLog.warn(
      31              :             'Warning: OTelFactory.otelFactory is null in ObservableResult.observe');
      32              :       }
      33              :       return;
      34              :     }
      35              : 
      36              :     // Add the measurement
      37              :     final measurement =
      38            5 :         OTelFactory.otelFactory!.createMeasurement<T>(value, attributes);
      39           10 :     _measurements.add(measurement);
      40              :   }
      41              : 
      42              :   /// Records an observation with attributes specified as a map.
      43              :   ///
      44              :   /// This is a convenience method that converts the map to Attributes
      45              :   /// and calls observe().
      46              :   ///
      47              :   /// @param value The observed value to record
      48              :   /// @param attributes Map of attribute names to values
      49            1 :   @override
      50              :   void observeWithMap(T value, Map<String, Object> attributes) {
      51            2 :     observe(value, attributes.toAttributes());
      52              :   }
      53              : 
      54              :   /// Returns all measurements recorded by this result.
      55              :   ///
      56              :   /// This method is used by the SDK to collect measurements after
      57              :   /// an observation callback has been executed.
      58              :   ///
      59              :   /// @return An unmodifiable list of all measurements recorded
      60            5 :   @override
      61           10 :   List<Measurement<T>> get measurements => List.unmodifiable(_measurements);
      62              : }
        

Generated by: LCOV version 2.0-1