prometheus_exporter 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6d3374059942d70d2bbebe1de9cb3769dc8ff562bef3c4c79aff97023d2a1b45
4
- data.tar.gz: 9903e75d79d6f92b014835e99793dd8979d464acef9ff1d82b91c0fe056ec1a7
3
+ metadata.gz: 5ce91417f88ed90178a27b9afabbd205375083260dae5eeaa07241dd03d71982
4
+ data.tar.gz: 95bcbb41f28bdf3c6ed0708c208d979f36728aa7ec339c0db69470789a4afb13
5
5
  SHA512:
6
- metadata.gz: c93a68aaf72d3ece28914b93c097ad3659397a5cd147d6917cd9c9cc72cea2f939e836b696f24dd30963c1deed0234a4e088a8cdfdb62df50f0a768c06e8cb4a
7
- data.tar.gz: f88f72c5ed0d34f26deeac798ea3d5397379d3e6f4ffdf2205f5b7cf1f44a84a346c07ed4bfb059a12ca3a92fcc96c3b816b63c090b949f1ec203e97615008f8
6
+ metadata.gz: 53f83e8fee67c6d3ad13508b0785ff83d96117ae0dc1e560e12643e8983c19ed81a7f34a5951f2de707ad7c9b96fb921ba3ff7d7d8b45684175ecbf0c4818140
7
+ data.tar.gz: f5c4c83a44bdf8a1cf09c449af256426d1596f33c9dd94571bfca426f5cb0a94acae7031d983e8a63f49744b1c93b32e2605a88c99d720e4eadf73a6b0d79bcd
data/bench/bench.rb ADDED
@@ -0,0 +1,37 @@
1
+ require_relative '../lib/prometheus_exporter'
2
+ require_relative '../lib/prometheus_exporter/client'
3
+ require_relative '../lib/prometheus_exporter/server'
4
+ require 'oj'
5
+
6
+ # test how long it takes a custom collector to process 10k messages
7
+
8
+ class Collector
9
+ def initialize(done)
10
+ @i = 0
11
+ @done = done
12
+ end
13
+
14
+ def process(message)
15
+ _parsed = JSON.parse(message)
16
+ p @i if @i % 100 == 0
17
+ @done.call if (@i += 1) == 10_000
18
+ end
19
+
20
+ def prometheus_metrics_text
21
+ end
22
+ end
23
+
24
+ @start = nil
25
+ done = lambda do
26
+ puts "Elapsed for 10k messages is #{Time.now - @start}"
27
+ end
28
+
29
+ collector = Collector.new(done)
30
+ server = PrometheusExporter::Server::WebServer.new port: 12349, collector: collector
31
+ server.start
32
+ client = PrometheusExporter::Client.new port: 12349, max_queue_size: 20_000
33
+
34
+ @start = Time.now
35
+ 10_000.times { client.send_json(hello: "world") }
36
+
37
+ sleep
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
3
  require 'socket'
5
4
  require 'thread'
6
5
 
@@ -15,7 +14,7 @@ class PrometheusExporter::Client
15
14
  end
16
15
 
17
16
  def observe(value = 1, keys = nil)
