opentracing-instrumentation 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.BUNDLER_VERSION +1 -0
  3. data/.drone.jsonnet +35 -0
  4. data/.gitignore +1 -0
  5. data/.gitlab-ci.yml +80 -0
  6. data/.rubocop.yml +36 -0
  7. data/.ruby-version +1 -0
  8. data/GEM_VERSION +1 -0
  9. data/Gemfile +19 -0
  10. data/Gemfile.lock +101 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +39 -0
  13. data/Rakefile +11 -0
  14. data/bin/console +16 -0
  15. data/bin/setup +8 -0
  16. data/lib/opentracing/instrumentation.rb +23 -0
  17. data/lib/opentracing/instrumentation/common.rb +13 -0
  18. data/lib/opentracing/instrumentation/common/error_writer.rb +56 -0
  19. data/lib/opentracing/instrumentation/faraday.rb +10 -0
  20. data/lib/opentracing/instrumentation/faraday/response_logger.rb +76 -0
  21. data/lib/opentracing/instrumentation/faraday/trace_middleware.rb +202 -0
  22. data/lib/opentracing/instrumentation/mongo.rb +12 -0
  23. data/lib/opentracing/instrumentation/mongo/direct_sanitazer.rb +16 -0
  24. data/lib/opentracing/instrumentation/mongo/query_sanitazer.rb +84 -0
  25. data/lib/opentracing/instrumentation/mongo/trace_subscriber.rb +107 -0
  26. data/lib/opentracing/instrumentation/object_wrapper.rb +59 -0
  27. data/lib/opentracing/instrumentation/rack.rb +11 -0
  28. data/lib/opentracing/instrumentation/rack/http_tagger.rb +69 -0
  29. data/lib/opentracing/instrumentation/rack/trace_middleware.rb +94 -0
  30. data/lib/opentracing/instrumentation/redis.rb +18 -0
  31. data/lib/opentracing/instrumentation/redis/config.rb +40 -0
  32. data/lib/opentracing/instrumentation/redis/span_builder.rb +85 -0
  33. data/lib/opentracing/instrumentation/redis/tracing_driver_wrapper.rb +117 -0
  34. data/lib/opentracing/instrumentation/sidekiq.rb +17 -0
  35. data/lib/opentracing/instrumentation/sidekiq/client_middleware.rb +66 -0
  36. data/lib/opentracing/instrumentation/sidekiq/job_tagger.rb +61 -0
  37. data/lib/opentracing/instrumentation/sidekiq/server_middleware.rb +70 -0
  38. data/lib/opentracing/instrumentation/sinatra.rb +11 -0
  39. data/lib/opentracing/instrumentation/sinatra/trace_middleware.rb +64 -0
  40. data/lib/opentracing/instrumentation/thrift.rb +15 -0
  41. data/lib/opentracing/instrumentation/thrift/config.rb +24 -0
  42. data/lib/opentracing/instrumentation/thrift/traced_protocol.rb +145 -0
  43. data/lib/opentracing/instrumentation/thrift/traced_protocol_factory.rb +48 -0
  44. data/lib/opentracing/instrumentation/version.rb +7 -0
  45. data/opentracing-instrumentation.gemspec +40 -0
  46. metadata +255 -0
