From abd7b62215e1d1badec03da6a63ea97c2e67e11a Mon Sep 17 00:00:00 2001 From: Lud Date: Fri, 21 Jun 2024 10:03:35 +0200 Subject: [PATCH] feat: log level depends on outcome of the service run --- lib/kirei/services/runner.rb | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/kirei/services/runner.rb b/lib/kirei/services/runner.rb index ca98ffc..0502793 100644 --- a/lib/kirei/services/runner.rb +++ b/lib/kirei/services/runner.rb @@ -24,12 +24,7 @@ def self.call(class_name, log_tags: {}, &block) stop = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) latency_in_ms = stop - T.must(start) - result = case service - when Services::Result - service.success? ? "success" : "failure" - else - "unknown" - end + result = service_result(service) metric_tags = Logging::Metric.inject_defaults({ "service.result" => result }) ::StatsD.measure(class_name, latency_in_ms, tags: metric_tags) @@ -42,13 +37,31 @@ def self.call(class_name, log_tags: {}, &block) } logtags.merge!(log_tags) - Logging::Logger.call(level: Logging::Level::INFO, label: "Service Finished", meta: logtags) + Logging::Logger.call(level: log_level(result), label: "Service Finished", meta: logtags) end sig { params(proc: T.proc.returns(T.untyped)).returns(String) } private_class_method def self.source_location(proc) proc.source_location.join(":").gsub(App.root.to_s, "") end + + sig { params(service: T.untyped).returns(String) } + def self.service_result(service) + case service + when Services::Result + service.success? ? "success" : "failure" + else + "unknown" + end + end + + sig { params(result: String).returns(Logging::Level) } + private_class_method def self.log_level(result) + return Logging::Level::INFO if result == "success" + return Logging::Level::WARN if result == "failure" + + Logging::Level::UNKNOWN + end end end end