From ee5c13ac80bc6867f45178b8b5186d1cc02b3969 Mon Sep 17 00:00:00 2001 From: xuan-cao-swi Date: Wed, 4 Sep 2024 15:08:40 -0400 Subject: [PATCH 01/10] feat: add gauge --- .../internal/proxy_instrument.rb | 2 +- .../lib/opentelemetry/internal/proxy_meter.rb | 1 + .../lib/opentelemetry/metrics/instrument.rb | 1 + .../opentelemetry/metrics/instrument/gauge.rb | 23 +++++++++ .../lib/opentelemetry/metrics/meter.rb | 7 ++- .../opentelemetry/sdk/metrics/instrument.rb | 1 + .../sdk/metrics/instrument/gauge.rb | 48 +++++++++++++++++++ .../lib/opentelemetry/sdk/metrics/meter.rb | 1 + .../sdk/metrics/instrument/gauge_test.rb | 33 +++++++++++++ 9 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb create mode 100644 metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb create mode 100644 metrics_sdk/test/opentelemetry/sdk/metrics/instrument/gauge_test.rb diff --git a/metrics_api/lib/opentelemetry/internal/proxy_instrument.rb b/metrics_api/lib/opentelemetry/internal/proxy_instrument.rb index 117a400778..124b434f06 100644 --- a/metrics_api/lib/opentelemetry/internal/proxy_instrument.rb +++ b/metrics_api/lib/opentelemetry/internal/proxy_instrument.rb @@ -19,7 +19,7 @@ def initialize(kind, name, unit, desc, callable) def upgrade_with(meter) @delegate = case @kind - when :counter, :histogram, :up_down_counter + when :counter, :histogram, :up_down_counter, :gauge meter.send("create_#{@kind}", @name, unit: @unit, description: @desc) when :observable_counter, :observable_gauge, :observable_up_down_counter meter.send("create_#{@kind}", @name, unit: @unit, description: @desc, callback: @callback) diff --git a/metrics_api/lib/opentelemetry/internal/proxy_meter.rb b/metrics_api/lib/opentelemetry/internal/proxy_meter.rb index 74d497c684..9f44b4bb67 100644 --- a/metrics_api/lib/opentelemetry/internal/proxy_meter.rb +++ b/metrics_api/lib/opentelemetry/internal/proxy_meter.rb @@ -45,6 +45,7 @@ def create_instrument(kind, name, unit, description, callback) case kind when :counter then @delegate.create_counter(name, unit: unit, description: description) when :histogram then @delegate.create_histogram(name, unit: unit, description: description) + when :gauge then @delegate.create_gauge(name, unit: unit, description: description) when :up_down_counter then @delegate.create_up_down_counter(name, unit: unit, description: description) when :observable_counter then @delegate.create_observable_counter(name, unit: unit, description: description, callback: callback) when :observable_gauge then @delegate.create_observable_gauge(name, unit: unit, description: description, callback: callback) diff --git a/metrics_api/lib/opentelemetry/metrics/instrument.rb b/metrics_api/lib/opentelemetry/metrics/instrument.rb index 3782a8420c..b40f31bc2d 100644 --- a/metrics_api/lib/opentelemetry/metrics/instrument.rb +++ b/metrics_api/lib/opentelemetry/metrics/instrument.rb @@ -6,6 +6,7 @@ require 'opentelemetry/metrics/instrument/counter' require 'opentelemetry/metrics/instrument/histogram' +require 'opentelemetry/metrics/instrument/gauge' require 'opentelemetry/metrics/instrument/observable_counter' require 'opentelemetry/metrics/instrument/observable_gauge' require 'opentelemetry/metrics/instrument/observable_up_down_counter' diff --git a/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb b/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb new file mode 100644 index 0000000000..84f5cb5f2a --- /dev/null +++ b/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +module OpenTelemetry + module Metrics + module Instrument + # No-op implementation of UpDownCounter. + class Gauge + # Increment or decrement the UpDownCounter by a fixed amount. + # + # @param [Numeric] amount The amount to be added, can be positive, negative or zero. + # @param [Hash{String => String, Numeric, Boolean, Array}] attributes + # Values must be non-nil and (array of) string, boolean or numeric type. + # Array values must not contain nil elements and all elements must be of + # the same basic type (string, numeric, boolean). + def record(amount, attributes: {}); end + end + end + end +end diff --git a/metrics_api/lib/opentelemetry/metrics/meter.rb b/metrics_api/lib/opentelemetry/metrics/meter.rb index 1335437c02..bfa07c1a5d 100644 --- a/metrics_api/lib/opentelemetry/metrics/meter.rb +++ b/metrics_api/lib/opentelemetry/metrics/meter.rb @@ -11,13 +11,14 @@ class Meter COUNTER = Instrument::Counter.new OBSERVABLE_COUNTER = Instrument::ObservableCounter.new HISTOGRAM = Instrument::Histogram.new + GAUGE = Instrument::Gauge.new OBSERVABLE_GAUGE = Instrument::ObservableGauge.new UP_DOWN_COUNTER = Instrument::UpDownCounter.new OBSERVABLE_UP_DOWN_COUNTER = Instrument::ObservableUpDownCounter.new NAME_REGEX = /\A[a-zA-Z][-.\w]{0,62}\z/ - private_constant(:COUNTER, :OBSERVABLE_COUNTER, :HISTOGRAM, :OBSERVABLE_GAUGE, :UP_DOWN_COUNTER, :OBSERVABLE_UP_DOWN_COUNTER) + private_constant(:COUNTER, :OBSERVABLE_COUNTER, :HISTOGRAM, :GAUGE, :OBSERVABLE_GAUGE, :UP_DOWN_COUNTER, :OBSERVABLE_UP_DOWN_COUNTER) DuplicateInstrumentError = Class.new(OpenTelemetry::Error) InstrumentNameError = Class.new(OpenTelemetry::Error) @@ -37,6 +38,10 @@ def create_histogram(name, unit: nil, description: nil) create_instrument(:histogram, name, unit, description, nil) { HISTOGRAM } end + def create_gauge(name, unit: nil, description: nil) + create_instrument(:gauge, name, unit, description, nil) { GAUGE } + end + def create_up_down_counter(name, unit: nil, description: nil) create_instrument(:up_down_counter, name, unit, description, nil) { UP_DOWN_COUNTER } end diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument.rb index c440634c8c..d137cfbdd4 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument.rb @@ -20,3 +20,4 @@ module Instrument require 'opentelemetry/sdk/metrics/instrument/observable_gauge' require 'opentelemetry/sdk/metrics/instrument/observable_up_down_counter' require 'opentelemetry/sdk/metrics/instrument/up_down_counter' +require 'opentelemetry/sdk/metrics/instrument/gauge' diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb new file mode 100644 index 0000000000..9a577b74e7 --- /dev/null +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +module OpenTelemetry + module SDK + module Metrics + module Instrument + # {Gauge} is the SDK implementation of {OpenTelemetry::Metrics::Gauge}. + class Gauge < OpenTelemetry::SDK::Metrics::Instrument::SynchronousInstrument + # Returns the instrument kind as a Symbol + # + # @return [Symbol] + def instrument_kind + :gauge + end + + # Increment or decremt the Gauge by a fixed amount. + # + # @param [numeric] increment The increment amount, which MUST be a non-negative numeric value. + # @param [Hash{String => String, Numeric, Boolean, Array}] attributes + # Values must be non-nil and (array of) string, boolean or numeric type. + # Array values must not contain nil elements and all elements must be of + # the same basic type (string, numeric, boolean). + def record(amount, attributes: {}) + # TODO: When the metrics SDK stabilizes and is merged into the main SDK, + # we can leverage the SDK Internal validation classes to enforce this: + # https://github.com/open-telemetry/opentelemetry-ruby/blob/6bec625ef49004f364457c26263df421526b60d6/sdk/lib/opentelemetry/sdk/internal.rb#L47 + update(amount, attributes) + nil + rescue StandardError => e + OpenTelemetry.handle_error(exception: e) + nil + end + + private + + # TODO: replace the default aggregation to LastValue + def default_aggregation + OpenTelemetry::SDK::Metrics::Aggregation::Sum.new + end + end + end + end + end +end diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb index 8db3ea992c..5eb438c97d 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb @@ -38,6 +38,7 @@ def create_instrument(kind, name, unit, description, callback) case kind when :counter then OpenTelemetry::SDK::Metrics::Instrument::Counter.new(name, unit, description, @instrumentation_scope, @meter_provider) when :observable_counter then OpenTelemetry::SDK::Metrics::Instrument::ObservableCounter.new(name, unit, description, callback, @instrumentation_scope, @meter_provider) + when :gauge then OpenTelemetry::SDK::Metrics::Instrument::Gauge.new(name, unit, description, @instrumentation_scope, @meter_provider) when :histogram then OpenTelemetry::SDK::Metrics::Instrument::Histogram.new(name, unit, description, @instrumentation_scope, @meter_provider) when :observable_gauge then OpenTelemetry::SDK::Metrics::Instrument::ObservableGauge.new(name, unit, description, callback, @instrumentation_scope, @meter_provider) when :up_down_counter then OpenTelemetry::SDK::Metrics::Instrument::UpDownCounter.new(name, unit, description, @instrumentation_scope, @meter_provider) diff --git a/metrics_sdk/test/opentelemetry/sdk/metrics/instrument/gauge_test.rb b/metrics_sdk/test/opentelemetry/sdk/metrics/instrument/gauge_test.rb new file mode 100644 index 0000000000..a648e7893c --- /dev/null +++ b/metrics_sdk/test/opentelemetry/sdk/metrics/instrument/gauge_test.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require 'test_helper' + +describe OpenTelemetry::SDK::Metrics::Instrument::Gauge do + let(:metric_exporter) { OpenTelemetry::SDK::Metrics::Export::InMemoryMetricPullExporter.new } + let(:meter) { OpenTelemetry.meter_provider.meter('test') } + let(:gauge) { meter.create_gauge('gauge', unit: 'smidgen', description: 'a small amount of something') } + + before do + reset_metrics_sdk + OpenTelemetry::SDK.configure + OpenTelemetry.meter_provider.add_metric_reader(metric_exporter) + end + + it 'gauge should count -2' do + gauge.record(-2, attributes: { 'foo' => 'bar' }) + metric_exporter.pull + last_snapshot = metric_exporter.metric_snapshots.last + + _(last_snapshot[0].name).must_equal('gauge') + _(last_snapshot[0].unit).must_equal('smidgen') + _(last_snapshot[0].description).must_equal('a small amount of something') + _(last_snapshot[0].instrumentation_scope.name).must_equal('test') + _(last_snapshot[0].data_points[0].attributes).must_equal('foo' => 'bar') + _(last_snapshot[0].data_points[0].value).must_equal(-2) + _(last_snapshot[0].aggregation_temporality).must_equal(:delta) + end +end From df10abf62ee37ff0f82a02ec345409f539bf7af0 Mon Sep 17 00:00:00 2001 From: Xuan <112967240+xuan-cao-swi@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:42:31 -0400 Subject: [PATCH 02/10] Update metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> --- metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb b/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb index 84f5cb5f2a..0c504db5b9 100644 --- a/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb +++ b/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb @@ -7,7 +7,7 @@ module OpenTelemetry module Metrics module Instrument - # No-op implementation of UpDownCounter. + # No-op implementation of Gauge. class Gauge # Increment or decrement the UpDownCounter by a fixed amount. # From 71ebe5b187f5ef36e16167b4e5ec00a09174841e Mon Sep 17 00:00:00 2001 From: Xuan <112967240+xuan-cao-swi@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:42:37 -0400 Subject: [PATCH 03/10] Update metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> --- metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb b/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb index 0c504db5b9..23e2cf3d51 100644 --- a/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb +++ b/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb @@ -9,7 +9,7 @@ module Metrics module Instrument # No-op implementation of Gauge. class Gauge - # Increment or decrement the UpDownCounter by a fixed amount. + # Record the current value for the Gauge # # @param [Numeric] amount The amount to be added, can be positive, negative or zero. # @param [Hash{String => String, Numeric, Boolean, Array}] attributes From d0f510a3999662fde020e7194e9694867b96730e Mon Sep 17 00:00:00 2001 From: Xuan <112967240+xuan-cao-swi@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:42:42 -0400 Subject: [PATCH 04/10] Update metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> --- metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb b/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb index 23e2cf3d51..6e65f94946 100644 --- a/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb +++ b/metrics_api/lib/opentelemetry/metrics/instrument/gauge.rb @@ -11,7 +11,7 @@ module Instrument class Gauge # Record the current value for the Gauge # - # @param [Numeric] amount The amount to be added, can be positive, negative or zero. + # @param [Numeric] amount The current absolute value. # @param [Hash{String => String, Numeric, Boolean, Array}] attributes # Values must be non-nil and (array of) string, boolean or numeric type. # Array values must not contain nil elements and all elements must be of From 80ebbf8d7acf0c0e4c030035419480bbdb5aab12 Mon Sep 17 00:00:00 2001 From: Xuan <112967240+xuan-cao-swi@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:42:47 -0400 Subject: [PATCH 05/10] Update metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> --- metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb index 9a577b74e7..ba7a9e3c8b 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb @@ -19,7 +19,7 @@ def instrument_kind # Increment or decremt the Gauge by a fixed amount. # - # @param [numeric] increment The increment amount, which MUST be a non-negative numeric value. + # @param [numeric] value The current absolute value. # @param [Hash{String => String, Numeric, Boolean, Array}] attributes # Values must be non-nil and (array of) string, boolean or numeric type. # Array values must not contain nil elements and all elements must be of From d7e6b547861efdbfaed57816eb24ceba36884d2c Mon Sep 17 00:00:00 2001 From: Xuan <112967240+xuan-cao-swi@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:42:52 -0400 Subject: [PATCH 06/10] Update metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> --- metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb index ba7a9e3c8b..0103cdceff 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb @@ -24,7 +24,7 @@ def instrument_kind # Values must be non-nil and (array of) string, boolean or numeric type. # Array values must not contain nil elements and all elements must be of # the same basic type (string, numeric, boolean). - def record(amount, attributes: {}) + def record(value, attributes: {}) # TODO: When the metrics SDK stabilizes and is merged into the main SDK, # we can leverage the SDK Internal validation classes to enforce this: # https://github.com/open-telemetry/opentelemetry-ruby/blob/6bec625ef49004f364457c26263df421526b60d6/sdk/lib/opentelemetry/sdk/internal.rb#L47 From e0e373a63657eabb2adc676e4c767f2c05b0e77a Mon Sep 17 00:00:00 2001 From: Xuan <112967240+xuan-cao-swi@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:42:58 -0400 Subject: [PATCH 07/10] Update metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> --- metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb index 0103cdceff..bc3e119be6 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb @@ -17,7 +17,7 @@ def instrument_kind :gauge end - # Increment or decremt the Gauge by a fixed amount. + # Record the absolute value of the Gauge. # # @param [numeric] value The current absolute value. # @param [Hash{String => String, Numeric, Boolean, Array}] attributes From 9078b841814923f210d128250957f4dfef0f6f8d Mon Sep 17 00:00:00 2001 From: Xuan <112967240+xuan-cao-swi@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:43:03 -0400 Subject: [PATCH 08/10] Update metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> --- metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb index bc3e119be6..98d748330e 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb @@ -28,7 +28,7 @@ def record(value, attributes: {}) # TODO: When the metrics SDK stabilizes and is merged into the main SDK, # we can leverage the SDK Internal validation classes to enforce this: # https://github.com/open-telemetry/opentelemetry-ruby/blob/6bec625ef49004f364457c26263df421526b60d6/sdk/lib/opentelemetry/sdk/internal.rb#L47 - update(amount, attributes) + update(value, attributes) nil rescue StandardError => e OpenTelemetry.handle_error(exception: e) From b9642e87284a954031e358934c4120f03813d1a7 Mon Sep 17 00:00:00 2001 From: Xuan <112967240+xuan-cao-swi@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:48:10 -0400 Subject: [PATCH 09/10] create gauge api doc --- metrics_api/lib/opentelemetry/metrics/meter.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/metrics_api/lib/opentelemetry/metrics/meter.rb b/metrics_api/lib/opentelemetry/metrics/meter.rb index bfa07c1a5d..3421b64365 100644 --- a/metrics_api/lib/opentelemetry/metrics/meter.rb +++ b/metrics_api/lib/opentelemetry/metrics/meter.rb @@ -38,6 +38,20 @@ def create_histogram(name, unit: nil, description: nil) create_instrument(:histogram, name, unit, description, nil) { HISTOGRAM } end + # Gauge is an synchronous Instrument which reports non-additive value(s) + # + # With this api call: + # + # meter.create_gauge("cpu.frequency", + # description: "the real-time CPU clock speed", + # unit: "ms") + # + # + # @param name [String] the name of the gauge. + # @param unit [optional String] an optional string provided by user. + # @param description [optional String] an optional free-form text provided by user. + # + # @return [nil] after creation of gauge, it will be stored in instrument_registry def create_gauge(name, unit: nil, description: nil) create_instrument(:gauge, name, unit, description, nil) { GAUGE } end From a93ffdcf378dc03b01064759e157cc0f6a6d2040 Mon Sep 17 00:00:00 2001 From: xuan-cao-swi Date: Mon, 4 Nov 2024 12:05:28 -0500 Subject: [PATCH 10/10] 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 98d748330e..cedb6fc98e 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 a648e7893c..791d2b696f 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