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.



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

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



33
34
35
# File 'lib/prometheus_exporter/server/collector.rb', line 33

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

#process_hash(obj) ⇒ Object



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

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

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

      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



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

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



29
30
31
# File 'lib/prometheus_exporter/server/collector.rb', line 29

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

#register_metric(metric) ⇒ Object



71
72
73
74
75
# File 'lib/prometheus_exporter/server/collector.rb', line 71

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