Skip to content

Ability to find metrics instruments after they are created #1896

@wsmoak

Description

@wsmoak

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 open-telemetry/opentelemetry-specification#2232 .

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    To do

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions