Class: PrometheusExporter::Client

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(host: 'localhost', port: PrometheusExporter::DEFAULT_PORT, max_queue_size: nil, thread_sleep: 0.5, json_serializer: nil, custom_labels: nil) ⇒ Client

Returns a new instance of Client.



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
# File 'lib/prometheus_exporter/client.rb', line 53

def initialize(host: 'localhost', port: PrometheusExporter::DEFAULT_PORT, max_queue_size: nil, thread_sleep: 0.5, json_serializer: nil, custom_labels: nil)
  @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

  @json_serializer = json_serializer == :oj ? PrometheusExporter::OjCompat : JSON

  @custom_labels = custom_labels
end

Class Method Details

.defaultObject



42
43
44
# File 'lib/prometheus_exporter/client.rb', line 42

def self.default
  @default ||= new
end

.default=(client) ⇒ Object



46
47
48
# File 'lib/prometheus_exporter/client.rb', line 46

def self.default=(client)
  @default = client
end

Instance Method Details

#custom_labels=(custom_labels) ⇒ Object



79
80
81
# File 'lib/prometheus_exporter/client.rb', line 79

def custom_labels=(custom_labels)
  @custom_labels = custom_labels
end

#process_queueObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/prometheus_exporter/client.rb', line 104

def process_queue
  while @queue.length > 0
    ensure_socket!

    begin
      message = @queue.pop
      @socket.write(message.bytesize.to_s(16).upcase)
      @socket.write("\r\n")
      @socket.write(message)
      @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



83
84
85
86
87
# File 'lib/prometheus_exporter/client.rb', line 83

def register(type, name, help)
  metric = RemoteMetric.new(type: type, name: name, help: help, client: self)
  @metrics << metric
  metric
end

#send(str) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/prometheus_exporter/client.rb', line 94

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



89
90
91
92
# File 'lib/prometheus_exporter/client.rb', line 89

def send_json(obj)
  payload = @custom_labels.nil? ? obj : obj.merge(custom_labels: @custom_labels)
  send(@json_serializer.dump(payload))
end

#stopObject



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/prometheus_exporter/client.rb', line 122

def stop
  @mutex.synchronize do
    @worker_thread&.kill
    while @worker_thread.alive?
      sleep 0.001
    end
    @worker_thread = nil
  end

  close_socket!
end