rubyzipkin 0.4.5 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,6 @@
1
1
  require 'finagle-thrift'
2
2
  require 'finagle-thrift/trace'
3
3
  require 'scribe'
4
-
5
4
  require 'rubyzipkin/version'
6
5
  require 'rubyzipkin/scriber'
7
6
  require 'rubyzipkin/headers'
@@ -27,73 +26,91 @@ module RubyZipkin extend self
27
26
  @uri_sample_filter_list = uri_sample_filter_list
28
27
  @scribe = Scribe.new("#{scribe_server}:#{scribe_port}")
29
28
  @redis_config = redis_config
30
- @sampled_ids = []
29
+ @sampled_ids = {}
31
30
  $redis_check_time = Time.now
32
31
  ::Trace.tracer = ::Trace::ZipkinTracer.new(Scriber.new(@scribe), scribe_max_buffer)
33
32
 
34
33
  begin
35
- @redis = redis_from_config(redis_config) if File.exists?(@redis_config)
34
+ if $redis.nil?
35
+ $redis = redis_from_config(@redis_config) if File.exist?(@redis_config)
36
+ end
36
37
  rescue => e
37
- $stderr.puts "No Redis instance"
38
+ $stderr.puts "ZIPKIN [ERR] Redis instance could not be configured (nil)"
38
39
  end
39
40
  end
40
41
 
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
42
+ def call(env)
43
+
44
+ if $redis.nil?
45
+ return
47
46
  end
48
- end
49
47
 
50
- def call(env)
51
48
  begin
52
49
  ::Trace.default_endpoint = ::Trace.default_endpoint.with_service_name(@service_name + "/#{top_level_path(env['PATH_INFO'])}")
53
50
  set_sample_rate(env)
54
51
  trace_id = get_or_create_trace_id(env)
55
- env[ZipkinTraceHeader::PARENT_SPAN_ID] = @spanid
56
- env[ZipkinTraceHeader::TRACE_ID] = @tid
57
-
58
52
  rescue => e
59
53
  $stderr.puts "ZIPKIN [ERR] #{e}"
60
54
  end
61
55
 
62
56
  begin
63
- @status, @headers, @response = @app.call(env)
64
- redis_sampling
65
- tracing_filter(trace_id, env, @headers)
57
+ status, headers, response = @app.call(env)
58
+ redis_sampling(headers)
59
+ tracing_filter(trace_id, env, headers, status)
66
60
  rescue => e
67
61
  # we want to do the call even if zipkin fails
68
62
  $stderr.puts "ZIPKIN [ERR] #{e} \n\n\n #{e.backtrace}"
69
63
  end
70
- [@status, @headers, @response]
64
+ [status, headers, response]
65
+ end
66
+
67
+ # Return the current set of IDS to be unsampled
68
+ # as selected from redis.
69
+ def redis_sampled_ids
70
+ @sampled_ids.to_s
71
71
  end
72
72
 
73
- private
73
+ def redis_from_config(file_name)
74
+ config = YAML.load_file(file_name)['production']
75
+ if config && (host = config['host']) && (port = config['port'])
76
+ instance = Redis.new(:host => host, :port => port, :thread_safe => true)
77
+ instance.select(config['db']) if config['db']
78
+ instance
79
+ end
80
+ end
81
+ private :redis_from_config
82
+
74
83
  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).get("zipkin_trace_keys")
84
+ if $redis and (Time.now - $redis_check_time) > 600 # seconds
85
+ @sampled_ids = {}
78
86
 
79
- # Lookup in trace keys only if som keys have been found
80
- unless keys.nil?
81
- keys.each { |i| @sampled_ids.push( i.split('_')[1] ) }
87
+ # ID's will be set as a sorted map of GUIDS from flexd's admin page
88
+ key = $redis.get("zipkin_traced_dguids")
89
+
90
+ unless key.nil?
91
+ begin
92
+ @sampled_ids = JSON.parse(key)
93
+ rescue => e
94
+ puts "ZIPKIN [ERR] unable to parse key set from redis keys: #{e.message}"
95
+ end
82
96
  end
83
97
 
84
98
  $redis_check_time = Time.now
85
99
  end
86
100
  end
101
+ private :get_ids_from_redis
87
102
 
88
- private
89
- def redis_sampling
103
+ def redis_sampling(headers)
90
104
  get_ids_from_redis
