moesif_rack 1.4.17 → 1.4.19

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b73acd14bb1af6f2d4ee8225fdd39ee19c6d1d9cf937ad37a9b10a7278842272
4
- data.tar.gz: c1e74c3f4afdbdbfe0cef5fc75dca534f8d289c0f0befdac9085b9f48d4575bf
3
+ metadata.gz: 9648d28da08853893881cd32fef5fcf61d4f8d2c6a573d06ced10bb4e01bda3c
4
+ data.tar.gz: cebdf8b64372dd77a80520e8cbec1da161f623a6435a8ced10a700aa86e57113
5
5
  SHA512:
6
- metadata.gz: 186263e17ebe464783f65297d205c6fea8dcde346ecf979bc1c79c0fd2468e31f474ab278ca94ff2664f6ebd5b9b5a8bc991f00c7f82532ccf709091d6db543e
7
- data.tar.gz: e7c29d009f3dc63464181025aa2679f45ee1b3972c72402934e43fa4fef0e778da300084d03b524f689c1b2dda81f3d16a351deb234c68d06cd01f21bd847111
6
+ metadata.gz: 35bb379b52dc6f4323d5dc98154c5ba741e438ec1cf98030f06775405c405e4a34871b9435a9a3f89d0b32e6545c2f6ec88083e59d2c4e97c271e765200e89bc
7
+ data.tar.gz: 72422bb99d80d78e91af4034ff4dd31f7f94c8424a498819a6767ce066045209e4dc8c1e9784a88377cf100e79fad3cb0733989f0869b449dedf60e91b916f60
data/README.md CHANGED
@@ -255,8 +255,17 @@ Optional. Boolean. Default false. If true, it will print out debug messages. In
255
255
 
256
256
  Optional. Boolean. Default true. If false, will not log request and response body to Moesif.
257
257
 
258
+ #### __`batch_size`__
259
+ Optional. int, default 200, Maximum batch size when sending to Moesif.
260
+
261
+ #### __`batch_max_time`__
262
+ Optional. int in seconds Default 2. This is the maximum wait time (approximately) before triggering flushing of the queue and sending to Moesif.
263
+
264
+ #### __`event_queue_size`__
265
+ Optional. int, Default 1000, Maximum number of events to hold in queue before sending to Moesif. In case of network issues when not able to connect/send event to Moesif, skips adding new to event to queue to prevent memory overflow.
266
+
258
267
  #### __`capture_outgoing_requests`__
