Class: OpenTelemetry::SDK::Metrics::Instrument::Counter

Inherits:
SynchronousInstrument show all
Defined in:
lib/opentelemetry/sdk/metrics/instrument/counter.rb

Overview

Counter is the SDK implementation of Metrics::Counter.

Instance Method Summary collapse

Methods inherited from SynchronousInstrument

#initialize, #register_with_new_metric_store

Constructor Details

This class inherits a constructor from OpenTelemetry::SDK::Metrics::Instrument::SynchronousInstrument

Instance Method Details

#add(increment, attributes: {}) ⇒ Object

Increment the Counter by a fixed amount.

Parameters:

  • increment (numeric)

    The increment amount, which MUST be a non-negative numeric value.

  • attributes (Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}) (defaults to: {})

    Values must be non-nil and (array of) string, boolean or numeric type. Array values must not contain nil elements and all elements must be of the same basic type (string, numeric, boolean).



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/opentelemetry/sdk/metrics/instrument/counter.rb', line 27

def add(increment, attributes: {})
  # TODO: When the metrics SDK stabilizes and is merged into the main SDK,
  # we can leverage the SDK Internal validation classes to enforce this:
  # https://github.com/open-telemetry/opentelemetry-ruby/blob/6bec625ef49004f364457c26263df421526b60d6/sdk/lib/opentelemetry/sdk/internal.rb#L47
  if increment.negative?
    OpenTelemetry.logger.warn("#{@name} received a negative value")
  else
    update(increment, attributes)
  end
  nil
rescue StandardError => e
  OpenTelemetry.handle_error(exception: e)
  nil
end

#instrument_kindSymbol

Returns the instrument kind as a Symbol

Returns:

  • (Symbol)


16
17
18
# File 'lib/opentelemetry/sdk/metrics/instrument/counter.rb', line 16

def instrument_kind
  :counter
end