Skip to content

Commit d38f200

Browse files
Polish the code and add plugins (#4)
1 parent 4f3e927 commit d38f200

27 files changed

+639
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ Release Notes.
77
#### Features
88
- Initialize the ruby agent core.
99
- Implement e2e tests.
10-
- Add docs.
1110

1211
#### Plugins
1312
* Support [Sinatra](https://github.com/sinatra/sinatra)
1413
* Support [redis-rb](https://github.com/redis/redis-rb)
1514
* Support [net-http](https://github.com/ruby/net-http)
15+
* Support [memcached](https://github.com/petergoldstein/dalli)
16+
* Support [elasticsearch](https://github.com/elastic/elasticsearch-ruby)
1617

1718
#### Documentation
1819
* Initialize the documentation.

docs/en/agent/plugins.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
The following plugins provide the distributed tracing capability, and the OAP backend would analyze the topology and
44
metrics based on the tracing data.
55

6-
| Library | Version | Plugin Name |
7-
|:----------------------------------------------|:---------|:------------|
8-
| [redis](https://github.com/redis/redis-rb) | ~> 5.0 | `redis` |
9-
| [net-http](https://github.com/ruby/net-http) | ~> 0.6.0 | `net_http` |
10-
| [sinatra](https://github.com/sinatra/sinatra) | ~> 4.1 | `sinatra` |
6+
| Library | Version | Plugin Name |
7+
|:---------------------------------------------------------------|:---------|:----------------|
8+
| [redis](https://github.com/redis/redis-rb) | ~> 5.0 | `redis5` |
9+
| [net-http](https://github.com/ruby/net-http) | ~> 0.6.0 | `net_http` |
10+
| [sinatra](https://github.com/sinatra/sinatra) | ~> 4.1 | `sinatra` |
11+
| [memcached](https://github.com/petergoldstein/dalli) | ~> 3.2 | `memcached` |
12+
| [elasticsearch](https://github.com/elastic/elasticsearch-ruby) | ~> 8.0.0 | `elasticsearch` |

docs/en/setup/quick-start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ The following lists all the configuration options:
9797
| log_file_name | SW_AGENT_LOG_FILE_NAME | skywalking | The name of the log file. |
9898
| log_file_path | SW_AGENT_LOG_FILE_PATH | Not set | The path to the log file. |
9999
| log_level | SW_AGENT_LOG_LEVEL | info | The log level. |
100-
| disable_plugins | SW_AGENT_DISABLE_PLUGINS | Not set | The plugins to disable. |
100+
| disable_plugins | SW_AGENT_DISABLE_PLUGINS | Not set | The plugins to disable, multiple names should be split by comma, e.g. 'redis5,elasticsearch'. |
101101
| report_protocol | SW_AGENT_REPORT_PROTOCOL | grpc | The protocol to use for reporting. |
102102
| re_ignore_operation | SW_AGENT_RE_IGNORE_OPERATION | Not set | Ignore specific URL paths. |
103103
| instance_properties_json | SW_AGENT_INSTANCE_PROPERTIES_JSON | Not set | A custom JSON string to be reported as service instance properties, e.g. `{"key": "value"}`. |

lib/skywalking/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Configuration
6767
:disable_plugins => {
6868
type: :string,
6969
default: '',
70-
desc: 'The plugins to disable'
70+
desc: "The plugins to disable, multiple names should be split by comma, e.g. 'redis5,elasticsearch'"
7171
},
7272
:report_protocol => {
7373
type: :string,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
class Elasticsearch < PluginsManager::SWPlugin
19+
def plugin_valid?
20+
defined?(::Elasticsearch)
21+
end
22+
23+
def install
24+
inst_target = if defined?(::Elastic::Transport::Client)
25+
::Elastic::Transport::Client
26+
elsif defined?(::Elasticsearch::Transport::Client)
27+
::Elasticsearch::Transport::Client
28+
end
29+
30+
inst_target.class_eval do
31+
def perform_request_with_skywalking(method, path, *args, &block)
32+
peer_info = transport.hosts.first
33+
db_statement = [{ params: args&.[](0) }]
34+
unless args[1].nil? || args[1].empty?
35+
db_statement << { body: args[1] }
36+
end
37+
38+
Tracing::ContextManager.new_exit_span(
39+
operation: "Elasticsearch/#{method}/#{path}",
40+
peer: "#{peer_info[:protocol]}://#{peer_info[:host]}:#{peer_info[:port]}",
41+
component: Tracing::Component::Elasticsearch
42+
) do |span|
43+
span&.tag(Tracing::TagDbType.new("Elasticsearch"))
44+
span&.tag(Tracing::TagDbStatement.new(db_statement))
45+
span&.layer = Tracing::Layer::Database
46+
47+
zuper_perform_request(method, path, *args, &block)
48+
rescue
49+
span&.error_occurred = true
50+
end
51+
end
52+
53+
alias_method :zuper_perform_request, :perform_request
54+
alias_method :perform_request, :perform_request_with_skywalking
55+
end
56+
end
57+
58+
register :elasticsearch
59+
end
60+
end
61+
end
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

lib/skywalking/plugins/net_http.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def request(req, body = nil, &block)
3535
Tracing::ContextManager.new_exit_span(
3636
operation: "#{method}:#{req_info}",
3737
peer: host,
38-
component: Tracing::Component::Http
38+
component: Tracing::Component::HttpClient
3939
) do |span|
4040
span&.tag(Tracing::TagHttpMethod.new(method))
4141
span&.tag(Tracing::TagHttpURL.new(uri))

lib/skywalking/plugins/redis5.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ def call_v(args, &block)
3030
span&.tag(Tracing::TagCacheOp.new(operation))
3131
span&.layer = Tracing::Layer::Cache
3232

33-
super(args, &block)
33+
begin
34+
super(args, &block)
35+
rescue
36+
span&.error_occurred = true
37+
end
3438
end
3539
end
3640

lib/skywalking/plugins/sinatra.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ def dispatch!(*args, &block)
2121
req_method = @request.request_method if @request.respond_to?(:request_method)
2222
carrier = Tracing::Carrier.new
2323
carrier.each do |item|
24-
item.val = request.env[item.key.capitalize] if request.env[item.key.capitalize]
24+
item.value = request.env["HTTP_#{item.key.upcase}"]
2525
end
2626

2727
Tracing::ContextManager.new_entry_span(
2828
operation: "#{req_method}:#{request.env['REQUEST_URI']}",
29-
carrier: carrier,
30-
inherit: Tracing::Component::General
29+
carrier: carrier
3130
) do |span|
3231
span&.tag(Tracing::TagHttpMethod.new(req_method))
3332
span&.tag(Tracing::TagHttpURL.new(request.env['REQUEST_URI']))
@@ -36,6 +35,8 @@ def dispatch!(*args, &block)
3635
span&.component = Tracing::Component::Sinatra
3736

3837
super(*args, &block)
38+
rescue
39+
span&.error_occurred = true
3940
end
4041
end
4142
end

lib/skywalking/plugins_manager.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def init_plugins
3434
Dir[File.join(__dir__, 'plugins', '*.rb')].each { |file| require file }
3535

3636
installed_plugins = self.class.installed.keys
37-
@enabled_plugins ||= installed_plugins - @config[:disable_plugins].split(',')
37+
@enabled_plugins ||= installed_plugins - @config[:disable_plugins].split(',').map(&:to_sym)
3838
@enabled_plugins.each do |plugin_name|
3939
self.class.installed[plugin_name].try_install(plugin_name)
4040
end

0 commit comments

Comments
 (0)