ruby-zipkin 0.2.5 → 0.2.6

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.
data/lib/ruby-zipkin.rb CHANGED
@@ -4,36 +4,47 @@ require 'ruby-zipkin/scriber'
4
4
  require 'ruby-zipkin/headers'
5
5
  require 'ruby-zipkin/metadata_logger'
6
6
  require 'ruby-zipkin/tracesampler'
7
+ require 'ruby-zipkin/trace_filter'
7
8
 
8
9
  module RubyZipkin extend self
9
10
 
10
11
  class RackHandler
11
12
 
12
- def initialize(app, service_name ='flexd', scribe_server = "204.77.168.48", scribe_port = 9410, scribe_max_buffer = 10 )
13
+ def initialize(app, service_name, scribe_server, scribe_port, uri_filter_list =[], http_header_filter_list = [], scribe_max_buffer = 10)
13
14
  @app = app
14
15
  @lock = Mutex.new
15
16
  @service_name = service_name
16
17
  @scribe_port = scribe_port
17
18
  @sample_rate = 0.1
19
+ @uri_filter_list = uri_filter_list
20
+ @http_header_filter_list = http_header_filter_list
18
21
  @scribe = Scribe.new("#{scribe_server}:#{scribe_port}")
22
+ puts "using scribe server: #{@scribe.to_s}"
23
+ puts "#{scribe_server}:#{scribe_port}"
19
24
  ::Trace.tracer = ::Trace::ZipkinTracer.new(Scriber.new(@scribe), scribe_max_buffer)
20
- end
25
+ end
21
26
 
22
27
  def call(env)
23
-
24
- ::Trace.default_endpoint = ::Trace.default_endpoint.with_service_name(@service_name).with_port(@scribe_port)
25
- ::Trace.sample_rate = 1
26
-
27
- env[ZipkinTraceHeader::PARENT_SPAN_ID] = @spanid
28
- env[ZipkinTraceHeader::TRACE_ID] = @tid
29
-
30
- @status, @headers, @response = @app.call(env)
31
- tracing_filter(get_or_create_trace_id(env), env, @headers)
28
+ begin
29
+ ::Trace.default_endpoint = ::Trace.default_endpoint.with_service_name(@service_name)
30
+ ::Trace.sample_rate = 0.1
31
+ env[ZipkinTraceHeader::PARENT_SPAN_ID] = @spanid
32
+ env[ZipkinTraceHeader::TRACE_ID] = @tid
33
+ @status, @headers, @response = @app.call(env)
34
+ tracing_filter(get_or_create_trace_id2(env), env, @headers)
35
+ rescue => e
36
+ #Tracing errors shouldn't block a request.
37
+ puts "Zipkin error: #{e} \n #{e.backtrace}"
38
+ end
32
39
  [@status, @headers, @response]
33
40
  end
34
41
 
35
42
  private
36
43
  def tracing_filter(trace_id, env, headers=nil)
44
+ if RubyZipkin::TraceFilter.UriFilterMatches?(env["PATH_INFO"], @uri_filter_list)
45
+ puts "ZIPKIN URI FILTER MATCHED. NOT LOGGING"
46
+ return
47
+ end
37
48
  @lock.synchronize do
38
49
  puts "zipkin trace id #{trace_id}"
39
50
  ::Trace.push(trace_id)
@@ -42,19 +53,23 @@ module RubyZipkin extend self
42
53
  ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::SERVER_RECV, ::Trace.default_endpoint))
43
54
  trace_hash(env)
44
55
  trace_hash(headers)
56
+ puts 'zipkin traced in'
45
57
  end
46
58
  yield if block_given?
47
59
  ensure
48
60
  @lock.synchronize do
49
61
  ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::SERVER_SEND, ::Trace.default_endpoint))
50
62
  ::Trace.pop
63
+ puts 'zipking trace out'
51
64
  end
52
65
  end
53
66
 
54
67
  def trace_hash(content)
55
68
  if content
56
69
  content.each do |k,v|
57
- MetadataLogger.log(k.to_s, v.to_s)
70
+ unless RubyZipkin::TraceFilter.KeywordFilterMatches?(k.to_s, @http_header_filter_list)
71
+ MetadataLogger.log(k.to_s, v)
72
+ end
58
73
  end
