sapience 2.8 → 2.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +1 -0
- data/CHANGELOG.md +3 -0
- data/README.md +38 -11
- data/docker-compose.yml +10 -0
- data/lib/sapience/extensions/rails/rack/logger.rb +13 -3
- data/lib/sapience/extensions/sinatra/middleware/logging.rb +122 -0
- data/lib/sapience/extensions/sinatra/timings.rb +26 -0
- data/lib/sapience/sinatra.rb +17 -0
- data/lib/sapience/version.rb +1 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5e40be4c3c23734786e06c3be620bab6a2c1a9a
|
4
|
+
data.tar.gz: 3397ec7107fc0ac63f23809262c7cd793cd43e91
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5def20affb7f31b2ef491d458d1e8095cc54ab2db62ff6ca12016e200f20d91f7104b494702c45084431cc93006f60ef753314817f94ed34e1805a42dfcd677b
|
7
|
+
data.tar.gz: 15a71d7c1491305fda7a7157bbcd82caf01f6bdc7ca6fa409aa2cca4e2192629f128e223054db957ccb73afddc6a0139233e39e2e8c3485c193706d95b031373
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.4.1
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -22,6 +22,33 @@ Add the gem:
|
|
22
22
|
gem "sapience", require: "sapience/rails"
|
23
23
|
```
|
24
24
|
|
25
|
+
### Sinatra
|
26
|
+
Add the gem:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
gem "sapience", require: "sapience/sinatra"
|
30
|
+
```
|
31
|
+
|
32
|
+
In your Base API class
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require "sapience/grape"
|
36
|
+
|
37
|
+
module Aslan
|
38
|
+
module API
|
39
|
+
class Base < Sinatra::Base
|
40
|
+
use Sapience::Extensions::Sinatra::Middleware::Logging, logger: Sapience[self]
|
41
|
+
|
42
|
+
get "/ping" do
|
43
|
+
{ ping: "PONG" }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
|
25
52
|
### Grape
|
26
53
|
Add the gem:
|
27
54
|
|
@@ -49,11 +76,11 @@ end
|
|
49
76
|
|
50
77
|
```
|
51
78
|
|
52
|
-
Also make sure you only use "rescue_from" when you want to return a 500 status. For any other status "dont" use
|
79
|
+
Also make sure you only use "rescue_from" when you want to return a 500 status. For any other status "dont" use
|
53
80
|
"rescue_from".
|
54
81
|
|
55
|
-
For example if you have some authentication code that raises an exception when the user is not authenticated,
|
56
|
-
dont use "rescue_from" to catch this exception and change the status to 403 within the rescue_from, handle this
|
82
|
+
For example if you have some authentication code that raises an exception when the user is not authenticated,
|
83
|
+
dont use "rescue_from" to catch this exception and change the status to 403 within the rescue_from, handle this
|
57
84
|
exception instead on the "before" block, or alternatively within your endpoint, like below:
|
58
85
|
|
59
86
|
|
@@ -67,10 +94,10 @@ before do
|
|
67
94
|
end
|
68
95
|
```
|
69
96
|
|
70
|
-
Likewise, for capturing any other exception for which you want to return a code other than 500, capture your
|
71
|
-
exception in the "before" block or within your endpoint, but make sure "rescue_from" is "only" used for 500
|
72
|
-
status, as grape will call rescue_from once is gone through all the middleware, so if you change the status
|
73
|
-
in a rescue_from, Sapience would not be able to log it correctly. So the below is ok because the rescue_from
|
97
|
+
Likewise, for capturing any other exception for which you want to return a code other than 500, capture your
|
98
|
+
exception in the "before" block or within your endpoint, but make sure "rescue_from" is "only" used for 500
|
99
|
+
status, as grape will call rescue_from once is gone through all the middleware, so if you change the status
|
100
|
+
in a rescue_from, Sapience would not be able to log it correctly. So the below is ok because the rescue_from
|
74
101
|
is using status 500:
|
75
102
|
|
76
103
|
```ruby
|
@@ -79,7 +106,7 @@ rescue_from :all do |e|
|
|
79
106
|
end
|
80
107
|
```
|
81
108
|
|
82
|
-
**Note**: if you already have got your grape applications sprinkled with calls to API.logger, and you do
|
109
|
+
**Note**: if you already have got your grape applications sprinkled with calls to API.logger, and you do
|
83
110
|
not want to have to replace all those calls to Sapience.logger manually, then just re-assign your logger
|
84
111
|
after including the Sapience middleware, like below:
|
85
112
|
|
@@ -89,7 +116,7 @@ API.logger = Sapience.logger
|
|
89
116
|
```
|
90
117
|
|
91
118
|
|
92
|
-
**Note**: If you're using the rackup command to run your server in development, pass the -q flag to silence the default
|
119
|
+
**Note**: If you're using the rackup command to run your server in development, pass the -q flag to silence the default
|
93
120
|
rack logger so you don't get double logging.
|
94
121
|
|
95
122
|
### Standalone
|
@@ -149,7 +176,7 @@ development:
|
|
149
176
|
- stream:
|
150
177
|
file_name: log/development.log
|
151
178
|
formatter: color
|
152
|
-
|
179
|
+
|
153
180
|
staging:
|
154
181
|
log_level: info
|
155
182
|
error_handler:
|
@@ -162,7 +189,7 @@ staging:
|
|
162
189
|
- stream:
|
163
190
|
io: STDOUT
|
164
191
|
formatter: json
|
165
|
-
|
192
|
+
|
166
193
|
production:
|
167
194
|
log_level: info
|
168
195
|
silent_rails: true # make rails logging less noisy
|
data/docker-compose.yml
CHANGED
@@ -108,3 +108,13 @@ services:
|
|
108
108
|
environment:
|
109
109
|
APP_NAME: grape_app
|
110
110
|
PATH: /usr/src/app/bin:/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
111
|
+
|
112
|
+
sinatra:
|
113
|
+
extends:
|
114
|
+
service: base
|
115
|
+
working_dir: /usr/src/app/test_apps/sinatra
|
116
|
+
entrypoint: /usr/src/app/dev-entrypoint.sh
|
117
|
+
command: bundle exec rspec
|
118
|
+
environment:
|
119
|
+
APP_NAME: sinatra_app
|
120
|
+
PATH: /usr/src/app/bin:/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
@@ -1,13 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
# Replace rack started message with a semantic equivalent
|
3
|
+
# request attributes taken from:
|
4
|
+
# https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_dispatch/http/request.rb
|
5
|
+
|
3
6
|
class Rails::Rack::Logger # rubocop:disable ClassAndModuleChildren
|
4
7
|
alias started_request_message_original started_request_message
|
5
|
-
def started_request_message(request)
|
8
|
+
def started_request_message(request) # rubocop:disable AbcSize
|
6
9
|
{
|
7
|
-
message: "Started",
|
10
|
+
message: "Started #{request.request_method} #{request.filtered_path}",
|
8
11
|
method: request.request_method,
|
9
12
|
path: request.filtered_path,
|
10
|
-
|
13
|
+
fullpath: request.fullpath,
|
14
|
+
original_fullpath: request.original_fullpath,
|
15
|
+
ip: request.env["HTTP_X_FORWARDED_FOR"] || request.remote_addr,
|
16
|
+
client_ip: request.ip,
|
17
|
+
remote_ip: request.remote_ip,
|
18
|
+
remote_host: request.env["HTTP_X_FORWARDED_HOST"] || request.remote_host,
|
19
|
+
content_type: request.media_type,
|
20
|
+
request_id: request.uuid,
|
11
21
|
}
|
12
22
|
end
|
13
23
|
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sapience
|
4
|
+
module Extensions
|
5
|
+
module Sinatra
|
6
|
+
module Middleware
|
7
|
+
class Logging
|
8
|
+
|
9
|
+
if defined?(ActiveRecord)
|
10
|
+
ActiveSupport::Notifications.subscribe("sql.active_record") do |*args|
|
11
|
+
event = ActiveSupport::Notifications::Event.new(*args)
|
12
|
+
Sinatra::Timings.append_db_runtime(event)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(app, options = {})
|
17
|
+
@app = app
|
18
|
+
@logger = options[:logger]
|
19
|
+
end
|
20
|
+
|
21
|
+
def call(env)
|
22
|
+
call!(env)
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
def call!(env)
|
28
|
+
@env = env
|
29
|
+
before
|
30
|
+
error = catch(:error) do
|
31
|
+
begin
|
32
|
+
@app_response = @app.call(@env)
|
33
|
+
rescue StandardError => e
|
34
|
+
after_exception(e)
|
35
|
+
raise e
|
36
|
+
end
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
if error
|
40
|
+
after_failure(error)
|
41
|
+
throw(:error, error)
|
42
|
+
else
|
43
|
+
@status, = *@app_response
|
44
|
+
after
|
45
|
+
end
|
46
|
+
@app_response
|
47
|
+
end
|
48
|
+
|
49
|
+
def before
|
50
|
+
reset_db_runtime
|
51
|
+
start_time
|
52
|
+
end
|
53
|
+
|
54
|
+
def after
|
55
|
+
stop_time
|
56
|
+
@logger.info(parameters)
|
57
|
+
end
|
58
|
+
|
59
|
+
def after_exception(exc) # rubocop:disable Lint/UnusedMethodArgument
|
60
|
+
@status = 500
|
61
|
+
after
|
62
|
+
end
|
63
|
+
|
64
|
+
def after_failure(error)
|
65
|
+
@status = error[:status]
|
66
|
+
after
|
67
|
+
end
|
68
|
+
|
69
|
+
def parameters # rubocop:disable AbcSize
|
70
|
+
{
|
71
|
+
method: request.request_method,
|
72
|
+
request_path: @env["REQUEST_URI"] || @env["PATH_INFO"],
|
73
|
+
status: @status,
|
74
|
+
route: @env["sinatra.route"].to_s,
|
75
|
+
host: request.host,
|
76
|
+
ip: (request.env["HTTP_X_FORWARDED_FOR"] || request.env["REMOTE_ADDR"]),
|
77
|
+
ua: request.env["HTTP_USER_AGENT"],
|
78
|
+
tags: Sapience.tags,
|
79
|
+
params: request.params,
|
80
|
+
runtimes: {
|
81
|
+
total: total_runtime,
|
82
|
+
view: view_runtime,
|
83
|
+
db: db_runtime,
|
84
|
+
},
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def request
|
91
|
+
@request ||= ::Rack::Request.new(@env)
|
92
|
+
end
|
93
|
+
|
94
|
+
def total_runtime
|
95
|
+
((stop_time - start_time) * 1000).round(3)
|
96
|
+
end
|
97
|
+
|
98
|
+
def view_runtime
|
99
|
+
total_runtime - db_runtime
|
100
|
+
end
|
101
|
+
|
102
|
+
def db_runtime
|
103
|
+
Sinatra::Timings.db_runtime.round(3)
|
104
|
+
end
|
105
|
+
|
106
|
+
def reset_db_runtime
|
107
|
+
Sinatra::Timings.reset_db_runtime
|
108
|
+
end
|
109
|
+
|
110
|
+
def start_time
|
111
|
+
@start_time ||= Time.now
|
112
|
+
end
|
113
|
+
|
114
|
+
def stop_time
|
115
|
+
@stop_time ||= Time.now
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Sapience
|
3
|
+
module Extensions
|
4
|
+
module Sinatra
|
5
|
+
module Timings
|
6
|
+
extend self
|
7
|
+
|
8
|
+
def db_runtime=(value)
|
9
|
+
Thread.current[:grape_db_runtime] = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def db_runtime
|
13
|
+
Thread.current[:grape_db_runtime] ||= 0
|
14
|
+
end
|
15
|
+
|
16
|
+
def reset_db_runtime
|
17
|
+
self.db_runtime = 0
|
18
|
+
end
|
19
|
+
|
20
|
+
def append_db_runtime(event)
|
21
|
+
self.db_runtime += event.duration
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "sapience"
|
3
|
+
require "sapience/extensions/sinatra/timings"
|
4
|
+
require "sapience/extensions/sinatra/middleware/logging"
|
5
|
+
|
6
|
+
module Sinatra
|
7
|
+
class Base
|
8
|
+
extend Sapience::Descendants
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module Sapience
|
13
|
+
class Sinatra
|
14
|
+
Sapience.configure
|
15
|
+
::Sinatra::Base.send(:include, Sapience::Loggable)
|
16
|
+
end
|
17
|
+
end
|
data/lib/sapience/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sapience
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikael Henriksson
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-
|
12
|
+
date: 2019-09-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: concurrent-ruby
|
@@ -358,6 +358,8 @@ files:
|
|
358
358
|
- lib/sapience/extensions/notifications.rb
|
359
359
|
- lib/sapience/extensions/rails/rack/logger.rb
|
360
360
|
- lib/sapience/extensions/rails/rack/logger_info_as_debug.rb
|
361
|
+
- lib/sapience/extensions/sinatra/middleware/logging.rb
|
362
|
+
- lib/sapience/extensions/sinatra/timings.rb
|
361
363
|
- lib/sapience/formatters/base.rb
|
362
364
|
- lib/sapience/formatters/color.rb
|
363
365
|
- lib/sapience/formatters/default.rb
|
@@ -376,6 +378,7 @@ files:
|
|
376
378
|
- lib/sapience/rails/railtie.rb
|
377
379
|
- lib/sapience/rails/silencer.rb
|
378
380
|
- lib/sapience/sapience.rb
|
381
|
+
- lib/sapience/sinatra.rb
|
379
382
|
- lib/sapience/sneakers.rb
|
380
383
|
- lib/sapience/subscriber.rb
|
381
384
|
- lib/sapience/version.rb
|
@@ -402,7 +405,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
402
405
|
version: '0'
|
403
406
|
requirements: []
|
404
407
|
rubyforge_project:
|
405
|
-
rubygems_version: 2.
|
408
|
+
rubygems_version: 2.6.11
|
406
409
|
signing_key:
|
407
410
|
specification_version: 4
|
408
411
|
summary: Hasslefree autoconfiguration for logging, metrics and exception collection.
|