Module: ActiveSupport::Cache::Tracer

Defined in:
lib/rails/active_support/cache/tracer.rb,
lib/rails/active_support/cache/subscriber.rb,
lib/rails/active_support/cache/manual_tracer.rb

Defined Under Namespace

Classes: Subscriber

Class Method Summary collapse

Class Method Details

.disableObject Also known as: clear_subscribers



24
25
26
27
28
29
30
31
# File 'lib/rails/active_support/cache/tracer.rb', line 24

def disable
  if  @subscribers
    @subscribers.each { |subscriber| ActiveSupport::Notifications.unsubscribe(subscriber) }
    @subscribers.clear
  end

  self
end

.instrument(tracer: OpenTracing.global_tracer, active_span: nil, dalli: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rails/active_support/cache/tracer.rb', line 9

def instrument(tracer: OpenTracing.global_tracer, active_span: nil, dalli: false)
  clear_subscribers
  events = %w(read write generate delete clear)
  @subscribers = events.map do |event|
    subscriber = ActiveSupport::Cache::Tracer::Subscriber.new(tracer: tracer,
                                                              active_span: active_span,
                                                              event: event)
    ActiveSupport::Notifications.subscribe("cache_#{event}.active_support", subscriber)
  end

  instrument_dalli(tracer: tracer, active_span: active_span) if dalli

  self
end

.instrument_dalli(tracer:, active_span:) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/rails/active_support/cache/tracer.rb', line 35

def instrument_dalli(tracer:, active_span:)
  return unless defined?(ActiveSupport::Cache::DalliStore)
  require 'rails/active_support/cache/dalli_tracer'

  Dalli::Tracer.instrument(tracer: tracer, active_span: active_span)
  instrument_dalli_logger(active_span: active_span)
end

.instrument_dalli_logger(active_span:, level: Logger::ERROR) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/rails/active_support/cache/tracer.rb', line 43

def instrument_dalli_logger(active_span:, level: Logger::ERROR)
  return unless defined?(Tracing::Logger)
  return unless active_span
  return if [Tracing::Logger, Tracing::CompositeLogger].any? { |t| Dalli.logger.is_a?(t) }

  tracing_logger = Tracing::Logger.new(active_span: active_span, level: level)
  loggers = [tracing_logger, Dalli.logger].compact
  Dalli.logger = Tracing::CompositeLogger.new(*loggers)
end

.start_span(operation_name, tracer: OpenTracing.global_tracer, active_span: nil, start_time: Time.now, event:, **fields) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rails/active_support/cache/manual_tracer.rb', line 5

def start_span(operation_name, tracer: OpenTracing.global_tracer, active_span: nil, start_time: Time.now, event:, **fields)
  span = tracer.start_span(operation_name,
                           child_of: active_span.respond_to?(:call) ? active_span.call : active_span,
                           start_time: start_time,
                           tags: {
                            'component' => 'ActiveSupport::Cache',
                            'span.kind' => 'client',
                            'cache.key' => fields.fetch(:key, 'unknown')
                           })

  if event == 'read'
    span.set_tag('cache.hit', fields.fetch(:hit, false))
  end

  span
end