18
- @client.send(
17
+ @client.send_json(
19
18
  type: @type,
20
19
  help: @help,
21
20
  name: @name,
@@ -28,7 +27,7 @@ class PrometheusExporter::Client
28
27
  MAX_SOCKET_AGE = 25
29
28
  MAX_QUEUE_SIZE = 10_000
30
29
 
31
- def initialize(host:, port:, max_queue_size: nil, thread_sleep: 0.5)
30
+ def initialize(host: 'localhost', port:, max_queue_size: nil, thread_sleep: 0.5)
32
31
  @metrics = []
33
32
 
34
33
  @queue = Queue.new
@@ -56,8 +55,12 @@ class PrometheusExporter::Client
56
55
  metric
57
56
  end
58
57
 
59
- def send(obj)
60
- @queue << obj.to_json
58
+ def send_json(obj)
59
+ send(obj.to_json)
60
+ end
61
+
62
+ def send(str)
63
+ @queue << str
61
64
  if @queue.length > @max_queue_size
62
65
  STDERR.puts "Prometheus Exporter client is dropping message cause queue is full"
63
66
  @queue.pop
@@ -136,23 +139,23 @@ class PrometheusExporter::Client
136
139
  end
137
140
 
138
141
  def close_socket_if_old!
139
- if @socket && ((@socket_started + MAX_SOCKET_AGE) > Time.now.to_f)
142
+ if @socket && ((@socket_started + MAX_SOCKET_AGE) < Time.now.to_f)
143
+ p "CLOSE OLD"
140
144
  close_socket!
141
145
  end
142
146
  end
143
147
 
144
148
  def ensure_socket!
145
149
  close_socket_if_old!
146
-
147
- @socket = TCPSocket.new @host, @port
148
-
149
- @socket.write("POST /send-metrics HTTP/1.1\r\n")
150
- @socket.write("Transfer-Encoding: chunked\r\n")
151
- @socket.write("Connection: Close\r\n")
152
- @socket.write("Content-Type: application/octet-stream\r\n")
153
- @socket.write("\r\n")
154
-
155
- @socket_started = Time.now.to_f
150
+ if !@socket
151
+ @socket = TCPSocket.new @host, @port
152
+ @socket.write("POST /send-metrics HTTP/1.1\r\n")
153
+ @socket.write("Transfer-Encoding: chunked\r\n")
154
+ @socket.write("Connection: Close\r\n")
155
+ @socket.write("Content-Type: application/octet-stream\r\n")
156
+ @socket.write("\r\n")
157
+ @socket_started = Time.now.to_f
158
+ end
156
159
 
157
160
  nil
158
161
  rescue
@@ -10,7 +10,8 @@ module PrometheusExporter::Server
10
10
  @mutex = Mutex.new
11
11
  end
12
12
 
13
- def process(obj)
13
+ def process(str)
14
+ obj = JSON.parse(str)
14
15
  @mutex.synchronize do
15
16
  metric = @metrics[obj["name"]]
16
17
  if !metric
@@ -11,6 +11,12 @@ module PrometheusExporter::Server
11
11
 
12
12
  def initialize(port: , collector: nil)
13
13
 
14
+ @total_metrics = PrometheusExporter::Metric::Counter.new("total_collector_metrics", "total metrics processed by exporter web")
15
+
16
+ @total_sessions = PrometheusExporter::Metric::Counter.new("total_collector_sessions", "total send_metric sessions processed by exporter web")
17
+
18
+ @total_bad_metrics = PrometheusExporter::Metric::Counter.new("total_collector_bad_metrics", "total mis-handled metrics by collector")
19
+
14
20
  @server = WEBrick::HTTPServer.new(
15
21
  Port: port,
16
22
  AccessLog: [],
@@ -48,10 +54,13 @@ module PrometheusExporter::Server
48
54
  end
49
55
 
50
56
  def handle_metrics(req, res)
57
+ @total_sessions.observe
51
58
  req.body do |block|
52
59
  begin
53
- @collector.process(JSON.parse(block))
60
+ @total_metrics.observe
61
+ @collector.process(block)
54
62
  rescue => e
63
+ @total_bad_metrics.observe
55
64
  res.body = "Bad Metrics #{e}"
56
65
  res.status = e.respond_to?(:status_code) ? e.status_code : 500
57
66
  return
@@ -87,22 +96,26 @@ module PrometheusExporter::Server
87
96
  STDERR.puts "Generating Prometheus metrics text timed out"
88
97
  end
89
98
 
90
- @metrics = []
99
+ metrics = []
91
100
 
92
- add_gauge(
101
+ metrics << add_gauge(
93
102
  "collector_working",
94
103
  "Is the master process collector able to collect metrics",
95
104
  metric_text && metric_text.length > 0 ? 1 : 0
96
105
  )
97
106
 
98
- add_gauge(
107
+ metrics << add_gauge(
99
108
  "collector_rss",
100
109
  "total memory used by collector process",
101
110
  get_rss
102
111
  )
103
112
 
113
+ metrics << @total_metrics
114
+ metrics << @total_sessions
115
+ metrics << @total_bad_metrics
116
+
104
117
  <<~TEXT
105
- #{@metrics.map(&:to_prometheus_text).join("\n\n")}
118
+ #{metrics.map(&:to_prometheus_text).join("\n\n")}
106
119
  #{metric_text}
107
120
  TEXT
108
121
  end
@@ -116,7 +129,7 @@ module PrometheusExporter::Server
116
129
  def add_gauge(name, help, value)
117
130
  gauge = PrometheusExporter::Metric::Gauge.new(name, help)
118
131
  gauge.observe(value)
119
- @metrics << gauge
132
+ gauge
120
133
  end
121
134
 
122
135
  end
@@ -1,3 +1,3 @@
1
1
  module PrometheusExporter
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prometheus_exporter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-11 00:00:00.000000000 Z
11
+ date: 2018-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -96,6 +96,7 @@ files:
96
96
  - LICENSE.txt
97
97
  - README.md
98
98
  - Rakefile
99
+ - bench/bench.rb
99
100
  - examples/custom_collector.rb
100
101
  - lib/prometheus_exporter.rb
101
102
  - lib/prometheus_exporter/client.rb