@@ -0,0 +1,145 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thrift'
4
+
5
+ module OpenTracing
6
+ module Instrumentation
7
+ module Thrift
8
+ # TracedProtocol wrap any raw Thrift protocol instance.
9
+ # Not thread safe!
10
+ #
11
+ # Usage (without multiplexed):
12
+ # buffered_transport = ::Thrift::BufferedTransport.new(transport)
13
+ # protocol = ::Thrift::BinaryProtocol.new(buffered_transport)
14
+ # traced_protocol = \
15
+ # OpenTracing::Instrumentation::Thrift::TracedProtocol.new(protocol)
16
+ #
17
+ # Usage (multiplexed):
18
+ # buffered_transport = ::Thrift::BufferedTransport.new(transport)
19
+ # protocol = ::Thrift::BinaryProtocol.new(buffered_transport)
20
+ # traced_protocol =
21
+ # OpenTracing::Instrumentation::Thrift::TracedProtocol
22
+ # .new(protocol)
23
+ # multiplexed_protocol =
24
+ # ::Thrift::MultiplexedProtocol
25
+ # .new(traced_protocol, 'OrderService')
26
+ class TracedProtocol < ::Thrift::BaseProtocol
27
+ extend Forwardable
28
+
29
+ include ::Thrift::ProtocolDecorator
30
+
31
+ attr_reader :protocol
32
+
33
+ def initialize(protocol, config: Config.new)
34
+ @protocol = protocol
35
+ @config = config
36
+ end
37
+
38
+ def write_message_begin(name, type, seqid)
39
+ request_tags = build_request_tags(name, type)
40
+ self.write_scope = tracer.start_active_span(
41
+ write_operation_name,
42
+ tags: request_tags,
43
+ )
44
+ handler_error(write_scope.span, type)
45
+
46
+ protocol.write_message_begin(name, type, seqid)
47
+ end
48
+
49
+ def write_message_end
50
+ super
51
+ write_scope.close
52
+ end
53
+
54
+ def read_message_begin
55
+ self.read_scope = tracer.start_active_span(read_operation_name)
56
+
57
+ name, type, rseqid = super
58
+
59
+ tags = build_request_tags(name, type)
60
+ write_span_tags(read_scope.span, tags)
61
+ handler_error(read_scope.span, type)
62
+
63
+ [name, type, rseqid]
64
+ end
65
+
66
+ def read_message_end
67
+ super
68
+ read_scope.close
69
+ end
70
+
71
+ private
72
+
73
+ def_delegators :@config,
74
+ :tracer,
75
+ :write_operation_name,
76
+ :read_operation_name
77
+
78
+ attr_accessor :write_scope,
79
+ :read_scope
80
+
81
+ METHOD_PART = 'method'
82
+ SERVICE_NAME_PART = 'service_name'
83
+ NAME_PATTER = /((?<service_name>\w+):)?(?<method>\w+)/.freeze
84
+ MESSAGE_TYPES = {
85
+ ::Thrift::MessageTypes::CALL => 'CALL',
86
+ ::Thrift::MessageTypes::REPLY => 'REPLY',
87
+ ::Thrift::MessageTypes::EXCEPTION => 'EXCEPTION',
88
+ ::Thrift::MessageTypes::ONEWAY => 'ONEWAY',
89
+ }.freeze
90
+
91
+ def parse_name(name)
92
+ name_matches = NAME_PATTER.match(name)
93
+ method = name_matches[METHOD_PART]
94
+ service_name = name_matches[SERVICE_NAME_PART]
95
+ [service_name, method]
96
+ end
97
+
98
+ def write_span_tags(span, tags)
99
+ tags.each do |tag, value|
100
+ span.set_tag(tag, value)
101
+ end
102
+ end
103
+
104
+ def build_request_tags(name, type)
105
+ service_name, method = parse_name(name)
106
+ {
107
+ 'thrift.method' => method,
108
+ 'thrift.service_name' => service_name,
109
+ 'thrift.type' => MESSAGE_TYPES[type],
110
+ 'thrift.multiplexed' => !service_name.nil?,
111
+ 'thrift.protocol' => protocol_name,
112
+ 'thrift.transport' => transport_name,
113
+ }.compact
114
+ end
115
+
116
+ def protocol_name
117
+ protocol.class.to_s
118
+ end
119
+
120
+ def build_transport_name(transport)
121
+ inner_transport = transport.instance_variable_get(:@transport)
122
+
123
+ if inner_transport
124
+ inner_transport_name = build_transport_name(inner_transport)
125
+ "#{transport.class}(#{inner_transport_name})"
126
+ else
127
+ transport.class.to_s
128
+ end
129
+ end
130
+
131
+ def transport_name
132
+ return @transport_name if defined?(@transport_name)
133
+
134
+ @transport_name = build_transport_name(@protocol.trans)
135
+ end
136
+
137
+ def handler_error(span, type)
138
+ return if type != ::Thrift::MessageTypes::EXCEPTION
139
+
140
+ span.set_tag('error', true)
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'opentracing/instrumentation/thrift/traced_protocol'
4
+
5
+ module OpenTracing
6
+ module Instrumentation
7
+ module Thrift
8
+ # Factory of TracedProtocol. Can be used thrift processor
9
+ #
10
+ # Usage (config.ru):
11
+ # multiplexed_processor = Thrift::MultiplexedProcessor.new
12
+ # binary_protocol_factory =
13
+ # OpenTracing::Instrumentation::Thrift::TracedProtocolFactory.new(
14
+ # Thrift::BinaryProtocolAcceleratedFactory.new,
15
+ # )
16
+ # json_protocol_factory =
17
+ # OpenTracing::Instrumentation::Thrift::TracedProtocolFactory.new(
18
+ # Thrift::JsonProtocolFactory.new,
19
+ # )
20
+ # protocol_factoreis_map = {
21
+ # binary_protocol_factory => ['application/x-thrift'],
22
+ # json_protocol_factory => ['application/json'],
23
+ # }
24
+ # thrift_app =
25
+ # ::MultiprotocolThriftRackApp.new(
26
+ # multiplexed_processor,
27
+ # protocol_factoreis_map,
28
+ # )
29
+ # run thrift_app
30
+ class TracedProtocolFactory < ::Thrift::BaseProtocolFactory
31
+ def initialize(protocol_factory, config: Config.new)
32
+ @protocol_factory = protocol_factory
33
+ @config = config
34
+ end
35
+
36
+ def get_protocol(trans)
37
+ protocol = protocol_factory.get_protocol(trans)
38
+ TracedProtocol.new(protocol, config: config)
39
+ end
40
+
41
+ private
42
+
43
+ attr_reader :protocol_factory,
44
+ :config
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenTracing
4
+ module Instrumentation
5
+ VERSION = File.read(File.expand_path('../../../GEM_VERSION', __dir__)).strip
6
+ end
7
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'opentracing/instrumentation/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'opentracing-instrumentation'
9
+ spec.version = OpenTracing::Instrumentation::VERSION
10
+ spec.authors = ['Fedorenko Dmitrij']
11
+ spec.email = ['job@fedorenko-d.ru']
12
+
13
+ spec.summary = 'OpenTracing instrumentation for popular ruby libraries'
14
+ spec.description = 'OpenTracing instrumentation for Mongo, Faraday, Rack and PORO'
15
+ spec.homepage = 'https://gitlab.com/c0va23/ruby-opentracing-instrumentation.'
16
+ spec.license = 'MIT'
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_runtime_dependency 'json', '~> 2.0'
28
+ spec.add_runtime_dependency 'opentracing', '~> 0.5.0'
29
+
30
+ spec.add_development_dependency 'bson', '~> 4.0'
31
+ spec.add_development_dependency 'bundler', File.read('.BUNDLER_VERSION').strip
32
+ spec.add_development_dependency 'faraday', '~> 0.9.2'
33
+ spec.add_development_dependency 'rack', '~> 2.2.2'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'redis', '~> 3.3.5'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ spec.add_development_dependency 'rubocop', '~> 0.80.0'
38
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.38.1'
39
+ spec.add_development_dependency 'thrift', '~> 0.11.0'
40
+ end
metadata ADDED
@@ -0,0 +1,255 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opentracing-instrumentation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fedorenko Dmitrij
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: opentracing
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bson
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.1.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 2.1.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.2.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.2.2
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: redis
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 3.3.5
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 3.3.5
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.80.0
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.80.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 1.38.1
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 1.38.1
167
+ - !ruby/object:Gem::Dependency
168
+ name: thrift
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 0.11.0
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 0.11.0
181
+ description: OpenTracing instrumentation for Mongo, Faraday, Rack and PORO
182
+ email:
183
+ - job@fedorenko-d.ru
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".BUNDLER_VERSION"
189
+ - ".drone.jsonnet"
190
+ - ".gitignore"
191
+ - ".gitlab-ci.yml"
192
+ - ".rubocop.yml"
193
+ - ".ruby-version"
194
+ - GEM_VERSION
195
+ - Gemfile
196
+ - Gemfile.lock
197
+ - LICENSE.txt
198
+ - README.md
199
+ - Rakefile
200
+ - bin/console
201
+ - bin/setup
202
+ - lib/opentracing/instrumentation.rb
203
+ - lib/opentracing/instrumentation/common.rb
204
+ - lib/opentracing/instrumentation/common/error_writer.rb
205
+ - lib/opentracing/instrumentation/faraday.rb
206
+ - lib/opentracing/instrumentation/faraday/response_logger.rb
207
+ - lib/opentracing/instrumentation/faraday/trace_middleware.rb
208
+ - lib/opentracing/instrumentation/mongo.rb
209
+ - lib/opentracing/instrumentation/mongo/direct_sanitazer.rb
210
+ - lib/opentracing/instrumentation/mongo/query_sanitazer.rb
211
+ - lib/opentracing/instrumentation/mongo/trace_subscriber.rb
212
+ - lib/opentracing/instrumentation/object_wrapper.rb
213
+ - lib/opentracing/instrumentation/rack.rb
214
+ - lib/opentracing/instrumentation/rack/http_tagger.rb
215
+ - lib/opentracing/instrumentation/rack/trace_middleware.rb
216
+ - lib/opentracing/instrumentation/redis.rb
217
+ - lib/opentracing/instrumentation/redis/config.rb
218
+ - lib/opentracing/instrumentation/redis/span_builder.rb
219
+ - lib/opentracing/instrumentation/redis/tracing_driver_wrapper.rb
220
+ - lib/opentracing/instrumentation/sidekiq.rb
221
+ - lib/opentracing/instrumentation/sidekiq/client_middleware.rb
222
+ - lib/opentracing/instrumentation/sidekiq/job_tagger.rb
223
+ - lib/opentracing/instrumentation/sidekiq/server_middleware.rb
224
+ - lib/opentracing/instrumentation/sinatra.rb
225
+ - lib/opentracing/instrumentation/sinatra/trace_middleware.rb
226
+ - lib/opentracing/instrumentation/thrift.rb
227
+ - lib/opentracing/instrumentation/thrift/config.rb
228
+ - lib/opentracing/instrumentation/thrift/traced_protocol.rb
229
+ - lib/opentracing/instrumentation/thrift/traced_protocol_factory.rb
230
+ - lib/opentracing/instrumentation/version.rb
231
+ - opentracing-instrumentation.gemspec
232
+ homepage: https://gitlab.com/c0va23/ruby-opentracing-instrumentation.
233
+ licenses:
234
+ - MIT
235
+ metadata: {}
236
+ post_install_message:
237
+ rdoc_options: []
238
+ require_paths:
239
+ - lib
240
+ required_ruby_version: !ruby/object:Gem::Requirement
241
+ requirements:
242
+ - - ">="
243
+ - !ruby/object:Gem::Version
244
+ version: '0'
245
+ required_rubygems_version: !ruby/object:Gem::Requirement
246
+ requirements:
247
+ - - ">="
248
+ - !ruby/object:Gem::Version
249
+ version: '0'
250
+ requirements: []
251
+ rubygems_version: 3.1.2
252
+ signing_key:
253
+ specification_version: 4
254
+ summary: OpenTracing instrumentation for popular ruby libraries
255
+ test_files: []