Skip to content

Instantly share code, notes, and snippets.

@sinsoku
Created November 9, 2023 11:13
Show Gist options
  • Save sinsoku/955c1e38fbdbad8b43d966348134a6aa to your computer and use it in GitHub Desktop.
Save sinsoku/955c1e38fbdbad8b43d966348134a6aa to your computer and use it in GitHub Desktop.
Sidekiq log formatter
# frozen_string_literal: true
require 'sidekiq/logger'
module App
module Formatters
class Sidekiq < ::Sidekiq::Logger::Formatters::Pretty
def call(severity, time, _program_name, message)
hash = {
timestamp: time.utc.iso8601(3),
level: severity,
message: super,
host: ENV['DD_HOSTNAME']
}.merge(dd_hash)
hash[:sidekiq] = { class: ctx[:class], jobid: ctx[:jid] } if ctx.any?
JSON.generate(hash) << "\n"
end
private
def dd_hash
correlation = ::Datadog::Tracing.correlation
dd = [:trace_id, :span_id, :env, :service, :version].index_with { |k| correlation.public_send(k).to_s }
{ dd: dd, ddsource: ['ruby'] }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment