Skip to content

Commit

Permalink
feat: log level depends on outcome of the service run
Browse files Browse the repository at this point in the history
  • Loading branch information
swiknaba committed Jun 21, 2024
1 parent 2046a0f commit abd7b62
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions lib/kirei/services/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

0 comments on commit abd7b62

Please sign in to comment.