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.

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of AsynchronousInstrument.



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

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

  init_callback(callback)
  meter_provider.register_asynchronous_instrument(self)
end

Instance Method Details

#add_attributes(attributes) ⇒ Object



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

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.



49
50
51
52
53
54
55
56
57
# File 'lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb', line 49

def init_callback(callback)
  if 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.



61
62
63
64
65
66
67
68
# File 'lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb', line 61

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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb', line 30

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
  )
  @metric_streams << ms
  metric_store.add_metric_stream(ms)
end

#timeout(timeout) ⇒ Object



74
75
76
# File 'lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb', line 74

def timeout(timeout)
  @timeout = timeout
end

#unregister(callback) ⇒ Object



70
71
72
# File 'lib/opentelemetry/sdk/metrics/instrument/asynchronous_instrument.rb', line 70

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