Class: OpenTelemetry::SDK::Metrics::MeterProvider
- Inherits:
-
Metrics::MeterProvider
- Object
- Metrics::MeterProvider
- OpenTelemetry::SDK::Metrics::MeterProvider
- Defined in:
- lib/opentelemetry/sdk/metrics/meter_provider.rb
Overview
MeterProvider is the SDK implementation of Metrics::MeterProvider.
Instance Attribute Summary collapse
-
#metric_readers ⇒ Object
readonly
Returns the value of attribute metric_readers.
-
#registered_views ⇒ Object
readonly
Returns the value of attribute registered_views.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
-
#add_metric_reader(metric_reader) ⇒ Object
Adds a new MetricReader to this MeterProvider.
-
#add_view(name, **options) ⇒ nil
A View provides SDK users with the flexibility to customize the metrics that are output by the SDK.
-
#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.
-
#initialize(resource: OpenTelemetry::SDK::Resources::Resource.create) ⇒ MeterProvider
constructor
A new instance of MeterProvider.
-
#meter(name, version: nil) ⇒ Meter
Returns a Meter instance.
- #register_synchronous_instrument(instrument) ⇒ Object private
-
#shutdown(timeout: nil) ⇒ Integer
Attempts to stop all the activity for this MeterProvider.
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_readers ⇒ Object (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_views ⇒ Object (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 |
#resource ⇒ Object (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.
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')
150 151 152 153 154 |
# File 'lib/opentelemetry/sdk/metrics/meter_provider.rb', line 150 def add_view(name, **) # TODO: add schema_url as part of options @registered_views << View::RegisteredView.new(name, **) 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.
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. 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
Returns a OpenTelemetry::SDK::Metrics::Meter instance.
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.
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. 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 |