zipkin-tracer 0.35.0 → 0.35.1

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: 3145da3d93716701cfe338e782717243946ec9d7b8337a4f67f4680dd2b3b06c
4
- data.tar.gz: 857aa5010bbf1eaa5de81bf4bbfb17ba008c3e986e417740cdee6491e327fd7a
3
+ metadata.gz: '09555ccde6ee7aa47e75a36c70445a2611bab1218b518c399bfad8d76d341063'
4
+ data.tar.gz: 8235afc71c8685567466a11d28ce2bda7596b5e4784f51e266db1c85f085914c
5
5
  SHA512:
6
- metadata.gz: '085a771261ce989e0412d80080607872fd440e814877be5d2095c6a1d4d8fb8da7654583e52e940dece17406b425d26e76cced98419d866b5acd6970641d5f70'
7
- data.tar.gz: 3d1041e44ae618d3886f47169b871b827e1246e37d6c7536207ea1eaf0b5120da8a1e56d878610b7f27c38beac26f2567a551f675d70325012122899ac6d128b
6
+ metadata.gz: 897ad8d5b52dacfdd982f36440b07db9a478489c9396f6b92144a848d75a987af0e20f246e348348d3ef7fce9500aca6f1f698e144c7fc64ad80843d9ef1a36e
7
+ data.tar.gz: 97dd7106d21aff8dfa9147b02cfd2b9224bda862216c0909dce21708ada1464580d46bc024e5656634d9f63dffcba22703c3b073ebd7562349943d81fa8fce49
@@ -4,7 +4,7 @@ module ZipkinTracer
4
4
  # Determines if our framework knows whether the request will be routed to a controller
5
5
  def self.routable_request?(env)
6
6
  return true unless defined?(Rails) # If not running on a Rails app, we can't verify if it is invalid
7
- path_info = env[ZipkinTracer::RackHandler::PATH_INFO]
7
+ path_info = env[ZipkinTracer::RackHandler::PATH_INFO] || ""
8
8
  http_method = env[ZipkinTracer::RackHandler::REQUEST_METHOD]
9
9
  Rails.application.routes.recognize_path(path_info, method: http_method)
10
10
  true
@@ -48,6 +48,10 @@ module ZipkinTracer
48
48
 
49
49
  Trace.sample_rate = @sample_rate
50
50
  Trace.trace_id_128bit = @trace_id_128bit
51
+
52
+ Trace.default_endpoint = Trace::Endpoint.local_endpoint(
53
+ domain_service_name(@service_name)
54
+ )
51
55
  end
52
56
 
53
57
  def adapter
@@ -66,6 +70,12 @@ module ZipkinTracer
66
70
 
67
71
  private
68
72
 
