Class: Switchman::ShardedInstrumenter

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/switchman/sharded_instrumenter.rb

Instance Method Summary collapse

Constructor Details

#initialize(instrumenter, shard_host) ⇒ ShardedInstrumenter



5
6
7
8
# File 'lib/switchman/sharded_instrumenter.rb', line 5

def initialize(instrumenter, shard_host)
  super(instrumenter)
  @shard_host = shard_host
end

Instance Method Details

#instrument(name, payload = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/switchman/sharded_instrumenter.rb', line 10

def instrument(name, payload = {})
  shard = @shard_host&.shard
  # attribute_methods_generated? will be false during a reload -
  # when we might be doing a query while defining attribute methods,
  # so just avoid logging then
  if shard.is_a?(Shard) && Shard.instance_variable_get(:@attribute_methods_generated)
    env = if ::Rails.version < "8.0"
            @shard_host.pool.connection_class&.current_role
          else
            @shard_host.pool.connection_descriptor.name.constantize&.current_role
          end

    payload[:shard] = {
      database_server_id: shard.database_server.id,
      id: shard.id,
      env: env
    }
  end
  super
end