zipkin-tracer 0.45.0 → 0.46.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d468909c2b28497b50dba2901e92f651ec45b555cca6fc1453258140e996f0d
4
- data.tar.gz: 79508ca6c29d18906610ddf0950e33951e05f5db185e169af895a3982df3c40d
3
+ metadata.gz: facfab4311d80aff894d2c5dec5253ef122ee5206328d875e41d2174373d060e
4
+ data.tar.gz: eb036ff2009dc9b70933881c12a93cead129127679bbdf9c538e6df067dc04c7
5
5
  SHA512:
6
- metadata.gz: e3f979dfa0f2be704ab85940dd4e6d6738fd9c587750b04929304eb72b55b122375a7e1c3e8ac2b8fac238aa8c154d24a8dae86e07bdf36f1d0e974a604cdbf5
7
- data.tar.gz: 8bc0b4f4b2ee75570563d0a4aaba3823a8f6b6a8ccb1c99c0063e38726e9cedf749ab9722be3eb3837db0947e8f43b2ee44c47df6a8e6f378815adf7a6ceca58
6
+ metadata.gz: 6f99f5a59fdfd10c968fb99779aa0c7a97a559b45a36a787c886cac40845964e5cb98a5048313094ecab328d420ed3b01936cd66e1929389fef2e5eed1401b44
7
+ data.tar.gz: 5edb8af0059d1486a1105cf325ec21ce548f795f0a5078b67e8846d0cda23d05db2ab83075a27d1f6f46b173c10bd324af0683e26bc7ea5bd0695424e13880ba
@@ -1,3 +1,6 @@
1
+ # 0.46.0
2
+ * Add Amazon SQS tracer.
3
+
1
4
  # 0.45.0
2
5
  * Add a `trace_context` option to the TraceWrapper utility class to retrieve trace data.
3
6
 
data/README.md CHANGED
@@ -81,6 +81,38 @@ end
81
81
 
82
82
  By default workers aren't traced. You can specify the workers that you want to trace with traceable_workers config option. If you want all your workers to be traced pass [:all] to traceable_workers option (traceable_workers: [:all]).
83
83
 
