zipkin-tracer 0.30.0 → 0.31.0

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: b3baf1dff1d2fa511cec7a66a040c4e817bb100d4e8984834d0471f8a1118d51
4
- data.tar.gz: b2047d302ad9d4c50f265c33f640a03d02a6223379707c8146995667812dc023
3
+ metadata.gz: 831effb5b1041bf625d6aa6ef01d759245674ed4973b2bfd8c989ee4dd4e5fcd
4
+ data.tar.gz: f90164cd91b0e4e4190d5d15e4e6084cf941e6334e22be7f4aada18600e0844a
5
5
  SHA512:
6
- metadata.gz: 9bfaaf722dcdad67fcb795db85cbaac6ab5b6234e76569d17982098ec38059e9d7f9fb5167ed0c0e177ff9857d987dbe68ab780e8dbfef0fc18e220dbb784664
7
- data.tar.gz: 8ddb0aba5a360756b11015aea6d13949a14d4653de0123664f7e7264c8e67655544cea722002d098358bef67e476be7ecdc6747a214b93886fdab9a76828997d
6
+ metadata.gz: 10b1de8bb1d255f6b85438b99b4a049423ebef3fb57c3489197a73d153d2b0ef7c9d72f2e4993813d425d7c911870519e2f4f88870eb87d0bd9a8ab261e069fd
7
+ data.tar.gz: c481a2312408e698c0b4fc5ca22692e7cccf2539806dc72615d2d8dc4f20d96fb8c5846b5bf0aa1cbd3239e4544a2eb7f22a17a241705d253fb5ec5f70a404a2
data/lib/zipkin-tracer.rb CHANGED
@@ -1,5 +1,3 @@
1
- require 'finagle-thrift'
2
- require 'base64' #Bug in finagle. They should be requiring this: finagle-thrift-1.4.1/lib/finagle-thrift/tracer.rb:115
3
1
  require 'zipkin-tracer/trace'
4
2
  require 'zipkin-tracer/rack/zipkin-tracer'
5
3
  require 'zipkin-tracer/sidekiq/middleware'
@@ -1,5 +1,3 @@
1
- require 'finagle-thrift/trace'
2
- require 'finagle-thrift/tracer'
3
1
  require 'uri'
4
2
  require 'excon'
5
3
 
@@ -1,6 +1,4 @@
1
1
  require 'faraday'
2
- require 'finagle-thrift/trace'
3
- require 'finagle-thrift/tracer'
4
2
  require 'uri'
5
3
 
6
4
  module ZipkinTracer
@@ -1,6 +1,4 @@
1
1
  require 'rack'
2
- require 'finagle-thrift/trace'
3
- require 'finagle-thrift/tracer'
4
2
  require 'zipkin-tracer/config'
5
3
  require 'zipkin-tracer/tracer_factory'
6
4
  require 'zipkin-tracer/rack/zipkin_env'
@@ -34,7 +34,7 @@ module ZipkinTracer
34
34
  trace_id, span_id = @env.values_at(*B3_REQUIRED_HEADERS)
35
35
  parent_span_id = @env['HTTP_X_B3_PARENTSPANID']
36
36
  else
37
- span_id = Trace.generate_id
37
+ span_id = TraceGenerator.new.generate_id
38
38
  trace_id = TraceGenerator.new.generate_id_from_span_id(span_id)
39
39
  parent_span_id = nil
40
40
  end
@@ -1,21 +1,134 @@
1
- require 'finagle-thrift/trace'
2
1
  require 'zipkin-tracer/zipkin_tracer_base'
3
- # Module with a mix of functions and overwrites from the finagle implementation:
2
+ # Most of this code is copied from Finagle
4
3
  # https://github.com/twitter/finagle/blob/finagle-6.39.0/finagle-thrift/src/main/ruby/lib/finagle-thrift/trace.rb
4
+ # But moved and improved here.
5
5
  module Trace
6
+ # These methods and attr_accessor below are used as global configuration of this gem
7
+ # Most of these are set by the config class and then used around.
8
+ # TODO: Move this out of the Trace module , take out that extend self and be happier
9
+ extend self
6
10
  attr_accessor :trace_id_128bit
7
11
 
8
- # We need this to access the tracer from the Faraday middleware.
9
12
  def self.tracer
10
13
  @tracer
11
14
  end
12
15
 
13
- def sample_rate
16
+ def self.sample_rate
14
17
  @sample_rate
15
18
  end
16
19
 
