|  | 
|  | 1 | +# frozen_string_literal: true | 
|  | 2 | + | 
|  | 3 | +# Copyright The OpenTelemetry Authors | 
|  | 4 | +# | 
|  | 5 | +# SPDX-License-Identifier: Apache-2.0 | 
|  | 6 | + | 
|  | 7 | +require 'test_helper' | 
|  | 8 | + | 
|  | 9 | +describe OpenTelemetry::SDK::Metrics::Export::PeriodicMetricReader do | 
|  | 10 | +  PeriodicMetricReader = OpenTelemetry::SDK::Metrics::Export::PeriodicMetricReader | 
|  | 11 | +  SUCCESS = OpenTelemetry::SDK::Metrics::Export::SUCCESS | 
|  | 12 | +  FAILURE = OpenTelemetry::SDK::Metrics::Export::FAILURE | 
|  | 13 | +  TIMEOUT = OpenTelemetry::SDK::Metrics::Export::TIMEOUT | 
|  | 14 | + | 
|  | 15 | +  class TestExporter | 
|  | 16 | +    def initialize(status_codes: nil) | 
|  | 17 | +      @status_codes = status_codes || [] | 
|  | 18 | +      @exported_metrics = [] | 
|  | 19 | +    end | 
|  | 20 | + | 
|  | 21 | +    attr_reader :exported_metrics | 
|  | 22 | + | 
|  | 23 | +    def export(metrics, timeout: nil) | 
|  | 24 | +      s = @status_codes.shift | 
|  | 25 | +      if s.nil? || s == SUCCESS | 
|  | 26 | +        @exported_metrics.concat(metrics) | 
|  | 27 | +        SUCCESS | 
|  | 28 | +      else | 
|  | 29 | +        s | 
|  | 30 | +      end | 
|  | 31 | +    end | 
|  | 32 | + | 
|  | 33 | +    def shutdown(timeout: nil) = SUCCESS | 
|  | 34 | + | 
|  | 35 | +    def force_flush(timeout: nil) = SUCCESS | 
|  | 36 | +  end | 
|  | 37 | + | 
|  | 38 | +  describe 'exporter with failure' do | 
|  | 39 | +    let(:exporter) { TestExporter.new(status_codes: [FAILURE]) } | 
|  | 40 | +    let(:reader) { PeriodicMetricReader.new(exporter: exporter) } | 
|  | 41 | + | 
|  | 42 | +    it 'logs export failure as error' do | 
|  | 43 | +      mock_logger = Minitest::Mock.new | 
|  | 44 | +      mock_logger.expect(:error, nil, [/Unable to export metrics/]) | 
|  | 45 | +      mock_logger.expect(:error, nil, [/Result code: 1/]) | 
|  | 46 | + | 
|  | 47 | +      # Stub collect to return a non-empty array so export is actually called | 
|  | 48 | +      reader.stub(:collect, ['mock_metric']) do | 
|  | 49 | +        OpenTelemetry.stub(:logger, mock_logger) do | 
|  | 50 | +          reader.force_flush | 
|  | 51 | +        end | 
|  | 52 | +      end | 
|  | 53 | + | 
|  | 54 | +      reader.shutdown | 
|  | 55 | +      mock_logger.verify | 
|  | 56 | +    end | 
|  | 57 | +  end | 
|  | 58 | + | 
|  | 59 | +  describe 'succesful exporter' do | 
|  | 60 | +    let(:exporter) { TestExporter.new(status_codes: [SUCCESS]) } | 
|  | 61 | +    let(:reader) { PeriodicMetricReader.new(exporter: exporter) } | 
|  | 62 | + | 
|  | 63 | +    it 'logs successful export as debug' do | 
|  | 64 | +      mock_logger = Minitest::Mock.new | 
|  | 65 | +      mock_logger.expect(:debug, nil, ['Successfully exported metrics']) | 
|  | 66 | + | 
|  | 67 | +      # Stub collect to return a non-empty array so export is actually called | 
|  | 68 | +      reader.stub(:collect, ['mock_metric']) do | 
|  | 69 | +        OpenTelemetry.stub(:logger, mock_logger) do | 
|  | 70 | +          reader.force_flush | 
|  | 71 | +        end | 
|  | 72 | +      end | 
|  | 73 | + | 
|  | 74 | +      reader.shutdown | 
|  | 75 | +      mock_logger.verify | 
|  | 76 | +    end | 
|  | 77 | +  end | 
|  | 78 | +end | 
0 commit comments