|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +module Skywalking |
| 17 | + module Plugins |
| 18 | + module MemcachedIntercept |
| 19 | + def self.included(klass) |
| 20 | + supported_method = |
| 21 | + [:add, :append, :delete, :cas, :incr, :increment, :prepend, :replace, :set, :get, :fetch] |
| 22 | + .select do |method_name| |
| 23 | + klass.method_defined?(method_name) || klass.private_method_defined?(method_name) |
| 24 | + end |
| 25 | + |
| 26 | + supported_method.each do |method_name| |
| 27 | + zuper_method = :"zuper_#{method_name}" |
| 28 | + method_with_skywalking = :"#{method_name}_with_skywalking" |
| 29 | + |
| 30 | + klass.class_eval do |
| 31 | + define_method(method_with_skywalking) do |*args, &block| |
| 32 | + cache_key = args[0].to_s if args.length && !args[0].is_a?(Array) |
| 33 | + Tracing::ContextManager.new_exit_span( |
| 34 | + operation: "Memcached/#{method_name}", |
| 35 | + peer: @normalized_servers.join(','), |
| 36 | + component: Tracing::Component::Memcached |
| 37 | + ) do |span| |
| 38 | + span&.layer = Tracing::Layer::Cache |
| 39 | + span&.tag(Tracing::TagCacheType.new("Memcached")) |
| 40 | + span&.tag(Tracing::TagCacheKey.new(cache_key)) |
| 41 | + |
| 42 | + resp = __send__(zuper_method, *args, &block) |
| 43 | + if method_name == :get && args.length && args[0].instance_of?(String) |
| 44 | + span&.tag(Tracing::TagCacheMiss.new(resp.nil?)) |
| 45 | + end |
| 46 | + |
| 47 | + resp |
| 48 | + rescue |
| 49 | + span&.error_occurred = true |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + alias_method zuper_method, method_name |
| 54 | + alias_method method_name, method_with_skywalking |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + class Memcached < PluginsManager::SWPlugin |
| 61 | + def plugin_valid? |
| 62 | + defined?(::Dalli::Client) |
| 63 | + end |
| 64 | + |
| 65 | + def install |
| 66 | + ::Dalli::Client.class_eval do |
| 67 | + include Skywalking::Plugins::MemcachedIntercept |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + register :memcached |
| 72 | + end |
| 73 | + end |
| 74 | +end |
0 commit comments