Class: ActiveSupport::Cache::Tracer::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/active_support/cache/subscriber.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracer: OpenTracing.global_tracer, active_span: nil, event:) ⇒ Subscriber

Returns a new instance of Subscriber.



7
8
9
10
11
12
# File 'lib/rails/active_support/cache/subscriber.rb', line 7

def initialize(tracer: OpenTracing.global_tracer, active_span: nil, event:)
  @tracer = tracer
  @active_span = active_span
  @event = event
  @operation_name = "cache.#{event}"
end

Instance Attribute Details

#active_spanObject (readonly)

Returns the value of attribute active_span.



5
6
7
# File 'lib/rails/active_support/cache/subscriber.rb', line 5

def active_span
  @active_span
end

#eventObject (readonly)

Returns the value of attribute event.



5
6
7
# File 'lib/rails/active_support/cache/subscriber.rb', line 5

def event
  @event
end

#operation_nameObject (readonly)

Returns the value of attribute operation_name.



5
6
7
# File 'lib/rails/active_support/cache/subscriber.rb', line 5

def operation_name
  @operation_name
end

#tracerObject (readonly)

Returns the value of attribute tracer.



5
6
7
# File 'lib/rails/active_support/cache/subscriber.rb', line 5

def tracer
  @tracer
end

Instance Method Details

#call(*args) ⇒ Object

For compatibility with Rails 3.2



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rails/active_support/cache/subscriber.rb', line 15

def call(*args)
  _, start, finish, _, payload = *args

  span = Tracer.start_span(operation_name,
                            event: event,
                            tracer: tracer,
                            active_span: active_span,
                            start_time: start,
                            **payload)

  if payload[:exception]
    Rails::Tracer::SpanHelpers.set_error(span, payload[:exception_object] || payload[:exception])
  end

  span.finish(end_time: finish)
end

#finish(name, _, payload) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rails/active_support/cache/subscriber.rb', line 43

def finish(name, _, payload)
  span = payload[:__OT_SPAN__]
  return unless span

  span.set_tag('cache.key', payload.fetch(:key, 'unknown'))

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

  if payload[:exception]
    Rails::Tracer::SpanHelpers.set_error(span, payload[:exception_object] || payload[:exception])
  end

  span.finish
end

#start(name, _, payload) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/rails/active_support/cache/subscriber.rb', line 32

def start(name, _, payload)
  span = tracer.start_span(operation_name,
                           child_of: active_span.respond_to?(:call) ? active_span.call : active_span,
                           tags: {
                            'component' => 'ActiveSupport::Cache',
                            'span.kind' => 'client'
                           })

  payload[:__OT_SPAN__] = span
end