rollday 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -1
- data/README.md +28 -11
- data/lib/rollday/client_middleware.rb +11 -0
- data/lib/rollday/configuration.rb +88 -0
- data/lib/rollday/middleware.rb +38 -0
- data/lib/rollday/rollbar_helper.rb +56 -0
- data/lib/rollday/use_middleware.rb +39 -0
- data/lib/rollday/version.rb +1 -1
- data/lib/rollday.rb +40 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2c78b5cc1611bf2a6d2766ffbd66df911882fc62e2fbcf3575450113c71e4b2
|
4
|
+
data.tar.gz: 2bc833d5d56efe20e819c66ec503074fe058444dc65f7640b393fab268782c04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f6a73ebb75e6854044f29da60c3cb98c68efd6f7dae56b66efbbe66c55847fbfdf08016c6dfacd520e261cec4162c263c7c227802c64303085e8260211cc2bc
|
7
|
+
data.tar.gz: f80905eebdc8af2e5dcff399dc3ad5ff42de2c3c75fbaf7b20b5139d8cec527237bb0a14acc6b32d90b337ef57b4254c5a7ee8fedf011db0ee9fde709af55373
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
# Rollday
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
`bin/console` for an interactive prompt.
|
3
|
+
Rollday is a a gem to integrate with Faraday requests. It adds a default middleware for your projecrts Faraday client to send a rollbar for configurable response status codes.
|
4
|
+
|
5
|
+
It can be configured once for th eentire project, or customized per Faraday request
|
7
6
|
|
8
|
-
TODO: Delete this and the text above, and describe your gem
|
9
7
|
|
10
8
|
## Installation
|
11
9
|
|
@@ -15,17 +13,36 @@ Add this line to your application's Gemfile:
|
|
15
13
|
gem 'rollday'
|
16
14
|
```
|
17
15
|
|
18
|
-
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
### Initialization
|
19
19
|
|
20
|
-
|
20
|
+
Intialization should happen in `app/initializers/rollday.rb`. All options below are the current defaults unless stated
|
21
|
+
```ruby
|
22
|
+
Rollday.configure do |config|
|
23
|
+
config.use_default_middleware! # [Not default option] set middleware for all Faraday requests (Faraday.get(...))
|
21
24
|
|
22
|
-
|
25
|
+
config.use_default_client_middleware! # [Not default option] set middleware for all Faraday clients.
|
23
26
|
|
24
|
-
|
27
|
+
config.status_code_regex = /[45]\d\d$/ # If status code matches, will attempt to send a rollbar
|
25
28
|
|
26
|
-
|
29
|
+
config.use_person_scope = true # Assign a person scope to the rollbar scope
|
30
|
+
|
31
|
+
config.use_params_scope = true # Assign a params scope to the rollbar scope. Configured from Faraday params for request
|
27
32
|
|
28
|
-
|
33
|
+
config.params_scope_sanitizer = [] # Array of Procs to sanitize params. Can remove params or call Rollbar::Scrubbers.scrub_value(*) to assign value
|
34
|
+
|
35
|
+
config.use_query_scope = true # Assign the url queries to the scope
|
36
|
+
|
37
|
+
config.params_query_sanitizer = [] # Array of Procs to sanitize query params. Can remove params or call Rollbar::Scrubbers.scrub_value(*) to assign value
|
38
|
+
|
39
|
+
config.message = ->(status, phrase, body, path, domain) { "[#{status}]: #{domain} - #{path}" } # Message to set for the Rollbar item. Value can be a proc or a static message
|
40
|
+
|
41
|
+
config.use_message_exception = true # When set to true, Exception will be used to establish a backtrace
|
42
|
+
|
43
|
+
config.rollbar_level = ->(_status) { :warning } # Rollbar level can be configurable based on the status code
|
44
|
+
end
|
45
|
+
```
|
29
46
|
|
30
47
|
## Development
|
31
48
|
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rollday/use_middleware"
|
4
|
+
require "rollbar"
|
5
|
+
|
6
|
+
module Rollday
|
7
|
+
class Configuration
|
8
|
+
ROLLBAR_LEVELS = [
|
9
|
+
DEBUG = :debug,
|
10
|
+
INFO = :info,
|
11
|
+
WARNING = :warning,
|
12
|
+
ERROR = :error,
|
13
|
+
CRITICAL = :critical
|
14
|
+
]
|
15
|
+
|
16
|
+
DEFAULT_STATUS_CODE_REGEX = /[45]\d\d$/
|
17
|
+
DEFAULT_MESSAGE_PROC = ->(status, phrase, domain) { "[#{status}]: #{domain}" }
|
18
|
+
DEFAULT_LEVEL_PROC = ->(_status) { WARNING }
|
19
|
+
|
20
|
+
attr_accessor :use_person_scope, :use_params_scope, :params_scope_sanitizer, :use_query_scope, :params_query_sanitizer, :use_message_exception
|
21
|
+
|
22
|
+
def initialize(options = {})
|
23
|
+
@status_code_regex = DEFAULT_STATUS_CODE_REGEX
|
24
|
+
@use_person_scope = true
|
25
|
+
|
26
|
+
@use_params_scope = true
|
27
|
+
@params_scope_sanitizer = []
|
28
|
+
|
29
|
+
@use_query_scope = true
|
30
|
+
@params_query_sanitizer = []
|
31
|
+
|
32
|
+
@message = DEFAULT_MESSAGE_PROC
|
33
|
+
@use_message_exception = true
|
34
|
+
|
35
|
+
@rollbar_level = DEFAULT_LEVEL_PROC
|
36
|
+
end
|
37
|
+
|
38
|
+
def rollbar_level=(level)
|
39
|
+
raise ConfigError, "level= must be passed a Proc or #{ROLLBAR_LEVELS}. But was passed a #{level} instead"
|
40
|
+
|
41
|
+
@rollbar_level = level
|
42
|
+
end
|
43
|
+
|
44
|
+
def rollbar_level
|
45
|
+
@rollbar_level
|
46
|
+
end
|
47
|
+
|
48
|
+
def message=(message)
|
49
|
+
raise ConfigError, "message= must be passed a Proc but was passed a #{message.class} instead"
|
50
|
+
|
51
|
+
@message = message
|
52
|
+
end
|
53
|
+
|
54
|
+
def message
|
55
|
+
@message
|
56
|
+
end
|
57
|
+
|
58
|
+
def status_code_regex
|
59
|
+
@status_code_regex
|
60
|
+
end
|
61
|
+
|
62
|
+
def status_code_regex=(regex)
|
63
|
+
raise ConfigError, "status_code_regex= must be passed a regex but was passed a #{regex.class} instead"
|
64
|
+
|
65
|
+
@status_code_regex = regex
|
66
|
+
end
|
67
|
+
|
68
|
+
def person_scope=(person_scope)
|
69
|
+
raise ConfigError, "person_scope= must be passed a Proc but was passed a #{person_scope.class} instead"
|
70
|
+
|
71
|
+
@person_scope = person_scope
|
72
|
+
end
|
73
|
+
|
74
|
+
def person_scope
|
75
|
+
return -> {} unless @use_person_scope
|
76
|
+
|
77
|
+
@person_scope || Rollbar.scope.scope_object.raw[:person] || -> {}
|
78
|
+
end
|
79
|
+
|
80
|
+
def use_default_middleware!
|
81
|
+
Rollday.use_default_middleware!
|
82
|
+
end
|
83
|
+
|
84
|
+
def use_default_client_middleware!
|
85
|
+
Rollday.use_default_client_middleware!
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "faraday"
|
4
|
+
require "rollday/rollbar_helper"
|
5
|
+
|
6
|
+
module Rollday
|
7
|
+
class Middleware < Faraday::Middleware
|
8
|
+
include RollbarHelper
|
9
|
+
|
10
|
+
def initialize(app, **)
|
11
|
+
super(app)
|
12
|
+
|
13
|
+
@app = app
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
result = @app.(env)
|
18
|
+
if ship_to_rollbar?(result.status)
|
19
|
+
send_rollbar(result)
|
20
|
+
end
|
21
|
+
|
22
|
+
result
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def ship_to_rollbar?(status)
|
28
|
+
status.to_s =~ Rollday.config.status_code_regex
|
29
|
+
end
|
30
|
+
|
31
|
+
def send_rollbar(result)
|
32
|
+
scope = rollbar_scope(result)
|
33
|
+
message = rollbar_message(result)
|
34
|
+
level = rollbar_level(result)
|
35
|
+
::Rollbar.log(level, message, **scope)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rollbar'
|
4
|
+
|
5
|
+
module Rollday
|
6
|
+
module RollbarHelper
|
7
|
+
|
8
|
+
def rollbar_level(result)
|
9
|
+
level = Rollday.config.rollbar_level
|
10
|
+
level.is_a?(Proc) ? level.(result.status) : level
|
11
|
+
end
|
12
|
+
|
13
|
+
def rollbar_message(result)
|
14
|
+
message = Rollday.config.message.(result.status, result.reason_phrase, result.body, URI(result.env.url).path, URI(result.env.url).host)
|
15
|
+
return message unless Rollday.config.use_message_exception
|
16
|
+
|
17
|
+
Rollday::Faraday.new(message)
|
18
|
+
end
|
19
|
+
|
20
|
+
def rollbar_scope(result)
|
21
|
+
{
|
22
|
+
framework: "Faraday: #{::Faraday::VERSION}; Rollday: #{Rollday::VERSION}",
|
23
|
+
host: URI(result.env.url).host,
|
24
|
+
method: result.env.method,
|
25
|
+
params: params_scope(result),
|
26
|
+
path: URI(result.env.url).path,
|
27
|
+
body: result.body,
|
28
|
+
person: person_scope,
|
29
|
+
query: query_scope(result),
|
30
|
+
status: result.status,
|
31
|
+
status_phrase: result.reason_phrase,
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def query_scope(result)
|
38
|
+
return {} unless Rollday.config.use_query_scope
|
39
|
+
|
40
|
+
query_scope = CGI::parse(URI(result.env.url).query)
|
41
|
+
Rollday.config.params_scope_sanitizer.each do |sanitizer|
|
42
|
+
query_scope = sanitizer.(query_params)
|
43
|
+
end
|
44
|
+
|
45
|
+
query_scope
|
46
|
+
end
|
47
|
+
|
48
|
+
def params_scope(result)
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
def person_scope
|
53
|
+
Rollday.config.person_scope.()
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rollday
|
4
|
+
module UseMiddleware
|
5
|
+
# https://github.com/lostisland/faraday/blob/816d824bc18453e86027c9c5fcf8427020566e50/lib/faraday/rack_builder.rb#L83-L86
|
6
|
+
# https://github.com/lostisland/faraday/blob/816d824bc18453e86027c9c5fcf8427020566e50/lib/faraday/rack_builder.rb#L156-L169
|
7
|
+
# Once the default middleware is crated and the Faraday Rack app has been built,
|
8
|
+
# it can not be removed from the middleware Faraday stack
|
9
|
+
# If removal is needed, then use the middleware per request instead of defaulted
|
10
|
+
# or use the `with_scope` to modify the parameters of the rollday gem
|
11
|
+
def self.use_default_middleware!
|
12
|
+
register_middleware!
|
13
|
+
|
14
|
+
return false if @add_default_middleware
|
15
|
+
|
16
|
+
idx = ::Faraday.default_connection.builder.handlers.size - 1
|
17
|
+
::Faraday.default_connection.builder.insert(idx, Middleware)
|
18
|
+
@add_default_middleware = true
|
19
|
+
end
|
20
|
+
|
21
|
+
# https://github.com/lostisland/faraday/issues/946#issuecomment-500607890
|
22
|
+
# Monkey patch to force this middleware into every single Faraday.new client
|
23
|
+
def self.use_default_client_middleware!
|
24
|
+
return false if @use_default_client_middleware
|
25
|
+
|
26
|
+
require "rollday/client_middleware"
|
27
|
+
::Faraday::ConnectionOptions.prepend(Rollday::FaradayConnectionOptions)
|
28
|
+
|
29
|
+
@use_default_client_middleware = true
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.register_middleware!
|
33
|
+
return false if @register_middleware
|
34
|
+
|
35
|
+
::Faraday::Middleware.register_middleware(Rollday::FARADAY_NAME => Middleware)
|
36
|
+
@register_middleware = true
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/rollday/version.rb
CHANGED
data/lib/rollday.rb
CHANGED
@@ -1,8 +1,47 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "rollday/version"
|
4
|
+
require "faraday"
|
5
|
+
require "rollday/configuration"
|
6
|
+
require "rollday/middleware"
|
4
7
|
|
5
8
|
module Rollday
|
6
9
|
class Error < StandardError; end
|
7
|
-
#
|
10
|
+
class Faraday < Error; end # used to create backtrace for rollbar
|
11
|
+
class ConfigError < Error; end
|
12
|
+
|
13
|
+
FARADAY_NAME = :rollday.freeze
|
14
|
+
|
15
|
+
def self.configure
|
16
|
+
yield configuration if block_given?
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.configuration
|
20
|
+
@configuration ||= Rollday::Configuration.new
|
21
|
+
end
|
22
|
+
|
23
|
+
class << self
|
24
|
+
alias_method :config, :configuration
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.configuration=(object)
|
28
|
+
raise ConfigError, "Expected configuration to be a Rollday::Configuration" unless object.is_a?(Rollday::Configuration)
|
29
|
+
|
30
|
+
@configuration = object
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.use_middleware!
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.use_default_middleware!
|
38
|
+
Rollday::UseMiddleware.use_default_middleware!
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.use_default_client_middleware!
|
42
|
+
Rollday::UseMiddleware.use_default_client_middleware!
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.with_scope()
|
46
|
+
end
|
8
47
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rollday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Taylor
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-06-
|
11
|
+
date: 2022-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -117,6 +117,11 @@ files:
|
|
117
117
|
- bin/setup
|
118
118
|
- docker-compose.yml
|
119
119
|
- lib/rollday.rb
|
120
|
+
- lib/rollday/client_middleware.rb
|
121
|
+
- lib/rollday/configuration.rb
|
122
|
+
- lib/rollday/middleware.rb
|
123
|
+
- lib/rollday/rollbar_helper.rb
|
124
|
+
- lib/rollday/use_middleware.rb
|
120
125
|
- lib/rollday/version.rb
|
121
126
|
- rollday.gemspec
|
122
127
|
homepage: https://github.com/matt-taylor/rollday
|