moesif_rack 1.4.4 → 1.4.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +59 -26
- data/lib/moesif_rack/client_ip.rb +2 -4
- data/lib/moesif_rack/moesif_middleware.rb +14 -3
- metadata +15 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91724c174f328bf7a8a4b988ee09b3d18438c043a1a64d321d779b21956f6be3
|
4
|
+
data.tar.gz: d5e702b86d078fd0655c13d2fc570e9403c91a6014f5c9c7f7705e2d22e4d1fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a94ea5d908d57e8e148c126212d9470b17f4ceff266c3bdf3b02950517e5b77a4e544128ca7f7cfdbe01c12c975fdedad6694947a77d1e274e9bf301a3ca6f58
|
7
|
+
data.tar.gz: 829537f2b578b9b3f555cd4d3988f3799db3bd0d4ed11e6b98f49bffbaa4d7e557e88030f523622784546348b1b5151819148e52d20870bfb33bb0fe5d96c013
|
data/README.md
CHANGED
@@ -7,33 +7,37 @@
|
|
7
7
|
[![Source Code][ico-source]][link-source]
|
8
8
|
|
9
9
|
Rack Middleware that logs API calls and sends
|
10
|
-
to [Moesif](https://www.moesif.com) for API analytics and
|
10
|
+
to [Moesif](https://www.moesif.com) for API analytics and monitoring.
|
11
11
|
|
12
|
-
Supports Ruby on Rails
|
12
|
+
Supports Ruby on Rails, Grape, and other Ruby frameworks built on Rack.
|
13
13
|
|
14
14
|
[Source Code on GitHub](https://github.com/moesif/moesif-rack)
|
15
15
|
|
16
16
|
## How to install
|
17
17
|
|
18
|
+
Install the Moesif gem.
|
19
|
+
|
18
20
|
```bash
|
19
21
|
gem install moesif_rack
|
20
22
|
```
|
21
23
|
|
22
|
-
|
24
|
+
If you're using Bundler, add the gem to your `Gemfile`.
|
23
25
|
|
26
|
+
```ruby
|
27
|
+
gem 'moesif_rack'
|
24
28
|
```
|
25
|
-
gem 'moesif_rack', '~> 1.4.2'
|
26
29
|
|
27
|
-
|
30
|
+
Then, run `bundle install`
|
28
31
|
|
29
32
|
## How to use
|
30
33
|
|
31
|
-
###
|
34
|
+
### 1. Enter Moesif Application Id
|
35
|
+
|
36
|
+
Create an options hash containing application_id and any other options.
|
32
37
|
|
33
38
|
```ruby
|
34
39
|
moesif_options = {
|
35
|
-
'application_id' => 'Your Moesif Application Id'
|
36
|
-
'log_body' => true,
|
40
|
+
'application_id' => 'Your Moesif Application Id'
|
37
41
|
}
|
38
42
|
```
|
39
43
|
|
@@ -44,43 +48,67 @@ You can always find your Moesif Application Id at any time by logging
|
|
44
48
|
into the [_Moesif Portal_](https://www.moesif.com/), click on the top right menu,
|
45
49
|
and then clicking _Installation_.
|
46
50
|
|
47
|
-
### Add
|
51
|
+
### 2. Add the middleware
|
52
|
+
|
53
|
+
#### For Rails 5.0 or newer:
|
48
54
|
|
49
55
|
Using strings or symbols for middleware class names is deprecated for newer frameworks like Ruby 5.0,
|
50
56
|
so you should pass the class directly.
|
51
57
|
|
52
|
-
#### For Rails 5.0 or newer:
|
53
|
-
|
54
58
|
```ruby
|
55
59
|
class Application < Rails::Application
|
56
|
-
|
60
|
+
moesif_options = {
|
61
|
+
'application_id' => 'Your Moesif Application Id'
|
62
|
+
}
|
57
63
|
|
58
64
|
config.middleware.use MoesifRack::MoesifMiddleware, moesif_options
|
59
|
-
|
60
|
-
# snip
|
61
65
|
end
|
62
66
|
```
|
63
67
|
|
64
|
-
#### For other frameworks:
|
68
|
+
#### For Rails 4.0 and other frameworks:
|
69
|
+
|
70
|
+
For most rack-based frameworks including Rails 4.x or older, add the middleware `MoesifRack::MoesifMiddleware`.
|
65
71
|
|
66
72
|
within `config/application.rb`
|
67
73
|
|
68
74
|
```ruby
|
69
75
|
class Application < Rails::Application
|
70
|
-
|
76
|
+
moesif_options = {
|
77
|
+
'application_id' => 'Your Moesif Application Id'
|
78
|
+
}
|
71
79
|
|
72
80
|
config.middleware.use "MoesifRack::MoesifMiddleware", moesif_options
|
81
|
+
end
|
82
|
+
```
|
73
83
|
|
74
|
-
|
84
|
+
#### For Grape API:
|
85
|
+
|
86
|
+
For [Grape APIs](https://github.com/ruby-grape/grape), we can add the middleware after any custom parsers or formatters.
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
module Acme
|
90
|
+
class Ping < Grape::API
|
91
|
+
format :json
|
92
|
+
|
93
|
+
moesif_options = {
|
94
|
+
'application_id' => 'Your Moesif Application Id'
|
95
|
+
}
|
96
|
+
|
97
|
+
insert_after Grape::Middleware::Formatter, MoesifRack::MoesifMiddleware, moesif_options
|
98
|
+
|
99
|
+
get '/ping' do
|
100
|
+
{ ping: 'pong' }
|
101
|
+
end
|
75
102
|
end
|
103
|
+
end
|
76
104
|
```
|
77
105
|
|
78
106
|
#### Order of Middleware Matters
|
79
107
|
|
80
|
-
Since Moesif Rack is a logging middleware, the ordering of middleware matters
|
81
|
-
|
108
|
+
Since Moesif Rack is a logging middleware, the ordering of middleware matters.
|
109
|
+
The best place for "MoesifRack::MoesifMidleware" is near the top (so it captures the data closest to the wire), but after
|
110
|
+
any body parsers or authentication middleware.
|
82
111
|
|
83
|
-
The best place for "MoesifRack::MoesifMidleware" is on top (so it captures the data closest to the wire).
|
84
112
|
Typically, right above the default logger of Rails apps, "Rails::Rack::Logger" is a good spot.
|
85
113
|
Or if you want to be as close as wire as possible, put it before "ActionDispatch::Static"
|
86
114
|
|
@@ -107,7 +135,7 @@ To see your current list of middleware:
|
|
107
135
|
|
108
136
|
## Configuration options
|
109
137
|
|
110
|
-
|
138
|
+
Options is a hash with these possible key/value pairs.
|
111
139
|
|
112
140
|
#### __`application_id`__
|
113
141
|
|
@@ -206,7 +234,12 @@ Optional. A Proc that takes env, headers, body and returns a boolean.
|
|
206
234
|
|
207
235
|
moesif_options['skip'] = Proc.new { |env, headers, body|
|
208
236
|
# Add your custom code that returns true to skip logging the API call
|
209
|
-
|
237
|
+
if env.key?("REQUEST_URI")
|
238
|
+
# Skip probes to health page
|
239
|
+
env["REQUEST_URI"].include? "/health"
|
240
|
+
else
|
241
|
+
false
|
242
|
+
end
|
210
243
|
}
|
211
244
|
|
212
245
|
```
|
@@ -500,11 +533,11 @@ response = MoesifRack::MoesifMiddleware.new(@app, @options).update_companies_bat
|
|
500
533
|
5. Invoke 'ruby test/moesif_rack_test.rb'
|
501
534
|
6. Invoke 'ruby -I test test/moesif_rack_test.rb -n test_capture_outgoing' to test capturing outgoing API calls from your app to third parties like Stripe, Github or to your own dependencies.
|
502
535
|
|
503
|
-
## Example
|
504
|
-
|
505
|
-
[Moesif Rack Example with Rails](https://github.com/Moesif/moesif-rails-example) is an example of Moesif Rack applied to a Rails application.
|
536
|
+
## Example Projects
|
506
537
|
|
507
|
-
[Moesif
|
538
|
+
- [Moesif Rails 5 Example](https://github.com/Moesif/moesif-rails5-example) is an example of Moesif with a Ruby on Rails 5 application.
|
539
|
+
- [Moesif Rails 4 Example](https://github.com/Moesif/moesif-rails4-example) is an example of Moesif with a Ruby on Rails 4 application.
|
540
|
+
- [Moesif Rack Example](https://github.com/Moesif/moesif-rack-example) is an example of Moesif applied to a Rack application.
|
508
541
|
|
509
542
|
## Other integrations
|
510
543
|
|
@@ -2,10 +2,8 @@
|
|
2
2
|
def is_ip?(value)
|
3
3
|
ipv4 = /^(?:(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/
|
4
4
|
ipv6 = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
|
5
|
-
|
6
|
-
|
7
|
-
!!(value =~ ipv4 or value=~ ipv6)
|
8
|
-
end
|
5
|
+
# We use !! to convert the return value to a boolean
|
6
|
+
!!(value =~ ipv4 or value=~ ipv6)
|
9
7
|
end
|
10
8
|
|
11
9
|
def get_client_ip_from_x_forwarded_for(value)
|
@@ -102,7 +102,10 @@ module MoesifRack
|
|
102
102
|
|
103
103
|
def parse_body(body, headers)
|
104
104
|
begin
|
105
|
-
if
|
105
|
+
if (body.instance_of?(Hash) || body.instance_of?(Array))
|
106
|
+
parsed_body = body
|
107
|
+
transfer_encoding = 'json'
|
108
|
+
elsif start_with_json(body)
|
106
109
|
parsed_body = JSON.parse(body)
|
107
110
|
transfer_encoding = 'json'
|
108
111
|
elsif headers.key?('content-encoding') && ((headers['content-encoding'].downcase).include? "gzip")
|
@@ -293,7 +296,7 @@ module MoesifRack
|
|
293
296
|
# Add Event to the queue
|
294
297
|
@events_queue << event_model
|
295
298
|
@helpers.log_debug("Event added to the queue ")
|
296
|
-
if Time.now.utc > (@
|
299
|
+
if Time.now.utc > (@last_worker_run + 60)
|
297
300
|
start_worker()
|
298
301
|
end
|
299
302
|
|
@@ -328,7 +331,12 @@ module MoesifRack
|
|
328
331
|
end
|
329
332
|
|
330
333
|
if !should_skip
|
331
|
-
|
334
|
+
begin
|
335
|
+
process_send.call
|
336
|
+
rescue => exception
|
337
|
+
@helpers.log_debug 'Error while logging event - '
|
338
|
+
@helpers.log_debug exception.to_s
|
339
|
+
end
|
332
340
|
end
|
333
341
|
|
334
342
|
[status, headers, body]
|
@@ -336,6 +344,9 @@ module MoesifRack
|
|
336
344
|
|
337
345
|
def get_response_body(response)
|
338
346
|
body = response.respond_to?(:body) ? response.body : response
|
347
|
+
if (body.instance_of?(Hash) || body.instance_of?(Array))
|
348
|
+
return body
|
349
|
+
end
|
339
350
|
body = body.inject("") { |i, a| i << a } if (body.respond_to?(:each) && body.respond_to?(:inject))
|
340
351
|
body.to_s
|
341
352
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: moesif_rack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Moesif, Inc
|
@@ -9,10 +9,9 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-06-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name: test-unit
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
16
|
requirements:
|
18
17
|
- - "~>"
|
@@ -21,8 +20,9 @@ dependencies:
|
|
21
20
|
- - ">="
|
22
21
|
- !ruby/object:Gem::Version
|
23
22
|
version: 3.1.5
|
24
|
-
|
23
|
+
name: test-unit
|
25
24
|
prerelease: false
|
25
|
+
type: :development
|
26
26
|
version_requirements: !ruby/object:Gem::Requirement
|
27
27
|
requirements:
|
28
28
|
- - "~>"
|
@@ -32,7 +32,6 @@ dependencies:
|
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 3.1.5
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
|
-
name: moesif_api
|
36
35
|
requirement: !ruby/object:Gem::Requirement
|
37
36
|
requirements:
|
38
37
|
- - "~>"
|
@@ -41,8 +40,9 @@ dependencies:
|
|
41
40
|
- - ">="
|
42
41
|
- !ruby/object:Gem::Version
|
43
42
|
version: 1.2.12
|
44
|
-
|
43
|
+
name: moesif_api
|
45
44
|
prerelease: false
|
45
|
+
type: :runtime
|
46
46
|
version_requirements: !ruby/object:Gem::Requirement
|
47
47
|
requirements:
|
48
48
|
- - "~>"
|
@@ -73,7 +73,14 @@ files:
|
|
73
73
|
homepage: https://moesif.com
|
74
74
|
licenses:
|
75
75
|
- Apache-2.0
|
76
|
-
metadata:
|
76
|
+
metadata:
|
77
|
+
bug_tracker_uri: https://github.com/Moesif/moesif-rack/issues
|
78
|
+
changelog_uri: https://github.com/Moesif/moesif-rack/releases
|
79
|
+
documentation_uri: https://www.moesif.com/docs/server-integration/rack/
|
80
|
+
homepage_uri: https://www.moesif.com
|
81
|
+
mailing_list_uri: https://github.com/Moesif/moesif-rack
|
82
|
+
source_code_uri: https://github.com/Moesif/moesif-rack
|
83
|
+
wiki_uri: https://github.com/Moesif/moesif-rack
|
77
84
|
post_install_message:
|
78
85
|
rdoc_options: []
|
79
86
|
require_paths:
|
@@ -90,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
97
|
version: '0'
|
91
98
|
requirements: []
|
92
99
|
rubyforge_project:
|
93
|
-
rubygems_version: 2.7.
|
100
|
+
rubygems_version: 2.7.10
|
94
101
|
signing_key:
|
95
102
|
specification_version: 4
|
96
103
|
summary: moesif_rack
|