rubyzipkin 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rubyzipkin.rb CHANGED
@@ -15,7 +15,7 @@ module RubyZipkin extend self
15
15
 
16
16
  class RackHandler
17
17
 
18
- def initialize(app, service_name, scribe_server, scribe_port, uri_filter_list = [], http_header_filter_list = [], uri_sample_filter_list = {}, sampling_rate = 0.0001, scribe_max_buffer = 50 )
18
+ def initialize(app, service_name, scribe_server, scribe_port, uri_filter_list = [], http_header_filter_list = [], uri_sample_filter_list = {}, sampling_rate = 0.0001, scribe_max_buffer = 50, redis_config = 'config/redis.yml')
19
19
  @app = app
20
20
  @lock = Mutex.new
21
21
 
@@ -26,12 +26,30 @@ module RubyZipkin extend self
26
26
  @http_header_filter_list = http_header_filter_list
27
27
  @uri_sample_filter_list = uri_sample_filter_list
28
28
  @scribe = Scribe.new("#{scribe_server}:#{scribe_port}")
29
+ @redis_config = redis_config
30
+ @sampled_ids = []
31
+ $redis_check_time = Time.now
29
32
  ::Trace.tracer = ::Trace::ZipkinTracer.new(Scriber.new(@scribe), scribe_max_buffer)
33
+
34
+ begin
35
+ @redis = redis_from_config(redis_config) if File.exists?(@redis_config)
36
+ rescue => e
37
+ $stderr.puts "No Redis instance"
38
+ end
39
+ end
40
+
41
+ def redis_from_config(file_name)
42
+ config = YAML.load_file(file_name)['production']
43
+ if config && (host = config['host']) && (port = config['port'])
44
+ instance = Redis.new(:host => host, :port => port, :logger => Rails.logger, :thread_safe => true)
45
+ instance.select(config['db']) if config['db']
46
+ instance
47
+ end
30
48
  end
31
49
 
32
50
  def call(env)
33
51
  begin
34
- ::Trace.default_endpoint = ::Trace.default_endpoint.with_service_name(@service_name + "_#{normalized_uri(env['PATH_INFO'])}")
52
+ ::Trace.default_endpoint = ::Trace.default_endpoint.with_service_name(@service_name + "/#{top_level_path(env['PATH_INFO'])}")
35
53
  set_sample_rate(env)
36
54
  trace_id = get_or_create_trace_id(env)
37
55
  env[ZipkinTraceHeader::PARENT_SPAN_ID] = @spanid
@@ -43,6 +61,7 @@ module RubyZipkin extend self
43
61
 
44
62
  begin
45
63
  @status, @headers, @response = @app.call(env)
64
+ redis_sampling
46
65
  tracing_filter(trace_id, env, @headers)
47
66
  rescue => e
48
67
  # we want to do the call even if zipkin fails
@@ -51,6 +70,24 @@ module RubyZipkin extend self
51
70
  [@status, @headers, @response]
52
71
  end
53
72
 
73
+ private
74
+ def get_ids_from_redis
75
+ if @redis and (Time.now - $redis_check_time) > 1 #5 minutes
76
+ @sampled_ids = []
77
+ keys = Redis::Safe.instance(:default).keys("zipkin*")
78
+ keys.each { |i| @sampled_ids.push( i.split('_')[1] ) }
79
+ $redis_check_time = Time.now
80
+ end
81
+ end
82
+
83
+ private
84
+ def redis_sampling
85
+ get_ids_from_redis
86
+ if @headers[ZipkinTraceHeader::LOOKOUT_SAMPLE] and @sampled_ids.include?(@headers[ZipkinTraceHeader::LOOKOUT_SAMPLE])
87
+ ::Trace.sample_rate = 1.0
88
+ end
89
+ end
90
+
54
91
  private
55
92
  def set_sample_rate(env)
56
93
  custom_sample_rate =RubyZipkin::TraceFilter.UriSamplingMatches?(normalized_uri(env["PATH_INFO"]), @uri_sample_filter_list)
@@ -72,7 +109,7 @@ module RubyZipkin extend self
72
109
  ::Trace.push(trace_id)
73
110
 
74
111
  set_trace_caption(env)
75
- ::Trace.record(::Trace::BinaryAnnotation.new("http.uri", normalized_uri(env["PATH_INFO"]), "STRING", ::Trace.default_endpoint))
112
+ ::Trace.record(::Trace::BinaryAnnotation.new("http.uri", top_level_path(env["PATH_INFO"]), "STRING", ::Trace.default_endpoint))
76
113
  ::Trace.record(::Trace::BinaryAnnotation.new("http.status", @status.to_s, "STRING", ::Trace.default_endpoint))
77
114
  ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::SERVER_RECV, ::Trace.default_endpoint))
78
115
 
@@ -101,8 +138,21 @@ module RubyZipkin extend self
101
138
  path.gsub(/\/\d+/, '/:id')
102
139
  end
103
140
 
141
+ def top_level_path(path)
142
+ path.split('/')[1]
143
+ end
144
+
145
+ def uri_summary(path)
146
+ suffix = path.match /\w+$/
147
+ if suffix.to_s.length > 10
148
+ normalized_uri(path.gsub(/\w+$/,''))
149
+ else
150
+ normalized_uri(path)
151
+ end
152
+ end
153
+
104
154
  def set_trace_caption(env)
105
- ::Trace.set_rpc_name("#{env['REQUEST_METHOD']} - #{@status.to_s} - #{normalized_uri(env['PATH_INFO'])}")
155
+ ::Trace.set_rpc_name("#{env['REQUEST_METHOD']} - #{@status.to_s} - #{uri_summary(env['PATH_INFO'])}")
106
156
  end
107
157
 
108
158
  def trace_post_data(content)
@@ -7,5 +7,6 @@ module RubyZipkin
7
7
  TRACE_SAMPLED = "X-B3-SAMPLED"
8
8
  TRACE_FLAGS = "X-B3-FLAGS"
9
9
  FORCE_SAMPLE = "X-B3-FORCE-SAMPLE"
10
+ LOOKOUT_SAMPLE = "X-LOOKOUT-DGUID"
10
11
  end
11
12
  end
@@ -15,7 +15,8 @@ module RubyZipkin
15
15
  "rack.request.form_vars",
16
16
  "action_controller.rescue",
17
17
  "action_controller.rescue.request",
18
- "action_controller.rescue.response"
18
+ "action_controller.rescue.response",
19
+ "gon", #rails internationalization
19
20
  ]
20
21
  end
21
22
  end
@@ -1,3 +1,3 @@
1
1
  module RubyZipkin
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyzipkin
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 2
10
- version: 0.4.2
9
+ - 3
10
+ version: 0.4.3
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-12-17 00:00:00 Z
18
+ date: 2014-01-26 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: scribe