opentelemetry-ruby icon indicating copy to clipboard operation
opentelemetry-ruby copied to clipboard

Ability to find metrics instruments after they are created

Open wsmoak opened this issue 4 months ago • 7 comments

Currently the SDK supports create methods like create_counter and create_observable_gauge.

Once created however, it seems there is no way to retrieve them. You must keep a reference to the instrument if you want to call add or record on it from other places in your application.

Assuming I do not want to use global variables, this results in having to manage my own instrument registry. Example: https://github.com/wsmoak/rails-otel-demo/blob/2c72d4538e5bb314a860b9b512fd4d3c6b32eabe/lib/rails_otel_demo/metrics.rb#L1-L13

module RailsOTelDemo
  class Metrics

    OTEL_METER = OpenTelemetry.meter_provider.meter('rails-otel-demo-meter')

    def self.instrument_registry
      @instrument_registry ||= {}
    end

    def self.find_instrument(instrument_name)
      instrument_registry[instrument_name] || raise("Instrument '#{instrument_name}' not found")
    end

The Meter already has its own instrument registry, it would be nice to have a find method in addition to create.

Related: it really should be possible to remove instruments as well. This is being discussed in https://github.com/open-telemetry/opentelemetry-specification/issues/2232 .

wsmoak avatar Aug 17 '25 13:08 wsmoak

Great callout. I've run into this problem myself, and I'm really not sure how other languages handle it or what the best practices are. The spec doesn't define an API like this, and I'm not sure why.

I'm going to ask around to see what solutions other languages have come up with. The next step might be proposing a spec change.

You could access the meter's instrumentation registry directly as a hacky workaround. For example, if the metric was http.server.request.duration:

OTEL_METER.instance_variable_get(:@instrument_registry)['http.server.request.duration']

I'm not necessarily recommending this, so if you choose to use it, do so with caution.

kaylareopelle avatar Aug 18 '25 23:08 kaylareopelle

I started a thread in #otel-devex to ask about this. Or how/where to ask.

wsmoak avatar Aug 24 '25 14:08 wsmoak

👋 This issue has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the keep label to hold stale off permanently, or do nothing. If you do nothing this issue will be closed eventually by the stale bot.

github-actions[bot] avatar Sep 24 '25 02:09 github-actions[bot]

👋 This issue has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the keep label to hold stale off permanently, or do nothing. If you do nothing this issue will be closed eventually by the stale bot.

github-actions[bot] avatar Oct 31 '25 02:10 github-actions[bot]

@wsmoak - I forget where were are with this one. Did you reach out to the specification SIG/repo?

kaylareopelle avatar Oct 31 '25 20:10 kaylareopelle

I wanted to see if I could solve it with the existing SDK -- I remembered there is something about 'duplicate' instruments in the spec and was hoping that we had just implemented a 'find or create'.

But no, we are currently replacing the instrument if you create a duplicate. There's a warning message logged.

https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#duplicate-instrument-registration

It is unspecified whether or under which conditions the same or different Instrument instance will be returned as a result of duplicate instrument registration. The term identical applied to Instruments describes instances where all identifying fields are equal. The term distinct applied to Instruments describes instances where at least one field value is different.

To accommodate the recommendations from the data model, the SDK MUST aggregate data from identical Instruments together in its export pipeline.

As it's unspecified it seems like we could just return the existing instrument if you try to create duplicate.

wsmoak avatar Nov 02 '25 17:11 wsmoak

I have started a thread in #otel-specification https://cloud-native.slack.com/archives/C01N7PP1THC/p1762103945922389

wsmoak avatar Nov 02 '25 17:11 wsmoak