|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Copyright The OpenTelemetry Authors |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +require_relative '../test_helper' |
| 8 | + |
| 9 | +describe OpenTelemetry::SDK do |
| 10 | + describe '#configure' do |
| 11 | + export = OpenTelemetry::SDK::Metrics::Export |
| 12 | + |
| 13 | + let(:captured_stdout) { StringIO.new } |
| 14 | + let(:metric_data1) { OpenTelemetry::SDK::Metrics::State::MetricData.new({ name: 'name1' }) } |
| 15 | + let(:metric_data2) { OpenTelemetry::SDK::Metrics::State::MetricData.new({ name: 'name2' }) } |
| 16 | + let(:metrics) { [metric_data1, metric_data2] } |
| 17 | + let(:exporter) { export::ConsoleMetricPullExporter.new } |
| 18 | + |
| 19 | + before do |
| 20 | + reset_metrics_sdk |
| 21 | + @original_stdout = $stdout |
| 22 | + $stdout = captured_stdout |
| 23 | + end |
| 24 | + |
| 25 | + after do |
| 26 | + $stdout = @original_stdout |
| 27 | + end |
| 28 | + |
| 29 | + it 'accepts an Array of MetricData as arg to #export and succeeds' do |
| 30 | + _(exporter.export(metrics)).must_equal export::SUCCESS |
| 31 | + end |
| 32 | + |
| 33 | + it 'accepts an Enumerable of MetricData as arg to #export and succeeds' do |
| 34 | + enumerable = Struct.new(:metric0, :metric1).new(metrics[0], metrics[1]) |
| 35 | + |
| 36 | + _(exporter.export(enumerable)).must_equal export::SUCCESS |
| 37 | + end |
| 38 | + |
| 39 | + it 'outputs to console on export (stdout)' do |
| 40 | + exporter.export(metrics) |
| 41 | + |
| 42 | + _(captured_stdout.string).must_match(/#<struct OpenTelemetry::SDK::Metrics::State::MetricData/) |
| 43 | + end |
| 44 | + |
| 45 | + it 'outputs to console using pull (stdout)' do |
| 46 | + OpenTelemetry::SDK.configure |
| 47 | + OpenTelemetry.meter_provider.add_metric_reader(exporter) |
| 48 | + meter = OpenTelemetry.meter_provider.meter('test') |
| 49 | + counter = meter.create_counter('counter', unit: 'smidgen', description: 'a small amount of something') |
| 50 | + |
| 51 | + counter.add(1) |
| 52 | + counter.add(2, attributes: { 'a' => 'b' }) |
| 53 | + counter.add(2, attributes: { 'a' => 'b' }) |
| 54 | + counter.add(3, attributes: { 'b' => 'c' }) |
| 55 | + counter.add(4, attributes: { 'd' => 'e' }) |
| 56 | + exporter.pull |
| 57 | + |
| 58 | + output = captured_stdout.string |
| 59 | + |
| 60 | + _(output).wont_be_empty |
| 61 | + _(output).must_match(/name="counter"/) |
| 62 | + _(output).must_match(/unit="smidgen"/) |
| 63 | + _(output).must_match(/description="a small amount of something"/) |
| 64 | + _(output).must_match(/OpenTelemetry::SDK::InstrumentationScope name="test"/) |
| 65 | + _(output).must_match(/#<struct OpenTelemetry::SDK::Metrics::Aggregation::NumberDataPoint/) |
| 66 | + end |
| 67 | + |
| 68 | + it 'accepts calls to #force_flush' do |
| 69 | + exporter.force_flush |
| 70 | + end |
| 71 | + |
| 72 | + it 'accepts calls to #shutdown' do |
| 73 | + exporter.shutdown |
| 74 | + end |
| 75 | + |
| 76 | + it 'fails to export after shutdown' do |
| 77 | + exporter.shutdown |
| 78 | + |
| 79 | + _(exporter.export(metrics)).must_equal export::FAILURE |
| 80 | + end |
| 81 | + end |
| 82 | +end |
0 commit comments