Class: PrometheusExporter::Client
- Inherits:
-
Object
- Object
- PrometheusExporter::Client
- Defined in:
- lib/prometheus_exporter/client.rb
Defined Under Namespace
Classes: RemoteMetric
Constant Summary collapse
- MAX_SOCKET_AGE =
25
- MAX_QUEUE_SIZE =
10_000
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(host: 'localhost', port: PrometheusExporter::DEFAULT_PORT, max_queue_size: nil, thread_sleep: 0.5) ⇒ Client
constructor
A new instance of Client.
- #process_queue ⇒ Object
- #register(type, name, help) ⇒ Object
- #send(str) ⇒ Object
- #send_json(obj) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(host: 'localhost', port: PrometheusExporter::DEFAULT_PORT, max_queue_size: nil, thread_sleep: 0.5) ⇒ Client
Returns a new instance of Client.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/prometheus_exporter/client.rb', line 38 def initialize(host: 'localhost', port: PrometheusExporter::DEFAULT_PORT, max_queue_size: nil, thread_sleep: 0.5) @metrics = [] @queue = Queue.new @socket = nil @socket_started = nil max_queue_size ||= MAX_QUEUE_SIZE max_queue_size = max_queue_size.to_i if max_queue_size.to_i <= 0 raise ArgumentError, "max_queue_size must be larger than 0" end @max_queue_size = max_queue_size @host = host @port = port @worker_thread = nil @mutex = Mutex.new @thread_sleep = thread_sleep end |
Class Method Details
.default ⇒ Object
27 28 29 |
# File 'lib/prometheus_exporter/client.rb', line 27 def self.default @default ||= new end |
.default=(client) ⇒ Object
31 32 33 |
# File 'lib/prometheus_exporter/client.rb', line 31 def self.default=(client) @default = client end |
Instance Method Details
#process_queue ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/prometheus_exporter/client.rb', line 80 def process_queue while @queue.length > 0 ensure_socket! begin = @queue.pop @socket.write(.bytesize.to_s(16).upcase) @socket.write("\r\n") @socket.write() @socket.write("\r\n") rescue => e STDERR.puts "Prometheus Exporter is dropping a message: #{e}" @socket = nil raise end end end |
#register(type, name, help) ⇒ Object
60 61 62 63 64 |
# File 'lib/prometheus_exporter/client.rb', line 60 def register(type, name, help) metric = RemoteMetric.new(type: type, name: name, help: help, client: self) @metrics << metric metric end |
#send(str) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/prometheus_exporter/client.rb', line 70 def send(str) @queue << str if @queue.length > @max_queue_size STDERR.puts "Prometheus Exporter client is dropping message cause queue is full" @queue.pop end ensure_worker_thread! end |
#send_json(obj) ⇒ Object
66 67 68 |
# File 'lib/prometheus_exporter/client.rb', line 66 def send_json(obj) send(obj.to_json) end |
#stop ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/prometheus_exporter/client.rb', line 98 def stop @mutex.synchronize do @worker_thread&.kill while @worker_thread.alive? sleep 0.001 end @worker_thread = nil end close_socket! end |