Skip to content

Commit

Permalink
feat: Create ConsoleMetricPullExporter (#1547)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert <robertlaurin@users.noreply.github.com>
  • Loading branch information
kaylareopelle and robertlaurin committed Feb 6, 2024
1 parent 540b77e commit f7737fd
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions metrics_sdk/lib/opentelemetry/sdk/metrics/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Original file line number Diff line number Diff line change
@@ -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
82 changes: 82 additions & 0 deletions metrics_sdk/test/integration/console_metric_pull_exporter.rb
Original file line number Diff line number Diff line change
@@ -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(/#<struct OpenTelemetry::SDK::Metrics::State::MetricData/)
end

it 'outputs to console using pull (stdout)' do
OpenTelemetry::SDK.configure
OpenTelemetry.meter_provider.add_metric_reader(exporter)
meter = OpenTelemetry.meter_provider.meter('test')
counter = meter.create_counter('counter', unit: 'smidgen', description: 'a small amount of something')

counter.add(1)
counter.add(2, attributes: { 'a' => '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(/#<struct OpenTelemetry::SDK::Metrics::Aggregation::NumberDataPoint/)
end

it 'accepts calls to #force_flush' do
exporter.force_flush
end

it 'accepts calls to #shutdown' do
exporter.shutdown
end

it 'fails to export after shutdown' do
exporter.shutdown

_(exporter.export(metrics)).must_equal export::FAILURE
end
end
end

0 comments on commit f7737fd

Please sign in to comment.