lookout-zipkin-tracer 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjQyZjdhNDc0YTI0ZDE4ZTAxNDAyNjAwMDBiZTkwYzkzYzE5MWFiYg==
4
+ ODdmODRhZmY4N2E4OWZmNzc5MzliOGUzMjI2NmJlMDRjY2IwMDhhNw==
5
5
  data.tar.gz: !binary |-
6
- YjQyMDE2OWNiODE2ZGI3NmE5ODQzZDRjYjYzYTI5ZWFjZWEzM2JhNA==
6
+ MTAxMDgzY2QzYTZmN2QxOGRmZjNjYzhiYTQ1NzZkYWI5NmZiZDY0NQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MzUzNTk0OTRiYTNjNzhiODg0OThkNTIxNTZmM2FlYTZkYmJlZTcwYWEyZWEy
10
- MzFjYzgwMDc1ZTc2MDBkYmI0ODAyN2U2MjdlOTRlMmQ2NzE3NmJiNmJmMTQx
11
- M2IyMTM4ZGYxOTg0ZjBjYzNlYzkwNGMyZjNmZjFiODQyYTQ4NWY=
9
+ MzJhNTdiNDQ2OGE1MDhjZWMwMzQ4M2JmMDc0OTg3NTY5ZTg1YTIwYzJiZjdh
10
+ MDRkYWRlYWY0NzZlYTkyYjQ4M2I2N2ZkOTUzZTRkODcxNWFhNzJlYjMzOWM1
11
+ MjQ4NmY1MDg5NjA1ZmFkMzE0Njc0ZWUxMTI2YjA0OWYwZjY4Mzg=
12
12
  data.tar.gz: !binary |-
13
- YWQ0MmU2ZWZlMWM5NjExODYyOTliNzk3YzAwMGEwYmM0YWI4ODJjYmRhYzNk
14
- ZGU2N2U5YTVjZDI5ZTIzNDNhYjRkNGNmNjI2ZjkzMzYyNTY4OTg5YTk3Y2Yw
15
- ZDc2NDQ1MGJmZDc1ZTU2Zjg5NWY2NTBlMDNjNmVlZGEwNjY3MDI=
13
+ NDAzNGU5ZGMwYTgyYzM2ZmIzZjIwODJkMTI3NTc5NDg1MjcxZGU4MTgwMjVh
14
+ MzgzZjNhYTlkZDc3MTkyYTQwYTIyYWRkNThjNmIxNWZmM2Y5NGMzMjNhYmY5
15
+ ZDc2YmUxYTgyZTNlYzU3NTYxYzU1ZTlkNmY5MjRmNjA1MGVhYzA=
data/lib/zipkin-tracer.rb CHANGED
@@ -21,7 +21,7 @@ module ZipkinTracer extend self
21
21
 
22
22
  class RackHandler
23
23
  B3_REQUIRED_HEADERS = %w[HTTP_X_B3_TRACEID HTTP_X_B3_PARENTSPANID HTTP_X_B3_SPANID HTTP_X_B3_SAMPLED]
24
- B3_HEADERS = B3_REQUIRED_HEADERS + %w[HTTP_X_B3_FLAGS]
24
+ B3_OPT_HEADERS = %w[HTTP_X_B3_FLAGS]
25
25
 
26
26
  def initialize(app, config=nil)
27
27
  @app = app
@@ -52,41 +52,65 @@ module ZipkinTracer extend self
52
52
  0.1
53
53
  end
54
54
 
55
+ @annotate_plugin = config[:annotate_plugin] # call for trace annotation
56
+ @filter_plugin = config[:filter_plugin] # skip tracing if returns false
57
+ @whitelist_plugin = config[:whitelist_plugin] # force sampling if returns true
58
+
55
59
  ::Trace.tracer = ::Trace::ZipkinTracer.new(CarelessScribe.new(scribe), scribe_max_buffer)
56
60
  end
