Skip to content

Commit 979d5ac

Browse files
committed
add push metrics to push gateway
1 parent fb55ef6 commit 979d5ac

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/process_ldap.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def script_type
100100
end
101101

102102
def process
103+
start = Time.now
103104
S.logger.info("start")
104105
S.logger.info("open files", file_base: @file_base) if @file_base
105106
Report.open(file_base: @file_base, script_type: script_type) do |report|
@@ -127,8 +128,9 @@ def process
127128
S.logger.error "ldap_error", code: ldap_result_code, message: ldap.get_operation_result.message
128129
Report.metrics.error.increment
129130
end
130-
131+
Report.metrics.job_duration_seconds.set({script_type: script_type}, Time.now - start)
131132
puts Report.print_metrics
133+
Report.push_metrics
132134
S.logger.info("end")
133135
end
134136

lib/report.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "yabeda/prometheus"
2+
require "uri"
23
class Report
34
def self.configure_yabeda!
45
Yabeda.configure do
@@ -8,6 +9,7 @@ def self.configure_yabeda!
89
gauge :found, comment: "Number of found patrons", tags: [:script_type]
910
gauge :statistic_category, comment: "Number of loaded patrons in a statistic category", tags: [:script_type, :name]
1011
gauge :error, comment: "Number of errors encountered while running the patron load", tags: [:script_type]
12+
gauge :job_duration_seconds, comment: "Number of seconds it took to run the patron load job", tags: [:script_type]
1113
end
1214
end
1315
Yabeda.configure!
@@ -21,6 +23,16 @@ def self.print_metrics
2123
Prometheus::Client::Formats::Text.marshal(Yabeda::Prometheus.registry)
2224
end
2325

26+
def self.push_metrics
27+
# The env var needs to be set to the push gateway url
28+
if ENV["PROMETHEUS_PUSH_GATEWAY"]&.match?(URI::DEFAULT_PARSER.make_regexp)
29+
Yabeda::Prometheus.push_gateway.add(Yabeda::Prometheus.registry)
30+
S.logger.info("Metrics sent to the push gateway")
31+
else
32+
S.logger.warn("PROMETHEUS_PUSH_GATEWAY env var not set. Metrics not sent to the push gateway")
33+
end
34+
end
35+
2436
def self.open(file_base:, script_type:, &block)
2537
if file_base
2638
File.open("#{file_base}.tsv", "w") do |fh|

0 commit comments

Comments
 (0)