opentracing-instrumentation 0.1.11 → 0.1.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 20f3950f2e40465d4758cd48491a59cd3b6bd46371cbd53f4f4b3b2fa6d58ffe
4
- data.tar.gz: d1fce50ac04f282113711fa3dc6658309df23b1f572ecc423bdf64e6b56ff0b6
3
+ metadata.gz: f70d5c2d46c230465ea78fc83db7690a40647f16bc22e4290838993828247cd9
4
+ data.tar.gz: '097e36d147eca7b17a44bcd7ba1e427fc472af82f3a276a93f92e4c9bfd3592f'
5
5
  SHA512:
6
- metadata.gz: a55c3242806bae23d495ffbe5b90b2f54383214350824a3a059e7d2c0f2eef0960b754c9926ba07c6d5603a4686416404e242c882cd8dfa0cb8671588b14e0f7
7
- data.tar.gz: 0ea4451a818e76213759257778a6ceee3538a08a8b5b2be8cf560eb18cacf6371b124eb8e96edea2b7d889d7b154b0d6a300d8bc36a42ba6ac95ff5255a58717
6
+ metadata.gz: b77c0678115a4ef11986fb2596ea844315474b0c4301d53c33aae33d30f35b02a55658089ecd46a9b8be060681c5f5dbe7bcffeec8cffcfd360f8b2d2624ebcf
7
+ data.tar.gz: acf108f2e70b483cb313aa5c0b2780b765fcf9896c4c4f7a80042322bca9e7695d6045c8fbd9a84df686525141be9dcd872baae55e540557cdf4650006a6f1ad
data/GEM_VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.11
1
+ 0.1.16
data/Gemfile.lock CHANGED
@@ -18,7 +18,7 @@ GIT
18
18
  PATH
19
19
  remote: .
20
20
  specs:
21
- opentracing-instrumentation (0.1.11)
21
+ opentracing-instrumentation (0.1.16)
22
22
  json
23
23
  opentracing (~> 0.5.0)
24
24
 
@@ -32,6 +32,11 @@ module OpenTracing
32
32
  log_error(span, exception, event)
33
33
  end
34
34
 
35
+ def ==(other)
36
+ set_error_tag == other.set_error_tag &&
37
+ log_error_event == other.log_error_event
38
+ end
39
+
35
40
  private
36
41
 
37
42
  def tag_error(span)
@@ -10,19 +10,16 @@ module OpenTracing
10
10
  class TraceSubscriber
11
11
  include Forwardable
12
12
 
13
- DEFAULT_OPERATION_NAME_PATTERN = \
14
- 'mongo(collection=%<database>s.%<collection>s, command=%<command>s)'
15
-
16
13
  def initialize(
17
14
  tracer: OpenTracing.global_tracer,
18
15
  scope_store: {},
19
- operation_name_pattern: DEFAULT_OPERATION_NAME_PATTERN,
16
+ operation_name_builder: OperationNameBuilder.new,
20
17
  sanitazer: QuerySanitazer.new
21
18
  )
22
19
  @tracer = tracer
23
20
  @monitor = Monitor.new
24
21
  @scope_store = scope_store
25
- @operation_name_pattern = operation_name_pattern
22
+ @operation_name_builder = operation_name_builder
26
23
  @sanitazer = sanitazer
27
24
  end
28
25
 
@@ -49,18 +46,11 @@ module OpenTracing
49
46
  attr_reader :tracer
50
47
  attr_reader :scope_store
51
48
  attr_reader :monitor
52
- attr_reader :operation_name_pattern
49
+ attr_reader :operation_name_builder
53
50
  attr_reader :sanitazer
54
51
 
55
52
  def build_operation_name(event)
56
- command_name = event.command_name
57
- collection_name = event.command[command_name]
58
- format(
59
- operation_name_pattern,
60
- database: event.database_name,
61
- collection: collection_name,
62
- command: command_name,
63
- )
53
+ operation_name_builder.build_operation_name(event)
64
54
  end
65
55
 
66
56
  def base_tags(event)
@@ -4,6 +4,8 @@ module OpenTracing
4
4
  module Instrumentation
5
5
  # Rack tracing middlewares
6
6
  module Rack
7
+ autoload :ExtractMiddleware,
8
+ 'opentracing/instrumentation/rack/extract_middleware'
7
9
  autoload :HttpTagger, 'opentracing/instrumentation/rack/http_tagger'
8
10
  autoload :RegexpHostSanitazer,
9
11
  'opentracing/instrumentation/rack/regexp_host_sanitazer'
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rack'
4
+
5
+ module OpenTracing
6
+ module Instrumentation
7
+ module Rack
8
+ # ExtractMiddleware extract trace context and push it to scope manager.
9
+ class ExtractMiddleware
10
+ # @private
11
+ class FakeSpan
12
+ attr_reader :context
13
+
14
+ def initialize(context)
15
+ @context = context
16
+ end
17
+ end
18
+
19
+ # @param app [RackApp] inner rack application
20
+ # @param tracer [OpenTracing::Tracer]
21
+ # @param logger [Logger]
22
+ def initialize(
23
+ app,
24
+ tracer: OpenTracing.global_tracer,
25
+ logger: nil
26
+ )
27
+ @app = app
28
+ @tracer = tracer
29
+ @logger = logger
30
+ end
31
+
32
+ # @param env [Hash<String, String>] rack env
33
+ def call(env)
34
+ span_context = @tracer.extract(OpenTracing::FORMAT_RACK, env)
35
+ return @app.call(env) unless span_context
36
+
37
+ with_scope(span_context) do
38
+ @app.call(env)
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def with_scope(span_context)
45
+ scope = safe_create_scope(span_context)
46
+
47
+ yield
48
+ ensure
49
+ safe_close_scope(scope)
50
+ end
51
+
52
+ def safe_create_scope(span_context)
53
+ fake_span = FakeSpan.new(span_context)
54
+ @tracer.scope_manager.activate(fake_span, finish_on_close: false)
55
+ rescue StandardError => e
56
+ @logger&.error(e)
57
+ nil
58
+ end
59
+
60
+ def safe_close_scope(scope)
61
+ scope&.close
62
+ rescue StandardError => e
63
+ @logger&.error(e)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -62,6 +62,8 @@ module OpenTracing
62
62
 
63
63
  def extract_references(job)
64
64
  span_context = tracer.extract(OpenTracing::FORMAT_TEXT_MAP, job)
65
+ return unless span_context
66
+
65
67
  [OpenTracing::Reference.follows_from(span_context)]
66
68
  end
67
69
  end
@@ -15,6 +15,14 @@ module OpenTracing
15
15
  ::Thrift::MessageTypes::ONEWAY => 'ONEWAY',
16
16
  }.freeze
17
17
 
18
+ autoload :TracedProcessor,
19
+ module_path + '/traced_processor'
20
+ autoload :TracedProcessorConfig,
21
+ module_path + '/traced_processor_config'
22
+ autoload :TracedProcessorOperationNameBuilder,
23
+ module_path + '/traced_processor_operation_name_builder'
24
+ autoload :TracedProcessorTagsBuilder,
25
+ module_path + '/traced_processor_tags_builder'
18
26
  autoload :TracedProtocol,
19
27
  module_path + '/traced_protocol'
