Module: RailsObservatory::Railties::ActiveJobInstrumentation

Defined in:
lib/rails_observatory/railties/active_job_instrumentation.rb

Instance Method Summary collapse

Instance Method Details

#perform_nowObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails_observatory/railties/active_job_instrumentation.rb', line 7

def perform_now
  TimeSeries.distribution("job.queue_latency", Time.now - enqueued_at, labels: { queue_name: }) unless enqueued_at.nil?
  labels = { job_class: self.class.name, queue_name: }
  TimeSeries.increment("job.count", labels:)
  TimeSeries.increment("job.retry_count", labels:) if executions > 1

  start_at = Time.now
  start_at_mono = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
  result = nil
  logs = []
  events = EventCollector.instance.collect_events do
    logs = LogCollector.collect_logs do
      result = super
    end
  end
  end_at_mono = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
  result
rescue Exception => error
  events = error.instance_variable_get(:@_trace_events)
  end_at_mono = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
  TimeSeries.increment("job.error_count", labels:)
  raise
ensure
  duration = end_at_mono - start_at_mono
  TimeSeries.distribution("job.latency", duration, labels:)
  JobTrace.new(
    job_id: job_id,
    time: start_at.to_f,
    duration:,
    queue_adapter: ActiveJob.adapter_name(queue_adapter),
    executions:,
    job_class: self.class.name,
    queue_name:,
    events: events.map { Serializer.serialize(_1) },
    logs:,
    error: error.present?
  ).save
end