Class: Sidekiq::Slog::ServerMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq-slog/middleware.rb

Instance Method Summary collapse

Instance Method Details

#call(worker, msg, queue) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sidekiq-slog/middleware.rb', line 54

def call(worker, msg, queue)
  Thread.current[:aj_job_id] = Sidekiq::Slog.job_id(msg)
  Thread.current[:aj_job_class] = Sidekiq::Slog.job_class(msg)

  context = { queue: queue }

  ts = Time.now.to_f
  # We are currently not logging start events.
  # This would most likely blow our SumoLogic limit.
  # start context, ts
  yield
  stop context, msg, ts
rescue => ex
  error context, ex, ts
  raise ex
ensure
  Thread.current[:aj_job_id] = nil
  Thread.current[:aj_job_class] = nil
end

#duration_ms(from, to = Time.now.to_f) ⇒ Object



97
98
99
# File 'lib/sidekiq-slog/middleware.rb', line 97

def duration_ms(from, to = Time.now.to_f)
  ((to - from) * 1000).round
end

#error(context, error, start) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/sidekiq-slog/middleware.rb', line 78

def error(context, error, start)
  data = context.merge(
    at: :error,
    duration: duration_ms(start),
    error: error.class.name,
    message: error.message[/\A.+$/].inspect)
  SLog.log('job_error', data)
end

#start(context, _time) ⇒ Object



74
75
76
# File 'lib/sidekiq-slog/middleware.rb', line 74

def start(context, _time)
  SLog.log('job_start', context)
end

#stop(context, _msg, start) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/sidekiq-slog/middleware.rb', line 87

def stop(context, _msg, start)
  data = context.merge(
    # It looks like this number is subject to clock drift.
                       # TODO: Figure out a way to make this accurate.
    # queued_duration: duration_ms(msg['enqueued_at'], start),
                       duration: duration_ms(start))

  SLog.log('job_stop', data)
end