20
28
  autoload :TracedProtocolConfig,
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenTracing
4
+ module Instrumentation
5
+ module Thrift
6
+ # Trace thrift processor
7
+ #
8
+ # Usage:
9
+ # processor =
10
+ # OrderService::Processor.new(orders_handler)
11
+ # traced_processor =
12
+ # OpenTracing::Instrumentation::Thrift::TracedProcessor.new(processor)
13
+ class TracedProcessor
14
+ extend Forwardable
15
+
16
+ # @private
17
+ class ReadCachedProtocol
18
+ include ::Thrift::ProtocolDecorator
19
+
20
+ def read_message_begin
21
+ @read_message_begin ||= @protocol.read_message_begin
22
+ end
23
+
24
+ def ==(other)
25
+ @protocol == other.protocol
26
+ end
27
+
28
+ protected
29
+
30
+ attr_reader :protocol
31
+ end
32
+
33
+ # @parama processor [Thrift::Processor] traced processor
34
+ # @param config [TracedProcessorConfig]
35
+ # @yieldparam [TracedProcessorConfig]
36
+ def initialize(processor, config: TracedProcessorConfig.new)
37
+ @processor = processor
38
+ yield config if block_given?
39
+ @config = config.dup
40
+ end
41
+
42
+ # @param iproto [Thrift::Protocol] input protocol
43
+ # @param oproto [Thrift::Protocol] output protocol
44
+ def process(iproto, oproto)
45
+ trace_process(iproto) do |cached_iproto|
46
+ processor.process(
47
+ wrap_protocol(cached_iproto),
48
+ wrap_protocol(oproto),
49
+ )
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ attr_reader :processor
56
+ attr_reader :config
57
+
58
+ def_delegators :config,
59
+ :tracer,
60
+ :trace_protocol,
61
+ :error_writer,
62
+ :operation_name_builder,
63
+ :tags_builder,
64
+ :logger
65
+
66
+ def trace_process(iproto)
67
+ cached_iproto = ReadCachedProtocol.new(iproto)
68
+
69
+ start_time = Time.now
70
+
71
+ name, type, seq_id = cached_iproto.read_message_begin
72
+
73
+ scope = safe_start_scope(iproto, name, type, seq_id, start_time)
74
+
75
+ yield cached_iproto
76
+ rescue StandardError => e
77
+ error_writer.write_error(scope.span, e) if scope&.span
78
+ raise e
79
+ ensure
80
+ safe_close_scope(scope)
81
+ end
82
+
83
+ def safe_start_scope(protocol, name, type, seq_id, start_time)
84
+ operation_name = operation_name_builder.build(name, type, seq_id)
85
+ tags = tags_builder.build_tags(protocol, name, type)
86
+ tracer.start_active_span(
87
+ operation_name,
88
+ start_time: start_time,
89
+ tags: tags,
90
+ )
91
+ rescue StandardError => e
92
+ logger&.error(e)
93
+ end
94
+
95
+ def safe_close_scope(scope)
96
+ scope&.close
97
+ rescue StandardError => e
98
+ logger&.error(e)
99
+ end
100
+
101
+ def wrap_protocol(protocol)
102
+ return protocol unless trace_protocol
103
+
104
+ TracedProtocol.new(protocol) do |config|
105
+ config.tracer = tracer
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenTracing
4
+ module Instrumentation
5
+ module Thrift
6
+ # Config of ThriftProcessor
7
+ class TracedProcessorConfig
8
+ # @return [OpenTracing::Tracer]
9
+ attr_accessor :tracer
10
+
11
+ # @return [Boolean] enabled wrap protocol into TracedProtocol
12
+ attr_accessor :trace_protocol
13
+
14
+ # @return [TracedProcessorOperationNameBuilder]
15
+ attr_accessor :operation_name_builder
16
+
17
+ # @return [TracedProcessorTagsBuilder]
18
+ attr_accessor :tags_builder
19
+
20
+ # @return [Common::ErrorWriter]
21
+ attr_accessor :error_writer
22
+
23
+ # @return [Logger] used for log errors. If nil (by default), then logging disabled,
24
+ attr_accessor :logger
25
+
26
+ def initialize
27
+ @tracer = OpenTracing.global_tracer
28
+ @trace_protocol = true
29
+ @operation_name_builder = TracedProcessorOperationNameBuilder.new
30
+ @tags_builder = TracedProcessorTagsBuilder.new
31
+ @error_writer = Common::ErrorWriter.new
32
+ @logger = nil
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenTracing
4
+ module Instrumentation
5
+ module Thrift
6
+ # Thrift processor trace operation name builder
7
+ class TracedProcessorOperationNameBuilder
8
+ DEFAULT_OPERATION_NAME_TEMPALTE = \
9
+ 'thrift_processor(%<name>s)'
10
+
11
+ # @param operation_name_tempalte [String]
12
+ def initialize(
13
+ operation_name_template: DEFAULT_OPERATION_NAME_TEMPALTE
14
+ )
15
+ @operation_name_template = operation_name_template
16
+ end
17
+
18
+ # @param name [String] method name
19
+ # @param type [Integer] type of message
20
+ # @param seq_id [Integer] numberr of message
21
+ # @return [String] operation name
22
+ def build(name, type, seq_id)
23
+ format_args = build_format_args(name, type, seq_id)
24
+ format(operation_name_template, **format_args)
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :operation_name_template
30
+
31
+ def build_format_args(name, type, seq_id)
32
+ {
33
+ name: name,
34
+ type: MESSAGE_TYPES[type],
35
+ seq_id: seq_id,
36
+ }
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenTracing
4
+ module Instrumentation
5
+ module Thrift
6
+ # Tags builder for TracedProcessor
7
+ class TracedProcessorTagsBuilder < TracedProtocolTagsBuilder
8
+ DEFAULT_STATIC_TAGS = {
9
+ 'span.kind' => 'server',
10
+ 'component' => 'thrift',
11
+ }.freeze
12
+
13
+ def initialize(static_tags: DEFAULT_STATIC_TAGS)
14
+ @static_tags = static_tags
15
+ end
16
+
17
+ def build_tags(protocol, name, type)
18
+ static_tags
19
+ .merge(build_protocol_tags(protocol))
20
+ .merge(build_message_tags(name, type))
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :static_tags
26
+ end
27
+ end
28
+ end
29
+ end
@@ -33,26 +33,32 @@ module OpenTracing
33
33
  super(protocol)