259
- Optional. boolean, Default `false`. Set to `true` to capture all outgoing API calls from your app to third parties like Stripe, Github or to your own dependencies while using [Net::HTTP](https://ruby-doc.org/stdlib-2.6.3/libdoc/net/http/rdoc/Net/HTTP.html) package. The options below is applied to outgoing API calls. When the request is outgoing, for options functions that take request and response as input arguments, the request and response objects passed in are [Request](https://www.rubydoc.info/stdlib/net/Net/HTTPRequest) request and [Response](https://www.rubydoc.info/stdlib/net/Net/HTTPResponse) response objects.
268
+ Optional. Boolean, Default `false`. Set to `true` to capture all outgoing API calls from your app to third parties like Stripe, Github or to your own dependencies while using [Net::HTTP](https://ruby-doc.org/stdlib-2.6.3/libdoc/net/http/rdoc/Net/HTTP.html) package. The options below is applied to outgoing API calls. When the request is outgoing, for options functions that take request and response as input arguments, the request and response objects passed in are [Request](https://www.rubydoc.info/stdlib/net/Net/HTTPRequest) request and [Response](https://www.rubydoc.info/stdlib/net/Net/HTTPResponse) response objects.
260
269
 
261
270
 
262
271
  ##### __`identify_user_outgoing`__
@@ -4,6 +4,7 @@ require 'time'
4
4
  require 'base64'
5
5
  require 'zlib'
6
6
  require 'stringio'
7
+ require 'rack'
7
8
  require_relative './client_ip.rb'
8
9
  require_relative './app_config.rb'
9
10
  require_relative './update_user.rb'
@@ -34,14 +35,17 @@ module MoesifRack
34
35
  @config = @app_config.get_config(@api_controller)
35
36
  @config_etag = nil
36
37
  @last_config_download_time = Time.now.utc
37
- @last_worker_run = Time.now.utc
38
38
  @config_dict = Hash.new
39
39
  @disable_transaction_id = options['disable_transaction_id'] || false
40
40
  @log_body = options.fetch('log_body', true)
41
- @batch_size = options['batch_size'] || 25
41
+ @batch_size = options['batch_size'] || 200
42
+ @event_queue_size = options['event_queue_size'] || 1000
42
43
  @batch_max_time = options['batch_max_time'] || 2
43
44
  @events_queue = Queue.new
44
45
  @event_response_config_etag = nil
46
+
47
+ # start the worker and Update the last worker run
48
+ @last_worker_run = Time.now.utc
45
49
  start_worker()
46
50
 
47
51
  begin
@@ -100,6 +104,24 @@ module MoesifRack
100
104
  end
101
105
  end
102
106
 
107
+ def parse_multipart(multipart_form_data, content_type)
108
+ @moesif_helpers.log_debug("try to parse multiple part #{content_type}")
109
+
110
+ sanitized_multipart_form_data = multipart_form_data.gsub(/\r?\n/, "\r\n")
111
+
112
+ io = StringIO.new(sanitized_multipart_form_data)
113
+ tempfile = Rack::Multipart::Parser::TEMPFILE_FACTORY
114
+ bufsize = Rack::Multipart::Parser::BUFSIZE
115
+ query_parser = Rack::Utils.default_query_parser
116
+ result = Rack::Multipart::Parser.parse(io, sanitized_multipart_form_data.length, content_type, tempfile, bufsize, query_parser)
117
+
118
+ @moesif_helpers.log_debug("multipart parse result")
119
+ @moesif_helpers.log_debug(result.inspect)
120
+
121
+ # this is a hash shold be treated as JSON down the road.
122
+ result.params
123
+ end
124
+
103
125
  def parse_body(body, headers)
104
126
  begin
105
127
  if (body.instance_of?(Hash) || body.instance_of?(Array))
@@ -108,6 +130,9 @@ module MoesifRack
108
130
  elsif start_with_json(body)
109
131
  parsed_body = JSON.parse(body)
110
132
  transfer_encoding = 'json'
133
+ elsif headers.key?('content-type') && ((headers['content-type'].downcase).include? 'multipart/form-data')
134
+ parsed_body = parse_multipart(body, headers['content-type'])
135
+ transfer_encoding = 'json'
111
136
  elsif headers.key?('content-encoding') && ((headers['content-encoding'].downcase).include? "gzip")
112
137
  uncompressed_string = decompress_body(body)
113
138
  parsed_body, transfer_encoding = base64_encode_body(uncompressed_string)
@@ -122,25 +147,29 @@ module MoesifRack
122
147
 
123
148
  def start_worker
124
149
  Thread::new do
125
- @last_worker_run = Time.now.utc
126
150
  loop do
151
+ # Update the last worker run, in case the events_queue is empty
152
+ @last_worker_run = Time.now.utc
127
153
  begin
128
154
  until @events_queue.empty? do
155
+ # Update the last worker run in case sending events take more than 60 seconds
156
+ @last_worker_run = Time.now.utc
157
+ # Populate the batch events from queue
129
158
  batch_events = []
130
- until batch_events.size == @batch_size || @events_queue.empty? do
159
+ until batch_events.size == @batch_size || @events_queue.empty? do
131
160
  batch_events << @events_queue.pop
132
- end
161
+ end
133
162
  @moesif_helpers.log_debug("Sending #{batch_events.size.to_s} events to Moesif")
134
163
  event_api_response = @api_controller.create_events_batch(batch_events)
135
164
  @event_response_config_etag = event_api_response[:x_moesif_config_etag]
136
165
  @moesif_helpers.log_debug(event_api_response.to_s)
137
166
  @moesif_helpers.log_debug("Events successfully sent to Moesif")
138
167
  end
139
-
168
+
140
169
  if @events_queue.empty?
141
170
  @moesif_helpers.log_debug("No events to read from the queue")
142
171
  end
143
-
172
+
144
173
  sleep @batch_max_time
145
174
  rescue MoesifApi::APIException => e
146
175
  if e.response_code.between?(401, 403)
@@ -177,6 +206,8 @@ module MoesifRack
177
206
  req_headers[new_key] = val
178
207
  end
179
208
 
209
+ # rewind first in case someone else already read the body
210
+ req.body.rewind
180
211
  req_body_string = req.body.read
181
212
  req.body.rewind
182
213
  req_body_transfer_encoding = nil
@@ -229,12 +260,12 @@ module MoesifRack
229
260
  end
230
261
 
231
262
  # Add Transaction Id to the Response Header
232
- if !transaction_id.nil?
263
+ if !transaction_id.nil?
233
264
  rsp_headers["X-Moesif-Transaction-Id"] = transaction_id
234
265
  end
235
266
 
236
267
  # Add Transaction Id to the Repsonse Header sent to the client
237
- if !transaction_id.nil?
268
+ if !transaction_id.nil?
238
269
  headers["X-Moesif-Transaction-Id"] = transaction_id
239
270
  end
240
271
 
@@ -254,7 +285,7 @@ module MoesifRack
254
285
  event_model.request = event_req
255
286
  event_model.response = event_rsp
256
287
  event_model.direction = "Incoming"
257
-
288
+
258
289
  if @identify_user
259
290
  @moesif_helpers.log_debug "calling identify user proc"
260
291
  event_model.user_id = @identify_user.call(env, headers, body)
@@ -285,7 +316,7 @@ module MoesifRack
285
316
  begin
286
317
  random_percentage = Random.rand(0.00..100.00)
287
318
 
288
- begin
319
+ begin
289
320
  sampling_percentage = @app_config.get_sampling_percentage(event_model, @config, event_model.user_id, event_model.company_id)
290
321
  @moesif_helpers.log_debug "Using sample rate #{sampling_percentage}"
291
322
  rescue => exception
@@ -294,17 +325,22 @@ module MoesifRack
294
325
  sampling_percentage = 100
295
326
  end
296
327
 
297
- if sampling_percentage > random_percentage
328
+ if sampling_percentage > random_percentage
298
329
  event_model.weight = @app_config.calculate_weight(sampling_percentage)
299
330
  # Add Event to the queue
300
- @events_queue << event_model
301
- @moesif_helpers.log_debug("Event added to the queue ")
331
+ if @events_queue.size >= @event_queue_size
332
+ @moesif_helpers.log_debug("Skipped Event due to events_queue size [#{@events_queue.size}] is over max #{@event_queue_size} ")
333
+ else
334
+ @events_queue << event_model
335
+ @moesif_helpers.log_debug("Event added to the queue ")
336
+ end
337
+
302
338
  if Time.now.utc > (@last_worker_run + 60)
303
339
  start_worker()
304
340
  end
305
341
 
306
342
  if !@event_response_config_etag.nil? && !@config_etag.nil? && @config_etag != @event_response_config_etag && Time.now.utc > (@last_config_download_time + 300)
307
- begin
343
+ begin
308
344
  new_config = @app_config.get_config(@api_controller)
309
345
  if !new_config.nil?
310
346
  @config, @config_etag, @last_config_download_time = @app_config.parse_configuration(new_config)
@@ -334,7 +370,7 @@ module MoesifRack
334
370
  end
335
371
 
336
372
  if !should_skip
337
- begin
373
+ begin
338
374
  process_send.call
339
375
  rescue => exception
340
376
  @moesif_helpers.log_debug 'Error while logging event - '
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.17
4
+ version: 1.4.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moesif, Inc
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-08-23 00:00:00.000000000 Z
12
+ date: 2023-02-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: test-unit