Skip to content

Commit f7737fd

Browse files
feat: Create ConsoleMetricPullExporter (#1547)
Co-authored-by: Robert <[email protected]>
1 parent 540b77e commit f7737fd

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

metrics_sdk/lib/opentelemetry/sdk/metrics/export.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ module Export
2525

2626
require 'opentelemetry/sdk/metrics/export/metric_reader'
2727
require 'opentelemetry/sdk/metrics/export/in_memory_metric_pull_exporter'
28+
require 'opentelemetry/sdk/metrics/export/console_metric_pull_exporter'
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
module OpenTelemetry
8+
module SDK
9+
module Metrics
10+
module Export
11+
# Outputs {MetricData} to the console
12+
#
13+
# Potentially useful for exploratory purposes.
14+
class ConsoleMetricPullExporter < MetricReader
15+
def initialize
16+
super
17+
@stopped = false
18+
end
19+
20+
def pull
21+
export(collect)
22+
end
23+
24+
def export(metrics, timeout: nil)
25+
return FAILURE if @stopped
26+
27+
Array(metrics).each { |metric| pp metric }
28+
29+
SUCCESS
30+
end
31+
32+
def force_flush(timeout: nil)
33+
SUCCESS
34+
end
35+
36+
def shutdown(timeout: nil)
37+
@stopped = true
38+
SUCCESS
39+
end
40+
end
41+
end
42+
end
43+
end
44+
end
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)