Skip to content

Commit 6168e9c

Browse files
feat: include async instrument for otlp exporter (#1855)
* feat: init update async instrument * feat: asynchronous inst - using meter for multi instrument callback registeration * feat: asyc instrument - lint and allow setting default attributes for instrument * feat: asyc instrument - separate callback from instrument init and after init * feat: asyc - revision * lint * update metrics_api * fix: include async in otlp exporter * lint * Apply suggestions from code review Co-authored-by: Kayla Reopelle <[email protected]> * merge --------- Co-authored-by: Kayla Reopelle <[email protected]>
1 parent 5ada394 commit 6168e9c

File tree

7 files changed

+89
-7
lines changed

7 files changed

+89
-7
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
require 'bundler/inline'
4+
5+
gemfile(true) do
6+
source 'https://rubygems.org'
7+
gem "opentelemetry-api"
8+
gem "opentelemetry-common"
9+
gem "opentelemetry-sdk"
10+
11+
gem 'opentelemetry-metrics-api', path: '../../metrics_api'
12+
gem 'opentelemetry-metrics-sdk', path: '../../metrics_sdk'
13+
end
14+
15+
require 'opentelemetry/sdk'
16+
require 'opentelemetry-metrics-sdk'
17+
18+
# this example manually configures the exporter, turn off automatic configuration
19+
ENV['OTEL_METRICS_EXPORTER'] = 'none'
20+
21+
OpenTelemetry::SDK.configure
22+
23+
console_metric_exporter = OpenTelemetry::SDK::Metrics::Export::ConsoleMetricPullExporter.new
24+
25+
OpenTelemetry.meter_provider.add_metric_reader(console_metric_exporter)
26+
27+
meter = OpenTelemetry.meter_provider.meter("SAMPLE_METER_NAME")
28+
29+
recoverable_callback = proc {
30+
cpu_usage = `ps -p #{Process.pid} -o %cpu=`.strip.to_f
31+
cpu_usage
32+
}
33+
34+
ob_counter = meter.create_observable_counter('observable_counter', callback: recoverable_callback, unit: 'ms')
35+
36+
ob_counter.observe
37+
38+
OpenTelemetry.meter_provider.metric_readers.each(&:pull)
39+
OpenTelemetry.meter_provider.shutdown

exporter/otlp-metrics/lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def as_otlp_metrics(metrics)
231231
)
232232
)
233233

