Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Add Database name to shard logging #3

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/octoball/log_subscriber.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
# Implementation courtesy of db-charmer.
class Octoball
module LogSubscriber
attr_accessor :current_shard
attr_accessor :current_shard, :current_dbn

def sql(event)
shard = event.payload[:connection]&.current_shard
self.current_shard = shard == ActiveRecord::Base.default_shard ? nil : shard
if shard == ActiveRecord::Base.default_shard
self.current_shard = nil
self.current_dbn = nil
else
self.current_shard = shard
self.current_dbn = event
.payload[:connection]
.instance_variable_get(:@connection)
.instance_variable_get(:@current_query_options)[:database]
end
super
end

private

def debug(progname = nil, &block)
conn = current_shard ? color("[Shard: #{current_shard}]", ActiveSupport::LogSubscriber::GREEN, true) : ''
conn = current_shard ? color("[Shard: #{current_shard}, db: #{current_dbn}]", ActiveSupport::LogSubscriber::GREEN, true) : ''
super(conn + progname.to_s, &block)
end
end
Expand Down