Module: Skywalking::Plugins::MemcachedIntercept

Defined in:
lib/skywalking/plugins/memcached.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



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
# File 'lib/skywalking/plugins/memcached.rb', line 19

def self.included(klass)
  supported_method =
    [:add, :append, :delete, :cas, :incr, :increment, :prepend, :replace, :set, :get, :fetch]
    .select do |method_name|
      klass.method_defined?(method_name) || klass.private_method_defined?(method_name)
    end

  supported_method.each do |method_name|
    zuper_method = :"zuper_#{method_name}"
    method_with_skywalking = :"#{method_name}_with_skywalking"

    klass.class_eval do
      define_method(method_with_skywalking) do |*args, &block|
        cache_key = args[0].to_s if args.length && !args[0].is_a?(Array)
        Tracing::ContextManager.new_exit_span(
          operation: "Memcached/#{method_name}",
          peer: @normalized_servers.join(','),
          component: Tracing::Component::Memcached
        ) do |span|
          span&.layer = Tracing::Layer::Cache
          span&.tag(Tracing::TagCacheType.new("Memcached"))
          span&.tag(Tracing::TagCacheKey.new(cache_key))

          resp = __send__(zuper_method, *args, &block)
          if method_name == :get && args.length && args[0].instance_of?(String)
            span&.tag(Tracing::TagCacheMiss.new(resp.nil?))
          end

          resp
        rescue
          span&.error_occurred = true
        end
      end

      alias_method zuper_method, method_name
      alias_method method_name, method_with_skywalking
    end
  end
end