57
61
 
58
62
  def call(env)
63
+ # skip certain requests
64
+ return @app.call(env) if filtered?(env)
65
+
59
66
  ::Trace.default_endpoint = ::Trace.default_endpoint.with_service_name(@service_name).with_port(@service_port)
60
67
  ::Trace.sample_rate=(@sample_rate)
61
- id = get_or_create_trace_id(env) # note that this depends on the sample rate being set
62
- tracing_filter(id, env) { @app.call(env) }
68
+ whitelisted = force_sample?(env)
69
+ id = get_or_create_trace_id(env, whitelisted) # note that this depends on the sample rate being set
70
+ tracing_filter(id, env, whitelisted) { @app.call(env) }
63
71
  end
64
72
 
65
73
  private
66
- def tracing_filter(trace_id, env)
74
+ def annotate(env, status, response_headers, response_body)
75
+ @annotate_plugin.call(env, status, response_headers, response_body) if @annotate_plugin
76
+ end
77
+
78
+ def filtered?(env)
79
+ @filter_plugin && !@filter_plugin.call(env)
80
+ end
81
+
82
+ def force_sample?(env)
83
+ @whitelist_plugin && @whitelist_plugin.call(env)
84
+ end
85
+
86
+ def tracing_filter(trace_id, env, whitelisted=false)
67
87
  @lock.synchronize do
68
88
  ::Trace.push(trace_id)
69
89
  ::Trace.set_rpc_name(env["REQUEST_METHOD"]) # get/post and all that jazz
70
90
  ::Trace.record(::Trace::BinaryAnnotation.new("http.uri", env["PATH_INFO"], "STRING", ::Trace.default_endpoint))
71
91
  ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::SERVER_RECV, ::Trace.default_endpoint))
92
+ ::Trace.record(::Trace::Annotation.new('whitelisted', ::Trace.default_endpoint)) if whitelisted
72
93
  end
73
- yield if block_given?
94
+ status, headers, body = yield if block_given?
74
95
  ensure
75
96
  @lock.synchronize do
76
97
  ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::SERVER_SEND, ::Trace.default_endpoint))
98
+ annotate(env, status, headers, body)
77
99
  ::Trace.pop
78
100
  end
79
101
  end
80
102
 
81
103
  private
82
- def get_or_create_trace_id(env, default_flags = ::Trace::Flags::EMPTY)
104
+ def get_or_create_trace_id(env, whitelisted, default_flags = ::Trace::Flags::EMPTY)
83
105
  trace_parameters = if B3_REQUIRED_HEADERS.all? { |key| env.has_key?(key) }
84
- env.values_at(*B3_HEADERS)
106
+ env.values_at(*B3_REQUIRED_HEADERS)
85
107
  else
86
108
  new_id = Trace.generate_id
87
- [new_id, nil, new_id, ("true" if Trace.should_sample?), default_flags]
109
+ [new_id, nil, new_id, ("true" if whitelisted || Trace.should_sample?)]
88
110
  end
89
111
  trace_parameters[3] = (trace_parameters[3] == "true")
112
+
113
+ trace_parameters += env.values_at(*B3_OPT_HEADERS) # always check flags
90
114
  trace_parameters[4] = (trace_parameters[4] || default_flags).to_i
91
115
 
92
116
  Trace::TraceId.new(*trace_parameters)
@@ -12,6 +12,6 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
  module ZipkinTracer
15
- VERSION = "0.2.0"
15
+ VERSION = "0.3.0"
16
16
  end
17
17
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lookout-zipkin-tracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ariel Salomon, Franklin Hu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-10 00:00:00.000000000 Z
11
+ date: 2014-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: finagle-thrift
@@ -38,6 +38,62 @@ dependencies:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.2.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: thin
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
41
97
  description: Adds tracing instrumentation for ruby applications
42
98
  email:
43
99
  - ariel@lookout.com, franklin@twitter.com