Class: Fluent::PrometheusMonitorInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_prometheus_monitor.rb

Defined Under Namespace

Classes: TimerWatcher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrometheusMonitorInput

Returns a new instance of PrometheusMonitorInput.



13
14
15
16
# File 'lib/fluent/plugin/in_prometheus_monitor.rb', line 13

def initialize
  super
  @registry = ::Prometheus::Client.registry
end

Instance Attribute Details

#registryObject (readonly)

Returns the value of attribute registry.



11
12
13
# File 'lib/fluent/plugin/in_prometheus_monitor.rb', line 11

def registry
  @registry
end

Instance Method Details

#configure(conf) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fluent/plugin/in_prometheus_monitor.rb', line 18

def configure(conf)
  super
  hostname = Socket.gethostname
  expander = Fluent::Prometheus.placeholder_expander(log)
  placeholders = expander.prepare_placeholders(
    Time.now, {'hostname' => hostname}, [])
  @base_labels = Fluent::Prometheus.parse_labels_elements(conf)
  @base_labels.each do |key, value|
    @base_labels[key] = expander.expand(value, placeholders)
  end

  @monitor_agent = Fluent::MonitorAgentInput.new

  buffer_queue_length = @registry.gauge(
    :fluentd_status_buffer_queue_length,
    'Current buffer queue length.')
  buffer_total_queued_size = @registry.gauge(
    :fluentd_status_buffer_total_bytes,
    'Current total size of queued buffers.')
  retry_counts = @registry.gauge(
    :fluentd_status_retry_count,
    'Current retry counts.')

  @monitor_info = {
    'buffer_queue_length' => buffer_queue_length,
    'buffer_total_queued_size' => buffer_total_queued_size,
    'retry_count' => retry_counts,
  }
end

#labels(plugin_info) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/fluent/plugin/in_prometheus_monitor.rb', line 93

def labels(plugin_info)
  @base_labels.merge(
    plugin_id: plugin_info["plugin_id"],
    plugin_category: plugin_info["plugin_category"],
    type: plugin_info["type"],
  )
end

#runObject



76
77
78
79
80
81
# File 'lib/fluent/plugin/in_prometheus_monitor.rb', line 76

def run
  @loop.run
rescue
  log.error "unexpected error", :error=>$!.to_s
  log.error_backtrace
end

#shutdownObject



70
71
72
73
74
# File 'lib/fluent/plugin/in_prometheus_monitor.rb', line 70

def shutdown
  @loop.watchers.each {|w| w.detach }
  @loop.stop
  @thread.join
end

#startObject



63
64
65
66
67
68
# File 'lib/fluent/plugin/in_prometheus_monitor.rb', line 63

def start
  @loop = Coolio::Loop.new
  @timer = TimerWatcher.new(@interval, true, log, &method(:update_monitor_info))
  @loop.attach(@timer)
  @thread = Thread.new(&method(:run))
end

#update_monitor_infoObject



83
84
85
86
87
88
89
90
91
# File 'lib/fluent/plugin/in_prometheus_monitor.rb', line 83

def update_monitor_info
  @monitor_agent.plugins_info_all.each do |info|
    @monitor_info.each do |name, metric|
      if info[name]
        metric.set(labels(info), info[name])
      end
    end
  end
end