Class: LogStash::Outputs::Prometheus
- Inherits:
-
Base
- Object
- Base
- LogStash::Outputs::Prometheus
- Defined in:
- lib/logstash/outputs/prometheus.rb
Overview
An prometheus output that does nothing.
Instance Method Summary collapse
-
#kill_thread ⇒ Object
def register.
- #receive(event) ⇒ Object
- #register ⇒ Object
Instance Method Details
#kill_thread ⇒ Object
def register
92 93 94 95 |
# File 'lib/logstash/outputs/prometheus.rb', line 92 def kill_thread() @thread.kill $prom_servers[@port] = nil end |
#receive(event) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/logstash/outputs/prometheus.rb', line 111 def receive(event) @increment.each do |metric_name, val| labels = setup_event_labels(val, event) $metrics[port.to_s + metric_name].increment(labels: labels) end @decrement.each do |metric_name, val| labels = setup_event_labels(val, event) $metrics[port.to_s + metric_name].decrement(labels: labels) end @set.each do |metric_name, val| labels = setup_event_labels(val, event) $metrics[port.to_s + metric_name].set(event.sprintf(val['value']).to_f,labels: labels) end @timer.each do |metric_name, val| labels = setup_event_labels(val, event) $metrics[port.to_s + metric_name].observe(event.sprintf(val['value']).to_f,labels: labels) end end |
#register ⇒ Object
24 25 26 27 28 29 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/logstash/outputs/prometheus.rb', line 24 def register $prom_servers ||= {} $metrics ||= {} if $prom_servers[@port].nil? $prom_servers[@port] = Prometheus::Client::Registry.new prom_server = $prom_servers[@port] app = Rack::Builder.new(@port) do use ::Rack::Deflater use ::Prometheus::Middleware::Exporter, registry: prom_server run ->(_) { [200, {'Content-Type' => 'text/html'}, ['Please access /metrics to see exposed metrics for this Logstash instance.']] } end.to_app @thread = Thread.new do Rack::Handler::WEBrick.run(app, Port: @port, BindAddress: "0.0.0.0", Host: "0.0.0.0") end end prom_server = $prom_servers[@port] @increment.each do |metric_name, val| val = setup_registry_labels(val) if $metrics[port.to_s + metric_name].nil? if val['type'] == "gauge" metric = prom_server.gauge(metric_name.to_sym, docstring: val['description'], labels: val['labels'].keys) else metric = prom_server.counter(metric_name.to_sym, docstring: val['description'], labels: val['labels'].keys) end $metrics[port.to_s + metric_name] = metric end end @decrement.each do |metric_name, val| val = setup_registry_labels(val) if $metrics[port.to_s + metric_name].nil? metric = prom_server.gauge(metric_name.to_sym, docstring: val['description'], labels: val['labels'].keys) $metrics[port.to_s + metric_name] = metric end end @set.each do |metric_name, val| val = setup_registry_labels(val) if $metrics[port.to_s + metric_name].nil? metric = prom_server.gauge(metric_name.to_sym, docstring: val['description'], labels: val['labels'].keys) $metrics[port.to_s + metric_name] = metric end end @timer.each do |metric_name, val| val = setup_registry_labels(val) if $metrics[port.to_s + metric_name].nil? if val['type'] == "histogram" metric = prom_server.histogram(metric_name.to_sym, docstring: val['description'], labels: val['labels'].keys, buckets: val['buckets']) else metric = prom_server.summary(metric_name.to_sym, labels: val['labels'].keys, docstring: val['description']) end $metrics[port.to_s + metric_name] = metric end end end |