Class: Fluent::PrometheusOutputMonitorInput

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

Defined Under Namespace

Classes: TimerWatcher

Constant Summary collapse

MONITOR_IVARS =
[
  :num_errors,
  :emit_count,

  # for v0.12
  :last_retry_time,

  # from v0.14
  :emit_records,
  :write_count,
  :rollback_count,
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ PrometheusOutputMonitorInput

Returns a new instance of PrometheusOutputMonitorInput.



25
26
27
28
# File 'lib/fluent/plugin/in_prometheus_output_monitor.rb', line 25

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

Instance Attribute Details

#registry ⇒ Object (readonly)

Returns the value of attribute registry.



10
11
12
# File 'lib/fluent/plugin/in_prometheus_output_monitor.rb', line 10

def registry
  @registry
end

Instance Method Details

#configure(conf) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
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
76
# File 'lib/fluent/plugin/in_prometheus_output_monitor.rb', line 30

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

  if defined?(Fluent::Plugin) && defined?(Fluent::Plugin::MonitorAgentInput)
    # from v0.14.6
    @monitor_agent = Fluent::Plugin::MonitorAgentInput.new
  else
    @monitor_agent = Fluent::MonitorAgentInput.new
  end

  @metrics = {
    buffer_queue_length: @registry.gauge(
      :fluentd_output_status_buffer_queue_length,
      'Current buffer queue length.'),
    buffer_total_queued_size: @registry.gauge(
      :fluentd_output_status_buffer_total_bytes,
      'Current total size of queued buffers.'),
    retry_counts: @registry.gauge(
      :fluentd_output_status_retry_count,
      'Current retry counts.'),
    num_errors: @registry.gauge(
      :fluentd_output_status_num_errors,
      'Current number of errors.'),
    emit_count: @registry.gauge(
      :fluentd_output_status_emit_count,
      'Current emit counts.'),
    emit_records: @registry.gauge(
      :fluentd_output_status_emit_records,
      'Current emit records.'),
    write_count: @registry.gauge(
      :fluentd_output_status_write_count,
      'Current write counts.'),
    rollback_count: @registry.gauge(
      :fluentd_output_status_rollback_count,
      'Current rollback counts.'),
    retry_wait: @registry.gauge(
      :fluentd_output_status_retry_wait,
      'Current retry wait'),
  }
end

#labels(plugin_info) ⇒ Object



171
172
173
174
175
176
# File 'lib/fluent/plugin/in_prometheus_output_monitor.rb', line 171

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

#run ⇒ Object



108
109
110
111
112
113
# File 'lib/fluent/plugin/in_prometheus_output_monitor.rb', line 108

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

#shutdown ⇒ Object



101
102
103
104
105
106
# File 'lib/fluent/plugin/in_prometheus_output_monitor.rb', line 101

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

#start ⇒ Object



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

def start
  super
  @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_info ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fluent/plugin/in_prometheus_output_monitor.rb', line 115

def update_monitor_info
  opts = {
    ivars: MONITOR_IVARS,
    with_retry: true,
  }

  agent_info = @monitor_agent.plugins_info_all(opts).select {|info|
    info['plugin_category'] == 'output'.freeze
  }

  monitor_info = {
    'buffer_queue_length' => @metrics[:buffer_queue_length],
    'buffer_total_queued_size' => @metrics[:buffer_total_queued_size],
    'retry_count' => @metrics[:retry_counts],
  }
  instance_vars_info = {
    num_errors: @metrics[:num_errors],
    write_count: @metrics[:write_count],
    emit_count: @metrics[:emit_count],
    emit_records: @metrics[:emit_records],
    rollback_count: @metrics[:rollback_count],
  }

  agent_info.each do |info|
    label = labels(info)

    monitor_info.each do |name, metric|
      if info[name]
        metric.set(label, info[name])
      end
    end

    if info['instance_variables']
      instance_vars_info.each do |name, metric|
        if info['instance_variables'][name]
          metric.set(label, info['instance_variables'][name])
        end
      end
    end

    # compute current retry_wait
    if info['retry']
      next_time = info['retry']['next_time']
      start_time = info['retry']['start']
      if start_time.nil? && info['instance_variables']
        # v0.12 does not include start, use last_retry_time instead
        start_time = info['instance_variables'][:last_retry_time]
      end
      if next_time && start_time
        wait = next_time - start_time
        @metrics[:retry_wait].set(label, wait.to_f)
      end
    end
  end
end