-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus2influxdb.rb
57 lines (45 loc) · 1.42 KB
/
prometheus2influxdb.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
# frozen_string_literal: true
require "singleton"
require "influxdb"
require "yabeda/prometheus/mmap"
require_relative "prometheus2influxdb/config"
require_relative "prometheus2influxdb/version"
module Yabeda
class Prometheus2InfluxDB
include Singleton
class Error < StandardError; end
def self.start!
instance.start!
end
def self.configured?
instance.config.configured?
end
attr_reader :influxdb_client, :instance_label, :config
def initialize
@config = Config.new
@influxdb_client = ::InfluxDB::Client.new(**config.to_h)
@instance_label = "#{Socket.gethostname}##{Process.pid}"
end
def start!
Thread.new do
loop do
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
Yabeda.collect!
influxdb_client.write_points(prepare_points)
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
sleep(config.export_interval_seconds - (finish - start))
end
end
end
private
def prepare_points
metrics = ::Prometheus::Client::Formats::Text.send(:load_metrics, ::Prometheus::Client.configuration.multiprocess_files_dir)
metrics.map do |_, samples:, **|
samples.map do |series, tags, value|
tags = Hash[tags].merge("instance" => instance_label)
{ series: series, tags: tags, values: { value: value } }
end
end.flatten
end
end
end