moesif_rack 1.4.3 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8bde32f3f60b62bfe1dfd5aef52ba6c56f21db3e2f8ac78743972c227ff1d2b
4
- data.tar.gz: d0fe24cc62aa794dab07b04fa6ce943f85d015db045eb654cd66e9827e9b97fe
3
+ metadata.gz: 8c44fb915ecec7479c930ab41b3b74eeb58a368f387d3fa8d47211e41c4a0c80
4
+ data.tar.gz: 6974bb001d57faeffc0f58df1a4c4661ac52d4a6f5280d78c1e35c7deb5fb183
5
5
  SHA512:
6
- metadata.gz: 0db637f28ba8d87d7e5e8a81b9642eeeb64f2c701d862c941e90ac0bdc5a2d15584f597df82420b736020dcd8f426c87e1e727cb9dcf081214e9aeecc07fdcd9
7
- data.tar.gz: 134f36829793337b6d78a7a51dedddeadd2d4e2fc1353e5b0ae5962f7ddb72f08562c5ff9f5f93248b7ee80db14a987f4935c58eb152b0229bfae9f7ea9e128c
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 get_config(api_controller, debug)
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
- puts 'Unauthorized access getting application configuration. Please check your Appplication Id.'
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
- if debug
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, debug)
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, debug)
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], response_body.fetch("sample_rate", 100), Time.now.utc
44
+ return response_body, config_api_response.headers[:x_moesif_config_etag], Time.now.utc
40
45
  else
41
- if debug
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
- if debug
49
- puts 'Error while parsing the configuration object, assuming default behavior'
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, debug)
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 !response_body.nil? then
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 = response_body.fetch('user_sample_rate', nil)
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 = response_body.fetch('company_sample_rate', nil)
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 response_body.fetch('sample_rate', 100)
83
+ return config_api_response.fetch('sample_rate', 100)
84
84
  else
85
- if debug
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
- if debug
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, debug)
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
- if debug
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
- if debug
120
- puts 'Error while decompressing the response body'
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
@@ -0,0 +1,14 @@
1
+ require 'time'
2
+
3
+ class Helpers
4
+
5
+ def initialize debug
6
+ @debug = debug
7
+ end
8
+
9
+ def log_debug(message)
10
+ if @debug
11
+ puts("#{Time.now.to_s} [Moesif Middleware] PID #{Process.pid} TID #{Thread.current.object_id} #{message}")
12
+ end
13
+ end
14
+ 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
- @config = @app_config.get_config(@api_controller, @debug)
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
- if !@config.nil?
48
- @config_etag, @sampling_percentage, @last_config_download_time = @app_config.parse_configuration(@config, @debug)
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
- @random_percentage = Random.rand(0.00..100.00)
280
+ random_percentage = Random.rand(0.00..100.00)
279
281
 
280
282
  begin
281
- @sampling_percentage = @app_config.get_sampling_percentage(@config, event_model.user_id, event_model.company_id, @debug)
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
- @sampling_percentage = 100
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 @sampling_percentage > @random_percentage
289
- event_model.weight = @app_config.calculate_weight(@sampling_percentage)
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
- @config = @app_config.get_config(@api_controller, @debug)
300
- @config_etag, @sampling_percentage, @last_config_download_time = @app_config.parse_configuration(@config, @debug)
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: " + @sampling_percentage.to_s + " and random percentage: " + @random_percentage.to_s)
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.3
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