-
Notifications
You must be signed in to change notification settings - Fork 277
feat: add synchronous gauge #1718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ee5c13a
e5f3bae
df10abf
71ebe5b
d0f510a
80ebbf8
d7e6b54
e0e373a
9078b84
b9642e8
c4ea8d0
dc1e4c9
a93ffdc
19d0d05
6ef760e
20fb6a5
5fa1a2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 Gauge. | ||
| class Gauge | ||
| # Record the current value for the Gauge | ||
| # | ||
| # @param [Numeric] amount The current absolute value. | ||
| # @param [Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}] 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # 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 | ||
|
|
||
| # Record the absolute value of the Gauge. | ||
| # | ||
| # @param [numeric] value The current absolute value. | ||
| # @param [Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}] 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(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 | ||
|
Comment on lines
+28
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we turn this TODO into an issue, or reference it on an existing issue?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I will create an issue to track it (e.g. something like integrate metrics_sdk to sdk?)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just seeing this! Yes, I think something like that would work and add a link to this code for reference. This is a good example for a similar TODO for logs: #1739 |
||
| update(value, attributes) | ||
| nil | ||
| rescue StandardError => e | ||
| OpenTelemetry.handle_error(exception: e) | ||
| nil | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def default_aggregation | ||
| OpenTelemetry::SDK::Metrics::Aggregation::LastValue.new | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # 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_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 | ||
|
|
||
| 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 'separate gauge should record their own last value' 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 |
Uh oh!
There was an error while loading. Please reload this page.