234-
when :counter, :up_down_counter
234+
when :counter, :up_down_counter, :observable_counter, :observable_up_down_counter
235235
Opentelemetry::Proto::Metrics::V1::Metric.new(
236236
name: metrics.name,
237237
description: metrics.description,

exporter/otlp-metrics/test/opentelemetry/exporter/otlp/metrics/metrics_exporter_test.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,20 @@
560560
assert_requested(stub_post)
561561
end
562562

563+
# when the exporter.pull, it will automatically invoke callback once;
564+
# if wants to record data (invoke callback) with attributes, do async_counter.observe(timeout, attributes)
565+
it 'exports an async metric' do
566+
stub_post = stub_request(:post, 'http://localhost:4318/v1/metrics').to_return(status: 200)
567+
meter_provider.add_metric_reader(exporter)
568+
meter = meter_provider.meter('test')
569+
pf_callback = proc { 10 }
570+
meter.create_observable_counter('test_async_counter', callback: pf_callback, unit: 'smidgen', description: 'a small amount of something')
571+
exporter.pull
572+
meter_provider.shutdown
573+
574+
assert_requested(stub_post)
575+
end
576+
563577
it 'compresses with gzip if enabled' do
564578
exporter = OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter.new(compression: 'gzip')
565579
stub_post = stub_request(:post, 'http://localhost:4318/v1/metrics').to_return do |request|
@@ -614,6 +628,10 @@
614628
exponential_histogram = meter.create_histogram('test_exponential_histogram', unit: 'smidgen', description: 'a small amount of something')
615629
exponential_histogram.record(20, attributes: { 'lox' => 'xol' })
616630

631+
pf_callback = proc { 30 }
632+
async_gauge = meter.create_observable_gauge('test_async_gauge', callback: pf_callback, unit: 'smidgen', description: 'a small amount of something')
633+
async_gauge.observe(attributes: { 'foo' => 'bar' })
634+
617635
exporter.pull
618636
meter_provider.shutdown
619637

@@ -750,6 +768,31 @@
750768
],
751769
aggregation_temporality: Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_DELTA
752770
)
771+
),
772+
Opentelemetry::Proto::Metrics::V1::Metric.new(
773+
name: 'test_async_gauge',
774+
description: 'a small amount of something',
775+
unit: 'smidgen',
776+
gauge: Opentelemetry::Proto::Metrics::V1::Gauge.new(
777+
data_points: [
778+
Opentelemetry::Proto::Metrics::V1::NumberDataPoint.new(
779+
attributes: [
780+
Opentelemetry::Proto::Common::V1::KeyValue.new(key: 'foo', value: Opentelemetry::Proto::Common::V1::AnyValue.new(string_value: 'bar'))
781+
],
782+
as_int: 30,
783+
start_time_unix_nano: 1_699_593_427_329_946_585,
784+
time_unix_nano: 1_699_593_427_329_946_586,
785+
exemplars: nil
786+
),
787+
Opentelemetry::Proto::Metrics::V1::NumberDataPoint.new(
788+
attributes: [],
789+
as_int: 30,
790+
start_time_unix_nano: 1_699_593_427_329_946_585,
791+
time_unix_nano: 1_699_593_427_329_946_586,
792+
exemplars: nil
793+
)
794+
]
795+
)
753796
)
754797
]
755798
)

metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/observable_counter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def instrument_kind
1818
:observable_counter
1919
end
2020

21-
# Observe the Counter with fixed timeout duration.
21+
# Observe the ObservableCounter with fixed timeout duration.
2222
#
2323
# @param [int] timeout The timeout duration for callback to run, which MUST be a non-negative numeric value.
2424
# @param [Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}] attributes
@@ -32,7 +32,7 @@ def observe(timeout: nil, attributes: {})
3232
private
3333

3434
def default_aggregation
35-
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new
35+
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(monotonic: true)
3636
end
3737
end
3838
end

metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/observable_gauge.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def observe(timeout: nil, attributes: {})
3232
private
3333

3434
def default_aggregation
35-
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new
35+
OpenTelemetry::SDK::Metrics::Aggregation::LastValue.new
3636
end
3737
end
3838
end

metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/observable_up_down_counter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def observe(timeout: nil, attributes: {})
3232
private
3333

3434
def default_aggregation
35-
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(aggregation_temporality: :delta)
35+
OpenTelemetry::SDK::Metrics::Aggregation::Sum.new(aggregation_temporality: :delta, monotonic: false)
3636
end
3737
end
3838
end

metrics_sdk/test/opentelemetry/sdk/metrics/instrument/observable_gauge_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
_(last_snapshot[0].instrumentation_scope.name).must_equal('test')
3030
_(last_snapshot[0].data_points[0].value).must_equal(10)
3131
_(last_snapshot[0].data_points[0].attributes).must_equal({})
32-
_(last_snapshot[0].aggregation_temporality).must_equal(:cumulative)
32+
_(last_snapshot[0].aggregation_temporality).must_equal(:delta)
3333
end
3434

3535
it 'counts with observe' do
@@ -49,6 +49,6 @@
4949

5050
_(last_snapshot[0].data_points[1].value).must_equal(10)
5151
_(last_snapshot[0].data_points[1].attributes).must_equal({})
52-
_(last_snapshot[0].aggregation_temporality).must_equal(:cumulative)
52+
_(last_snapshot[0].aggregation_temporality).must_equal(:delta)
5353
end
5454
end

0 commit comments

Comments
 (0)