59
74
  end
60
75
  end
@@ -77,11 +92,11 @@ module RubyZipkin extend self
77
92
  env[ZipkinTraceHeader::SPAN_ID] = @spanid
78
93
 
79
94
  # from Finagle source: flags are 0 = none , 1 = debug
80
- Trace::TraceId.new @tid, @parentspan, @spanid, true, 0
95
+ Trace::TraceId.new @tid, @parentspan, @spanid, true, ::TRACE::Flags::empty
81
96
  end
82
97
 
83
98
  def get_or_create_trace_id2(env)
84
- id = ::Trace::TraceId.new(::Trace.generate_id, nil, ::Trace.generate_id, true, ::Trace::Flags::EMPTY)
99
+ id = ::Trace::TraceId.new(::Trace.generate_id, nil, ::Trace.generate_id, true, ::Trace::Flags::DEBUG)
85
100
  end
86
101
  end
87
102
  end
@@ -5,19 +5,19 @@ require 'finagle-thrift/trace'
5
5
  module RubyZipkin
6
6
  class MetadataLogger
7
7
 
8
+ # log a key value pair into a zipkin trace as a KV span value
8
9
  def self.log(key, value)
9
- ::Trace.record(::Trace::BinaryAnnotation.new(key, value, "STRING", ::Trace.default_endpoint))
10
+ if key.to_s == "action_controller.rescue.request" || key.to_s == "action_controller.rescue.response"
11
+ ::Trace.record(::Trace::BinaryAnnotation.new(key, value.as_json.to_s, "STRING", ::Trace.default_endpoint))
12
+ else
13
+ ::Trace.record(::Trace::BinaryAnnotation.new(key, value.to_s, "STRING", ::Trace.default_endpoint))
14
+ end
10
15
  end
11
16
 
12
17
  # Do a full dump of a request header into zipkin.
13
- def self.log_request(headers, service_name = "UNDEFINED")
18
+ def self.log_request(headers)
14
19
  if headers
15
-
16
- #request = Rack::Request.new(headers)
17
- # MetadataLogger.log("PARAMETERS", headers.params)
18
-
19
20
  headers.each do |header, value|
20
- puts "logging on zipkin: #{header}, #{value}"
21
21
  MetadataLogger.log("HEADER_#{header}", value)
22
22
  end
23
23
  end
@@ -2,13 +2,31 @@
2
2
  module RubyZipkin
3
3
  class TraceFilter
4
4
 
5
- def self.UriFilter(uri)
5
+ # Filter uri's from being traced based on their type
6
+ # or a keyword listed in the blacklist
7
+ # Known static content requests like fonts are filtered as well
8
+ def self.UriFilterMatches?(uri, keyword_blacklist = [])
6
9
  #exclude static content requests
7
- if uri.match /.*(\.svg)|(\.ttf)|(.woff)/
8
- return false
10
+ if uri.to_s.match(/.*(\.svg)|(\.ttf)|(\.ott)|(\.woff)/)
11
+ puts "zipkin uri #{uri} matches!"
12
+ return true
9
13
  end
10
- true
14
+
15
+ #check the uri against the blacklist
16
+ return KeywordFilterMatches?(uri, keyword_blacklist)
11
17
  end
12
18
 
19
+ # Filter a string based on a keyword blacklist
20
+ # Meant to define if a key should be traced or not.
21
+ def self.KeywordFilterMatches?(keyword, keyword_blacklist = [])
22
+ #exclude static content requests
23
+ keyword_blacklist.each do |blacklisted|
24
+ if keyword.to_s.match(/.*(#{blacklisted}).*/)
25
+ puts "zipkin keyword #{keyword} matches #{keyword}!"
26
+ return true
27
+ end
28
+ end
29
+ false
30
+ end
13
31
  end
14
32
  end
@@ -1,3 +1,3 @@
1
1
  module RubyZipkin
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-zipkin
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 5
10
- version: 0.2.5
9
+ - 6
10
+ version: 0.2.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ivan Marcin
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-09-27 00:00:00 Z
18
+ date: 2013-10-02 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: finagle-thrift