Class: OpenTelemetry::SDK::Metrics::Instrument::AsynchronousInstrument

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb

Overview

AsynchronousInstrument contains the common functionality shared across the asynchronous instruments SDK instruments.

Constant Summary collapse

NOOP_EXEMPLAR_RESERVOIR =
Exemplar::NoopExemplarReservoir.new

Instance Method Summary collapse

Constructor Details

#initialize(name, unit, description, callback, instrumentation_scope, meter_provider, exemplar_filter, exemplar_reservoir) ⇒ AsynchronousInstrument

Returns a new instance of AsynchronousInstrument.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb', line 16

def initialize(name, unit, description, callback, instrumentation_scope, meter_provider, exemplar_filter, exemplar_reservoir)
  @name = name
  @unit = unit
  @description = description
  @instrumentation_scope = instrumentation_scope
  @meter_provider = meter_provider
  @metric_streams = []
  @callbacks = []
  @timeout   = nil
  @attributes = {}
  @exemplar_filter = exemplar_filter || meter_provider.exemplar_filter
  @exemplar_reservoir = exemplar_reservoir

  init_callback(callback)
  meter_provider.register_asynchronous_instrument(self)
end

Instance Method Details

#add_attributes(attributes) ⇒ Object

Merges attributes into those reported alongside observed values.



89
90
91
# File 'lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb', line 89

def add_attributes(attributes)
  @attributes.merge!(attributes) if attributes.instance_of?(Hash)
end

#init_callback(callback) ⇒ Object

The API MUST support creation of asynchronous instruments by passing zero or more callback functions to be permanently registered to the newly created instrument.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb', line 55

def init_callback(callback)
  if callback.nil?
    nil # zero callbacks permanently registered at creation is permitted
  elsif callback.instance_of?(Proc)
    @callbacks << callback
  elsif callback.instance_of?(Array)
    callback.each { |cb| @callbacks << cb if cb.instance_of?(Proc) }
  else
    OpenTelemetry.logger.warn "Only accept single Proc or Array of Proc for initialization with callback (given callback #{callback.class}"
  end
end

#register_callback(callback) ⇒ Object

Where the API supports registration of callback functions after asynchronous instrumentation creation, the user MUST be able to undo registration of the specific callback after its registration by some means.



69
70
71
72
73
74
75
76
# File 'lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb', line 69

def register_callback(callback)
  if callback.instance_of?(Proc)
    @callbacks << callback
    callback
  else
    OpenTelemetry.logger.warn "Only accept single Proc for registering callback (given callback #{callback.class}"
  end
end

#register_with_new_metric_store(metric_store, aggregation: default_aggregation) ⇒ 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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb', line 34

def register_with_new_metric_store(metric_store, aggregation: default_aggregation)
  ms = OpenTelemetry::SDK::Metrics::State::AsynchronousMetricStream.new(
    @name,
    @description,
    @unit,
    instrument_kind,
    @meter_provider,
    @instrumentation_scope,
    aggregation,
    @callbacks,
    @timeout,
    @attributes,
    @exemplar_filter,
    @exemplar_reservoir
  )
  @metric_streams << ms
  metric_store.add_metric_stream(ms)
end

#timeout(timeout) ⇒ Object

Sets the timeout, in seconds, for invoking callbacks.



84
85
86
# File 'lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb', line 84

def timeout(timeout)
  @timeout = timeout
end

#unregister(callback) ⇒ Object

Removes a previously registered callback.



79
80
81
# File 'lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb', line 79

def unregister(callback)
  @callbacks.delete(callback)
end