34
34
  @config = config.dup
35
35
  yield @config if block_given?
36
- @static_tags = config.tags_builder.build_static_tags(protocol)
36
+ @protocol_tags = config.tags_builder.build_protocol_tags(protocol)
37
+ end
38
+
39
+ def ==(other)
40
+ protocol == other.protocol &&
41
+ config == other.config &&
42
+ protocol_tags == other.protocol_tags
37
43
  end
38
44
 
39
45
  def write_message_begin(name, type, seqid)
40
- self.write_scope = \
41
- safe_start_active_span(WRITE_DIRECTION, name, type)
46
+ self.write_span = \
47
+ safe_start_span(WRITE_DIRECTION, name, type)
42
48
 
43
49
  # Call protocol instaed super, beacaus ProtocolDecorator do not
44
50
  # pass arguments to protocol.write_message_begin
45
51
  protocol.write_message_begin(name, type, seqid)
46
52
  rescue StandardError => e
47
- write_error(write_scope&.span, e)
48
- safe_close_span(write_scope)
53
+ write_error(write_span, e)
54
+ safe_close_span(write_span)
49
55
  raise e
50
56
  end
51
57
 
52
58
  def write_message_end
53
59
  super
54
60
  ensure
55
- safe_close_span(write_scope)
61
+ safe_close_span(write_span)
56
62
  end
57
63
 
58
64
  def read_message_begin
@@ -60,9 +66,9 @@ module OpenTracing
60
66
 
61
67
  name, type, rseqid = super
62
68
 
63
- self.read_scope = \
64
- safe_start_active_span(READ_DIRECTION, name, type,
65
- start_time: start_time)
69
+ self.read_span = \
70
+ safe_start_span(READ_DIRECTION, name, type,
71
+ start_time: start_time)
66
72
 
67
73
  [name, type, rseqid]
68
74
  end
@@ -70,25 +76,28 @@ module OpenTracing
70
76
  def read_message_end
71
77
  super
72
78
  ensure
73
- safe_close_span(read_scope)
79
+ safe_close_span(read_span)
74
80
  end
75
81
 
76
- private
82
+ protected
77
83
 
78
- attr_reader :static_tags
84
+ attr_reader :protocol_tags
79
85
  attr_reader :protocol
86
+ attr_reader :config
80
87
 
81
- def_delegators :@config,
88
+ private
89
+
90
+ def_delegators :config,
82
91
  :tracer,
83
92
  :tags_builder,
84
93
  :operation_name_builder,
85
94
  :error_writer,
86
95
  :logger
87
96
 
88
- attr_accessor :write_scope,
89
- :read_scope
97
+ attr_accessor :write_span,
98
+ :read_span
90
99
 
