-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75a4885
commit ee5c13a
Showing
9 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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<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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/gauge.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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: {}) | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
metrics_sdk/test/opentelemetry/sdk/metrics/instrument/gauge_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |