Class: Dyndnsd::TextfileReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/dyndnsd/textfile_reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ TextfileReporter

Returns a new instance of TextfileReporter.

Parameters:

  • file (String)
  • options (Hash{Symbol => Object}) (defaults to: {})


14
15
16
17
18
19
20
21
22
# File 'lib/dyndnsd/textfile_reporter.rb', line 14

def initialize(file, options = {})
  @file = file

  @prefix = options[:prefix]

  @registry  = options[:registry] || Metriks::Registry.default
  @interval  = options[:interval] || 60
  @on_error  = options[:on_error] || proc { |ex| } # default: ignore errors
end

Instance Attribute Details

#fileString (readonly)

Returns:

  • (String)


10
11
12
# File 'lib/dyndnsd/textfile_reporter.rb', line 10

def file
  @file
end

Instance Method Details

#restartvoid

This method returns an undefined value.



46
47
48
49
# File 'lib/dyndnsd/textfile_reporter.rb', line 46

def restart
  stop
  start
end

#startvoid

This method returns an undefined value.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dyndnsd/textfile_reporter.rb', line 25

def start
  @thread ||= Thread.new do
    loop do
      sleep @interval

      Thread.new do
        write
      rescue StandardError => e
        @on_error[e] rescue nil
      end
    end
  end
end

#stopvoid

This method returns an undefined value.



40
41
42
43
# File 'lib/dyndnsd/textfile_reporter.rb', line 40

def stop
  @thread&.kill
  @thread = nil
end

#writevoid

This method returns an undefined value.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dyndnsd/textfile_reporter.rb', line 52

def write
  File.open(@file, 'w') do |f|
    @registry.each do |name, metric|
      case metric
      when Metriks::Meter
        write_metric f, name, metric, [
          :count, :one_minute_rate, :five_minute_rate,
          :fifteen_minute_rate, :mean_rate
        ]
      when Metriks::Counter
        write_metric f, name, metric, [
          :count
        ]
      when Metriks::UtilizationTimer
        write_metric f, name, metric, [
          :count, :one_minute_rate, :five_minute_rate,
          :fifteen_minute_rate, :mean_rate,
          :min, :max, :mean, :stddev,
          :one_minute_utilization, :five_minute_utilization,
          :fifteen_minute_utilization, :mean_utilization
        ], [
          :median, :get_95th_percentile
        ]
      when Metriks::Timer
        write_metric f, name, metric, [
          :count, :one_minute_rate, :five_minute_rate,
          :fifteen_minute_rate, :mean_rate,
          :min, :max, :mean, :stddev
        ], [
          :median, :get_95th_percentile
        ]
      when Metriks::Histogram
        write_metric f, name, metric, [
          :count, :min, :max, :mean, :stddev
        ], [
          :median, :get_95th_percentile
        ]
      end
    end
  end
end

#write_metric(file, base_name, metric, keys, snapshot_keys = []) ⇒ void

This method returns an undefined value.

Parameters:

  • file (String)
  • base_name (String)
  • metric (Object)
  • keys (Array<Symbol>)
  • snapshot_keys (Array<Symbol>) (defaults to: [])


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dyndnsd/textfile_reporter.rb', line 100

def write_metric(file, base_name, metric, keys, snapshot_keys = [])
  time = Time.now.to_i

  base_name = base_name.to_s.gsub(/ +/, '_')
  base_name = "#{@prefix}.#{base_name}" if @prefix

  keys.flatten.each do |key|
    name = key.to_s.gsub(/^get_/, '')
    value = metric.send(key)
    file.write("#{base_name}.#{name} #{value} #{time}\n")
  end

  unless snapshot_keys.empty?
    snapshot = metric.snapshot
    snapshot_keys.flatten.each do |key|
      name = key.to_s.gsub(/^get_/, '')
      value = snapshot.send(key)
      file.write("#{base_name}.#{name} #{value} #{time}\n")
    end
  end
end