Class: Dalli::Tracer

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

Class Method Summary collapse

Class Method Details

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rails/active_support/cache/dalli_tracer.rb', line 6

def instrument(tracer: OpenTracing.global_tracer, active_span: nil)
  ::Dalli::Server.class_eval do
    @tracer = tracer
    @active_span = active_span

    class << self
      attr_reader :tracer, :active_span
    end

    def tracer
      self.class.tracer
    end

    def active_span
      self.class.active_span
    end

    alias_method :request_without_instrumentation, :request

    def request(op, *args)
      tags = {
        'component' => 'Dalli::Server',
        'span.kind' => 'client',
        'db.statement' => op.to_s,
        'db.type' => 'memcached',
        'peer.hostname' => hostname,
        'peer.port' => port,
        'peer.weight' => weight
      }
      # Method::Tracer.trace("Dalli::Server#request", tracer: tracer, child_of: active_span, tags: tags) do
      #   request_without_instrumentation(op, *args)
      # end
      parent_span = active_span.respond_to?(:call) ? active_span.call : active_span
      span = tracer.start_span("Dalli::Server#request", child_of: parent_span, tags: tags)

      begin
        request_without_instrumentation(op, *args)
      rescue  => e
        if span
          span.set_tag("error", true)
          span.log_kv(key: "message", value: error.message)
        end
      ensure
        span.finish() if span
      end
    end
  end

  ::Dalli::Client.class_eval do
    @tracer = tracer
    @active_span = active_span

    class << self
      attr_reader :tracer, :active_span
    end

    def tracer
      self.class.tracer
    end

    def active_span
      self.class.active_span
    end

    alias_method :perform_without_instrumentation, :perform

    def perform(*args)
      parent_span = active_span.respond_to?(:call) ? active_span.call : active_span
      span = tracer.start_span("Dalli::Client#perform", child_of: active_span, tags: {})

      begin
        perform_without_instrumentation(*args)
      rescue => error
        if span
          span.set_tag("error", true)
          span.log_kv(key: "message", value: error.message)
        end
      ensure
        span.finish() if span
      end
      # Method::Tracer.trace("Dalli::Client#perform", tracer: tracer, child_of: active_span) do
      #   perform_without_instrumentation(*args)
      # end
    end
  end
end

.remove_instrumentationObject



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rails/active_support/cache/dalli_tracer.rb', line 93

def remove_instrumentation
  ::Dalli::Server.class_eval do
    alias_method :request, :request_without_instrumentation
    remove_method :request_without_instrumentation
  end

  ::Dalli::Client.class_eval do
    alias_method :perform, :perform_without_instrumentation
    remove_method :perform_without_instrumentation
  end
end