imprint 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p484
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+ source "http://geminabox.lsqa.net"
3
+
4
+ # Specify your gem's dependencies in imprint.gemspec
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dan Mayer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # Imprint
2
+
3
+ Imprint helps track requests across multiple log lines or applications. It consists of a lightweight class and middleware to help set tracing ids.
4
+
5
+ It also has a file which can be used to bootstrap default rails logging to embedding the imprint `trace_id` on each line logged.
6
+
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
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'imprint'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install imprint
22
+
23
+
24
+ ## Usage
25
+
26
+ After installing the gems and requiring them if needed.
27
+
28
+ To configure in a Rails 3 or 4 application
29
+
30
+ edit `config/application.rb` and append the line below
31
+
32
+ require 'imprint/rails_logger'
33
+
34
+ create or update your middleware configuration (for example: `config/initializers/middleware.rb`)
35
+
36
+ ```ruby
37
+ require 'imprint'
38
+
39
+ Rails.application.config.middleware.insert_before Rails::Rack::Logger, Imprint::Middleware
40
+ ```
41
+
42
+ If you are using any additional loggers that you wanted tagged that are not part of the normal Rails.logger you should update them as well. For example, we have some scribe logs:
43
+
44
+ def log(message = nil, severity = :info)
45
+ mirror_logger.add(parse_severity(severity), message) if mirror_logger
46
+ log_raw(message, severity) do
47
+ message = yield if block_given?
48
+ # append imprint trace
49
+ if (defined?(Imprint::Middleware.get_trace_id)) && message && message.is_a?(String) && message.length > 1 && Imprint::Middleware.get_trace_id.get_trace_id.to_s != '-1'
50
+ message = "#{message}\n" unless message[-1] == "\n"
51
+ message = message.gsub("\n"," [trace_id=#{Imprint::Middleware.get_trace_id}]\n")
52
+ end
53
+ format = []
54
+ format << Time.now.to_f
55
+ format << @host
56
+ format << $$
57
+ format << format_severity(severity)
58
+ format << "app_name"
59
+ format << message
60
+ format.flatten.join("\t")
61
+ end
62
+
63
+ ## Example Queries
64
+
65
+ These queries should work in Kibana/ElasticSearch, Splunk, or other log solutions. We may need to support different output formatters in the future depending on how various logging systems handle default field extraction.
66
+
67
+ First query to find a group of requests you are particularly interested in, perhaps all errors on an app:
68
+
69
+ source="app_name" error status=500
70
+
71
+ From the results find a specific request that caused the error and use the trace_id to dig in futher, by crafting a query with the trace_id.
72
+
73
+ Find all log lines in a particular app related to a single request:
74
+
75
+ source="app_name" "trace_id=1396448370_wdeYND"
76
+
77
+ Find all long lines related to a single request across apps:
78
+
79
+ "trace_id=1396448370_wdeYND"
80
+
81
+ Since the trace_id is appended to all log lines during the duration of the request, any `logger.info`, `logger.error`, or other log output is easy to track back to the initial request information, params, headers, or other system logged information such as if the request was successfully authorize and by whom.
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create new Pull Request
90
+
91
+ ## License
92
+
93
+ See LICENSE.txt for details.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :test
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/*_test.rb'
8
+ test.verbose = true
9
+ end
data/imprint.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'imprint/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "imprint"
8
+ spec.version = Imprint::VERSION
9
+ spec.authors = ["Dan Mayer"]
10
+ spec.email = ["dan.mayer@livingsocial.com"]
11
+ spec.description = %q{A gem to help improve logging. Focused on request tracing and cross app tracing.}
12
+ spec.summary = %q{A gem to help improve logging. Focused on request tracing and cross app tracing.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "mocha", "~> 0.14.0"
24
+ spec.add_development_dependency "shoulda"
25
+ spec.add_development_dependency "rack"
26
+ spec.add_development_dependency "simplecov"
27
+ end
@@ -0,0 +1,20 @@
1
+ module Imprint
2
+ class Middleware
3
+
4
+ def self.set_trace_id(rails_env, rack_env)
5
+ existing_id = rack_env[Imprint::Tracer::TRACER_HEADER]
6
+ existing_id ||= "#{Time.now.to_i}_#{Imprint::Tracer.rand_trace_id}"
7
+ Imprint::Tracer.set_trace_id(existing_id, rails_env, rack_env)
8
+ end
9
+
10
+ def initialize(app, opts = {})
11
+ @app = app
12
+ end
13
+
14
+ def call(env)
15
+ ::Imprint::Middleware.set_trace_id(ENV, env)
16
+ @app.call(env)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,38 @@
1
+ if defined?(ActiveSupport::BufferedLogger)
2
+ #Rails 3
3
+ module ActiveSupport
4
+ class BufferedLogger
5
+ def add(severity, message = nil, progname = nil, &block)
6
+ return if @level > severity
7
+ message = (message || (block && block.call) || progname).to_s
8
+ # If a newline is necessary then create a new message ending with a newline.
9
+ # Ensures that the original message is not mutated.
10
+ message = "#{message}\n" unless message[-1] == "\n"
11
+ if (defined?(Imprint::Tracer)) && message && message.is_a?(String) && message.length > 1 && Imprint::Tracer.get_trace_id
12
+ message = message.gsub("\n"," [trace_id=#{Imprint::Tracer.get_trace_id}]\n")
13
+ end
14
+ buffer << message
15
+ auto_flush
16
+ message
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+
23
+ if defined?(ActiveSupport::Logger::SimpleFormatter)
24
+ #Rails 4
25
+ class ActiveSupport::Logger::SimpleFormatter
26
+ def call(severity, time, progname, message)
27
+ message = (message || (block && block.call) || progname).to_s
28
+ # If a newline is necessary then create a new message ending with a newline.
29
+ # Ensures that the original message is not mutated.
30
+ message = "#{message}\n" unless message[-1] == "\n"
31
+ if (defined?(Imprint::Tracer)) && message && message.is_a?(String) && message.length > 1 && Imprint::Tracer.get_trace_id
32
+ message = message.gsub("\n"," [trace_id=#{Imprint::Tracer.get_trace_id}]\n")
33
+ end
34
+ message
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,3 @@
1
+ module Imprint
2
+ VERSION = "1.0.1"
3
+ end
data/lib/imprint.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'imprint/version'
2
+ require 'imprint/middleware'
3
+
4
+ module Imprint
5
+ class Tracer
6
+ TRACER_HEADER = 'HTTP_IMPRINTID'
7
+ TRACER_KEY = 'IMPRINTID'
8
+
9
+ TRACE_CHARS = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
10
+
11
+ def self.set_trace_id(id, rails_env, rack_env)
12
+ rails_env[TRACER_KEY] = id
13
+ rack_env[TRACER_KEY] = id
14
+ end
15
+
16
+ #this assumes the rails ENV is available at ENV
17
+ def self.get_trace_id(rails_env = ENV)
18
+ rails_env[TRACER_KEY]
19
+ end
20
+
21
+ def self.rand_trace_id
22
+ (0...6).map { TRACE_CHARS[rand(TRACE_CHARS.length)] }.join
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'simplecov'
3
+ require 'test/unit'
4
+ require 'shoulda'
5
+ require 'mocha/setup'
6
+ require 'rack'
7
+
8
+ SimpleCov.start do
9
+ add_filter 'specs/ruby/1.9.1/gems/'
10
+ add_filter '/test/'
11
+ add_filter '/config/'
12
+ end
13
+
14
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
15
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
16
+ Mocha::Configuration.prevent(:stubbing_non_existent_method)
17
+
18
+ require 'imprint'
@@ -0,0 +1,33 @@
1
+ require File.expand_path('../test_helper', File.dirname(__FILE__))
2
+
3
+ class MiddlewareTest < Test::Unit::TestCase
4
+
5
+ should "call app" do
6
+ request = Rack::MockRequest.env_for("/anything.json")
7
+ middleware = Imprint::Middleware.new(fake_app)
8
+ results = middleware.call(request)
9
+ assert_equal "/anything.json", results.last
10
+ end
11
+
12
+ should 'pass all rack lint checks' do
13
+ app = Rack::Lint.new(Imprint::Middleware.new(fake_app))
14
+ env = Rack::MockRequest.env_for('/hello')
15
+ app.call(env)
16
+ end
17
+
18
+ should "set trace_id before calling app" do
19
+ request = Rack::MockRequest.env_for("/anything.json")
20
+ middleware = Imprint::Middleware.new(fake_app)
21
+ results = middleware.call(request)
22
+ assert_equal "/anything.json", results.last
23
+ assert_not_nil ::Imprint::Tracer.get_trace_id(ENV)
24
+ assert ::Imprint::Tracer.get_trace_id(ENV)!='-1'
25
+ end
26
+
27
+ private
28
+
29
+ def fake_app
30
+ @app ||= lambda { |env| [200, {'Content-Type' => 'text/plain'}, env['PATH_INFO']] }
31
+ end
32
+
33
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path('../test_helper', File.dirname(__FILE__))
2
+
3
+ class TracerTest < Test::Unit::TestCase
4
+
5
+ should "set trace id" do
6
+ fake_trace = "tracer"
7
+ Imprint::Tracer.set_trace_id(fake_trace, ENV, {})
8
+ assert_equal fake_trace, Imprint::Tracer.get_trace_id(ENV)
9
+ end
10
+
11
+ should "get trace id defaults" do
12
+ assert_not_nil Imprint::Tracer.get_trace_id(ENV)
13
+ end
14
+
15
+ should "generate rand trace id" do
16
+ trace_id = Imprint::Tracer.rand_trace_id
17
+ assert_not_nil trace_id
18
+ assert_equal 6, trace_id.length
19
+ assert trace_id.match(/[A-Za-z]/)
20
+ end
21
+
22
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imprint
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dan Mayer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-04-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
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
+ - !ruby/object:Gem::Dependency
63
+ name: shoulda
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rack
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: A gem to help improve logging. Focused on request tracing and cross app
111
+ tracing.
112
+ email:
113
+ - dan.mayer@livingsocial.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .ruby-version
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - imprint.gemspec
125
+ - lib/imprint.rb
126
+ - lib/imprint/middleware.rb
127
+ - lib/imprint/rails_logger.rb
128
+ - lib/imprint/version.rb
129
+ - test/test_helper.rb
130
+ - test/unit/middleware_test.rb
131
+ - test/unit/tracer_test.rb
132
+ homepage: ''
133
+ licenses:
134
+ - MIT
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ segments:
146
+ - 0
147
+ hash: 2134087639551202536
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ segments:
155
+ - 0
156
+ hash: 2134087639551202536
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 1.8.23
160
+ signing_key:
161
+ specification_version: 3
162
+ summary: A gem to help improve logging. Focused on request tracing and cross app tracing.
163
+ test_files:
164
+ - test/test_helper.rb
165
+ - test/unit/middleware_test.rb
166
+ - test/unit/tracer_test.rb