Class: RubyScriptExporter::Probe

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

Defined Under Namespace

Classes: Builder, ScriptError, ScriptTimeout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, service) ⇒ Probe

Returns a new instance of Probe.



24
25
26
27
28
29
30
31
# File 'lib/ruby_script_exporter/probe.rb', line 24

def initialize(name, service)
  @name = name
  @service = service
  @labels = {}
  @last_measurements = nil
  @last_run_at = nil
  @timeout = 1
end

Instance Attribute Details

#cache_forObject

Returns the value of attribute cache_for.



11
12
13
# File 'lib/ruby_script_exporter/probe.rb', line 11

def cache_for
  @cache_for
end

#labelsObject (readonly)

Returns the value of attribute labels.



10
11
12
# File 'lib/ruby_script_exporter/probe.rb', line 10

def labels
  @labels
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/ruby_script_exporter/probe.rb', line 9

def name
  @name
end

#runner_proc=(value) ⇒ Object (writeonly)

Sets the attribute runner_proc

Parameters:

  • value

    the value to set the attribute runner_proc to.



13
14
15
# File 'lib/ruby_script_exporter/probe.rb', line 13

def runner_proc=(value)
  @runner_proc = value
end

#serviceObject (readonly)

Returns the value of attribute service.



14
15
16
# File 'lib/ruby_script_exporter/probe.rb', line 14

def service
  @service
end

#timeoutObject

Returns the value of attribute timeout.



12
13
14
# File 'lib/ruby_script_exporter/probe.rb', line 12

def timeout
  @timeout
end

Class Method Details

.raise_errorsObject



20
21
22
# File 'lib/ruby_script_exporter/probe.rb', line 20

def self.raise_errors
  @raise_errors
end

.raise_errors=(raise_errors) ⇒ Object



16
17
18
# File 'lib/ruby_script_exporter/probe.rb', line 16

def self.raise_errors=(raise_errors)
  @raise_errors = raise_errors
end

Instance Method Details

#caches_result?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/ruby_script_exporter/probe.rb', line 39

def caches_result?
  !!cache_for
end

#combined_labelsObject



33
34
35
36
37
# File 'lib/ruby_script_exporter/probe.rb', line 33

def combined_labels
  @service.combined_labels.merge({
    probe: @name,
  }).merge(@labels).compact
end

#runObject

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby_script_exporter/probe.rb', line 43

def run
  if caches_result? && @last_measurements && @last_run_at > (Time.now.to_f - @cache_for)
    return [@last_measurements, :cached]
  end

  raise ArgumentError, 'No runner given' unless @runner_proc

  runner = Runner.new(self)

  start_time = nil
  end_time = nil
  begin
    Timeout::timeout(@timeout) do
      start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      runner.instance_eval(&@runner_proc)
      end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    end
  rescue Timeout::Error
    raise ScriptTimeout
  rescue StandardError => e
    if self.class.raise_errors
      raise e
    end

    raise ScriptError, e.inspect
  end

  execution_time = end_time - start_time

  @last_run_at = Time.now.to_f
  @last_measurements = runner.measurements
  [@last_measurements, execution_time]
end