LCOV - code coverage report
Current view: top level - lib/src/trace/sampling - composite_sampler.dart (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 20 20
Test Date: 2026-07-11 13:35:19 Functions: - 0 0

            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              : import '../../otel.dart';
       6              : import 'sampler.dart';
       7              : 
       8              : /// A sampler that combines multiple samplers using a specified operation.
       9              : class CompositeSampler implements Sampler {
      10              :   final List<Sampler> _samplers;
      11              :   final _Operation _operation;
      12              : 
      13            1 :   @override
      14              :   String get description =>
      15            8 :       'CompositeSampler{${_operation.name},[${_samplers.map((s) => s.description).join(',')}]}';
      16              : 
      17              :   /// Creates a CompositeSampler that requires all samplers to accept.
      18            2 :   const CompositeSampler.and(List<Sampler> samplers)
      19            2 :       : this._(samplers, _Operation.and);
      20              : 
      21              :   /// Creates a CompositeSampler that requires any sampler to accept.
      22            2 :   const CompositeSampler.or(List<Sampler> samplers)
      23            2 :       : this._(samplers, _Operation.or);
      24              : 
      25            2 :   const CompositeSampler._(this._samplers, this._operation);
      26              : 
      27            2 :   @override
      28              :   SamplingResult shouldSample({
      29              :     required Context parentContext,
      30              :     required String traceId,
      31              :     required String name,
      32              :     required SpanKind spanKind,
      33              :     required Attributes? attributes,
      34              :     required List<SpanLink>? links,
      35              :   }) {
      36            4 :     if (_samplers.isEmpty) {
      37              :       return const SamplingResult(
      38              :         decision: SamplingDecision.recordAndSample,
      39              :         source: SamplingDecisionSource.tracerConfig,
      40              :       );
      41              :     }
      42              : 
      43              :     Attributes? combinedAttributes;
      44              : 
      45            4 :     for (final sampler in _samplers) {
      46            2 :       final result = sampler.shouldSample(
      47              :         parentContext: parentContext,
      48              :         traceId: traceId,
      49              :         name: name,
      50              :         spanKind: spanKind,
      51              :         attributes: attributes,
      52              :         links: links,
      53              :       );
      54              : 
      55              :       // For AND, if any sampler drops, return drop
      56              :       // For OR, if any sampler samples, return sample
      57            4 :       if (_operation == _Operation.and &&
      58            4 :           result.decision == SamplingDecision.drop) {
      59              :         return result;
      60            4 :       } else if (_operation == _Operation.or &&
      61            4 :           result.decision == SamplingDecision.recordAndSample) {
      62              :         return result;
      63              :       }
      64              : 
      65              :       // Combine attributes if present
      66            2 :       if (result.attributes != null) {
      67            1 :         combinedAttributes ??= OTel.attributes();
      68            2 :         combinedAttributes.copyWithAttributes(result.attributes!);
      69              :       }
      70              :     }
      71              : 
      72              :     // For AND, all samplers accepted, return recordAndSample
      73              :     // For OR, no sampler accepted, return drop
      74            2 :     return SamplingResult(
      75            4 :       decision: _operation == _Operation.and
      76              :           ? SamplingDecision.recordAndSample
      77              :           : SamplingDecision.drop,
      78              :       source: SamplingDecisionSource.tracerConfig,
      79              :       attributes: combinedAttributes,
      80              :     );
      81              :   }
      82              : }
      83              : 
      84              : enum _Operation { and, or }
        

Generated by: LCOV version 2.0-1