Class: Syslogstash::PrometheusExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/syslogstash/prometheus_exporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrometheusExporter

Returns a new instance of PrometheusExporter.



9
10
11
12
13
14
# File 'lib/syslogstash/prometheus_exporter.rb', line 9

def initialize
  @msg_in  = prom.counter(:syslogstash_messages_received, "The number of syslog messages received from each log socket")
  @msg_out = prom.counter(:syslogstash_messages_sent, "The number of logstash messages sent to each logstash server")
  @lag     = prom.gauge(:syslogstash_lag_ms, "How far behind we are in relaying messages")
  @queue   = prom.gauge(:syslogstash_queue_size, "How many messages are queued to be sent")
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



7
8
9
# File 'lib/syslogstash/prometheus_exporter.rb', line 7

def thread
  @thread
end

Instance Method Details

#received(socket, stamp) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/syslogstash/prometheus_exporter.rb', line 16

def received(socket, stamp)
  @msg_in.increment(socket_path: socket)
  @queue.set({}, (@queue.get({}) || 0) + 1)

  if @most_recent_received.nil? || @most_recent_received < stamp
    @most_recent_received = stamp

    refresh_lag
  end
end

#runObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/syslogstash/prometheus_exporter.rb', line 38

def run
  @thread = Thread.new do
    app = Rack::Builder.new
    app.use Prometheus::Client::Rack::Exporter
    app.run ->(env) { [404, {'Content-Type' => 'text/plain'}, ['Nope']] }

    logger = Logger.new($stderr)
    logger.level = Logger::INFO
    logger.formatter = proc { |s, t, p, m| "[Syslogstash::PrometheusExporter::WEBrick] #{m}\n" }

    Rack::Handler::WEBrick.run app, Host: '::', Port: 9159, Logger: logger, AccessLog: []
  end
end

#sent(server, stamp) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/syslogstash/prometheus_exporter.rb', line 27

def sent(server, stamp)
  @msg_out.increment(logstash_server: server)
  @queue.set({}, @queue.get({}) - 1)

  if @most_recent_sent.nil? || @most_recent_sent < stamp
    @most_recent_sent = stamp

    refresh_lag
  end
end