20
+ def self.tracer=(tracer)
21
+ @tracer = tracer
22
+ end
23
+
24
+ def self.sample_rate=(sample_rate)
25
+ if sample_rate > 1 || sample_rate < 0
26
+ raise ArgumentError.new("sample rate must be [0,1]")
27
+ end
28
+ @sample_rate = sample_rate
29
+ end
30
+
31
+ def default_endpoint=(endpoint)
32
+ @default_endpoint = endpoint
33
+ end
34
+
35
+ def default_endpoint
36
+ @default_endpoint
37
+ end
38
+
39
+ # These classes all come from Finagle-thrift + some needed modifications (.to_h)
40
+ # Moved here as a first step, eventually move them out of the Trace module
41
+
42
+ class Annotation
43
+ CLIENT_SEND = "cs"
44
+ CLIENT_RECV = "cr"
45
+ SERVER_SEND = "ss"
46
+ SERVER_RECV = "sr"
47
+
48
+ attr_reader :value, :host, :timestamp
49
+ def initialize(value, host)
50
+ @timestamp = (Time.now.to_f * 1000 * 1000).to_i # micros
51
+ @value = value
52
+ @host = host
53
+ end
54
+
55
+ def to_h
56
+ {
57
+ value: @value,
58
+ timestamp: @timestamp,
59
+ endpoint: host.to_h
60
+ }
61
+ end
62
+ end
63
+
64
+ class BinaryAnnotation
65
+ SERVER_ADDRESS = 'sa'.freeze
66
+ URI = 'http.url'.freeze
67
+ METHOD = 'http.method'.freeze
68
+ PATH = 'http.path'.freeze
69
+ STATUS = 'http.status'.freeze
70
+ LOCAL_COMPONENT = 'lc'.freeze
71
+ ERROR = 'error'.freeze
72
+
73
+ module Type
74
+ BOOL = "BOOL"
75
+ STRING = "STRING"
76
+ end
77
+ attr_reader :key, :value, :host
78
+
79
+ def initialize(key, value, annotation_type, host)
80
+ @key = key
81
+ @value = value
82
+ @annotation_type = annotation_type
83
+ @host = host
84
+ end
85
+
86
+ def to_h
87
+ {
88
+ key: @key,
89
+ value: @value,
90
+ endpoint: host.to_h
91
+ }
92
+ end
93
+ end
94
+
95
+ class Flags
96
+ # no flags set
97
+ EMPTY = 0
98
+ # the debug flag is used to ensure we pass all the sampling layers and that the trace is stored
99
+ DEBUG = 1
100
+ end
101
+
102
+ class SpanId
103
+ HEX_REGEX = /^[a-f0-9]{16,32}$/i
104
+ MAX_SIGNED_I64 = 9223372036854775807
105
+ MASK = (2 ** 64) - 1
106
+
107
+ def self.from_value(v)
108
+ if v.is_a?(String) && v =~ HEX_REGEX
109
+ # drops any bits higher than 64 by selecting right-most 16 characters
110
+ new(v.length > 16 ? v[v.length - 16, 16].hex : v.hex)
111
+ elsif v.is_a?(Numeric)
112
+ new(v)
113
+ elsif v.is_a?(SpanId)
114
+ v
115
+ end
116
+ end
117
+
118
+ def initialize(value)
119
+ @value = value
120
+ @i64 = if @value > MAX_SIGNED_I64
121
+ -1 * ((@value ^ MASK) + 1)
122
+ else
123
+ @value
124
+ end
125
+ end
126
+
127
+ def to_s; "%016x" % @value; end
128
+ def to_i; @i64; end
129
+ end
130
+
17
131
  # A TraceId contains all the information of a given trace id
18
- # This class is defined in finagle-thrift. We are overwriting it here
19
132
  class TraceId
20
133
  attr_reader :trace_id, :parent_id, :span_id, :sampled, :flags
21
134
 
@@ -27,8 +140,8 @@ module Trace
27
140
  @flags = flags
28
141
  end
29
142
 
30
- def next_id
31
- TraceId.new(@trace_id, @span_id, Trace.generate_id, @sampled, @flags)
143
+ def next_id(next_span_id)
144
+ TraceId.new(@trace_id, @span_id, next_span_id, @sampled, @flags)
32
145
  end
33
146
 
34
147
  # the debug flag is used to ensure the trace passes ALL samplers
@@ -45,8 +158,6 @@ module Trace
45
158
  end
46
159
  end
47
160
 
48
- # This class is the 128-bit version of the SpanId class:
49
- # https://github.com/twitter/finagle/blob/finagle-6.39.0/finagle-thrift/src/main/ruby/lib/finagle-thrift/trace.rb#L102
50
161
  class TraceId128Bit < SpanId
51
162
  HEX_REGEX_16 = /^[a-f0-9]{16}$/i
