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
# 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)
end

Instance Method Details

#process(str) ⇒ Object



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

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

#process_hash(obj) ⇒ Object



36
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
# File 'lib/prometheus_exporter/server/collector.rb', line 36

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



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

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



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

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

#register_metric(metric) ⇒ Object



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

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