opentracing-instrumentation 0.1.14 → 0.1.15

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: 0deff013f5e114188fd45e5babe206deb828806074e5d5cdb64d2d0bd12039b9
4
- data.tar.gz: b9db37bc148a276e95f6b9b843c8524ac0fcb81b9a136aa9e95532f7d18f9d25
3
+ metadata.gz: b1d6a7f7070f79e0d57f243777fa02a2b8ac6d289b1f5bf54a3c47c171f86603
4
+ data.tar.gz: fb4a1c4b4260c32be8e5b12b6cc48eec27cdf7bf220cb6bb82fba9fd7ed45b3d
5
5
  SHA512:
6
- metadata.gz: 7dac1db7d02ba70d447d2db46f28b31a2549e2d9636a9ead1013940af8e9338d5d75dfa6ce86a6d71b8eabbc26f17119be80b1110a9b0ccc3be8013f237b78a9
7
- data.tar.gz: c604dfdd5b4f0e77f7bb5c29cc3a2c11e77540e6d8c69b901509af2f89c48575d6ff57cdc44d97e4f52ad4815a842684a16a723bfa874daa4ec9735fbc043e11
6
+ metadata.gz: 11b622db2d6df23aaa474cb5ad37a34c5a2bcd9136e92c0d966b9c36bc8cbea7ee5c3d85d31e47ceb44010dbc487b94042dedb23cf505d51ee2c1a9b8e5e700c
7
+ data.tar.gz: 1623f9d0ca9a70ce161db8c9350cc3228932aa24e1367b0e2beb069b920747eeffd406c34b9949c08ab0f11371a033d0202e593aa2b0c12d72c18f06bb593032
data/GEM_VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.14
1
+ 0.1.15
data/Gemfile.lock CHANGED
@@ -18,7 +18,7 @@ GIT
18
18
  PATH
19
19
  remote: .
20
20
  specs:
21
- opentracing-instrumentation (0.1.14)
21
+ opentracing-instrumentation (0.1.15)
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)
@@ -18,15 +18,21 @@ module OpenTracing
18
18
 
19
19
  # @param app [RackApp] inner rack application
20
20
  # @param tracer [OpenTracing::Tracer]
21
- def initialize(app, tracer: OpenTracing.global_tracer)
21
+ # @param logger [Logger]
22
+ def initialize(
23
+ app,
24
+ tracer: OpenTracing.global_tracer,
25
+ logger: nil
26
+ )
22
27
  @app = app
23
28
  @tracer = tracer
29
+ @logger = logger
24
30
  end
25
31
 
26
32
  # @param env [Hash<String, String>] rack env
27
33
  def call(env)
28
34
  span_context = @tracer.extract(OpenTracing::FORMAT_RACK, env)
29
- return @app.call unless span_context
35
+ return @app.call(env) unless span_context
30
36
 
31
37
  with_scope(span_context) do
32
38
  @app.call(env)
@@ -36,12 +42,25 @@ module OpenTracing
36
42
  private
37
43
 
38
44
  def with_scope(span_context)
39
- fake_span = FakeSpan.new(span_context)
40
- scope = @tracer.scope_manager.activate(fake_span, finish_on_close: false)
45
+ scope = safe_create_scope(span_context)
41
46
 
42
47
  yield
43
48
  ensure
44
- scope.close
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)
45
64
  end
46
65
  end
47
66
  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_reader :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.14
4
+ version: 0.1.15
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-02 00:00:00.000000000 Z
11
+ date: 2020-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -320,6 +320,10 @@ files:
320
320
  - lib/opentracing/instrumentation/sinatra.rb
321
321
  - lib/opentracing/instrumentation/sinatra/trace_middleware.rb
322
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
323
327
  - lib/opentracing/instrumentation/thrift/traced_protocol.rb
324
328
  - lib/opentracing/instrumentation/thrift/traced_protocol_config.rb
325
329
  - lib/opentracing/instrumentation/thrift/traced_protocol_factory.rb