From a93ffdcf378dc03b01064759e157cc0f6a6d2040 Mon Sep 17 00:00:00 2001 From: xuan-cao-swi Date: Mon, 4 Nov 2024 12:05:28 -0500 Subject: [PATCH] change aggr to last value and update test --- .../sdk/metrics/instrument/gauge.rb | 2 +- .../sdk/metrics/instrument/gauge_test.rb | 27 ++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb index 98d748330..cedb6fc98 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb @@ -39,7 +39,7 @@ def record(value, attributes: {}) # TODO: replace the default aggregation to LastValue def default_aggregation - OpenTelemetry::SDK::Metrics::Aggregation::Sum.new + OpenTelemetry::SDK::Metrics::Aggregation::LastValue.new end end end diff --git a/metrics_sdk/test/opentelemetry/sdk/metrics/instrument/gauge_test.rb b/metrics_sdk/test/opentelemetry/sdk/metrics/instrument/gauge_test.rb index a648e7893..791d2b696 100644 --- a/metrics_sdk/test/opentelemetry/sdk/metrics/instrument/gauge_test.rb +++ b/metrics_sdk/test/opentelemetry/sdk/metrics/instrument/gauge_test.rb @@ -20,7 +20,7 @@ it 'gauge should count -2' do gauge.record(-2, attributes: { 'foo' => 'bar' }) metric_exporter.pull - last_snapshot = metric_exporter.metric_snapshots.last + last_snapshot = metric_exporter.metric_snapshots _(last_snapshot[0].name).must_equal('gauge') _(last_snapshot[0].unit).must_equal('smidgen') @@ -30,4 +30,29 @@ _(last_snapshot[0].data_points[0].value).must_equal(-2) _(last_snapshot[0].aggregation_temporality).must_equal(:delta) end + + it 'gauge should count 1 for last recording' do + gauge.record(-2, attributes: { 'foo' => 'bar' }) + gauge.record(1, attributes: { 'foo' => 'bar' }) + metric_exporter.pull + last_snapshot = metric_exporter.metric_snapshots + + _(last_snapshot.size).must_equal(1) + _(last_snapshot[0].data_points.size).must_equal(1) + _(last_snapshot[0].data_points[0].value).must_equal(1) + end + + it 'gauge should count 1 for last recording' do + gauge.record(-2, attributes: { 'foo' => 'bar' }) + gauge.record(1, attributes: { 'foo' => 'bar' }) + gauge2 = meter.create_gauge('gauge2', unit: 'smidgen', description: 'a small amount of something') + gauge2.record(10, attributes: {}) + + metric_exporter.pull + last_snapshot = metric_exporter.metric_snapshots + + _(last_snapshot.size).must_equal(2) + _(last_snapshot[0].data_points[0].value).must_equal(1) + _(last_snapshot[1].data_points[0].value).must_equal(10) + end end