Skip to content

Commit 9c80c40

Browse files
authored
feat: add exponential histogram in otlp metrics exporter (#1851)
1 parent 4621c02 commit 9c80c40

File tree

3 files changed

+96
-10
lines changed

3 files changed

+96
-10
lines changed

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

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,29 +246,50 @@ def as_otlp_metrics(metrics)
246246
)
247247

248248
when :histogram
249+
histogram_data_point(metrics)
250+
251+
end
252+
end
253+
254+
def as_otlp_aggregation_temporality(type)
255+
case type
256+
when :delta then Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_DELTA
257+
when :cumulative then Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_CUMULATIVE
258+
else Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_UNSPECIFIED
259+
end
260+
end
261+
262+
def histogram_data_point(metrics)
263+
return if metrics.data_points.empty?
264+
265+
if metrics.data_points.first.instance_of?(OpenTelemetry::SDK::Metrics::Aggregation::ExponentialHistogramDataPoint)
266+
Opentelemetry::Proto::Metrics::V1::Metric.new(
267+
name: metrics.name,
268+
description: metrics.description,
269+
unit: metrics.unit,
270+
exponential_histogram: Opentelemetry::Proto::Metrics::V1::ExponentialHistogram.new(
271+
aggregation_temporality: as_otlp_aggregation_temporality(metrics.aggregation_temporality),
272+
data_points: metrics.data_points.map do |ehdp|
273+
exponential_histogram_data_point(ehdp)
274+
end
275+
)
276+
)
277+
elsif metrics.data_points.first.instance_of?(OpenTelemetry::SDK::Metrics::Aggregation::HistogramDataPoint)
249278
Opentelemetry::Proto::Metrics::V1::Metric.new(
250279
name: metrics.name,
251280
description: metrics.description,
252281
unit: metrics.unit,
253282
histogram: Opentelemetry::Proto::Metrics::V1::Histogram.new(
254283
aggregation_temporality: as_otlp_aggregation_temporality(metrics.aggregation_temporality),
255284
data_points: metrics.data_points.map do |hdp|
256-
histogram_data_point(hdp)
285+
explicit_histogram_data_point(hdp)
257286
end
258287
)
259288
)
260289
end
261290
end
262291

263-
def as_otlp_aggregation_temporality(type)
264-
case type
265-
when :delta then Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_DELTA
266-
when :cumulative then Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_CUMULATIVE
267-
else Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_UNSPECIFIED
268-
end
269-
end
270-
271-
def histogram_data_point(hdp)
292+
def explicit_histogram_data_point(hdp)
272293
Opentelemetry::Proto::Metrics::V1::HistogramDataPoint.new(
273294
attributes: hdp.attributes.map { |k, v| as_otlp_key_value(k, v) },
274295
start_time_unix_nano: hdp.start_time_unix_nano,
@@ -283,6 +304,31 @@ def histogram_data_point(hdp)
283304
)
284305
end
285306

307+
def exponential_histogram_data_point(ehdp)
308+
Opentelemetry::Proto::Metrics::V1::ExponentialHistogramDataPoint.new(
309+
attributes: ehdp.attributes.map { |k, v| as_otlp_key_value(k, v) },
310+
start_time_unix_nano: ehdp.start_time_unix_nano,
311+
time_unix_nano: ehdp.time_unix_nano,
312+
count: ehdp.count,
313+
sum: ehdp.sum,
314+
scale: ehdp.scale,
315+
zero_count: ehdp.zero_count,
316+
positive: Opentelemetry::Proto::Metrics::V1::ExponentialHistogramDataPoint::Buckets.new(
317+
offset: ehdp.positive.offset,
318+
bucket_counts: ehdp.positive.counts
319+
),
320+
negative: Opentelemetry::Proto::Metrics::V1::ExponentialHistogramDataPoint::Buckets.new(
321+
offset: ehdp.negative.offset,
322+
bucket_counts: ehdp.negative.counts
323+
),
324+
flags: ehdp.flags,
325+
exemplars: ehdp.exemplars,
326+
min: ehdp.min,
327+
max: ehdp.max,
328+
zero_threshold: ehdp.zero_threshold
329+
)
330+
end
331+
286332
def number_data_point(ndp)
287333
args = {
288334
attributes: ndp.attributes.map { |k, v| as_otlp_key_value(k, v) },

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,11 @@
609609
gauge = meter.create_gauge('test_gauge', unit: 'smidgen', description: 'a small amount of something')
610610
gauge.record(15, attributes: { 'baz' => 'qux' })
611611

612+
meter_provider.add_view('*exponential*', aggregation: OpenTelemetry::SDK::Metrics::Aggregation::ExponentialBucketHistogram.new(max_scale: 20), type: :histogram, unit: 'smidgen')
613+
614+
exponential_histogram = meter.create_histogram('test_exponential_histogram', unit: 'smidgen', description: 'a small amount of something')
615+
exponential_histogram.record(20, attributes: { 'lox' => 'xol' })
616+
612617
exporter.pull
613618
meter_provider.shutdown
614619

@@ -711,6 +716,40 @@
711716
)
712717
]
713718
)
719+
),
720+
Opentelemetry::Proto::Metrics::V1::Metric.new(
721+
name: 'test_exponential_histogram',
722+
description: 'a small amount of something',
723+
unit: 'smidgen',
724+
exponential_histogram: Opentelemetry::Proto::Metrics::V1::ExponentialHistogram.new(
725+
data_points: [
726+
Opentelemetry::Proto::Metrics::V1::ExponentialHistogramDataPoint.new(
727+
attributes: [
728+
Opentelemetry::Proto::Common::V1::KeyValue.new(key: 'lox', value: Opentelemetry::Proto::Common::V1::AnyValue.new(string_value: 'xol'))
729+
],
730+
start_time_unix_nano: 1_699_593_427_329_946_585,
731+
time_unix_nano: 1_699_593_427_329_946_586,
732+
count: 1,
733+
sum: 20,
734+
scale: 20,
735+
zero_count: 0,
736+
positive: Opentelemetry::Proto::Metrics::V1::ExponentialHistogramDataPoint::Buckets.new(
737+
offset: 4_531_870,
738+
bucket_counts: [1]
739+
),
740+
negative: Opentelemetry::Proto::Metrics::V1::ExponentialHistogramDataPoint::Buckets.new(
741+
offset: 0,
742+
bucket_counts: [0]
743+
),
744+
flags: 0,
745+
exemplars: nil,
746+
min: 20,
747+
max: 20,
748+
zero_threshold: 0
749+
)
750+
],
751+
aggregation_temporality: Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_DELTA
752+
)
714753
)
715754
]
716755
)

exporter/otlp-metrics/test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def collect(start_time, end_time, data_points)
2727
OpenTelemetry::SDK::Metrics::Aggregation::Sum.prepend(MockSum)
2828
OpenTelemetry::SDK::Metrics::Aggregation::ExplicitBucketHistogram.prepend(MockSum)
2929
OpenTelemetry::SDK::Metrics::Aggregation::LastValue.prepend(MockSum)
30+
OpenTelemetry::SDK::Metrics::Aggregation::ExponentialBucketHistogram.prepend(MockSum)
3031

3132
def create_metrics_data(name: '', description: '', unit: '', instrument_kind: :counter, resource: nil,
3233
instrumentation_scope: OpenTelemetry::SDK::InstrumentationScope.new('', 'v0.0.1'),

0 commit comments

Comments
 (0)