moesif_rack 1.4.3 → 1.4.8
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/app_config.rb +34 -42
- data/lib/moesif_rack/client_ip.rb +2 -4
- data/lib/moesif_rack/helpers.rb +14 -0
- data/lib/moesif_rack/moesif_middleware.rb +58 -41
- metadata +16 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c83ae80cf118c921a212121b0be6af3720b5cd9b0d0e83c6665828f4e542f10
|
4
|
+
data.tar.gz: 0a709decc75d43649da2e1305cd6370e44cbf8cb78b5c931e7df3afb894845c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 703794d208c4cceb7bcefa28e43fa7ae3c2e624569dbf6d8bf6697e70d3235d89e073f9b0c568967d0b7640b6b60650ba9d9bb2f3fa3277affae6ef75a6193b2
|
7
|
+
data.tar.gz: f95e69f49e07f89312c3e7d1c25347e35fd621f064cdf604fbd50f1159b73d3ade37098cfd13e2fead7d8110ebb3d74c11400701b66a0df6ce18e2f8fa058b6f
|
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
|
|
@@ -3,71 +3,71 @@ require 'json'
|
|
3
3
|
require 'time'
|
4
4
|
require 'zlib'
|
5
5
|
require 'stringio'
|
6
|
+
require_relative './helpers.rb'
|
6
7
|
|
7
8
|
class AppConfig
|
8
9
|
|
9
|
-
def
|
10
|
+
def initialize debug
|
11
|
+
@debug = debug
|
12
|
+
@helpers = Helpers.new(debug)
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_config(api_controller)
|
10
16
|
# Get Application Config
|
11
17
|
begin
|
12
18
|
config_api_response = api_controller.get_app_config()
|
19
|
+
@helpers.log_debug("new config downloaded")
|
20
|
+
@helpers.log_debug(config_api_response.to_s)
|
13
21
|
return config_api_response
|
14
22
|
rescue MoesifApi::APIException => e
|
15
23
|
if e.response_code.between?(401, 403)
|
16
|
-
|
17
|
-
end
|
18
|
-
if debug
|
19
|
-
puts 'Error getting application configuration, with status code:'
|
20
|
-
puts e.response_code
|
24
|
+
@helpers.log_debug 'Unauthorized access getting application configuration. Please check your Appplication Id.'
|
21
25
|
end
|
26
|
+
@helpers.log_debug 'Error getting application configuration, with status code:'
|
27
|
+
@helpers.log_debug e.response_code
|
22
28
|
rescue => e
|
23
|
-
|
24
|
-
puts e.to_s
|
25
|
-
end
|
29
|
+
@helpers.log_debug e.to_s
|
26
30
|
end
|
27
31
|
rescue
|
28
32
|
end
|
29
33
|
|
30
|
-
def parse_configuration(config_api_response
|
34
|
+
def parse_configuration(config_api_response)
|
31
35
|
# Parse configuration object and return Etag, sample rate and last updated time
|
32
36
|
begin
|
33
37
|
# Rails return gzipped compressed response body, so decompressing it and getting JSON response body
|
34
|
-
response_body = decompress_gzip_body(config_api_response
|
38
|
+
response_body = decompress_gzip_body(config_api_response)
|
39
|
+
@helpers.log_debug(response_body.to_s)
|
35
40
|
|
36
41
|
# Check if response body is not nil
|
37
42
|
if !response_body.nil? then
|
38
43
|
# Return Etag, sample rate and last updated time
|
39
|
-
return config_api_response.headers[:x_moesif_config_etag],
|
44
|
+
return response_body, config_api_response.headers[:x_moesif_config_etag], Time.now.utc
|
40
45
|
else
|
41
|
-
|
42
|
-
puts 'Response body is nil, assuming default behavior'
|
43
|
-
end
|
46
|
+
@helpers.log_debug 'Response body is nil, assuming default behavior'
|
44
47
|
# Response body is nil, so assuming default behavior
|
45
48
|
return nil, 100, Time.now.utc
|
46
49
|
end
|
47
50
|
rescue => exception
|
48
|
-
|
49
|
-
|
50
|
-
puts exception.to_s
|
51
|
-
end
|
51
|
+
@helpers.log_debug 'Error while parsing the configuration object, assuming default behavior'
|
52
|
+
@helpers.log_debug exception.to_s
|
52
53
|
# Assuming default behavior
|
53
54
|
return nil, 100, Time.now.utc
|
54
55
|
end
|
55
56
|
end
|
56
57
|
|
57
|
-
def get_sampling_percentage(config_api_response, user_id, company_id
|
58
|
+
def get_sampling_percentage(config_api_response, user_id, company_id)
|
58
59
|
# Get sampling percentage
|
59
60
|
begin
|
60
|
-
# Rails return gzipped compressed response body, so decompressing it and getting JSON response body
|
61
|
-
response_body = decompress_gzip_body(config_api_response, debug)
|
62
|
-
|
63
61
|
# Check if response body is not nil
|
64
|
-
if !
|
65
|
-
|
62
|
+
if !config_api_response.nil? then
|
63
|
+
@helpers.log_debug("Getting sample rate for user #{user_id} company #{company_id}")
|
64
|
+
@helpers.log_debug(config_api_response.to_s)
|
65
|
+
|
66
66
|
# Get user sample rate object
|
67
|
-
user_sample_rate =
|
67
|
+
user_sample_rate = config_api_response.fetch('user_sample_rate', nil)
|
68
68
|
|
69
69
|
# Get company sample rate object
|
70
|
-
company_sample_rate =
|
70
|
+
company_sample_rate = config_api_response.fetch('company_sample_rate', nil)
|
71
71
|
|
72
72
|
# Get sample rate for the user if exist
|
73
73
|
if !user_id.nil? && !user_sample_rate.nil? && user_sample_rate.key?(user_id)
|
@@ -80,22 +80,18 @@ class AppConfig
|
|
80
80
|
end
|
81
81
|
|
82
82
|
# Return sample rate
|
83
|
-
return
|
83
|
+
return config_api_response.fetch('sample_rate', 100)
|
84
84
|
else
|
85
|
-
|
86
|
-
puts 'Assuming default behavior as response body is nil - '
|
87
|
-
end
|
85
|
+
@helpers.log_debug 'Assuming default behavior as response body is nil - '
|
88
86
|
return 100
|
89
87
|
end
|
90
88
|
rescue => exception
|
91
|
-
|
92
|
-
puts 'Error while geting sampling percentage, assuming default behavior'
|
93
|
-
end
|
89
|
+
@helpers.log_debug 'Error while geting sampling percentage, assuming default behavior'
|
94
90
|
return 100
|
95
91
|
end
|
96
92
|
end
|
97
93
|
|
98
|
-
def decompress_gzip_body(config_api_response
|
94
|
+
def decompress_gzip_body(config_api_response)
|
99
95
|
# Decompress gzip response body
|
100
96
|
begin
|
101
97
|
# Check if the content-encoding header exist and is of type zip
|
@@ -110,16 +106,12 @@ class AppConfig
|
|
110
106
|
# Return the parsed body
|
111
107
|
return JSON.parse( uncompressed_string )
|
112
108
|
else
|
113
|
-
|
114
|
-
puts 'Content Encoding is of type other than gzip, returning nil'
|
115
|
-
end
|
109
|
+
@helpers.log_debug 'Content Encoding is of type other than gzip, returning nil'
|
116
110
|
return nil
|
117
111
|
end
|
118
112
|
rescue => exception
|
119
|
-
|
120
|
-
|
121
|
-
puts exception.to_s
|
122
|
-
end
|
113
|
+
@helpers.log_debug 'Error while decompressing the response body'
|
114
|
+
@helpers.log_debug exception.to_s
|
123
115
|
return nil
|
124
116
|
end
|
125
117
|
end
|
@@ -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)
|
@@ -6,6 +6,7 @@ require_relative './client_ip.rb'
|
|
6
6
|
require_relative './app_config.rb'
|
7
7
|
require_relative './update_user.rb'
|
8
8
|
require_relative './update_company.rb'
|
9
|
+
require_relative './helpers.rb'
|
9
10
|
require 'zlib'
|
10
11
|
require 'stringio'
|
11
12
|
|
@@ -28,10 +29,10 @@ module MoesifRack
|
|
28
29
|
@mask_data = options['mask_data']
|
29
30
|
@skip = options['skip']
|
30
31
|
@debug = options['debug']
|
31
|
-
@app_config = AppConfig.new
|
32
|
-
@
|
32
|
+
@app_config = AppConfig.new(@debug)
|
33
|
+
@helpers = Helpers.new(@debug)
|
34
|
+
@config = @app_config.get_config(@api_controller)
|
33
35
|
@config_etag = nil
|
34
|
-
@sampling_percentage = 100
|
35
36
|
@last_config_download_time = Time.now.utc
|
36
37
|
@last_worker_run = Time.now.utc
|
37
38
|
@config_dict = Hash.new
|
@@ -44,17 +45,18 @@ module MoesifRack
|
|
44
45
|
start_worker()
|
45
46
|
|
46
47
|
begin
|
47
|
-
|
48
|
-
|
48
|
+
new_config = @app_config.get_config(@api_controller)
|
49
|
+
if !new_config.nil?
|
50
|
+
@config, @config_etag, @last_config_download_time = @app_config.parse_configuration(new_config)
|
49
51
|
end
|
50
52
|
rescue => exception
|
51
|
-
log_debug 'Error while parsing application configuration on initialization'
|
52
|
-
log_debug exception.to_s
|
53
|
+
@helpers.log_debug 'Error while parsing application configuration on initialization'
|
54
|
+
@helpers.log_debug exception.to_s
|
53
55
|
end
|
54
56
|
@capture_outoing_requests = options['capture_outoing_requests']
|
55
57
|
@capture_outgoing_requests = options['capture_outgoing_requests']
|
56
58
|
if @capture_outoing_requests || @capture_outgoing_requests
|
57
|
-
log_debug 'Start Capturing outgoing requests'
|
59
|
+
@helpers.log_debug 'Start Capturing outgoing requests'
|
58
60
|
require_relative '../../moesif_capture_outgoing/httplog.rb'
|
59
61
|
MoesifCaptureOutgoing.start_capture_outgoing(options)
|
60
62
|
end
|
@@ -92,7 +94,7 @@ module MoesifRack
|
|
92
94
|
return Base64.encode64(body), 'base64'
|
93
95
|
end
|
94
96
|
|
95
|
-
def log_debug(message)
|
97
|
+
def @helpers.log_debug(message)
|
96
98
|
if @debug
|
97
99
|
puts("#{Time.now.to_s} [Moesif Middleware] PID #{Process.pid} TID #{Thread.current.object_id} #{message}")
|
98
100
|
end
|
@@ -100,7 +102,10 @@ module MoesifRack
|
|
100
102
|
|
101
103
|
def parse_body(body, headers)
|
102
104
|
begin
|
103
|
-
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)
|
104
109
|
parsed_body = JSON.parse(body)
|
105
110
|
transfer_encoding = 'json'
|
106
111
|
elsif headers.key?('content-encoding') && ((headers['content-encoding'].downcase).include? "gzip")
|
@@ -125,26 +130,26 @@ module MoesifRack
|
|
125
130
|
until batch_events.size == @batch_size || @events_queue.empty? do
|
126
131
|
batch_events << @events_queue.pop
|
127
132
|
end
|
128
|
-
log_debug("Sending #{batch_events.size.to_s} events to Moesif")
|
133
|
+
@helpers.log_debug("Sending #{batch_events.size.to_s} events to Moesif")
|
129
134
|
event_api_response = @api_controller.create_events_batch(batch_events)
|
130
135
|
@event_response_config_etag = event_api_response[:x_moesif_config_etag]
|
131
|
-
log_debug(event_api_response.to_s)
|
132
|
-
log_debug("Events successfully sent to Moesif")
|
136
|
+
@helpers.log_debug(event_api_response.to_s)
|
137
|
+
@helpers.log_debug("Events successfully sent to Moesif")
|
133
138
|
end
|
134
139
|
|
135
140
|
if @events_queue.empty?
|
136
|
-
log_debug("No events to read from the queue")
|
141
|
+
@helpers.log_debug("No events to read from the queue")
|
137
142
|
end
|
138
143
|
|
139
144
|
sleep @batch_max_time
|
140
145
|
rescue MoesifApi::APIException => e
|
141
146
|
if e.response_code.between?(401, 403)
|
142
147
|
puts "Unathorized accesss sending event to Moesif. Please verify your Application Id."
|
143
|
-
log_debug(e.to_s)
|
148
|
+
@helpers.log_debug(e.to_s)
|
144
149
|
end
|
145
|
-
log_debug("Error sending event to Moesif, with status code #{e.response_code.to_s}")
|
150
|
+
@helpers.log_debug("Error sending event to Moesif, with status code #{e.response_code.to_s}")
|
146
151
|
rescue => e
|
147
|
-
log_debug(e.to_s)
|
152
|
+
@helpers.log_debug(e.to_s)
|
148
153
|
end
|
149
154
|
end
|
150
155
|
end
|
@@ -153,7 +158,7 @@ module MoesifRack
|
|
153
158
|
def call env
|
154
159
|
start_time = Time.now.utc.iso8601
|
155
160
|
|
156
|
-
log_debug('Calling Moesif middleware')
|
161
|
+
@helpers.log_debug('Calling Moesif middleware')
|
157
162
|
|
158
163
|
status, headers, body = @app.call env
|
159
164
|
end_time = Time.now.utc.iso8601
|
@@ -248,67 +253,71 @@ module MoesifRack
|
|
248
253
|
event_model.direction = "Incoming"
|
249
254
|
|
250
255
|
if @identify_user
|
251
|
-
log_debug "calling identify user proc"
|
256
|
+
@helpers.log_debug "calling identify user proc"
|
252
257
|
event_model.user_id = @identify_user.call(env, headers, body)
|
253
258
|
end
|
254
259
|
|
255
260
|
if @identify_company
|
256
|
-
log_debug "calling identify company proc"
|
261
|
+
@helpers.log_debug "calling identify company proc"
|
257
262
|
event_model.company_id = @identify_company.call(env, headers, body)
|
258
263
|
end
|
259
264
|
|
260
265
|
if @get_metadata
|
261
|
-
log_debug "calling get_metadata proc"
|
266
|
+
@helpers.log_debug "calling get_metadata proc"
|
262
267
|
event_model.metadata = @get_metadata.call(env, headers, body)
|
263
268
|
end
|
264
269
|
|
265
270
|
if @identify_session
|
266
|
-
log_debug "calling identify session proc"
|
271
|
+
@helpers.log_debug "calling identify session proc"
|
267
272
|
event_model.session_token = @identify_session.call(env, headers, body)
|
268
273
|
end
|
269
274
|
if @mask_data
|
270
|
-
log_debug "calling mask_data proc"
|
275
|
+
@helpers.log_debug "calling mask_data proc"
|
271
276
|
event_model = @mask_data.call(event_model)
|
272
277
|
end
|
273
278
|
|
274
|
-
log_debug "sending data to moesif"
|
275
|
-
log_debug event_model.to_json
|
279
|
+
@helpers.log_debug "sending data to moesif"
|
280
|
+
@helpers.log_debug event_model.to_json
|
276
281
|
# Perform the API call through the SDK function
|
277
282
|
begin
|
278
|
-
|
283
|
+
random_percentage = Random.rand(0.00..100.00)
|
279
284
|
|
280
285
|
begin
|
281
|
-
|
286
|
+
sampling_percentage = @app_config.get_sampling_percentage(@config, event_model.user_id, event_model.company_id)
|
287
|
+
@helpers.log_debug "Using sample rate #{sampling_percentage}"
|
282
288
|
rescue => exception
|
283
|
-
log_debug 'Error while getting sampling percentage, assuming default behavior'
|
284
|
-
log_debug exception.to_s
|
285
|
-
|
289
|
+
@helpers.log_debug 'Error while getting sampling percentage, assuming default behavior'
|
290
|
+
@helpers.log_debug exception.to_s
|
291
|
+
sampling_percentage = 100
|
286
292
|
end
|
287
293
|
|
288
|
-
if
|
289
|
-
event_model.weight = @app_config.calculate_weight(
|
294
|
+
if sampling_percentage > random_percentage
|
295
|
+
event_model.weight = @app_config.calculate_weight(sampling_percentage)
|
290
296
|
# Add Event to the queue
|
291
297
|
@events_queue << event_model
|
292
|
-
log_debug("Event added to the queue ")
|
298
|
+
@helpers.log_debug("Event added to the queue ")
|
293
299
|
if Time.now.utc > (@last_config_download_time + 60)
|
294
300
|
start_worker()
|
295
301
|
end
|
296
302
|
|
297
303
|
if !@event_response_config_etag.nil? && !@config_etag.nil? && @config_etag != @event_response_config_etag && Time.now.utc > (@last_config_download_time + 300)
|
298
304
|
begin
|
299
|
-
|
300
|
-
|
305
|
+
new_config = @app_config.get_config(@api_controller)
|
306
|
+
if !new_config.nil?
|
307
|
+
@config, @config_etag, @last_config_download_time = @app_config.parse_configuration(new_config)
|
308
|
+
end
|
309
|
+
|
301
310
|
rescue => exception
|
302
|
-
log_debug 'Error while updating the application configuration'
|
303
|
-
log_debug exception.to_s
|
311
|
+
@helpers.log_debug 'Error while updating the application configuration'
|
312
|
+
@helpers.log_debug exception.to_s
|
304
313
|
end
|
305
314
|
end
|
306
315
|
else
|
307
|
-
log_debug("Skipped Event due to sampling percentage: " +
|
316
|
+
@helpers.log_debug("Skipped Event due to sampling percentage: " + sampling_percentage.to_s + " and random percentage: " + random_percentage .to_s)
|
308
317
|
end
|
309
318
|
rescue => exception
|
310
|
-
log_debug "Error adding event to the queue "
|
311
|
-
log_debug exception.to_s
|
319
|
+
@helpers.log_debug "Error adding event to the queue "
|
320
|
+
@helpers.log_debug exception.to_s
|
312
321
|
end
|
313
322
|
|
314
323
|
end
|
@@ -322,7 +331,12 @@ module MoesifRack
|
|
322
331
|
end
|
323
332
|
|
324
333
|
if !should_skip
|
325
|
-
|
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
|
326
340
|
end
|
327
341
|
|
328
342
|
[status, headers, body]
|
@@ -330,6 +344,9 @@ module MoesifRack
|
|
330
344
|
|
331
345
|
def get_response_body(response)
|
332
346
|
body = response.respond_to?(:body) ? response.body : response
|
347
|
+
if (body.instance_of?(Hash) || body.instance_of?(Array))
|
348
|
+
return body
|
349
|
+
end
|
333
350
|
body = body.inject("") { |i, a| i << a } if (body.respond_to?(:each) && body.respond_to?(:inject))
|
334
351
|
body.to_s
|
335
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.8
|
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-03-11 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
|
- - "~>"
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- lib/moesif_rack.rb
|
63
63
|
- lib/moesif_rack/app_config.rb
|
64
64
|
- lib/moesif_rack/client_ip.rb
|
65
|
+
- lib/moesif_rack/helpers.rb
|
65
66
|
- lib/moesif_rack/moesif_middleware.rb
|
66
67
|
- lib/moesif_rack/update_company.rb
|
67
68
|
- lib/moesif_rack/update_user.rb
|
@@ -72,7 +73,14 @@ files:
|
|
72
73
|
homepage: https://moesif.com
|
73
74
|
licenses:
|
74
75
|
- Apache-2.0
|
75
|
-
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
|
76
84
|
post_install_message:
|
77
85
|
rdoc_options: []
|
78
86
|
require_paths:
|
@@ -89,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
97
|
version: '0'
|
90
98
|
requirements: []
|
91
99
|
rubyforge_project:
|
92
|
-
rubygems_version: 2.7.
|
100
|
+
rubygems_version: 2.7.10
|
93
101
|
signing_key:
|
94
102
|
specification_version: 4
|
95
103
|
summary: moesif_rack
|