-
Notifications
You must be signed in to change notification settings - Fork 277
feat: add basic implementation of asynchronous metrics #1610
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
Merged
kaylareopelle
merged 22 commits into
open-telemetry:main
from
xuan-cao-swi:metrics-asynchronous
Aug 6, 2025
Merged
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a736fe9
feat: init update async instrument
xuan-cao-swi 807e32c
Merge branch 'main' of github.com:xuan-cao-swi/opentelemetry-ruby int…
xuan-cao-swi f8c9566
Merge branch 'main' of github.com:xuan-cao-swi/opentelemetry-ruby int…
xuan-cao-swi 4111c98
feat: asynchronous inst - using meter for multi instrument callback r…
xuan-cao-swi 71eda00
feat: asyc instrument - lint and allow setting default attributes for…
xuan-cao-swi 0f62d15
feat: asyc instrument - separate callback from instrument init and af…
xuan-cao-swi ee8fff5
Merge branch 'main' into metrics-asynchronous
xuan-cao-swi 5fdd84a
feat: asyc - revision
xuan-cao-swi ebdb289
merge and resolve conflict
xuan-cao-swi 0a92445
lint
xuan-cao-swi 5566e01
Merge branch 'main' into metrics-asynchronous
xuan-cao-swi f376b51
Merge branch 'main' into metrics-asynchronous
kaylareopelle b3c486b
Merge branch 'main' into metrics-asynchronous
xuan-cao-swi 5537615
update metrics_api
xuan-cao-swi 8c9194b
Update metrics_api/lib/opentelemetry/metrics/instrument/observable_ga…
xuan-cao-swi 09bd03a
Update metrics_api/lib/opentelemetry/metrics/instrument/observable_co…
xuan-cao-swi 5802b3b
Update metrics_api/lib/opentelemetry/metrics/instrument/observable_up…
xuan-cao-swi ffdc502
update doc
xuan-cao-swi 4cfd9e2
Update observable_gauge.rb
xuan-cao-swi 9008083
Update metrics_api/lib/opentelemetry/metrics/instrument/observable_up…
xuan-cao-swi 9e68670
Merge branch 'main' into metrics-asynchronous
kaylareopelle 49f059a
Merge branch 'main' into metrics-asynchronous
kaylareopelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
93 changes: 93 additions & 0 deletions
93
metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb
This file contains hidden or 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,93 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| module OpenTelemetry | ||
| module SDK | ||
| module Metrics | ||
| module Instrument | ||
| # {AsynchronousInstrument} contains the common functionality shared across | ||
| # the asynchronous instruments SDK instruments. | ||
| class AsynchronousInstrument | ||
| def initialize(name, unit, description, callback, instrumentation_scope, meter_provider) | ||
| @name = name | ||
| @unit = unit | ||
| @description = description | ||
| @instrumentation_scope = instrumentation_scope | ||
| @meter_provider = meter_provider | ||
| @metric_streams = [] | ||
| @callbacks = [] | ||
| @timeout = nil | ||
| @attributes = {} | ||
|
|
||
| init_callback(callback) | ||
| meter_provider.register_asynchronous_instrument(self) | ||
| end | ||
|
|
||
| # @api private | ||
| def register_with_new_metric_store(metric_store, aggregation: default_aggregation) | ||
| ms = OpenTelemetry::SDK::Metrics::State::AsynchronousMetricStream.new( | ||
| @name, | ||
| @description, | ||
| @unit, | ||
| instrument_kind, | ||
| @meter_provider, | ||
| @instrumentation_scope, | ||
| aggregation, | ||
| @callbacks, | ||
| @timeout, | ||
| @attributes | ||
| ) | ||
| @metric_streams << ms | ||
| metric_store.add_metric_stream(ms) | ||
| end | ||
|
|
||
| # The API MUST support creation of asynchronous instruments by passing zero or more callback functions | ||
| # to be permanently registered to the newly created instrument. | ||
| def init_callback(callback) | ||
| if callback.instance_of?(Proc) | ||
| @callbacks << callback | ||
| elsif callback.instance_of?(Array) | ||
| callback.each { |cb| @callbacks << cb if cb.instance_of?(Proc) } | ||
| else | ||
| OpenTelemetry.logger.warn "Only accept single Proc or Array of Proc for initialization with callback (given callback #{callback.class}" | ||
| end | ||
| end | ||
|
|
||
| # Where the API supports registration of callback functions after asynchronous instrumentation creation, | ||
| # the user MUST be able to undo registration of the specific callback after its registration by some means. | ||
| def register_callback(callback) | ||
| if callback.instance_of?(Proc) | ||
| @callbacks << callback | ||
| callback | ||
| else | ||
| OpenTelemetry.logger.warn "Only accept single Proc for registering callback (given callback #{callback.class}" | ||
| end | ||
| end | ||
|
|
||
| def unregister(callback) | ||
| @callbacks.delete(callback) | ||
| end | ||
|
|
||
| def timeout(timeout) | ||
| @timeout = timeout | ||
| end | ||
|
|
||
| def add_attributes(attributes) | ||
| @attributes.merge!(attributes) if attributes.instance_of?(Hash) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # update the observed value (after calling observe) | ||
| # invoke callback will execute callback and export metric_data that is observed | ||
| def update(timeout, attributes) | ||
| @metric_streams.each { |ms| ms.invoke_callback(timeout, attributes) } | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.