52
163
  HEX_REGEX_32 = /^[a-f0-9]{32}$/i
@@ -79,8 +190,8 @@ module Trace
79
190
  end
80
191
 
81
192
  # A span may contain many annotations
82
- # This class is defined in finagle-thrift. We are adding extra methods here
83
193
  class Span
194
+ attr_accessor :name, :annotations, :binary_annotations, :debug
84
195
  def initialize(name, span_id)
85
196
  @name = name
86
197
  @span_id = span_id
@@ -136,51 +247,33 @@ module Trace
136
247
  end
137
248
  end
138
249
 
139
- # This class is defined in finagle-thrift. We are adding extra methods here
140
- class Annotation
141
- def to_h
142
- {
143
- value: @value,
144
- timestamp: @timestamp,
145
- endpoint: host.to_h
146
- }
147
- end
148
- end
149
-
150
- # This class is defined in finagle-thrift. We are adding extra methods here
151
- class BinaryAnnotation
152
- SERVER_ADDRESS = 'sa'.freeze
153
- URI = 'http.uri'.freeze
154
- METHOD = 'http.method'.freeze
155
- PATH = 'http.path'.freeze
156
- STATUS = 'http.status'.freeze
157
- LOCAL_COMPONENT = 'lc'.freeze
158
- ERROR = 'error'.freeze
250
+ class Endpoint < Struct.new(:ipv4, :port, :service_name, :ip_format)
251
+ MAX_I32 = ((2 ** 31) - 1)
252
+ MASK = (2 ** 32) - 1
253
+ UNKNOWN_URL = 'unknown'.freeze
159
254
 
160
- def to_h
161
- {
162
- key: @key,
163
- value: @value,
164
- endpoint: host.to_h
165
- }
166
- end
167
- end
255
+ def self.host_to_i32(host)
256
+ unsigned_i32 = Socket.getaddrinfo(host, nil)[0][3].split(".").map do |i|
257
+ i.to_i
258
+ end.inject(0) { |a,e| (a << 8) + e }
168
259
 
169
- # This class is defined in finagle-thrift. We are adding extra methods here
170
- class Endpoint
171
- UNKNOWN_URL = 'unknown'.freeze
260
+ signed_i32 = if unsigned_i32 > MAX_I32
261
+ -1 * ((unsigned_i32 ^ MASK) + 1)
262
+ else
263
+ unsigned_i32
264
+ end
172
265
 
173
- # we cannot override the initializer to add an extra parameter so use a factory
174
- attr_accessor :ip_format
266
+ signed_i32
267
+ end
175
268
 
176
269
  def self.local_endpoint(service_port, service_name, ip_format)
177
270
  hostname = Socket.gethostname
178
- Endpoint.make_endpoint(hostname, service_port, service_name, ip_format)
271
+ Endpoint.new(hostname, service_port, service_name, ip_format)
179
272
  end
180
273
 
181
274
  def self.remote_endpoint(url, remote_service_name, ip_format)
182
275
  service_name = remote_service_name || url.host.split('.').first || UNKNOWN_URL # default to url-derived service name
183
- Endpoint.make_endpoint(url.host, url.port, service_name, ip_format)
276
+ Endpoint.new(url.host, url.port, service_name, ip_format)
184
277
  end
185
278
 
186
279
  def to_h
@@ -191,12 +284,5 @@ module Trace
191
284
  }
192
285
  end
193
286
 
194
- private
195
- def self.make_endpoint(hostname, service_port, service_name, ip_format)
196
- ep = Endpoint.new(hostname, service_port, service_name)
197
- ep.ip_format = ip_format
198
- ep
199
- end
200
-
201
287
  end
202
288
  end
@@ -4,7 +4,7 @@ module ZipkinTracer
4
4
  # Next id, based on the current information in the container
5
5
  def next_trace_id
6
6
  if TraceContainer.tracing_information_set?
7
- TraceContainer.current.next_id
7
+ TraceContainer.current.next_id(generate_id)
8
8
  else
9
9
  generate_trace_id
10
10
  end
@@ -23,12 +23,12 @@ module ZipkinTracer
23
23
  Trace.trace_id_128bit ? generate_id_128bit(span_id) : span_id
24
24
  end
25
25
 
26
- private
27
-
28
26
  def generate_id
29
27
  rand(TRACE_ID_UPPER_BOUND)
30
28
  end
31
29
 
30
+ private
31
+
32
32
  def generate_id_128bit(span_id)
33
33
  trace_id_low_64bit = '%016x' % span_id
34
34
  "#{trace_id_epoch_seconds}#{trace_id_high_32bit}#{trace_id_low_64bit}".hex
