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
40
# 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
  @condition = ConditionVariable.new
  @export_mutex = Mutex.new

  start
end

Instance Method Details

#alive?Boolean

Check both @thread and @continue object to determine if current PeriodicMetricReader is still alive. If one of them is true/alive, then PeriodicMetricReader is determined as alive

Returns:

  • (Boolean)


84
85
86
# File 'lib/opentelemetry/sdk/metrics/export/periodic_metric_reader.rb', line 84

def alive?
  @continue || @thread.alive?
end

#force_flush(timeout: nil) ⇒ Integer

Export all metrics to the configured Exporter that have not yet been exported.

This method should only be called in cases where it is absolutely necessary, such as when using some FaaS providers that may suspend the process after an invocation, but before the PeriodicMetricReader exports the completed metrics.

Parameters:

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

    An optional timeout in seconds.

Returns:

  • (Integer)

    SUCCESS if no error occurred, FAILURE if a non-specific failure occurred.



74
75
76
77
78
79
# File 'lib/opentelemetry/sdk/metrics/export/periodic_metric_reader.rb', line 74

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

#shutdown(timeout: nil) ⇒ Integer

Shuts the @thread down and set @continue to false; it will block until the shutdown thread is finished.

Parameters:

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

    An optional timeout in seconds.

Returns:

  • (Integer)

    SUCCESS if no error occurred, FAILURE if a non-specific failure occurred.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/opentelemetry/sdk/metrics/export/periodic_metric_reader.rb', line 48

def shutdown(timeout: nil)
  thread = lock do
    @continue = false # force termination in next iteration
    @condition.signal
    @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