lookout-zipkin-tracer 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MjQyZjdhNDc0YTI0ZDE4ZTAxNDAyNjAwMDBiZTkwYzkzYzE5MWFiYg==
5
+ data.tar.gz: !binary |-
6
+ YjQyMDE2OWNiODE2ZGI3NmE5ODQzZDRjYjYzYTI5ZWFjZWEzM2JhNA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MzUzNTk0OTRiYTNjNzhiODg0OThkNTIxNTZmM2FlYTZkYmJlZTcwYWEyZWEy
10
+ MzFjYzgwMDc1ZTc2MDBkYmI0ODAyN2U2MjdlOTRlMmQ2NzE3NmJiNmJmMTQx
11
+ M2IyMTM4ZGYxOTg0ZjBjYzNlYzkwNGMyZjNmZjFiODQyYTQ4NWY=
12
+ data.tar.gz: !binary |-
13
+ YWQ0MmU2ZWZlMWM5NjExODYyOTliNzk3YzAwMGEwYmM0YWI4ODJjYmRhYzNk
14
+ ZGU2N2U5YTVjZDI5ZTIzNDNhYjRkNGNmNjI2ZjkzMzYyNTY4OTg5YTk3Y2Yw
15
+ ZDc2NDQ1MGJmZDc1ZTU2Zjg5NWY2NTBlMDNjNmVlZGEwNjY3MDI=
@@ -0,0 +1,96 @@
1
+ # Copyright 2012 Twitter Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ require 'finagle-thrift'
15
+ require 'finagle-thrift/trace'
16
+ require 'scribe'
17
+
18
+ require 'zipkin-tracer/careless_scribe'
19
+
20
+ module ZipkinTracer extend self
21
+
22
+ class RackHandler
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]
25
+
26
+ def initialize(app, config=nil)
27
+ @app = app
28
+ @lock = Mutex.new
29
+
30
+ config ||= app.config.zipkin_tracer # if not specified, try on app (e.g. Rails 3+)
31
+ @service_name = config[:service_name]
32
+ @service_port = config[:service_port]
33
+
34
+ scribe =
35
+ if config[:scribe_server] then
36
+ Scribe.new(config[:scribe_server])
37
+ else
38
+ Scribe.new()
39
+ end
40
+
41
+ scribe_max_buffer =
42
+ if config[:scribe_max_buffer] then
43
+ config[:scribe_max_buffer]
44
+ else
45
+ 10
46
+ end
47
+
48
+ @sample_rate =
49
+ if config[:sample_rate] then
50
+ config[:sample_rate]
51
+ else
52
+ 0.1
53
+ end
54
+
55
+ ::Trace.tracer = ::Trace::ZipkinTracer.new(CarelessScribe.new(scribe), scribe_max_buffer)
56
+ end
57
+
58
+ def call(env)
59
+ ::Trace.default_endpoint = ::Trace.default_endpoint.with_service_name(@service_name).with_port(@service_port)
60
+ ::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) }
63
+ end
64
+
65
+ private
66
+ def tracing_filter(trace_id, env)
67
+ @lock.synchronize do
68
+ ::Trace.push(trace_id)
69
+ ::Trace.set_rpc_name(env["REQUEST_METHOD"]) # get/post and all that jazz
70
+ ::Trace.record(::Trace::BinaryAnnotation.new("http.uri", env["PATH_INFO"], "STRING", ::Trace.default_endpoint))
71
+ ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::SERVER_RECV, ::Trace.default_endpoint))
72
+ end
73
+ yield if block_given?
74
+ ensure
75
+ @lock.synchronize do
76
+ ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::SERVER_SEND, ::Trace.default_endpoint))
77
+ ::Trace.pop
78
+ end
79
+ end
80
+
81
+ private
82
+ def get_or_create_trace_id(env, default_flags = ::Trace::Flags::EMPTY)
83
+ trace_parameters = if B3_REQUIRED_HEADERS.all? { |key| env.has_key?(key) }
84
+ env.values_at(*B3_HEADERS)
85
+ else
86
+ new_id = Trace.generate_id
87
+ [new_id, nil, new_id, ("true" if Trace.should_sample?), default_flags]
88
+ end
89
+ trace_parameters[3] = (trace_parameters[3] == "true")
90
+ trace_parameters[4] = (trace_parameters[4] || default_flags).to_i
91
+
92
+ Trace::TraceId.new(*trace_parameters)
93
+ end
94
+ end
95
+
96
+ end
@@ -0,0 +1,35 @@
1
+ # Copyright 2012 Twitter Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ class CarelessScribe
16
+ def initialize(scribe)
17
+ @scribe = scribe
18
+ end
19
+
20
+ def log(*args)
21
+ @scribe.log(*args)
22
+ rescue ThriftClient::NoServersAvailable, Thrift::Exception
23
+ # I couldn't care less
24
+ end
25
+
26
+ def batch(&block)
27
+ @scribe.batch(&block)
28
+ rescue ThriftClient::NoServersAvailable, Thrift::Exception
29
+ # I couldn't care less
30
+ end
31
+
32
+ def method_missing(name, *args)
33
+ @scribe.send(name, *args)
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ # Copyright 2012 Twitter Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ module ZipkinTracer
15
+ VERSION = "0.2.0"
16
+ end
17
+
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lookout-zipkin-tracer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Ariel Salomon, Franklin Hu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: finagle-thrift
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.3.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: scribe
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.4
41
+ description: Adds tracing instrumentation for ruby applications
42
+ email:
43
+ - ariel@lookout.com, franklin@twitter.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/zipkin-tracer.rb
49
+ - lib/zipkin-tracer/careless_scribe.rb
50
+ - lib/zipkin-tracer/version.rb
51
+ homepage: https://github.com/lookout/zipkin
52
+ licenses: []
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: 1.3.5
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.2.2
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Ruby tracing via Zipkin
74
+ test_files: []