Class: OpenTelemetry::SDK::Metrics::MeterProvider

Inherits:
Metrics::MeterProvider
  • Object
show all
Defined in:
lib/opentelemetry/sdk/metrics/meter_provider.rb

Overview

MeterProvider is the SDK implementation of Metrics::MeterProvider.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource: OpenTelemetry::SDK::Resources::Resource.create) ⇒ MeterProvider

Returns a new instance of MeterProvider.



19
20
21
22
23
24
25
26
# File 'lib/opentelemetry/sdk/metrics/meter_provider.rb', line 19

def initialize(resource: OpenTelemetry::SDK::Resources::Resource.create)
  @mutex = Mutex.new
  @meter_registry = {}
  @stopped = false
  @metric_readers = []
  @resource = resource
  @registered_views = []
end

Instance Attribute Details

#metric_readersObject (readonly)

Returns the value of attribute metric_readers.



17
18
19
# File 'lib/opentelemetry/sdk/metrics/meter_provider.rb', line 17

def metric_readers
  @metric_readers
end

#registered_viewsObject (readonly)

Returns the value of attribute registered_views.



17
18
19
# File 'lib/opentelemetry/sdk/metrics/meter_provider.rb', line 17

def registered_views
  @registered_views
end

#resourceObject (readonly)

Returns the value of attribute resource.



17
18
19
# File 'lib/opentelemetry/sdk/metrics/meter_provider.rb', line 17

def resource
  @resource
end

Instance Method Details

#add_metric_reader(metric_reader) ⇒ Object

Adds a new MetricReader to this OpenTelemetry::SDK::Metrics::MeterProvider.

Parameters:

  • metric_reader

    the new MetricReader to be added.



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/opentelemetry/sdk/metrics/meter_provider.rb', line 108

def add_metric_reader(metric_reader)
  @mutex.synchronize do
    if @stopped
      OpenTelemetry.logger.warn('calling MetricProvider#add_metric_reader after shutdown.')
    else
      @metric_readers.push(metric_reader)
      @meter_registry.each_value { |meter| meter.add_metric_reader(metric_reader) }
    end

    nil
  end
end

#add_view(name, **options) ⇒ nil

A View provides SDK users with the flexibility to customize the metrics that are output by the SDK.

Example:

OpenTelemetry.meter_provider.add_view('test', :aggregation => Aggregation::Drop.new, :type => :counter, :unit => 'smidgen', :meter_name => 'test', :meter_version => '1.0')

Parameters:

  • name (String)

    Name of the view.

  • options (optional Hash)

    For more precise matching, View and MetricsStream options may include: aggregation: An instance of an aggregation class, e.g. ExplicitBucketHistogram, Sum, LastValue type: A Symbol representing the instrument kind, e.g. :observable_gauge, :counter unit: A String matching an instrumentation unit, e.g. 'smidgen' meter_name: A String matching a meter name, e.g. meter_provider.meter('sample_meter_name', version: '1.2.0'), would be 'sample_meter_name' meter_version: A String matching a meter version, e.g. meter_provider.meter('sample_meter_name', version: '1.2.0'), would be '1.2.0'

Returns:

  • (nil)

    returns nil



150
151
152
153
154
# File 'lib/opentelemetry/sdk/metrics/meter_provider.rb', line 150

def add_view(name, **options)
  # TODO: add schema_url as part of options
  @registered_views << View::RegisteredView.new(name, **options)
  nil
end

#force_flush(timeout: nil) ⇒ Integer

This method provides a way for provider to notify the registered MetricReader instances, so they can do as much as they could to consume or send the metrics. Note: unlike Push Metric Exporter which can send data on its own schedule, Pull Metric Exporter can only send the data when it is being asked by the scraper, so ForceFlush would not make much sense.

Parameters:

  • timeout (optional Numeric) (defaults to: nil)

    An optional timeout in seconds.

Returns:

  • (Integer)

    Export::SUCCESS if no error occurred, Export::FAILURE if a non-specific failure occurred, Export::TIMEOUT if a timeout occurred.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/opentelemetry/sdk/metrics/meter_provider.rb', line 85

def force_flush(timeout: nil)
  @mutex.synchronize do
    if @stopped
      Export::SUCCESS
    else
      start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
      results = @metric_readers.map do |metric_reader|
        remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
        if remaining_timeout&.zero?
          Export::TIMEOUT
        else
          metric_reader.force_flush(timeout: remaining_timeout)
        end
      end

      results.max || Export::SUCCESS
    end
  end
end

#meter(name, version: nil) ⇒ Meter

Parameters:

  • name (String)

    Instrumentation package name

  • version (optional String) (defaults to: nil)

    Instrumentation package version

Returns:



34
35
36
37
38
39
40
41
42
43
# File 'lib/opentelemetry/sdk/metrics/meter_provider.rb', line 34

def meter(name, version: nil)
  version ||= ''
  if @stopped
    OpenTelemetry.logger.warn 'calling MeterProvider#meter after shutdown, a noop meter will be returned.'
    OpenTelemetry::Metrics::Meter.new
  else
    OpenTelemetry.logger.warn "Invalid meter name provided: #{name.nil? ? 'nil' : 'empty'} value" if name.to_s.empty?
    @mutex.synchronize { @meter_registry[Key.new(name, version)] ||= Meter.new(name, version, self) }
  end
end

#register_synchronous_instrument(instrument) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



122
123
124
125
126
127
128
# File 'lib/opentelemetry/sdk/metrics/meter_provider.rb', line 122

def register_synchronous_instrument(instrument)
  @mutex.synchronize do
    @metric_readers.each do |mr|
      instrument.register_with_new_metric_store(mr.metric_store)
    end
  end
end

#shutdown(timeout: nil) ⇒ Integer

Attempts to stop all the activity for this OpenTelemetry::SDK::Metrics::MeterProvider.

Calls MetricReader#shutdown for all registered MetricReaders.

After this is called all the newly created OpenTelemetry::SDK::Metrics::Meters will be no-op.

Parameters:

  • timeout (optional Numeric) (defaults to: nil)

    An optional timeout in seconds.

Returns:

  • (Integer)

    Export::SUCCESS if no error occurred, Export::FAILURE if a non-specific failure occurred, Export::TIMEOUT if a timeout occurred.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/opentelemetry/sdk/metrics/meter_provider.rb', line 54

def shutdown(timeout: nil)
  @mutex.synchronize do
    if @stopped
      OpenTelemetry.logger.warn('calling MetricProvider#shutdown multiple times.')
      Export::FAILURE
    else
      start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
      results = @metric_readers.map do |metric_reader|
        remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
        if remaining_timeout&.zero?
          Export::TIMEOUT
        else
          metric_reader.shutdown(timeout: remaining_timeout)
        end
      end

      @stopped = true
      results.max || Export::SUCCESS
    end
  end
end