diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/export.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/export.rb index 37716c801d..64a038f819 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/export.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/export.rb @@ -25,3 +25,4 @@ module Export require 'opentelemetry/sdk/metrics/export/metric_reader' require 'opentelemetry/sdk/metrics/export/in_memory_metric_pull_exporter' +require 'opentelemetry/sdk/metrics/export/console_metric_pull_exporter' diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/export/console_metric_pull_exporter.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/export/console_metric_pull_exporter.rb new file mode 100644 index 0000000000..8264c8e2e0 --- /dev/null +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/export/console_metric_pull_exporter.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +module OpenTelemetry + module SDK + module Metrics + module Export + # Outputs {MetricData} to the console + # + # Potentially useful for exploratory purposes. + class ConsoleMetricPullExporter < MetricReader + def initialize + super + @stopped = false + end + + def pull + export(collect) + end + + def export(metrics, timeout: nil) + return FAILURE if @stopped + + Array(metrics).each { |metric| pp metric } + + SUCCESS + end + + def force_flush(timeout: nil) + SUCCESS + end + + def shutdown(timeout: nil) + @stopped = true + SUCCESS + end + end + end + end + end +end diff --git a/metrics_sdk/test/integration/console_metric_pull_exporter.rb b/metrics_sdk/test/integration/console_metric_pull_exporter.rb new file mode 100644 index 0000000000..57f8fb1e90 --- /dev/null +++ b/metrics_sdk/test/integration/console_metric_pull_exporter.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require_relative '../test_helper' + +describe OpenTelemetry::SDK do + describe '#configure' do + export = OpenTelemetry::SDK::Metrics::Export + + let(:captured_stdout) { StringIO.new } + let(:metric_data1) { OpenTelemetry::SDK::Metrics::State::MetricData.new({ name: 'name1' }) } + let(:metric_data2) { OpenTelemetry::SDK::Metrics::State::MetricData.new({ name: 'name2' }) } + let(:metrics) { [metric_data1, metric_data2] } + let(:exporter) { export::ConsoleMetricPullExporter.new } + + before do + reset_metrics_sdk + @original_stdout = $stdout + $stdout = captured_stdout + end + + after do + $stdout = @original_stdout + end + + it 'accepts an Array of MetricData as arg to #export and succeeds' do + _(exporter.export(metrics)).must_equal export::SUCCESS + end + + it 'accepts an Enumerable of MetricData as arg to #export and succeeds' do + enumerable = Struct.new(:metric0, :metric1).new(metrics[0], metrics[1]) + + _(exporter.export(enumerable)).must_equal export::SUCCESS + end + + it 'outputs to console on export (stdout)' do + exporter.export(metrics) + + _(captured_stdout.string).must_match(/# 'b' }) + counter.add(2, attributes: { 'a' => 'b' }) + counter.add(3, attributes: { 'b' => 'c' }) + counter.add(4, attributes: { 'd' => 'e' }) + exporter.pull + + output = captured_stdout.string + + _(output).wont_be_empty + _(output).must_match(/name="counter"/) + _(output).must_match(/unit="smidgen"/) + _(output).must_match(/description="a small amount of something"/) + _(output).must_match(/OpenTelemetry::SDK::InstrumentationScope name="test"/) + _(output).must_match(/#