Class: OpenTelemetry::SDK::Metrics::Export::PeriodicMetricReader

Inherits:
MetricReader
  • Object
show all
Defined in:
lib/opentelemetry/sdk/metrics/export/periodic_metric_reader.rb

Overview

PeriodicMetricReader provides a minimal example implementation.

Instance Attribute Summary

Attributes inherited from MetricReader

#metric_store

Instance Method Summary collapse

Methods inherited from MetricReader

#collect

Constructor Details

#initialize(export_interval_millis: Float(ENV.fetch('OTEL_METRIC_EXPORT_INTERVAL', 60_000)), export_timeout_millis: Float(ENV.fetch('OTEL_METRIC_EXPORT_TIMEOUT', 30_000)), exporter: nil) ⇒ Object

Parameters:

  • export_interval_millis (Integer) (defaults to: Float(ENV.fetch('OTEL_METRIC_EXPORT_INTERVAL', 60_000)))

    the maximum interval time. Defaults to the value of the OTEL_METRIC_EXPORT_INTERVAL environment variable, if set, or 60_000.

  • export_timeout_millis (Integer) (defaults to: Float(ENV.fetch('OTEL_METRIC_EXPORT_TIMEOUT', 30_000)))

    the maximum export timeout. Defaults to the value of the OTEL_METRIC_EXPORT_TIMEOUT environment variable, if set, or 30_000.

  • exporter (MetricReader) (defaults to: nil)

    the (duck type) MetricReader to where the recorded metrics are pushed after certain interval.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/opentelemetry/sdk/metrics/export/periodic_metric_reader.rb', line 25

def initialize(export_interval_millis: Float(ENV.fetch('OTEL_METRIC_EXPORT_INTERVAL', 60_000)),
               export_timeout_millis: Float(ENV.fetch('OTEL_METRIC_EXPORT_TIMEOUT', 30_000)),
               exporter: nil)
  super()

  @export_interval = export_interval_millis / 1000.0
  @export_timeout = export_timeout_millis / 1000.0
  @exporter = exporter
  @thread   = nil
  @continue = false
  @mutex = Mutex.new
  @export_mutex = Mutex.new

  start
end

Instance Method Details

#force_flush(timeout: nil) ⇒ Object



55
56
57
58
59
60
# File 'lib/opentelemetry/sdk/metrics/export/periodic_metric_reader.rb', line 55

def force_flush(timeout: nil)
  export(timeout: timeout)
  Export::SUCCESS
rescue StandardError
  Export::FAILURE
end

#shutdown(timeout: nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/opentelemetry/sdk/metrics/export/periodic_metric_reader.rb', line 41

def shutdown(timeout: nil)
  thread = lock do
    @continue = false # force termination in next iteration
    @thread
  end
  thread&.join(@export_interval)
  @exporter.force_flush if @exporter.respond_to?(:force_flush)
  @exporter.shutdown
  Export::SUCCESS
rescue StandardError => e
  OpenTelemetry.handle_error(exception: e, message: 'Fail to shutdown PeriodicMetricReader.')
  Export::FAILURE
end