@@ -1,3 +1,3 @@
1
1
  module ZipkinTracer
2
- VERSION = '0.30.0'.freeze
2
+ VERSION = '0.31.0'.freeze
3
3
  end
@@ -2,6 +2,7 @@
2
2
  begin
3
3
  require 'hermann/producer'
4
4
  require 'hermann/discovery/zookeeper'
5
+ require 'finagle-thrift'
5
6
  rescue LoadError => e
6
7
  end
7
8
 
@@ -26,7 +27,7 @@ module Trace
26
27
  end
27
28
  super(options)
28
29
  end
29
-
30
+
30
31
  def flush!
31
32
  resolved_spans = ::ZipkinTracer::HostnameResolver.new.spans_with_ips(spans)
32
33
  resolved_spans.each do |span|
@@ -1,5 +1,4 @@
1
1
  require 'faraday'
2
- require 'finagle-thrift/tracer'
3
2
 
4
3
  module Trace
5
4
  # This class is a base for tracers sending information to Zipkin.
@@ -7,7 +6,7 @@ module Trace
7
6
  # is done with its request
8
7
  # Traces dealing with zipkin should inherit from this class and implement the
9
8
  # flush! method which actually sends the information
10
- class ZipkinTracerBase < Tracer
9
+ class ZipkinTracerBase
11
10
 
12
11
  def initialize(options={})
13
12
  @options = options
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.30.0
4
+ version: 0.31.0
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: 2018-12-06 00:00:00.000000000 Z
16
+ date: 2019-01-08 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: faraday
@@ -29,20 +29,6 @@ dependencies:
29
29
  - - "~>"
30
30
  - !ruby/object:Gem::Version
31
31
  version: '0.8'
32
- - !ruby/object:Gem::Dependency
33
- name: finagle-thrift
34
- requirement: !ruby/object:Gem::Requirement
35
- requirements:
36
- - - "~>"
37
- - !ruby/object:Gem::Version
38
- version: 1.4.2
39
- type: :runtime
40
- prerelease: false
41
- version_requirements: !ruby/object:Gem::Requirement
42
- requirements:
43
- - - "~>"
44
- - !ruby/object:Gem::Version
45
- version: 1.4.2
46
32
  - !ruby/object:Gem::Dependency
47
33
  name: rack
48
34
  requirement: !ruby/object:Gem::Requirement
@@ -91,28 +77,28 @@ dependencies:
91
77
  requirements:
92
78
  - - "~>"
93
79
  - !ruby/object:Gem::Version
94
- version: '3.3'
80
+ version: '3.8'
95
81
  type: :development
96
82
  prerelease: false
97
83
  version_requirements: !ruby/object:Gem::Requirement
98
84
  requirements:
99
85
  - - "~>"
100
86
  - !ruby/object:Gem::Version
101
- version: '3.3'
87
+ version: '3.8'
102
88
  - !ruby/object:Gem::Dependency
103
89
  name: rack-test
104
90
  requirement: !ruby/object:Gem::Requirement
105
91
  requirements:
106
92
  - - "~>"
107
93
  - !ruby/object:Gem::Version
108
- version: '0.6'
94
+ version: '1.1'
109
95
  type: :development
110
96
  prerelease: false
111
97
  version_requirements: !ruby/object:Gem::Requirement
112
98
  requirements:
113
99
  - - "~>"
114
100
  - !ruby/object:Gem::Version
115
- version: '0.6'
101
+ version: '1.1'
116
102
  - !ruby/object:Gem::Dependency
117
103
  name: rake
118
104
  requirement: !ruby/object:Gem::Requirement
@@ -155,6 +141,20 @@ dependencies:
155
141
  - - "~>"
156
142
  - !ruby/object:Gem::Version
157
143
  version: '3.0'
144
+ - !ruby/object:Gem::Dependency
145
+ name: simplecov
146
+ requirement: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - "~>"
149
+ - !ruby/object:Gem::Version
150
+ version: '0.16'
151
+ type: :development
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - "~>"
156
+ - !ruby/object:Gem::Version
157
+ version: '0.16'
158
158
  description: Adds tracing instrumentation for ruby applications
159
159
  email:
160
160
  - franklin@twitter.com
@@ -206,8 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
206
  - !ruby/object:Gem::Version
207
207
  version: 1.3.5
208
208
  requirements: []
209
- rubyforge_project:
210
- rubygems_version: 2.7.8
209
+ rubygems_version: 3.0.2
211
210
  signing_key:
212
211
  specification_version: 4
213
212
  summary: Ruby tracing via Zipkin