73
+ # Use the Domain environment variable to extract the service name, otherwise use the default config name
74
+ # TODO: move to the config object
75
+ def domain_service_name(default_name)
76
+ ENV["DOMAIN"].to_s.empty? ? default_name : ENV["DOMAIN"].split('.').first
77
+ end
78
+
69
79
  DEFAULTS = {
70
80
  sample_rate: 0.1,
71
81
  sampled_as_boolean: true,
@@ -47,7 +47,7 @@ module ZipkinTracer
47
47
  end
48
48
 
49
49
  def remote_endpoint(url, service_name)
50
- Trace::Endpoint.remote_endpoint(url, service_name, Trace.default_endpoint.ip_format) # The endpoint we are calling.
50
+ Trace::Endpoint.remote_endpoint(url, service_name) # The endpoint we are calling.
51
51
  end
52
52
 
53
53
  def service_name(datum, default)
@@ -40,7 +40,7 @@ module ZipkinTracer
40
40
  # handle either a URI object (passed by Faraday v0.8.x in testing), or something string-izable
41
41
  method = env[:method].to_s
42
42
  url = env[:url].respond_to?(:host) ? env[:url] : URI.parse(env[:url].to_s)
43
- remote_endpoint = Trace::Endpoint.remote_endpoint(url, @service_name, Trace.default_endpoint.ip_format) # The endpoint we are calling.
43
+ remote_endpoint = Trace::Endpoint.remote_endpoint(url, @service_name) # The endpoint we are calling.
44
44
  Trace.tracer.with_new_span(trace_id, method.downcase) do |span|
45
45
  @span = span # So we can record on exceptions
46
46
  # annotate with method (GET/POST/etc.) and uri path
@@ -5,13 +5,14 @@ module ZipkinTracer
5
5
  # Resolving hostnames is a very expensive operation. We want to store them raw in the main thread
6
6
  # and resolve them in a different thread where we do not affect execution times.
7
7
  class HostnameResolver
8
- def spans_with_ips(spans)
9
- host_to_ip = hosts_to_ipv4(spans)
8
+ def spans_with_ips(spans, ip_format)
9
+ hosts = unique_hosts(spans)
10
+ resolved_hosts = resolve(hosts, ip_format)
10
11
 
11
12
  each_endpoint(spans) do |endpoint|
12
13
  hostname = endpoint.ipv4
13
14
  unless resolved_ip_address?(hostname.to_s)
14
- endpoint.ipv4 = host_to_ip[hostname]
15
+ endpoint.ipv4 = resolved_hosts[hostname]
15
16
  end
16
17
  end
17
18
  end
@@ -19,6 +20,8 @@ module ZipkinTracer
19
20
  private
20
21
  LOCALHOST = '127.0.0.1'.freeze
21
22
  LOCALHOST_I32 = 0x7f000001.freeze
23
+ MAX_I32 = ((2 ** 31) - 1)
24
+ MASK = (2 ** 32) - 1
22
25
  IP_FIELD = 3
23
26
 
24
27
  def resolved_ip_address?(ip_string)
@@ -37,31 +40,42 @@ module ZipkinTracer
37
40
  end
38
41
 
39
42
  # Using this to resolve only once per host
40
- def hosts_to_ipv4(spans)
43
+ def unique_hosts(spans)
41
44
  hosts = []
42
45
  each_endpoint(spans) do |endpoint|
43
46
  hosts.push(endpoint)
44
47
  end
45
- hosts.uniq!
46
- resolve(hosts)
48
+ hosts.uniq
47
49
  end
48
50
 
49
- def resolve(hosts)
51
+ def resolve(hosts, ip_format)
50
52
  hosts.inject({}) do |host_map, host|
51
53
  hostname = host.ipv4 # This field has been temporarly used to store the hostname.
52
- ip_format = host.ip_format
53
- host_map[hostname] = host_to_ip(hostname, ip_format)
54
+ host_map[hostname] = host_to_format(hostname, ip_format)
54
55
  host_map
55
56
  end
56
57
  end
57
58
 
58
- def host_to_ip(hostname, ip_format)
59
+ def host_to_format(hostname, ip_format)
59
60
  begin
60
- ip_format == :string ? Socket.getaddrinfo(hostname, nil, :INET).first[IP_FIELD] : Trace::Endpoint.host_to_i32(hostname)
61
+ ip_format == :string ? Socket.getaddrinfo(hostname, nil, :INET).first[IP_FIELD] : host_to_i32(hostname)
61
62
  rescue
62
63
  ip_format == :string ? LOCALHOST : LOCALHOST_I32
63
64
  end
64
65
  end
65
- end
66
66
 
67
+ def host_to_i32(host)
68
+ unsigned_i32 = Socket.getaddrinfo(host, nil)[0][3].split(".").map do |i|
69
+ i.to_i
70
+ end.inject(0) { |a,e| (a << 8) + e }
71
+
72
+ signed_i32 = if unsigned_i32 > MAX_I32
73
+ -1 * ((unsigned_i32 ^ MASK) + 1)
74
+ else
75
+ unsigned_i32
76
+ end
77
+
78
+ signed_i32
79
+ end
80
+ end
67
81
  end
@@ -250,33 +250,17 @@ module Trace
250
250
  end
251
251
  end
252
252
 
253
- class Endpoint < Struct.new(:ipv4, :port, :service_name, :ip_format)
254
- MAX_I32 = ((2 ** 31) - 1)
255
- MASK = (2 ** 32) - 1
253
+ class Endpoint < Struct.new(:ipv4, :port, :service_name)
256
254
  UNKNOWN_URL = 'unknown'.freeze
257
255
 
258
- def self.host_to_i32(host)
259
- unsigned_i32 = Socket.getaddrinfo(host, nil)[0][3].split(".").map do |i|
260
- i.to_i
261
- end.inject(0) { |a,e| (a << 8) + e }
262
-
263
- signed_i32 = if unsigned_i32 > MAX_I32
264
- -1 * ((unsigned_i32 ^ MASK) + 1)
265
- else
266
- unsigned_i32
267
- end
268
-
269
- signed_i32
270
- end
271
-
272
- def self.local_endpoint(service_name, ip_format)
256
+ def self.local_endpoint(service_name)
273
257
  hostname = Socket.gethostname
274
- Endpoint.new(hostname, nil, service_name, ip_format)
258
+ Endpoint.new(hostname, nil, service_name)
275
259
  end
276
260
 
277
- def self.remote_endpoint(url, remote_service_name, ip_format)
261
+ def self.remote_endpoint(url, remote_service_name)
278
262
  service_name = remote_service_name || url.host.split('.').first || UNKNOWN_URL # default to url-derived service name
279
- Endpoint.new(url.host, url.port, service_name, ip_format)
263
+ Endpoint.new(url.host, url.port, service_name)
280
264
  end
281
265
 
282
266
  def to_h
@@ -25,19 +25,8 @@ module ZipkinTracer
25
25
  end
26
26
  Trace.tracer = tracer
27
27
 
28
- # TODO: move this to the TracerBase and kill scribe tracer
29
- ip_format = [:kafka, :kafka_producer].include?(config.adapter) ? :i32 : :string
30
- Trace.default_endpoint = Trace::Endpoint.local_endpoint(
31
- service_name(config.service_name),
32
- ip_format
33
- )
34
28
  tracer
35
29
  end
36
30
 
37
- # Use the Domain environment variable to extract the service name, otherwise use the default config name
38
- # TODO: move to the config object
39
- def service_name(default_name)
40
- ENV["DOMAIN"].to_s.empty? ? default_name : ENV["DOMAIN"].split('.').first
41
- end
42
31
  end
43
32
  end
@@ -1,3 +1,3 @@
1
1
  module ZipkinTracer
2
- VERSION = '0.35.0'.freeze
2
+ VERSION = '0.35.1'.freeze
3
3
  end
@@ -8,7 +8,8 @@ class AsyncJsonApiClient
8
8
  SPANS_PATH = '/api/v2/spans'
9
9
 
10
10
  def perform(json_api_host, spans)
11
- spans_with_ips = ::ZipkinTracer::HostnameResolver.new.spans_with_ips(spans).map(&:to_h)
11
+ spans_with_ips =
12
+ ::ZipkinTracer::HostnameResolver.new.spans_with_ips(spans, ZipkinJsonTracer::IP_FORMAT).map(&:to_h)
12
13
  resp = Faraday.new(json_api_host).post do |req|
13
14
  req.url SPANS_PATH
14
15
  req.headers['Content-Type'] = 'application/json'
@@ -27,6 +28,7 @@ module Trace
27
28
  # This class sends information to the Zipkin API.
28
29
  # The API accepts a JSON representation of a list of spans
29
30
  class ZipkinJsonTracer < ZipkinTracerBase
31
+ IP_FORMAT = :string
30
32
 
31
33
  def initialize(options)
32
34
  SuckerPunch.logger = options[:logger]
@@ -14,6 +14,7 @@ module Trace
14
14
  # Spans are encoded using Thrift
15
15
  class ZipkinKafkaTracer < ZipkinTracerBase
16
16
  DEFAULT_KAFKA_TOPIC = "zipkin".freeze
17
+ IP_FORMAT = :i32
17
18
 
18
19
  def initialize(options = {})
19
20
  @topic = options[:topic] || DEFAULT_KAFKA_TOPIC
@@ -29,7 +30,7 @@ module Trace
29
30
  end
30
31
 
31
32
  def flush!
32
- resolved_spans = ::ZipkinTracer::HostnameResolver.new.spans_with_ips(spans)
33
+ resolved_spans = ::ZipkinTracer::HostnameResolver.new.spans_with_ips(spans, IP_FORMAT)
33
34
  resolved_spans.each do |span|
34
35
  buf = ''
35
36
  trans = Thrift::MemoryBufferTransport.new(buf)
@@ -5,6 +5,7 @@ require 'json'
5
5
  module Trace
6
6
  class ZipkinLoggerTracer < ZipkinTracerBase
7
7
  TRACING_KEY = 'Tracing information'
8
+ IP_FORMAT = :string
8
9
 
9
10
  def initialize(options)
10
11
  @logger = options[:logger]
@@ -13,7 +14,7 @@ module Trace
13
14
  end
14
15
 
15
16
  def flush!
16
- formatted_spans = ::ZipkinTracer::HostnameResolver.new.spans_with_ips(spans).map(&:to_h)
17
+ formatted_spans = ::ZipkinTracer::HostnameResolver.new.spans_with_ips(spans, IP_FORMAT).map(&:to_h)
17
18
  if @logger_accepts_data
18
19
  @logger.info_with_data(TRACING_KEY, formatted_spans)
19
20
  else
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.35.0
4
+ version: 0.35.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franklin Hu
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2019-03-30 00:00:00.000000000 Z
16
+ date: 2019-04-03 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: faraday