moesif_rack 1.4.3 → 1.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/moesif_rack/app_config.rb +34 -42
- data/lib/moesif_rack/helpers.rb +14 -0
- data/lib/moesif_rack/moesif_middleware.rb +45 -39
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c44fb915ecec7479c930ab41b3b74eeb58a368f387d3fa8d47211e41c4a0c80
|
4
|
+
data.tar.gz: 6974bb001d57faeffc0f58df1a4c4661ac52d4a6f5280d78c1e35c7deb5fb183
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '08b7997c9a683384cc7725dff3248a41b5ddb15974be2d259d9553fb1903075814260e8df0cda6a7d1e6340b7d8d27d3c99f725f0dd088caaae28476539f162c'
|
7
|
+
data.tar.gz: c0d75f0f8ab0f8d87d4a86fa4396e4100a94d7a0f37190260bbbe3db91ec40dcaa91f4d1d945f768a1fd0483dde2a816eb388a7e9cfd5fd89c4df33881104639
|
@@ -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
|
@@ -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
|
@@ -125,26 +127,26 @@ module MoesifRack
|
|
125
127
|
until batch_events.size == @batch_size || @events_queue.empty? do
|
126
128
|
batch_events << @events_queue.pop
|
127
129
|
end
|
128
|
-
log_debug("Sending #{batch_events.size.to_s} events to Moesif")
|
130
|
+
@helpers.log_debug("Sending #{batch_events.size.to_s} events to Moesif")
|
129
131
|
event_api_response = @api_controller.create_events_batch(batch_events)
|
130
132
|
@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")
|
133
|
+
@helpers.log_debug(event_api_response.to_s)
|
134
|
+
@helpers.log_debug("Events successfully sent to Moesif")
|
133
135
|
end
|
134
136
|
|
135
137
|
if @events_queue.empty?
|
136
|
-
log_debug("No events to read from the queue")
|
138
|
+
@helpers.log_debug("No events to read from the queue")
|
137
139
|
end
|
138
140
|
|
139
141
|
sleep @batch_max_time
|
140
142
|
rescue MoesifApi::APIException => e
|
141
143
|
if e.response_code.between?(401, 403)
|
142
144
|
puts "Unathorized accesss sending event to Moesif. Please verify your Application Id."
|
143
|
-
log_debug(e.to_s)
|
145
|
+
@helpers.log_debug(e.to_s)
|
144
146
|
end
|
145
|
-
log_debug("Error sending event to Moesif, with status code #{e.response_code.to_s}")
|
147
|
+
@helpers.log_debug("Error sending event to Moesif, with status code #{e.response_code.to_s}")
|
146
148
|
rescue => e
|
147
|
-
log_debug(e.to_s)
|
149
|
+
@helpers.log_debug(e.to_s)
|
148
150
|
end
|
149
151
|
end
|
150
152
|
end
|
@@ -153,7 +155,7 @@ module MoesifRack
|
|
153
155
|
def call env
|
154
156
|
start_time = Time.now.utc.iso8601
|
155
157
|
|
156
|
-
log_debug('Calling Moesif middleware')
|
158
|
+
@helpers.log_debug('Calling Moesif middleware')
|
157
159
|
|
158
160
|
status, headers, body = @app.call env
|
159
161
|
end_time = Time.now.utc.iso8601
|
@@ -248,67 +250,71 @@ module MoesifRack
|
|
248
250
|
event_model.direction = "Incoming"
|
249
251
|
|
250
252
|
if @identify_user
|
251
|
-
log_debug "calling identify user proc"
|
253
|
+
@helpers.log_debug "calling identify user proc"
|
252
254
|
event_model.user_id = @identify_user.call(env, headers, body)
|
253
255
|
end
|
254
256
|
|
255
257
|
if @identify_company
|
256
|
-
log_debug "calling identify company proc"
|
258
|
+
@helpers.log_debug "calling identify company proc"
|
257
259
|
event_model.company_id = @identify_company.call(env, headers, body)
|
258
260
|
end
|
259
261
|
|
260
262
|
if @get_metadata
|
261
|
-
log_debug "calling get_metadata proc"
|
263
|
+
@helpers.log_debug "calling get_metadata proc"
|
262
264
|
event_model.metadata = @get_metadata.call(env, headers, body)
|
263
265
|
end
|
264
266
|
|
265
267
|
if @identify_session
|
266
|
-
log_debug "calling identify session proc"
|
268
|
+
@helpers.log_debug "calling identify session proc"
|
267
269
|
event_model.session_token = @identify_session.call(env, headers, body)
|
268
270
|
end
|
269
271
|
if @mask_data
|
270
|
-
log_debug "calling mask_data proc"
|
272
|
+
@helpers.log_debug "calling mask_data proc"
|
271
273
|
event_model = @mask_data.call(event_model)
|
272
274
|
end
|
273
275
|
|
274
|
-
log_debug "sending data to moesif"
|
275
|
-
log_debug event_model.to_json
|
276
|
+
@helpers.log_debug "sending data to moesif"
|
277
|
+
@helpers.log_debug event_model.to_json
|
276
278
|
# Perform the API call through the SDK function
|
277
279
|
begin
|
278
|
-
|
280
|
+
random_percentage = Random.rand(0.00..100.00)
|
279
281
|
|
280
282
|
begin
|
281
|
-
|
283
|
+
sampling_percentage = @app_config.get_sampling_percentage(@config, event_model.user_id, event_model.company_id)
|
284
|
+
@helpers.log_debug "Using sample rate #{sampling_percentage}"
|
282
285
|
rescue => exception
|
283
|
-
log_debug 'Error while getting sampling percentage, assuming default behavior'
|
284
|
-
log_debug exception.to_s
|
285
|
-
|
286
|
+
@helpers.log_debug 'Error while getting sampling percentage, assuming default behavior'
|
287
|
+
@helpers.log_debug exception.to_s
|
288
|
+
sampling_percentage = 100
|
286
289
|
end
|
287
290
|
|
288
|
-
if
|
289
|
-
event_model.weight = @app_config.calculate_weight(
|
291
|
+
if sampling_percentage > random_percentage
|
292
|
+
event_model.weight = @app_config.calculate_weight(sampling_percentage)
|
290
293
|
# Add Event to the queue
|
291
294
|
@events_queue << event_model
|
292
|
-
log_debug("Event added to the queue ")
|
295
|
+
@helpers.log_debug("Event added to the queue ")
|
293
296
|
if Time.now.utc > (@last_config_download_time + 60)
|
294
297
|
start_worker()
|
295
298
|
end
|
296
299
|
|
297
300
|
if !@event_response_config_etag.nil? && !@config_etag.nil? && @config_etag != @event_response_config_etag && Time.now.utc > (@last_config_download_time + 300)
|
298
301
|
begin
|
299
|
-
|
300
|
-
|
302
|
+
new_config = @app_config.get_config(@api_controller)
|
303
|
+
if !new_config.nil?
|
304
|
+
@config, @config_etag, @last_config_download_time = @app_config.parse_configuration(new_config)
|
305
|
+
end
|
306
|
+
|
301
307
|
rescue => exception
|
302
|
-
log_debug 'Error while updating the application configuration'
|
303
|
-
log_debug exception.to_s
|
308
|
+
@helpers.log_debug 'Error while updating the application configuration'
|
309
|
+
@helpers.log_debug exception.to_s
|
304
310
|
end
|
305
311
|
end
|
306
312
|
else
|
307
|
-
log_debug("Skipped Event due to sampling percentage: " +
|
313
|
+
@helpers.log_debug("Skipped Event due to sampling percentage: " + sampling_percentage.to_s + " and random percentage: " + random_percentage .to_s)
|
308
314
|
end
|
309
315
|
rescue => exception
|
310
|
-
log_debug "Error adding event to the queue "
|
311
|
-
log_debug exception.to_s
|
316
|
+
@helpers.log_debug "Error adding event to the queue "
|
317
|
+
@helpers.log_debug exception.to_s
|
312
318
|
end
|
313
319
|
|
314
320
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Moesif, Inc
|
@@ -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
|