imprint 1.1.3 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +13 -0
- data/lib/imprint/log_helpers.rb +39 -0
- data/lib/imprint/tracer.rb +23 -0
- data/lib/imprint/version.rb +1 -1
- data/lib/imprint.rb +8 -19
- data/test/unit/log_helpers_test.rb +92 -0
- metadata +8 -4
data/README.md
CHANGED
@@ -6,6 +6,8 @@ It also has a file which can be used to bootstrap default rails logging to embed
|
|
6
6
|
|
7
7
|
Supporting tracing between applications requires updating client calls between applications, at the moment we don't try to monkey patch any of that in and expect responsible clients to add the header manually as described in the Usage section below.
|
8
8
|
|
9
|
+
If you have seen [ActionDispatch::RequestId](http://api.rubyonrails.org/classes/ActionDispatch/RequestId.html). Imprint is basically a generic Rack version of that idea. It works with Rails 3, Sinatra, and Pure Rack. Beyond that it also provides some helpers and configuration around the trace_id usage.
|
10
|
+
|
9
11
|
## Installation
|
10
12
|
|
11
13
|
Add this line to your application's Gemfile:
|
@@ -145,6 +147,17 @@ def self.http_get(url)
|
|
145
147
|
end
|
146
148
|
```
|
147
149
|
|
150
|
+
## Notes / TODO
|
151
|
+
|
152
|
+
Looking at ZipKin, it tries to accomplish many of the same goals as Imprint. I think it would make sence to support the same headers and format so they could be compatible. Although the ZipKin service tracing isn't as useful to me as the full detailed splunk / elastic search logs.
|
153
|
+
|
154
|
+
* [ZipKin intro / docs](http://twitter.github.io/zipkin/index.html)
|
155
|
+
* [Railsconf ZipKin intro: Distributed Request Tracing](http://www.confreaks.com/videos/3326-railsconf-distributed-request-tracing) by [Kenneth Hoxworth (@hoxworth)](https://twitter.com/hoxworth)
|
156
|
+
* [ZipKin Header Formats](https://github.com/twitter/finagle/blob/master/finagle-http/src/main/scala/com/twitter/finagle/http/Codec.scala#L216)
|
157
|
+
* [Existing Ruby ZipKin Tracer](https://github.com/mszenher/zipkin-tracer)
|
158
|
+
* [Twitter Minimal Zipkin Tracer](https://github.com/twitter/zipkin/tree/04a755c29e6b2ff3bd99534bb95d760f112fda08/zipkin-gems/zipkin-tracer)
|
159
|
+
* [Docker ZipKin install for testing](https://github.com/itszero/docker-zipkin)
|
160
|
+
|
148
161
|
## Contributing
|
149
162
|
|
150
163
|
1. Fork it
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Imprint
|
2
|
+
module LogHelpers
|
3
|
+
|
4
|
+
# Not relying on default rails logging, more often using lograge
|
5
|
+
# Still want to log incoming params safely, which lograge doesn't include
|
6
|
+
# this does the same sensative param filtering as rails defaults
|
7
|
+
# it also allows injecting some other variables useful for tracing logs
|
8
|
+
def log_entrypoint
|
9
|
+
raise "you must call Imprint.configuration and configure the gem before using LogHelpers" if Imprint.configuration.nil?
|
10
|
+
log_filter = ActionDispatch::Http::ParameterFilter.new(Imprint.configuration[:log_filters] || Rails.application.config.filter_parameters)
|
11
|
+
header_blacklist = Imprint.configuration[:header_blacklist] || []
|
12
|
+
variables_to_append = Imprint.configuration[:variables_to_append] || []
|
13
|
+
cookies_whitelist = Imprint.configuration[:cookies_whitelist] || []
|
14
|
+
|
15
|
+
http_request_headers = request.headers.select{|header_name, header_value| header_name.match("^HTTP.*") && !header_blacklist.include?(header_name) }
|
16
|
+
data_append = "headers: "
|
17
|
+
http_request_headers.each_pair{|k,v| data_append << " #{k}=\"#{v}\"" }
|
18
|
+
|
19
|
+
data_append << " params: "
|
20
|
+
log_filter.filter(params).each_pair{|k,v| data_append << " #{k}=\"#{v}\"" }
|
21
|
+
|
22
|
+
variables_to_append.each do |var|
|
23
|
+
var_val = self.instance_variable_get("@#{var}".to_sym).try(:id)
|
24
|
+
var_val ||= 'nil'
|
25
|
+
data_append << " #{var}_id=\"#{var_val}\""
|
26
|
+
end
|
27
|
+
|
28
|
+
cookies_whitelist.each do |cookie_key|
|
29
|
+
cookie_val = cookies[cookie_key] ? cookies[cookie_key] : 'nil'
|
30
|
+
data_append << " #{cookie_key}=\"#{cookie_val}\""
|
31
|
+
end
|
32
|
+
|
33
|
+
logger.info "Started request_method=#{request.method.inspect} request_url=\"#{request.url.inspect}\" at request_time=\"#{Time.now.to_default_s}\" request_ip=#{request.remote_ip.inspect} #{data_append}"
|
34
|
+
rescue
|
35
|
+
logger.error "error logging log_entrypoint for request"
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Imprint
|
2
|
+
class Tracer
|
3
|
+
TRACER_HEADER = 'HTTP_IMPRINTID'
|
4
|
+
TRACER_KEY = 'IMPRINTID'
|
5
|
+
RAILS_REQUEST_ID = "action_dispatch.request_id"
|
6
|
+
|
7
|
+
TRACE_CHARS = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
|
8
|
+
|
9
|
+
def self.set_trace_id(id, rack_env = {})
|
10
|
+
Thread.current[TRACER_KEY] = id
|
11
|
+
# setting to the rack_env, gives error tracking support in some systems
|
12
|
+
rack_env[TRACER_KEY] = id
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.get_trace_id
|
16
|
+
Thread.current[TRACER_KEY]
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.rand_trace_id
|
20
|
+
(0...6).map { TRACE_CHARS[rand(TRACE_CHARS.length)] }.join
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/imprint/version.rb
CHANGED
data/lib/imprint.rb
CHANGED
@@ -1,27 +1,16 @@
|
|
1
1
|
require 'imprint/version'
|
2
2
|
require 'imprint/middleware'
|
3
|
+
require 'imprint/tracer'
|
3
4
|
|
4
5
|
module Imprint
|
5
|
-
class Tracer
|
6
|
-
TRACER_HEADER = 'HTTP_IMPRINTID'
|
7
|
-
TRACER_KEY = 'IMPRINTID'
|
8
|
-
RAILS_REQUEST_ID = "action_dispatch.request_id"
|
9
6
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
Thread.current[TRACER_KEY] = id
|
14
|
-
# setting to the rack_env, gives error tracking support in some systems
|
15
|
-
rack_env[TRACER_KEY] = id
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.get_trace_id
|
19
|
-
Thread.current[TRACER_KEY]
|
20
|
-
end
|
7
|
+
class << self
|
8
|
+
attr_accessor :configuration
|
9
|
+
end
|
21
10
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
11
|
+
def self.configure(configs = {})
|
12
|
+
self.configuration ||= {}
|
13
|
+
self.configuration = configuration.merge(configs)
|
26
14
|
end
|
15
|
+
|
27
16
|
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require File.expand_path('../test_helper', File.dirname(__FILE__))
|
2
|
+
require 'imprint/log_helpers'
|
3
|
+
|
4
|
+
####
|
5
|
+
# Testing Rails integration without Rails isn't fun
|
6
|
+
# The below test double classes let us do that.
|
7
|
+
# along with the implemented fakes as protected methods
|
8
|
+
####
|
9
|
+
module ActionDispatch
|
10
|
+
module Http
|
11
|
+
class ParameterFilter
|
12
|
+
def initialize(opts)
|
13
|
+
end
|
14
|
+
|
15
|
+
def filter(params)
|
16
|
+
params
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Rails
|
23
|
+
def self.application
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Time
|
28
|
+
def to_default_s
|
29
|
+
self.to_s
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class LogHelpersTest < Test::Unit::TestCase
|
34
|
+
include Imprint::LogHelpers
|
35
|
+
Imprint.configure({})
|
36
|
+
|
37
|
+
should "log entry" do
|
38
|
+
stub_rails
|
39
|
+
logger.expects(:info).with(anything).once
|
40
|
+
log_entrypoint
|
41
|
+
end
|
42
|
+
|
43
|
+
should "log entry catches exceptions and logs them" do
|
44
|
+
logger.expects(:error).with(anything).once
|
45
|
+
log_entrypoint
|
46
|
+
end
|
47
|
+
|
48
|
+
protected
|
49
|
+
|
50
|
+
def request
|
51
|
+
request = Rack::MockRequest.env_for("/anything.json")
|
52
|
+
|
53
|
+
def request.headers
|
54
|
+
{}
|
55
|
+
end
|
56
|
+
|
57
|
+
def request.method
|
58
|
+
{}
|
59
|
+
end
|
60
|
+
|
61
|
+
def request.url
|
62
|
+
''
|
63
|
+
end
|
64
|
+
|
65
|
+
def request.remote_ip
|
66
|
+
''
|
67
|
+
end
|
68
|
+
|
69
|
+
request
|
70
|
+
end
|
71
|
+
|
72
|
+
def params
|
73
|
+
{}
|
74
|
+
end
|
75
|
+
|
76
|
+
def stub_rails
|
77
|
+
rails_config ||= mock('config')
|
78
|
+
rails_config.stubs(:filter_parameters).returns([])
|
79
|
+
rails_app ||= mock('application')
|
80
|
+
rails_app.stubs(:config).returns(rails_config)
|
81
|
+
Rails.stubs(:application).returns(rails_app)
|
82
|
+
end
|
83
|
+
|
84
|
+
def logger
|
85
|
+
@fake_log ||= mock('logger')
|
86
|
+
end
|
87
|
+
|
88
|
+
def cookies
|
89
|
+
{}
|
90
|
+
end
|
91
|
+
|
92
|
+
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.1.
|
4
|
+
version: 1.1.4
|
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-05-
|
12
|
+
date: 2014-05-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -123,10 +123,13 @@ files:
|
|
123
123
|
- Rakefile
|
124
124
|
- imprint.gemspec
|
125
125
|
- lib/imprint.rb
|
126
|
+
- lib/imprint/log_helpers.rb
|
126
127
|
- lib/imprint/middleware.rb
|
127
128
|
- lib/imprint/rails_logger.rb
|
129
|
+
- lib/imprint/tracer.rb
|
128
130
|
- lib/imprint/version.rb
|
129
131
|
- test/test_helper.rb
|
132
|
+
- test/unit/log_helpers_test.rb
|
130
133
|
- test/unit/middleware_test.rb
|
131
134
|
- test/unit/tracer_test.rb
|
132
135
|
homepage: ''
|
@@ -144,7 +147,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
144
147
|
version: '0'
|
145
148
|
segments:
|
146
149
|
- 0
|
147
|
-
hash: -
|
150
|
+
hash: -3977275337200593217
|
148
151
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
152
|
none: false
|
150
153
|
requirements:
|
@@ -153,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
156
|
version: '0'
|
154
157
|
segments:
|
155
158
|
- 0
|
156
|
-
hash: -
|
159
|
+
hash: -3977275337200593217
|
157
160
|
requirements: []
|
158
161
|
rubyforge_project:
|
159
162
|
rubygems_version: 1.8.23
|
@@ -162,5 +165,6 @@ specification_version: 3
|
|
162
165
|
summary: A gem to help improve logging. Focused on request tracing and cross app tracing.
|
163
166
|
test_files:
|
164
167
|
- test/test_helper.rb
|
168
|
+
- test/unit/log_helpers_test.rb
|
165
169
|
- test/unit/middleware_test.rb
|
166
170
|
- test/unit/tracer_test.rb
|