84
+ ### Tracing Amazon SQS messages
85
+
86
+ Amazon SQS tracing can be turned on by requiring [zipkin-tracer/sqs/adapter](lib/zipkin-tracer/sqs/adapter.rb):
87
+ ```ruby
88
+ require 'zipkin-tracer/sqs/adapter'
89
+ ```
90
+
91
+ This SQS adapter overrides the `send_message` and `send_message_batch` methods to add trace data as message attributes and to generate a producer span when the methods are called. Since all SQS messages are affected, it is not recommended to use this feature with the [SQS sender](lib/zipkin-tracer/zipkin_sqs_sender.rb).
92
+
93
+ When receiving messages, you need to pass the `message_attribute_names: ['All']` option to retrive message attributes:
94
+ ```ruby
95
+ resp = sqs.receive_message(
96
+ queue_url: queue_url,
97
+ message_attribute_names: ['All']
98
+ )
99
+ ```
100
+
101
+ Then you can utilize the [TraceWrapper](#tracewrapper) class to generate a consumer span:
102
+ ```ruby
103
+ msg = resp.messages.first
104
+ trace_context = msg.message_attributes.each_with_object({}) { |(key, value), hsh| hsh[key.to_sym] = value.string_value }
105
+
106
+ TraceWrapper.wrap_in_custom_span(config, 'receive_message',
107
+ span_kind: Trace::Span::Kind::CONSUMER,
108
+ trace_context: trace_context
109
+ ) do |span|
110
+ span.remote_endpoint = Trace::Endpoint.remote_endpoint(nil, 'amazon-sqs')
111
+ span.record_tag('queue.url', queue_url)
112
+ :
113
+ end
114
+ ```
115
+
84
116
  ### Local tracing
85
117
 
86
118
  `ZipkinTracer::TraceClient` provides an API to record local traces in your application.
@@ -11,15 +11,17 @@ module ZipkinTracer
11
11
 
12
12
  each_endpoint(spans) do |endpoint|
13
13
  hostname = endpoint.ipv4
14
- unless resolved_ip_address?(hostname.to_s)
15
- endpoint.ipv4 = resolved_hosts[hostname]
16
- end
14
+ next unless hostname
15
+ next if resolved_ip_address?(hostname.to_s)
16
+
17
+ endpoint.ipv4 = resolved_hosts[hostname]
17
18
  end
18
19
  end
19
20
 
20
21
  private
22
+
21
23
  LOCALHOST = '127.0.0.1'.freeze
22
- LOCALHOST_I32 = 0x7f000001.freeze
24
+ LOCALHOST_I32 = 0x7f000001
23
25
  MAX_I32 = ((2 ** 31) - 1)
24
26
  MASK = (2 ** 32) - 1
25
27
  IP_FIELD = 3
@@ -49,10 +51,9 @@ module ZipkinTracer
49
51
  end
50
52
 
51
53
  def resolve(hosts, ip_format)
52
- hosts.inject({}) do |host_map, host|
54
+ hosts.each_with_object({}) do |host, host_map|
53
55
  hostname = host.ipv4 # This field has been temporarly used to store the hostname.
54
- host_map[hostname] = host_to_format(hostname, ip_format)
55
- host_map
56
+ host_map[hostname] = host_to_format(hostname, ip_format) if hostname
56
57
  end
57
58
  end
58
59
 
@@ -0,0 +1,4 @@
1
+ require 'aws-sdk-sqs'
2
+ require 'zipkin-tracer/sqs/zipkin-tracer'
3
+
4
+ Aws::SQS::Client.prepend(ZipkinTracer::SqsHandler)
@@ -0,0 +1,57 @@
1
+ module ZipkinTracer
2
+ # This module is designed to prepend to the SQS client to add trace data as message attributes.
3
+ # https://github.com/aws/aws-sdk-ruby/blob/master/gems/aws-sdk-sqs/lib/aws-sdk-sqs/client.rb
4
+ module SqsHandler
5
+ def send_message(params = {}, options = {})
6
+ zipkin_sqs_trace_wrapper(params, __method__) { |params_with_trace| super(params_with_trace, options) }
7
+ end
8
+
9
+ def send_message_batch(params = {}, options = {})
10
+ zipkin_sqs_trace_wrapper(params, __method__) { |params_with_trace| super(params_with_trace, options) }
11
+ end
12
+
13
+ private
14
+
15
+ ZIPKIN_KEYS = %i[trace_id parent_id span_id sampled].freeze
16
+ ZIPKIN_REMOTE_ENDPOINT_SQS = Trace::Endpoint.remote_endpoint(nil, 'amazon-sqs')
17
+
18
+ def zipkin_sqs_trace_wrapper(params, method_name)
19
+ trace_id = TraceGenerator.new.next_trace_id
20
+ zipkin_set_message_attributes(params, method_name, trace_id)
21
+
22
+ TraceContainer.with_trace_id(trace_id) do
23
+ if Trace.tracer && trace_id.sampled?
24
+ Trace.tracer.with_new_span(trace_id, method_name) do |span|
25
+ span.kind = Trace::Span::Kind::PRODUCER
26
+ span.remote_endpoint = ZIPKIN_REMOTE_ENDPOINT_SQS
27
+ span.record_tag('queue.url', params[:queue_url])
28
+ yield(params)
29
+ end
30
+ else
31
+ yield(params)
32
+ end
33
+ end
34
+ end
35
+
36
+ def zipkin_set_message_attributes(params, method_name, trace_id)
37
+ attributes = zipkin_message_attributes(trace_id)
38
+ case method_name
39
+ when :send_message
40
+ params[:message_attributes] = attributes.merge(params[:message_attributes] || {})
41
+ when :send_message_batch
42
+ params[:entries].each do |entry|
43
+ entry[:message_attributes] = attributes.merge(entry[:message_attributes] || {})
44
+ end
45
+ end
46
+ end
47
+
48
+ def zipkin_message_attributes(trace_id)
49
+ ZIPKIN_KEYS.each_with_object({}) do |zipkin_key, message_attributes|
50
+ zipkin_value = trace_id.send(zipkin_key)
51
+ next unless zipkin_value
52
+
53
+ message_attributes[zipkin_key] = { string_value: zipkin_value.to_s, data_type: 'String' }
54
+ end
55
+ end
56
+ end
57
+ end
@@ -277,8 +277,8 @@ module Trace
277
277
  end
278
278
 
279
279
  def self.remote_endpoint(url, remote_service_name)
280
- service_name = remote_service_name || url.host.split('.').first || UNKNOWN_URL # default to url-derived service name
281
- Endpoint.new(url.host, url.port, service_name)
280
+ service_name = remote_service_name || url&.host&.split('.')&.first || UNKNOWN_URL # default to url-derived service name
281
+ Endpoint.new(url&.host, url&.port, service_name)
282
282
  end
283
283
 
284
284
  def to_h
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ZipkinTracer
4
- VERSION = '0.45.0'
4
+ VERSION = '0.46.0'
5
5
  end
@@ -7,8 +7,7 @@ module Trace
7
7
  # Senders dealing with zipkin should inherit from this class and implement the
8
8
  # flush! method which actually sends the information
9
9
  class ZipkinSenderBase
10
-
11
- def initialize(options={})
10
+ def initialize(options = {})
12
11
  @options = options
13
12
  reset
14
13
  end
@@ -40,7 +39,10 @@ module Trace
40
39
 
41
40
  def skip_flush?(span)
42
41
  return true if span.kind == Trace::Span::Kind::CLIENT && span.has_parent_span?
43
- return true if span.kind == Trace::Span::Kind::PRODUCER && spans.any? { |s| s.kind == Trace::Span::Kind::SERVER }
42
+
43
+ if span.kind == Trace::Span::Kind::PRODUCER
44
+ return true if spans.any? { |s| s.kind == Trace::Span::Kind::SERVER || s.kind == Trace::Span::Kind::CONSUMER }
45
+ end
44
46
  end
45
47
 
46
48
  def flush!
@@ -62,6 +64,5 @@ module Trace
62
64
  def reset
63
65
  Thread.current[THREAD_KEY] = []
64
66
  end
65
-
66
67
  end
67
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zipkin-tracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.45.0
4
+ version: 0.46.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franklin Hu
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2020-05-22 00:00:00.000000000 Z
17
+ date: 2020-05-29 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: faraday
@@ -241,6 +241,8 @@ files:
241
241
  - lib/zipkin-tracer/rack/zipkin-tracer.rb
242
242
  - lib/zipkin-tracer/rack/zipkin_env.rb
243
243
  - lib/zipkin-tracer/sidekiq/middleware.rb
244
+ - lib/zipkin-tracer/sqs/adapter.rb
245
+ - lib/zipkin-tracer/sqs/zipkin-tracer.rb
244
246
  - lib/zipkin-tracer/trace.rb
245
247
  - lib/zipkin-tracer/trace_client.rb
246
248
  - lib/zipkin-tracer/trace_container.rb