91
- if @headers[ZipkinTraceHeader::LOOKOUT_SAMPLE] and @sampled_ids.include?(@headers[ZipkinTraceHeader::LOOKOUT_SAMPLE])
92
- ::Trace.sample_rate = 1.0
93
- end
105
+ ::Trace.sample_rate = 1.0 if id_in_sample_set?(headers)
94
106
  end
107
+ private :redis_sampling
108
+
109
+ def id_in_sample_set?(headers)
110
+ headers[ZipkinTraceHeader::LOOKOUT_SAMPLE] and @sampled_ids.keys.include?(headers[ZipkinTraceHeader::LOOKOUT_SAMPLE])
111
+ end
112
+ private :id_in_sample_set?
95
113
 
96
- private
97
114
  def set_sample_rate(env)
98
115
  custom_sample_rate =RubyZipkin::TraceFilter.UriSamplingMatches?(normalized_uri(env["PATH_INFO"]), @uri_sample_filter_list)
99
116
 
@@ -103,19 +120,19 @@ module RubyZipkin extend self
103
120
  ::Trace.sample_rate = @sample_rate
104
121
  end
105
122
  end
123
+ private :set_sample_rate
106
124
 
107
- private
108
- def tracing_filter(trace_id, env, headers=nil)
125
+ def tracing_filter(trace_id, env, headers=nil, status='')
109
126
  if RubyZipkin::TraceFilter.UriFilterMatches?(normalized_uri(env["PATH_INFO"]), @uri_filter_list)
110
- "ZIPKIN: path matches filter. returning"
111
127
  return
112
128
  end
113
129
  @lock.synchronize do
114
130
  ::Trace.push(trace_id)
115
131
 
116
- set_trace_caption(env)
132
+ set_trace_caption(env, status)
133
+
117
134
  ::Trace.record(::Trace::BinaryAnnotation.new("http.uri", top_level_path(env["PATH_INFO"]), "STRING", ::Trace.default_endpoint))
118
- ::Trace.record(::Trace::BinaryAnnotation.new("http.status", @status.to_s, "STRING", ::Trace.default_endpoint))
135
+ ::Trace.record(::Trace::BinaryAnnotation.new("http.status", status.to_s, "STRING", ::Trace.default_endpoint))
119
136
  ::Trace.record(::Trace::Annotation.new(::Trace::Annotation::SERVER_RECV, ::Trace.default_endpoint))
120
137
 
121
138
  add_referer(env)
@@ -132,6 +149,7 @@ module RubyZipkin extend self
132
149
  p = ::Trace.pop
133
150
  end
134
151
  end
152
+ private :tracing_filter
135
153
 
136
154
  def add_referer(env)
137
155
  if env['HTTP_REFERER']
@@ -156,12 +174,11 @@ module RubyZipkin extend self
156
174
  end
157
175
  end
158
176
 
159
- def set_trace_caption(env)
160
- ::Trace.set_rpc_name("#{env['REQUEST_METHOD']} - #{@status.to_s} - #{uri_summary(env['PATH_INFO'])}")
177
+ def set_trace_caption(env, status)
178
+ ::Trace.set_rpc_name("#{env['REQUEST_METHOD']} - #{status.to_s} - #{uri_summary(env['PATH_INFO'])}")
161
179
  end
162
180
 
163
181
  def trace_post_data(content)
164
-
165
182
  # Slightly large size cap to the request input to accomodate larger JSON bodies.
166
183
  MetadataLogger.log('HTTP_REQUEST_INPUT', content.to_s, 1024)
167
184
  end
@@ -198,7 +215,7 @@ module RubyZipkin extend self
198
215
 
199
216
  @spanid = Trace.generate_id
200
217
 
201
- # Save into headers
218
+ # Save into HTTP headers
202
219
  env[ZipkinTraceHeader::TRACE_ID] = @trace_id
203
220
  env[ZipkinTraceHeader::SPAN_ID] = @spanid
204
221
  env[ZipkinTraceHeader::PARENT_SPAN_ID] = @parentspan
@@ -1,3 +1,3 @@
1
1
  module RubyZipkin
2
- VERSION = "0.4.5"
2
+ VERSION = "0.4.6"
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: 5
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 5
10
- version: 0.4.5
9
+ - 6
10
+ version: 0.4.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: 2014-02-06 00:00:00 Z
18
+ date: 2014-02-14 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: scribe