Class: PrometheusExporter::Server::Collector

Inherits:
CollectorBase show all
Defined in:
lib/prometheus_exporter/server/collector.rb

Instance Method Summary collapse

Constructor Details

#initialize(json_serializer: nil) ⇒ Collector

Returns a new instance of Collector.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/prometheus_exporter/server/collector.rb', line 5

def initialize(json_serializer: nil)
  @process_metrics = []
  @metrics = {}
  @mutex = Mutex.new
  @collectors = {}
  @json_serializer = PrometheusExporter.detect_json_serializer(json_serializer)
  register_collector(WebCollector.new)
  register_collector(ProcessCollector.new)
  register_collector(SidekiqCollector.new)
  register_collector(SidekiqQueueCollector.new)
  register_collector(SidekiqProcessCollector.new)
  register_collector(SidekiqStatsCollector.new)
  register_collector(DelayedJobCollector.new)
  register_collector(PumaCollector.new)
  register_collector(HutchCollector.new)
  register_collector(UnicornCollector.new)
  register_collector(ActiveRecordCollector.new)
  register_collector(ShoryukenCollector.new)
  register_collector(ResqueCollector.new)
  register_collector(GoodJobCollector.new)
end

Instance Method Details

#process(str) ⇒ Object



31
32
33
# File 'lib/prometheus_exporter/server/collector.rb', line 31

def process(str)
  process_hash(@json_serializer.parse(str))
end

#process_hash(obj) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/prometheus_exporter/server/collector.rb', line 35

def process_hash(obj)
  @mutex.synchronize do
    if collector = @collectors[obj["type"]]
      collector.collect(obj)
    else
      metric = @metrics[obj["name"]]
      metric = register_metric_unsafe(obj) if !metric

      keys = obj["keys"] || {}
      keys = obj["custom_labels"].merge(keys) if obj["custom_labels"]

      case obj["prometheus_exporter_action"]
      when "increment"
        metric.increment(keys, obj["value"])
      when "decrement"
        metric.decrement(keys, obj["value"])
      else
        metric.observe(obj["value"], keys)
      end
    end
  end
end

#prometheus_metrics_textObject



58
59
60
61
62
63
64
# File 'lib/prometheus_exporter/server/collector.rb', line 58

def prometheus_metrics_text
  @mutex.synchronize do
    (@metrics.values + @collectors.values.map(&:metrics).flatten).map(
      &:to_prometheus_text
    ).join("\n")
  end
end

#register_collector(collector) ⇒ Object



27
28
29
# File 'lib/prometheus_exporter/server/collector.rb', line 27

def register_collector(collector)
  @collectors[collector.type] = collector
end

#register_metric(metric) ⇒ Object



66
67
68
# File 'lib/prometheus_exporter/server/collector.rb', line 66

def register_metric(metric)
  @mutex.synchronize { @metrics[metric.name] = metric }
end