lookout-zipkin-tracer 0.4.0-java

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d590ed9849cf8a45bc1346e7be7f14de75257715
4
+ data.tar.gz: a81d55e21d250d3fc2470676ef45290a94e2d342
5
+ SHA512:
6
+ metadata.gz: 4491283ce8e1d5ec5d7cb4bb7008b38246df809ebe1ec6efc4df8091adfc70386c7189547ee6c2b41a545f49806df764deb84f91df3427f3033f41efcb02a9cc
7
+ data.tar.gz: b37e411569e345294bdabf78be543de197707b3aacd8871b328736a5df48e1241506aa5cc8868a7dc582fb6afd39d2e3a269c72c85b5c1c0e77269421b07cc38
@@ -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.4.0"
16
+ end
17
+
@@ -0,0 +1,65 @@
1
+ require 'finagle-thrift'
2
+ require 'finagle-thrift/tracer'
3
+ require 'hermann/producer'
4
+ require 'hermann/discovery/zookeeper'
5
+
6
+ module Trace
7
+ class ZipkinKafkaTracer < Tracer
8
+ TRACER_CATEGORY = "zipkin".freeze
9
+ DEFAULT_KAFKA_TOPIC = "zipkin_kafka".freeze
10
+
11
+ def initialize(opts={})
12
+ @logger = opts[:logger]
13
+ @topic = opts[:topic] || DEFAULT_KAFKA_TOPIC
14
+ reset
15
+ end
16
+
17
+ # need to connect after initialization
18
+ def connect(zookeepers)
19
+ broker_ids = Hermann::Discovery::Zookeeper.new(zookeepers).get_brokers
20
+ @producer = Hermann::Producer.new(nil, broker_ids)
21
+ end
22
+
23
+ def record(id, annotation)
24
+ return unless id.sampled?
25
+ span = get_span_for_id(id)
26
+
27
+ case annotation
28
+ when BinaryAnnotation
29
+ span.binary_annotations << annotation
30
+ when Annotation
31
+ span.annotations << annotation
32
+ end
33
+
34
+ flush!
35
+ end
36
+
37
+ def set_rpc_name(id, name)
38
+ return unless id.sampled?
39
+ span = get_span_for_id(id)
40
+ span.name = name.to_s
41
+ end
42
+
43
+ private
44
+ def reset
45
+ @spans = {}
46
+ end
47
+
48
+ def get_span_for_id(id)
49
+ key = id.span_id.to_s
50
+ @spans[key] ||= begin
51
+ Span.new("", id)
52
+ end
53
+ end
54
+
55
+ def flush!
56
+ messages = @spans.values.map do |span|
57
+ buf = ''
58
+ trans = Thrift::MemoryBufferTransport.new(buf)
59
+ oprot = Thrift::BinaryProtocol.new(trans)
60
+ span.to_thrift.write(oprot)
61
+ @producer.push(buf, :topic => @topic).value!
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,111 @@
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
+ require 'zipkin-tracer/zipkin_kafka_tracer'
20
+
21
+ module ZipkinTracer extend self
22
+
23
+ class RackHandler
24
+ B3_REQUIRED_HEADERS = %w[HTTP_X_B3_TRACEID HTTP_X_B3_PARENTSPANID HTTP_X_B3_SPANID HTTP_X_B3_SAMPLED]
25
+ B3_OPT_HEADERS = %w[HTTP_X_B3_FLAGS]
26
+
27
+ def initialize(app, config=nil)
28
+ @app = app
29
+ @lock = Mutex.new
30
+
31
+ config ||= app.config.zipkin_tracer # if not specified, try on app (e.g. Rails 3+)
32
+ @service_name = config[:service_name]
33
+ @service_port = config[:service_port]
34
+
35
+ if config[:scribe_server]
36
+ scribe = config[:scribe_server] ? Scribe.new(config[:scribe_server]) : Scribe.new()
37
+ careless_scribe = CarelessScribe.new(scribe)
38
+ scribe_max_buffer = config[:scribe_max_buffer] ? config[:scribe_max_buffer] : 10
39
+ ::Trace.tracer = ::Trace::ZipkinTracer.new(careless_scribe, scribe_max_buffer)
40
+ elsif config[:zookeeper]
41
+ ::Trace.tracer = ::Trace::ZipkinKafkaTracer.new(config[:zookeeper])
42
+ end
43
+
44
+ @sample_rate = config[:sample_rate] ? config[:sample_rate] : 0.1
45
+
46
+ @annotate_plugin = config[:annotate_plugin] # call for trace annotation
47
+ @filter_plugin = config[:filter_plugin] # skip tracing if returns false
48
+ @whitelist_plugin = config[:whitelist_plugin] # force sampling if returns true
49
+
50
+
51
+ end
52
+
53
+ def call(env)
54
+ # skip certain requests
55
+ return @app.call(env) if filtered?(env)
56
+
57
+ ::Trace.default_endpoint = ::Trace.default_endpoint.with_service_name(@service_name).with_port(@service_port)
58
+ ::Trace.sample_rate=(@sample_rate)
59
+ whitelisted = force_sample?(env)
60
+ id = get_or_create_trace_id(env, whitelisted) # note that this depends on the sample rate being set
61
+ tracing_filter(id, env, whitelisted) { @app.call(env) }
62
+ end
63
+
64
+ private
65
+ def annotate(env, status, response_headers, response_body)
66
+ @annotate_plugin.call(env, status, response_headers, response_body) if @annotate_plugin
67
+ end
68
+
69
+ def filtered?(env)
70
+ @filter_plugin && !@filter_plugin.call(env)
71
+ end
72
+
73
+ def force_sample?(env)
74
+ @whitelist_plugin && @whitelist_plugin.call(env)
75
+ end
76
+
77
+ def tracing_filter(trace_id, env, whitelisted=false)
78
+ @lock.synchronize do
79
+ ::Trace.push(trace_id)
80
+ ::Trace.set_rpc_name(env["REQUEST_METHOD"]) # get/post and all that jazz
81
+ ::Trace.record(::Trace::BinaryAnnotation.new("http.uri", env["PATH_INFO"], "STRING", ::Trace.default_endpoint))
82
+ ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::SERVER_RECV, ::Trace.default_endpoint))
83
+ ::Trace.record(::Trace::Annotation.new('whitelisted', ::Trace.default_endpoint)) if whitelisted
84
+ end
85
+ status, headers, body = yield if block_given?
86
+ ensure
87
+ @lock.synchronize do
88
+ ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::SERVER_SEND, ::Trace.default_endpoint))
89
+ annotate(env, status, headers, body)
90
+ ::Trace.pop
91
+ end
92
+ end
93
+
94
+ private
95
+ def get_or_create_trace_id(env, whitelisted, default_flags = ::Trace::Flags::EMPTY)
96
+ trace_parameters = if B3_REQUIRED_HEADERS.all? { |key| env.has_key?(key) }
97
+ env.values_at(*B3_REQUIRED_HEADERS)
98
+ else
99
+ new_id = Trace.generate_id
100
+ [new_id, nil, new_id, ("true" if whitelisted || Trace.should_sample?)]
101
+ end
102
+ trace_parameters[3] = (trace_parameters[3] == "true")
103
+
104
+ trace_parameters += env.values_at(*B3_OPT_HEADERS) # always check flags
105
+ trace_parameters[4] = (trace_parameters[4] || default_flags).to_i
106
+
107
+ Trace::TraceId.new(*trace_parameters)
108
+ end
109
+ end
110
+
111
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lookout-zipkin-tracer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: java
6
+ authors:
7
+ - Ariel Salomon, Franklin Hu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: finagle-thrift
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.0
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 1.3.0
25
+ prerelease: false
26
+ type: :runtime
27
+ - !ruby/object:Gem::Dependency
28
+ name: scribe
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.4
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 0.2.4
39
+ prerelease: false
40
+ type: :runtime
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ prerelease: false
54
+ type: :runtime
55
+ - !ruby/object:Gem::Dependency
56
+ name: hermann
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.20.1
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 0.20.1
67
+ prerelease: false
68
+ type: :runtime
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.0
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ~>
79
+ - !ruby/object:Gem::Version
80
+ version: 3.0.0
81
+ prerelease: false
82
+ type: :development
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack-test
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ prerelease: false
96
+ type: :development
97
+ description: Adds tracing instrumentation for ruby applications
98
+ email:
99
+ - ariel@lookout.com, franklin@twitter.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - lib/zipkin-tracer.rb
105
+ - lib/zipkin-tracer/careless_scribe.rb
106
+ - lib/zipkin-tracer/version.rb
107
+ - lib/zipkin-tracer/zipkin_kafka_tracer.rb
108
+ homepage: https://github.com/lookout/zipkin
109
+ licenses: []
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: 1.3.5
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.1.9
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Ruby tracing via Zipkin
131
+ test_files: []