91
- def safe_start_active_span(
100
+ def safe_start_span(
92
101
  direction,
93
102
  name,
94
103
  type,
@@ -97,7 +106,7 @@ module OpenTracing
97
106
  operation_name = build_operation_name(direction, name, type)
98
107
  request_tags = build_tags(name, type)
99
108
 
100
- tracer.start_active_span(
109
+ tracer.start_span(
101
110
  operation_name,
102
111
  tags: request_tags,
103
112
  start_time: start_time,
@@ -106,10 +115,10 @@ module OpenTracing
106
115
  logger&.error(e)
107
116
  end
108
117
 
109
- def safe_close_span(scope)
110
- return if scope.nil?
118
+ def safe_close_span(span)
119
+ return if span.nil?
111
120
 
112
- scope.close
121
+ span.finish
113
122
  rescue StandardError => e
114
123
  logger&.error(e)
115
124
  end
@@ -131,7 +140,7 @@ module OpenTracing
131
140
  end
132
141
 
133
142
  def build_tags(name, type)
134
- static_tags
143
+ protocol_tags
135
144
  .merge(tags_builder.build_message_tags(name, type))
136
145
  .compact
137
146
  end
@@ -24,6 +24,13 @@ module OpenTracing
24
24
  @error_writer = error_writer
25
25
  @logger = logger
26
26
  end
27
+
28
+ def ==(other)
29
+ tracer == other.tracer &&
30
+ tags_builder == other.tags_builder &&
31
+ operation_name_builder == other.operation_name_builder &&
32
+ error_writer == other.error_writer
33
+ end
27
34
  end
28
35
  end
29
36
  end
@@ -10,8 +10,6 @@ module OpenTracing
10
10
  DEFAULT_OPERATION_NAME_PATTERN = \
11
11
  'thrift(direction=%<direction>s, name=%<name>s, type=%<type>s)'
12
12
 
13
- attr_reader :operation_name_pattern
14
-
15
13
  # @param operation_name_pattern [String]
16
14
  def initialize(
17
15
  operation_name_pattern: DEFAULT_OPERATION_NAME_PATTERN
@@ -28,6 +26,14 @@ module OpenTracing
28
26
  format(operation_name_pattern, **format_args)
29
27
  end
30
28
 
29
+ def ==(other)
30
+ operation_name_pattern == other.operation_name_pattern
31
+ end
32
+
33
+ protected
34
+
35
+ attr_reader :operation_name_pattern
36
+
31
37
  private
32
38
 
33
39
  def build_format_args(direction, name, type)
@@ -17,7 +17,7 @@ module OpenTracing
17
17
  }.merge(error_tags(type))
18
18
  end
19
19
 
20
- def build_static_tags(protocol)
20
+ def build_protocol_tags(protocol)
21
21
  transport = protocol.trans
22
22
  {
23
23
  'thrift.protocol' => build_protocol_name(protocol),
@@ -25,6 +25,10 @@ module OpenTracing
25
25
  }
26
26
  end
27
27
 
28
+ def ==(other)
29
+ self.class == other.class
30
+ end
31
+
28
32
  private
29
33
 
30
34
  NAME_PATTER = /((?<service_name>\w+):)?(?<method>\w+)/.freeze
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opentracing-instrumentation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fedorenko Dmitrij
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-01 00:00:00.000000000 Z
11
+ date: 2020-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -301,6 +301,7 @@ files:
301
301
  - lib/opentracing/instrumentation/mongo/trace_subscriber.rb
302
302
  - lib/opentracing/instrumentation/object_wrapper.rb
303
303
  - lib/opentracing/instrumentation/rack.rb
304
+ - lib/opentracing/instrumentation/rack/extract_middleware.rb
304
305
  - lib/opentracing/instrumentation/rack/http_tagger.rb
305
306
  - lib/opentracing/instrumentation/rack/regexp_host_sanitazer.rb
306
307
  - lib/opentracing/instrumentation/rack/regexp_path_sanitazer.rb
@@ -319,6 +320,10 @@ files:
319
320
  - lib/opentracing/instrumentation/sinatra.rb
320
321
  - lib/opentracing/instrumentation/sinatra/trace_middleware.rb
321
322
  - lib/opentracing/instrumentation/thrift.rb
323
+ - lib/opentracing/instrumentation/thrift/traced_processor.rb
324
+ - lib/opentracing/instrumentation/thrift/traced_processor_config.rb
325
+ - lib/opentracing/instrumentation/thrift/traced_processor_operation_name_builder.rb
326
+ - lib/opentracing/instrumentation/thrift/traced_processor_tags_builder.rb
322
327
  - lib/opentracing/instrumentation/thrift/traced_protocol.rb
323
328
  - lib/opentracing/instrumentation/thrift/traced_protocol_config.rb
324
329
  - lib/opentracing/instrumentation/thrift/traced_protocol_factory.rb