Class: RubyScriptExporter::Measurement

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_script_exporter/measurement.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(measurement, value, timestamp: nil, probe: nil, **labels) ⇒ Measurement

Returns a new instance of Measurement.



7
8
9
10
11
12
13
# File 'lib/ruby_script_exporter/measurement.rb', line 7

def initialize(measurement, value, timestamp: nil, probe: nil, **labels)
  @measurement = measurement
  @value = value
  @probe = probe
  @labels = labels
  @timestamp = timestamp
end

Instance Attribute Details

#measurementObject (readonly)

Returns the value of attribute measurement.



4
5
6
# File 'lib/ruby_script_exporter/measurement.rb', line 4

def measurement
  @measurement
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/ruby_script_exporter/measurement.rb', line 5

def value
  @value
end

Instance Method Details

#combined_labelsObject



19
20
21
22
23
# File 'lib/ruby_script_exporter/measurement.rb', line 19

def combined_labels
  return @labels unless @probe

  @probe.combined_labels.merge(@labels)
end

#format_as_open_metricObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_script_exporter/measurement.rb', line 25

def format_as_open_metric
  line = @measurement.to_s

  if combined_labels.any?
    line << '{'
    line << combined_labels.map do |key, value|
      "#{key}=\"#{value}\""
    end.join(',')
    line << '}'
  end

  line << ' '
  line << @value.to_s

  if @probe&.caches_result?
    line << ' '
    line << @timestamp.to_s
  end

  line
end

#to_sObject



15
16
17
# File 'lib/ruby_script_exporter/measurement.rb', line 15

def to_s
  "<Measurement #{@measurement} #{@value} #{@labels.inspect}>"
end