imprint 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +0 -1
- data/LICENSE.txt +1 -1
- data/README.md +146 -9
- data/Rakefile +0 -9
- data/imprint.gemspec +0 -1
- data/lib/imprint/log_helpers.rb +10 -6
- data/lib/imprint/middleware.rb +24 -6
- data/lib/imprint/rails_logger.rb +10 -10
- data/lib/imprint/tracer.rb +15 -2
- data/lib/imprint/version.rb +1 -1
- data/test/test_helper.rb +8 -3
- data/test/unit/log_helpers_test.rb +31 -16
- data/test/unit/middleware_test.rb +21 -8
- data/test/unit/tracer_test.rb +7 -5
- metadata +4 -20
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,21 @@
|
|
1
1
|
# Imprint
|
2
2
|
|
3
|
-
Imprint helps track requests across multiple log lines or applications. It
|
3
|
+
Imprint helps track requests across multiple log lines or applications. It
|
4
|
+
consists of a lightweight class and middleware to help set tracing ids.
|
4
5
|
|
5
|
-
It also has a file which can be used to bootstrap default rails logging to
|
6
|
+
It also has a file which can be used to bootstrap default rails logging to
|
7
|
+
embedding the imprint `trace_id` on each line logged.
|
6
8
|
|
7
|
-
Supporting tracing between applications requires updating client calls between
|
9
|
+
Supporting tracing between applications requires updating client calls between
|
10
|
+
applications, at the moment we don't try to monkey patch any of that in and
|
11
|
+
expect responsible clients to add the header manually as described in the Usage
|
12
|
+
section below.
|
8
13
|
|
9
|
-
If you have seen
|
14
|
+
If you have seen
|
15
|
+
[ActionDispatch::RequestId](http://api.rubyonrails.org/classes/ActionDispatch/RequestId.html).
|
16
|
+
Imprint is basically a generic Rack version of that idea. It works with Rails 3,
|
17
|
+
Sinatra, and Pure Rack. Beyond that it also provides some helpers and
|
18
|
+
configuration around the trace_id usage.
|
10
19
|
|
11
20
|
## Installation
|
12
21
|
|
@@ -56,10 +65,7 @@ def log(message = nil, severity = :info)
|
|
56
65
|
log_raw(message, severity) do
|
57
66
|
message = yield if block_given?
|
58
67
|
# append imprint trace
|
59
|
-
|
60
|
-
message = "#{message}\n" unless message[-1] == "\n"
|
61
|
-
message = message.gsub("\n"," [trace_id=#{Imprint::Middleware.get_trace_id}]\n")
|
62
|
-
end
|
68
|
+
Imprint::Tracer.insert_trace_id_in_message(message) if defined?(Imprint::Tracer)
|
63
69
|
format = []
|
64
70
|
format << Time.now.to_f
|
65
71
|
format << @host
|
@@ -72,9 +78,27 @@ def log(message = nil, severity = :info)
|
|
72
78
|
end
|
73
79
|
```
|
74
80
|
|
81
|
+
## Params logging options
|
82
|
+
|
83
|
+
By default imprint will only log the query_params opposed to all params. This is because some of our apps don't filter logs well enough. If you are filtering correctly you might want more of the parameters logged. All params are still filtered by the `Rails.application.config.filter_parameters`.
|
84
|
+
|
85
|
+
There are three options:
|
86
|
+
|
87
|
+
* `FULL_PARAMS` log all params
|
88
|
+
* `QUERY_PARAMS` log query params only (default)
|
89
|
+
* `FULL_GET_PARAMS` log full params on get requests and query only on post requests
|
90
|
+
|
91
|
+
To change from the default send the config option `:param_level` to `Imprint.configure`. You most likely want to do this in your `environment.rb`
|
92
|
+
|
93
|
+
Imprint.configure({
|
94
|
+
:param_level => Imprint::FULL_GET_PARAMS
|
95
|
+
})
|
96
|
+
|
97
|
+
|
98
|
+
|
75
99
|
## Optional Helpers
|
76
100
|
|
77
|
-
You can get a configurable log entrypoint for apps that allows for some
|
101
|
+
You can get a configurable log entrypoint for apps that allows for some initial logging on each request. This is intended to work well and be combined with lograge, but can be helpful on its own. To use the helpers follow the steps below.
|
78
102
|
|
79
103
|
edit `config/application.rb` and append the lines below, with whatever options make sense for your projects:
|
80
104
|
|
@@ -163,6 +187,119 @@ def self.http_get(url)
|
|
163
187
|
end
|
164
188
|
end
|
165
189
|
```
|
190
|
+
## Why, and How
|
191
|
+
|
192
|
+
Large systems that are composed of multiple communicating, cooperating parts can
|
193
|
+
be difficult to understand. The idea of Imprint is that it is very useful to
|
194
|
+
assign a unique identifier to each *top-level*, *initiating* event that starts a
|
195
|
+
series of operations within your system, and have that identifier propagated
|
196
|
+
throughout the entire system, and attached to relevant diagnostic information
|
197
|
+
(especially log messages). If a system is consistent about doing this, it
|
198
|
+
becomes easy to trace or visualize *all* of the actions that are taken as a part
|
199
|
+
of processing some request or event, or as side effects. That can be extremely
|
200
|
+
useful for diagnosing bugs, finding bottlenecks, documenting intrusions, or
|
201
|
+
simply understanding the structure of a large complex system.
|
202
|
+
|
203
|
+
Imprint calls these identifiers *trace ids*.
|
204
|
+
|
205
|
+
Initiating events are, technically, anything that takes place in your system
|
206
|
+
that does not already have a trace id associated with it. Typically, they
|
207
|
+
include:
|
208
|
+
|
209
|
+
* Initial browser requests from users (internal or external) using web
|
210
|
+
applications
|
211
|
+
* Cron jobs or other scheduled jobs that initiate periodic processing
|
212
|
+
* API requests that come from integration partners
|
213
|
+
|
214
|
+
Imprint and tools like it should log all initiating events; that is, they
|
215
|
+
should log each time that they assign a new trace id to a request or event
|
216
|
+
that does not already have one. If you see initiating events where you do not
|
217
|
+
expect them, that might just mean that part of your system is not propagating
|
218
|
+
existing trace ids properly as it sends messages or calls other services.
|
219
|
+
However, it might indicate an attempt to penetrate your system in an
|
220
|
+
unauthorized way. It is a good idea to catalog the known initiating events in
|
221
|
+
your system, and set up some kind of monitoring to notice and alert you of
|
222
|
+
unexpected ones.
|
223
|
+
|
224
|
+
Request tracing across a complex system can't be accomplished just by a single
|
225
|
+
Ruby gem. It requires cooperation from all of the applications, services, and
|
226
|
+
components of the system. The rest of this section documents how Imprint works
|
227
|
+
and what it expects from the other parts of the system, to help you implement
|
228
|
+
request tracing across all of the parts of your system, even if they are not
|
229
|
+
Rails applications or even written in Ruby.
|
230
|
+
|
231
|
+
### What Imprint Does
|
232
|
+
|
233
|
+
Here's what Imprint actually does. You should implement similar functionality
|
234
|
+
for parts of your system that are not written in Ruby, or are not Rails
|
235
|
+
applications.
|
236
|
+
|
237
|
+
1. Imprint patches the Rails logger so that *all* log messages incorporate the
|
238
|
+
trace id if one is in effect when the message is logged. Each line of the
|
239
|
+
log messages ends like this: " trace_id=1411414337_pDLsqp".
|
240
|
+
2. Immediately upon receipt of each new request, Imprint checks to see whether
|
241
|
+
the request came with an attached trace id, by checking for the presence
|
242
|
+
of an `::Imprint::Tracer::TRACER_HEADER` ("HTTP_TRACE_ID") HTTP header. If
|
243
|
+
present, that trace id is kept as the trace id of the current request.
|
244
|
+
3. If no trace id was included with the request, a new trace id is assigned.
|
245
|
+
The new trace id consists of an integer timestamp (the number of seconds
|
246
|
+
since the Unix epoch) plus a random string of six upper- and lowercase
|
247
|
+
ASCII letters, separated by an underscore (e.g., "1411414337_pDLsqp").
|
248
|
+
Then it logs an initiating event
|
249
|
+
("trace_status=initiated trace_id=1411414337_pDLsqp").
|
250
|
+
4. Once a trace id has been found or generated, it is placed where every part
|
251
|
+
of the application that participates in the current request has access to
|
252
|
+
it (a variable scoped to the current thread, accessed via
|
253
|
+
`::Imprint::Tracer::get_trace_id`).
|
254
|
+
|
255
|
+
### What Cooperating Applications and Components Should Do
|
256
|
+
|
257
|
+
Imprint is just part of the total solution; applications and services have
|
258
|
+
responsibilities as well.
|
259
|
+
|
260
|
+
1. Either use Imprint (if you're building a Rails app) or implement equivalent
|
261
|
+
functionality in your app.
|
262
|
+
2. Any HTTP requests, Resque jobs, Kafka messages, or anything else that
|
263
|
+
involves a different application or service in processing the request
|
264
|
+
should be passed the trace id, either in the HTTP header or in an envelope
|
265
|
+
field or something similar with the name `::Imprint::Tracer::TRACER_KEY`
|
266
|
+
("trace_id"). (See "Threading Considerations" below if your app employs
|
267
|
+
concurrency in request processing.)
|
268
|
+
|
269
|
+
### Threading Considerations
|
270
|
+
|
271
|
+
In request tracing, it's crucial that the trace id is associated with
|
272
|
+
*everything* that is done because of the initiating event. This means that
|
273
|
+
every part of an application or component must have access to the current trace
|
274
|
+
id, even if they are in different threads or processes than the one that
|
275
|
+
initially recorded the trace id.
|
276
|
+
|
277
|
+
The Rails architecture makes this easy. From the time a Rails app receives a
|
278
|
+
request, all of the processing for that request takes place in a single thread.
|
279
|
+
So Imprint puts the trace id in a variable scoped to the current thread.
|
280
|
+
|
281
|
+
However, many applications or frameworks employ concurrency during the
|
282
|
+
processing of a single request. Such systems need to ensure that the other
|
283
|
+
threads (or processes, perhaps) that participate also know the trace id of
|
284
|
+
what they're working on.
|
285
|
+
|
286
|
+
If the initial thread simply spawns new threads to do part of the work, it
|
287
|
+
might work to simply use something like Java's `InheritableThreadLocal`.
|
288
|
+
|
289
|
+
More typically, though, parts of the work will be farmed out to worker threads
|
290
|
+
(actors, for example) that already exist in a pool and handle work for many
|
291
|
+
requests during their lifetime. In such cases, the messages or task
|
292
|
+
descriptions that are sent to those workers should include the trace id
|
293
|
+
associated with that work, and the workers should ensure that the appropriate
|
294
|
+
trace id is included in all of their processes.
|
295
|
+
|
296
|
+
So, in short: each such worker thread should be treated as if it were a
|
297
|
+
separate service: it should receive a trace id with each work request, attach
|
298
|
+
that trace id to all log messages that are part of that work request, and
|
299
|
+
propagate it in any other service or work requests that it sends. The exception
|
300
|
+
is that it should be considered an error if those internal worker threads
|
301
|
+
receive a request without a trace id; it's not reasonable for that to be an
|
302
|
+
initiating event.
|
166
303
|
|
167
304
|
## Notes / TODO
|
168
305
|
|
data/Rakefile
CHANGED
@@ -7,12 +7,3 @@ Rake::TestTask.new(:test) do |test|
|
|
7
7
|
test.pattern = 'test/**/*_test.rb'
|
8
8
|
test.verbose = true
|
9
9
|
end
|
10
|
-
|
11
|
-
|
12
|
-
# bundle exec rake console
|
13
|
-
# LS::BrowseService.host = 'http://browse-service.browse-service.qa.livingsocial.net/'
|
14
|
-
# LS::BrowseService::Client.new.get_category('washington-d-c','food-deals')
|
15
|
-
desc "start a console with the gem loaded"
|
16
|
-
task :console do
|
17
|
-
sh "irb -rubygems -I lib -r ls/browse-service-client.rb"
|
18
|
-
end
|
data/imprint.gemspec
CHANGED
@@ -20,7 +20,6 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
22
|
spec.add_development_dependency "rake"
|
23
|
-
spec.add_development_dependency "mocha", "~> 0.14.0"
|
24
23
|
spec.add_development_dependency "shoulda"
|
25
24
|
spec.add_development_dependency "rack"
|
26
25
|
spec.add_development_dependency "simplecov"
|
data/lib/imprint/log_helpers.rb
CHANGED
@@ -22,6 +22,7 @@ module Imprint
|
|
22
22
|
end
|
23
23
|
|
24
24
|
data_append << " params: "
|
25
|
+
|
25
26
|
if param_level==Imprint::FULL_PARAMS
|
26
27
|
set_full_params(log_filter, data_append)
|
27
28
|
elsif param_level==Imprint::FULL_GET_PARAMS
|
@@ -34,14 +35,17 @@ module Imprint
|
|
34
35
|
set_query_params(log_filter, data_append)
|
35
36
|
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
if defined? cookies
|
39
|
+
cookies_whitelist.each do |cookie_key|
|
40
|
+
cookie_val = cookies[cookie_key] ? cookies[cookie_key] : 'nil'
|
41
|
+
data_append << " #{cookie_key}=\"#{cookie_val}\""
|
42
|
+
end
|
43
|
+
end
|
41
44
|
|
42
45
|
logger.info "Started request_method=#{request.method.inspect} request_url=\"#{request.path}\" request_time=\"#{Time.now.to_default_s}\" request_ip=#{request.remote_ip.inspect} #{data_append}"
|
43
|
-
rescue
|
44
|
-
logger.error "error logging log_entrypoint for request"
|
46
|
+
rescue => e
|
47
|
+
logger.error "error logging log_entrypoint for request: #{e.inspect}"
|
48
|
+
logger.error e.backtrace.take(10).join("\n")
|
45
49
|
end
|
46
50
|
|
47
51
|
def set_full_params(log_filter, data_append)
|
data/lib/imprint/middleware.rb
CHANGED
@@ -1,16 +1,34 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
1
3
|
module Imprint
|
2
4
|
class Middleware
|
3
|
-
|
5
|
+
|
4
6
|
def self.set_request_trace_id(rack_env)
|
5
|
-
|
6
|
-
|
7
|
-
|
7
|
+
trace_id = rack_env[Imprint::Tracer::TRACER_HEADER] || rack_env[Imprint::Tracer::RAILS_REQUEST_ID]
|
8
|
+
if trace_id.nil?
|
9
|
+
trace_id = "#{Time.now.to_i}_#{Imprint::Tracer.rand_trace_id}"
|
10
|
+
logger.info("trace_status=initiated trace_id=#{trace_id}")
|
11
|
+
end
|
12
|
+
Imprint::Tracer.set_trace_id(trace_id, rack_env)
|
13
|
+
trace_id
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.logger=(logger)
|
17
|
+
@logger = logger
|
8
18
|
end
|
9
|
-
|
19
|
+
|
20
|
+
def self.logger
|
21
|
+
@logger ||= if defined?(Rails.logger)
|
22
|
+
Rails.logger
|
23
|
+
else
|
24
|
+
Logger.new(STDOUT)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
10
28
|
def initialize(app, opts = {})
|
11
29
|
@app = app
|
12
30
|
end
|
13
|
-
|
31
|
+
|
14
32
|
def call(env)
|
15
33
|
::Imprint::Middleware.set_request_trace_id(env)
|
16
34
|
@app.call(env)
|
data/lib/imprint/rails_logger.rb
CHANGED
@@ -10,21 +10,21 @@ if defined?(ActiveSupport::BufferedLogger)
|
|
10
10
|
# If a newline is necessary then create a new message ending with a newline.
|
11
11
|
# Ensures that the original message is not mutated.
|
12
12
|
message = "#{message}\n" unless message[-1] == "\n"
|
13
|
-
if
|
14
|
-
|
13
|
+
if defined?(Imprint::Tracer)
|
14
|
+
Imprint::Tracer.insert_trace_id_in_message(message)
|
15
15
|
end
|
16
16
|
buffer << message
|
17
17
|
auto_flush
|
18
18
|
message
|
19
19
|
else
|
20
|
-
# rails 3.2.x
|
20
|
+
# rails 3.2.x
|
21
21
|
return if !level.nil? && (level > severity)
|
22
22
|
message = (message || (block && block.call) || progname).to_s
|
23
23
|
# If a newline is necessary then create a new message ending with a newline.
|
24
24
|
# Ensures that the original message is not mutated.
|
25
25
|
message = "#{message}\n" unless message[-1] == "\n"
|
26
|
-
if
|
27
|
-
|
26
|
+
if defined?(Imprint::Tracer)
|
27
|
+
Imprint::Tracer.insert_trace_id_in_message(message)
|
28
28
|
end
|
29
29
|
@log.add(severity, message, progname, &block)
|
30
30
|
end
|
@@ -42,20 +42,20 @@ if defined?(ActiveSupport::Logger::SimpleFormatter)
|
|
42
42
|
# If a newline is necessary then create a new message ending with a newline.
|
43
43
|
# Ensures that the original message is not mutated.
|
44
44
|
message = "#{message}\n" unless message[-1] == "\n"
|
45
|
-
if
|
46
|
-
|
45
|
+
if defined?(Imprint::Tracer)
|
46
|
+
Imprint::Tracer.insert_trace_id_in_message(message)
|
47
47
|
end
|
48
48
|
message
|
49
49
|
end
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
#Rails 4 production newly generated apps
|
53
53
|
class Logger::Formatter
|
54
54
|
def call(severity, time, progname, msg)
|
55
55
|
message = msg2str(msg)
|
56
56
|
message = "#{message}\n" unless message[-1] == "\n"
|
57
|
-
if
|
58
|
-
|
57
|
+
if defined?(Imprint::Tracer)
|
58
|
+
Imprint::Tracer.insert_trace_id_in_message(message)
|
59
59
|
end
|
60
60
|
Format % [severity[0..0], format_datetime(time), $$, severity, progname, message]
|
61
61
|
end
|
data/lib/imprint/tracer.rb
CHANGED
@@ -3,6 +3,7 @@ module Imprint
|
|
3
3
|
TRACER_HEADER = 'HTTP_IMPRINTID'
|
4
4
|
TRACER_KEY = 'IMPRINTID'
|
5
5
|
RAILS_REQUEST_ID = "action_dispatch.request_id"
|
6
|
+
TRACE_ID_DEFAULT = -1
|
6
7
|
|
7
8
|
TRACE_CHARS = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
|
8
9
|
|
@@ -13,11 +14,23 @@ module Imprint
|
|
13
14
|
end
|
14
15
|
|
15
16
|
def self.get_trace_id
|
16
|
-
Thread.current
|
17
|
+
if Thread.current.key?(TRACER_KEY)
|
18
|
+
Thread.current[TRACER_KEY]
|
19
|
+
else
|
20
|
+
TRACE_ID_DEFAULT
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.insert_trace_id_in_message(message)
|
25
|
+
if message && message.is_a?(String) &&
|
26
|
+
message.length > 1 && !message.include?('trace_id=') &&
|
27
|
+
trace_id = get_trace_id && trace_id != TRACE_ID_DEFAULT
|
28
|
+
message.gsub!("\n"," trace_id=#{trace_id}\n")
|
29
|
+
end
|
17
30
|
end
|
18
31
|
|
19
32
|
def self.rand_trace_id
|
20
|
-
(0...6).map { TRACE_CHARS[rand(TRACE_CHARS.length)] }.join
|
33
|
+
(0...6).map { TRACE_CHARS[rand(TRACE_CHARS.length)] }.join
|
21
34
|
end
|
22
35
|
end
|
23
36
|
end
|
data/lib/imprint/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'simplecov'
|
3
|
-
require '
|
3
|
+
require 'minitest/autorun'
|
4
4
|
require 'shoulda'
|
5
|
-
require 'mocha/setup'
|
6
5
|
require 'rack'
|
7
6
|
|
8
7
|
SimpleCov.start do
|
@@ -13,6 +12,12 @@ end
|
|
13
12
|
|
14
13
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
15
14
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
16
|
-
Mocha::Configuration.prevent(:stubbing_non_existent_method)
|
17
15
|
|
18
16
|
require 'imprint'
|
17
|
+
|
18
|
+
class Minitest::Test
|
19
|
+
def before_setup
|
20
|
+
# Remove any existing trace id before each test
|
21
|
+
Imprint::Tracer.set_trace_id nil
|
22
|
+
end
|
23
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
|
-
require File.expand_path('
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
require 'imprint/log_helpers'
|
3
|
+
require 'logger'
|
3
4
|
|
4
5
|
####
|
5
6
|
# Testing Rails integration without Rails isn't fun
|
@@ -21,6 +22,15 @@ end
|
|
21
22
|
|
22
23
|
class Rails
|
23
24
|
def self.application
|
25
|
+
@app ||= Object.new.tap do |app|
|
26
|
+
def app.config
|
27
|
+
@config ||= Object.new.tap do |config|
|
28
|
+
def config.filter_parameters
|
29
|
+
[]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
24
34
|
end
|
25
35
|
end
|
26
36
|
|
@@ -30,19 +40,32 @@ class Time
|
|
30
40
|
end
|
31
41
|
end
|
32
42
|
|
33
|
-
class LogHelpersTest < Test
|
43
|
+
class LogHelpersTest < Minitest::Test
|
34
44
|
include Imprint::LogHelpers
|
35
45
|
Imprint.configure({})
|
36
46
|
|
37
47
|
should "log entry" do
|
38
|
-
|
39
|
-
|
40
|
-
|
48
|
+
count = 0
|
49
|
+
increment_count = Proc.new { count += 1 }
|
50
|
+
|
51
|
+
logger.stub :info, increment_count do
|
52
|
+
log_entrypoint
|
53
|
+
end
|
54
|
+
|
55
|
+
assert_equal 1, count
|
41
56
|
end
|
42
57
|
|
43
58
|
should "log entry catches exceptions and logs them" do
|
44
|
-
|
45
|
-
|
59
|
+
Imprint.stub :configuration, nil do
|
60
|
+
count = 0
|
61
|
+
increment_count = Proc.new { count += 1 }
|
62
|
+
|
63
|
+
logger.stub :error, increment_count do
|
64
|
+
log_entrypoint
|
65
|
+
end
|
66
|
+
|
67
|
+
assert_equal 2, count
|
68
|
+
end
|
46
69
|
end
|
47
70
|
|
48
71
|
protected
|
@@ -77,16 +100,8 @@ class LogHelpersTest < Test::Unit::TestCase
|
|
77
100
|
request
|
78
101
|
end
|
79
102
|
|
80
|
-
def stub_rails
|
81
|
-
rails_config ||= mock('config')
|
82
|
-
rails_config.stubs(:filter_parameters).returns([])
|
83
|
-
rails_app ||= mock('application')
|
84
|
-
rails_app.stubs(:config).returns(rails_config)
|
85
|
-
Rails.stubs(:application).returns(rails_app)
|
86
|
-
end
|
87
|
-
|
88
103
|
def logger
|
89
|
-
@fake_log ||=
|
104
|
+
@fake_log ||= Logger.new(STDOUT)
|
90
105
|
end
|
91
106
|
|
92
107
|
def cookies
|
@@ -1,10 +1,21 @@
|
|
1
|
-
require File.expand_path('
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
|
3
|
-
class MiddlewareTest < Test
|
3
|
+
class MiddlewareTest < Minitest::Test
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@logger = Minitest::Mock.new
|
7
|
+
Imprint::Middleware.logger = @logger
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
Imprint::Middleware.logger = nil
|
12
|
+
@logger.verify
|
13
|
+
end
|
4
14
|
|
5
15
|
should "call app" do
|
6
16
|
request = Rack::MockRequest.env_for("/anything.json")
|
7
17
|
middleware = Imprint::Middleware.new(fake_app)
|
18
|
+
@logger.expect(:info, nil) { true }
|
8
19
|
results = middleware.call(request)
|
9
20
|
assert_equal "/anything.json", results.last
|
10
21
|
end
|
@@ -12,16 +23,18 @@ class MiddlewareTest < Test::Unit::TestCase
|
|
12
23
|
should 'pass all rack lint checks' do
|
13
24
|
app = Rack::Lint.new(Imprint::Middleware.new(fake_app))
|
14
25
|
env = Rack::MockRequest.env_for('/hello')
|
26
|
+
@logger.expect(:info, nil) { true }
|
15
27
|
app.call(env)
|
16
28
|
end
|
17
29
|
|
18
30
|
should "set trace_id before calling app" do
|
19
31
|
request = Rack::MockRequest.env_for("/anything.json")
|
20
32
|
middleware = Imprint::Middleware.new(fake_app)
|
33
|
+
@logger.expect(:info, nil) {|x| x=~ /trace_status=initiated/ }
|
34
|
+
|
21
35
|
results = middleware.call(request)
|
22
36
|
assert_equal "/anything.json", results.last
|
23
|
-
|
24
|
-
assert ::Imprint::Tracer.get_trace_id!='-1'
|
37
|
+
assert ::Imprint::Tracer.get_trace_id != ::Imprint::Tracer::TRACE_ID_DEFAULT
|
25
38
|
end
|
26
39
|
|
27
40
|
should "set trace_id from rails request_id" do
|
@@ -29,8 +42,8 @@ class MiddlewareTest < Test::Unit::TestCase
|
|
29
42
|
middleware = Imprint::Middleware.new(fake_app)
|
30
43
|
results = middleware.call(request)
|
31
44
|
assert_equal "/anything.json", results.last
|
32
|
-
|
33
|
-
|
45
|
+
refute_nil ::Imprint::Tracer.get_trace_id
|
46
|
+
assert_equal 'existing_id', ::Imprint::Tracer.get_trace_id
|
34
47
|
end
|
35
48
|
|
36
49
|
should "set trace_id from passed in imprint header" do
|
@@ -38,8 +51,8 @@ class MiddlewareTest < Test::Unit::TestCase
|
|
38
51
|
middleware = Imprint::Middleware.new(fake_app)
|
39
52
|
results = middleware.call(request)
|
40
53
|
assert_equal "/anything.json", results.last
|
41
|
-
|
42
|
-
|
54
|
+
refute_nil ::Imprint::Tracer.get_trace_id
|
55
|
+
assert_equal 'existing_trace_id', ::Imprint::Tracer.get_trace_id
|
43
56
|
end
|
44
57
|
|
45
58
|
private
|
data/test/unit/tracer_test.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require File.expand_path('
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
|
3
|
-
class TracerTest < Test
|
3
|
+
class TracerTest < Minitest::Test
|
4
4
|
|
5
5
|
should "set trace id" do
|
6
6
|
fake_trace = "tracer"
|
@@ -9,14 +9,16 @@ class TracerTest < Test::Unit::TestCase
|
|
9
9
|
end
|
10
10
|
|
11
11
|
should "get trace id defaults" do
|
12
|
-
|
12
|
+
assert_equal Imprint::Tracer::TRACE_ID_DEFAULT, Imprint::Tracer.get_trace_id
|
13
|
+
Imprint::Tracer.set_trace_id("fake_trace", fake_rack_env)
|
14
|
+
refute_nil Imprint::Tracer.get_trace_id
|
13
15
|
Imprint::Tracer.set_trace_id(nil, fake_rack_env)
|
14
|
-
assert_equal
|
16
|
+
assert_equal Imprint::Tracer::TRACE_ID_DEFAULT, Imprint::Tracer.get_trace_id
|
15
17
|
end
|
16
18
|
|
17
19
|
should "generate rand trace id" do
|
18
20
|
trace_id = Imprint::Tracer.rand_trace_id
|
19
|
-
|
21
|
+
refute_nil trace_id
|
20
22
|
assert_equal 6, trace_id.length
|
21
23
|
assert trace_id.match(/[A-Za-z]/)
|
22
24
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: imprint
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-10-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -43,22 +43,6 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
-
name: mocha
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
|
-
requirements:
|
51
|
-
- - ~>
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: 0.14.0
|
54
|
-
type: :development
|
55
|
-
prerelease: false
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
|
-
requirements:
|
59
|
-
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: 0.14.0
|
62
46
|
- !ruby/object:Gem::Dependency
|
63
47
|
name: shoulda
|
64
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -147,7 +131,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
147
131
|
version: '0'
|
148
132
|
segments:
|
149
133
|
- 0
|
150
|
-
hash:
|
134
|
+
hash: 1702384810650900036
|
151
135
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
136
|
none: false
|
153
137
|
requirements:
|
@@ -156,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
140
|
version: '0'
|
157
141
|
segments:
|
158
142
|
- 0
|
159
|
-
hash:
|
143
|
+
hash: 1702384810650900036
|
160
144
|
requirements: []
|
161
145
|
rubyforge_project:
|
162
146
|
rubygems_version: 1.8.23
|