Class: Kirei::Services::Runner

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/kirei/services/runner.rb

Class Method Summary collapse

Class Method Details

.call(class_name, log_tags: {}, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kirei/services/runner.rb', line 18

def self.call(class_name, log_tags: {}, &block)
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
  service = yield

  service
ensure
  stop = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
  latency_in_ms = stop - T.must(start)

  result = service_result(service)

  metric_tags = Logging::Metric.inject_defaults({ "service.result" => result })
  ::StatsD.measure(class_name, latency_in_ms, tags: metric_tags)

  logtags = {
    "service.name" => class_name.to_s,
    "service.latency_in_ms" => latency_in_ms,
    "service.result" => result,
    "service.source_location" => source_location(block),
  }
  logtags.merge!(log_tags)

  Logging::Logger.call(level: log_level(result), label: "Service Finished", meta: logtags)
end

.service_result(service) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/kirei/services/runner.rb', line 49

def self.service_result(service)
  case service
  when Services::Result
    service.success? ? "success" : "failure"
  else
    "unknown"
  end
end