zipkin-tracer 0.19.0 → 0.19.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/zipkin-tracer/rack/zipkin-tracer.rb +3 -70
- data/lib/zipkin-tracer/rack/zipkin_env.rb +72 -0
- data/lib/zipkin-tracer/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4657331271ed370854d23b33e9980047aeded135
|
4
|
+
data.tar.gz: 57ffc30cb2d965e4d4a257de2dbcf3d944e447e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42b879ff2bc2dff5106025e0639bfc6b3f87e63e8de628cd88e2d8e2ca9e22f6396aadfae57e469816b4f0e44c39d6ae30b26d6a6217d3894fd2bcc2067985ae
|
7
|
+
data.tar.gz: a8b99ee0c3edbbade8f4812c3b02353b99c24acaaaa6e29f2bf4d63eee0b205a5bab079adc124f417544ec81bce5463b3c5707ba4383871063513eb2eb5ad826
|
@@ -15,15 +15,12 @@ require 'finagle-thrift/trace'
|
|
15
15
|
require 'finagle-thrift/tracer'
|
16
16
|
require 'zipkin-tracer/config'
|
17
17
|
require 'zipkin-tracer/tracer_factory'
|
18
|
+
require 'zipkin-tracer/rack/zipkin_env'
|
18
19
|
|
19
20
|
module ZipkinTracer
|
20
|
-
|
21
21
|
# This middleware reads Zipkin headers from the request and sets/creates a Trace.id usable by the rest of the app
|
22
22
|
# It will also send the trace to the Zipkin service using one of the methods configured.
|
23
23
|
class RackHandler
|
24
|
-
B3_REQUIRED_HEADERS = %w[HTTP_X_B3_TRACEID HTTP_X_B3_PARENTSPANID HTTP_X_B3_SPANID HTTP_X_B3_SAMPLED].freeze
|
25
|
-
B3_OPT_HEADERS = %w[HTTP_X_B3_FLAGS].freeze
|
26
|
-
|
27
24
|
def initialize(app, config = nil)
|
28
25
|
@app = app
|
29
26
|
@config = Config.new(app, config).freeze
|
@@ -37,7 +34,7 @@ module ZipkinTracer
|
|
37
34
|
if !trace_id.sampled? || !routable_request?(env)
|
38
35
|
@app.call(env)
|
39
36
|
else
|
40
|
-
@tracer.with_new_span(trace_id,
|
37
|
+
@tracer.with_new_span(trace_id, env['REQUEST_METHOD'].to_s.downcase) do |span|
|
41
38
|
trace!(span, zipkin_env) { @app.call(env) }
|
42
39
|
end
|
43
40
|
end
|
@@ -55,7 +52,7 @@ module ZipkinTracer
|
|
55
52
|
end
|
56
53
|
|
57
54
|
def trace!(span, zipkin_env, &block)
|
58
|
-
#if called by a service, the caller already added the information
|
55
|
+
# if called by a service, the caller already added the information
|
59
56
|
trace_request_information(span, zipkin_env.env) unless zipkin_env.called_with_zipkin_headers?
|
60
57
|
span.record(Trace::Annotation::SERVER_RECV)
|
61
58
|
span.record('whitelisted') if zipkin_env.force_sample?
|
@@ -68,69 +65,5 @@ module ZipkinTracer
|
|
68
65
|
def trace_request_information(span, env)
|
69
66
|
span.record_tag(Trace::BinaryAnnotation::PATH, env['PATH_INFO'])
|
70
67
|
end
|
71
|
-
|
72
|
-
# Environment with Zipkin information in it
|
73
|
-
class ZipkinEnv
|
74
|
-
attr_reader :env
|
75
|
-
|
76
|
-
def initialize(env, config)
|
77
|
-
@env = env
|
78
|
-
@config = config
|
79
|
-
end
|
80
|
-
|
81
|
-
def trace_id(default_flags = Trace::Flags::EMPTY)
|
82
|
-
trace_parameters = if called_with_zipkin_headers?
|
83
|
-
@env.values_at(*B3_REQUIRED_HEADERS)
|
84
|
-
else
|
85
|
-
new_id = Trace.generate_id
|
86
|
-
[new_id, nil, new_id, nil]
|
87
|
-
end
|
88
|
-
trace_parameters[3] = sampled_header_value(trace_parameters[3])
|
89
|
-
trace_parameters += @env.values_at(*B3_OPT_HEADERS) # always check flags
|
90
|
-
trace_parameters[4] = (trace_parameters[4] || default_flags).to_i
|
91
|
-
|
92
|
-
Trace::TraceId.new(*trace_parameters)
|
93
|
-
end
|
94
|
-
|
95
|
-
def called_with_zipkin_headers?
|
96
|
-
@called_with_zipkin_headers ||= B3_REQUIRED_HEADERS.all? { |key| @env.has_key?(key) }
|
97
|
-
end
|
98
|
-
|
99
|
-
def force_sample?
|
100
|
-
@force_sample ||= @config.whitelist_plugin && @config.whitelist_plugin.call(@env)
|
101
|
-
end
|
102
|
-
|
103
|
-
private
|
104
|
-
|
105
|
-
def new_sampled_header_value(sampled)
|
106
|
-
case [@config.sampled_as_boolean, sampled]
|
107
|
-
when [true, true]
|
108
|
-
'true'
|
109
|
-
when [true, false]
|
110
|
-
'false'
|
111
|
-
when [false, true]
|
112
|
-
'1'
|
113
|
-
when [false, false]
|
114
|
-
'0'
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
def current_trace_sampled?
|
119
|
-
rand < @config.sample_rate
|
120
|
-
end
|
121
|
-
|
122
|
-
def sampled_header_value(parent_trace_sampled)
|
123
|
-
if parent_trace_sampled # A service upstream decided this goes in all the way
|
124
|
-
parent_trace_sampled
|
125
|
-
else
|
126
|
-
new_sampled_header_value(force_sample? || current_trace_sampled? && !filtered?)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def filtered?
|
131
|
-
@config.filter_plugin && !@config.filter_plugin.call(@env)
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
68
|
end
|
136
69
|
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module ZipkinTracer
|
2
|
+
# This class manages the Zipkin related information in the Rack environment.
|
3
|
+
# It is only used by the rack middleware.
|
4
|
+
class ZipkinEnv
|
5
|
+
attr_reader :env
|
6
|
+
|
7
|
+
def initialize(env, config)
|
8
|
+
@env = env
|
9
|
+
@config = config
|
10
|
+
end
|
11
|
+
|
12
|
+
def trace_id(default_flags = Trace::Flags::EMPTY)
|
13
|
+
trace_id, span_id, parent_span_id = retrieve_or_generate_ids
|
14
|
+
sampled = sampled_header_value(@env['HTTP_X_B3_SAMPLED'])
|
15
|
+
flags = (@env['HTTP_X_B3_FLAGS'] || default_flags).to_i
|
16
|
+
Trace::TraceId.new(trace_id, parent_span_id, span_id, sampled, flags)
|
17
|
+
end
|
18
|
+
|
19
|
+
def called_with_zipkin_headers?
|
20
|
+
@called_with_zipkin_headers ||= B3_REQUIRED_HEADERS.all? { |key| @env.key?(key) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def force_sample?
|
24
|
+
@force_sample ||= @config.whitelist_plugin && @config.whitelist_plugin.call(@env)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
B3_REQUIRED_HEADERS = %w(HTTP_X_B3_TRACEID HTTP_X_B3_SPANID).freeze
|
30
|
+
B3_OPT_HEADERS = %w(HTTP_X_B3_PARENTSPANID HTTP_X_B3_SAMPLED HTTP_X_B3_FLAGS).freeze
|
31
|
+
|
32
|
+
def retrieve_or_generate_ids
|
33
|
+
if called_with_zipkin_headers?
|
34
|
+
trace_id, span_id = @env.values_at(*B3_REQUIRED_HEADERS)
|
35
|
+
parent_span_id = @env['HTTP_X_B3_PARENTSPANID']
|
36
|
+
else
|
37
|
+
trace_id = span_id = Trace.generate_id
|
38
|
+
parent_span_id = nil
|
39
|
+
end
|
40
|
+
[trace_id, span_id, parent_span_id]
|
41
|
+
end
|
42
|
+
|
43
|
+
def new_sampled_header_value(sampled)
|
44
|
+
case [@config.sampled_as_boolean, sampled]
|
45
|
+
when [true, true]
|
46
|
+
'true'
|
47
|
+
when [true, false]
|
48
|
+
'false'
|
49
|
+
when [false, true]
|
50
|
+
'1'
|
51
|
+
when [false, false]
|
52
|
+
'0'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def current_trace_sampled?
|
57
|
+
rand < @config.sample_rate
|
58
|
+
end
|
59
|
+
|
60
|
+
def sampled_header_value(parent_trace_sampled)
|
61
|
+
if parent_trace_sampled # A service upstream decided this goes in all the way
|
62
|
+
parent_trace_sampled
|
63
|
+
else
|
64
|
+
new_sampled_header_value(force_sample? || current_trace_sampled? && !filtered?)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def filtered?
|
69
|
+
@config.filter_plugin && !@config.filter_plugin.call(@env)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
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.19.
|
4
|
+
version: 0.19.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: 2016-11-
|
16
|
+
date: 2016-11-28 00:00:00.000000000 Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: faraday
|
@@ -159,6 +159,7 @@ files:
|
|
159
159
|
- lib/zipkin-tracer/faraday/zipkin-tracer.rb
|
160
160
|
- lib/zipkin-tracer/hostname_resolver.rb
|
161
161
|
- lib/zipkin-tracer/rack/zipkin-tracer.rb
|
162
|
+
- lib/zipkin-tracer/rack/zipkin_env.rb
|
162
163
|
- lib/zipkin-tracer/trace.rb
|
163
164
|
- lib/zipkin-tracer/trace_client.rb
|
164
165
|
- lib/zipkin-tracer/tracer_factory.rb
|