moesif_rack 1.4.2 → 1.4.3

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: 9d8635baa7838edc611616b3fcd19d30c6a8c9034f985b1a48df744eae3fc252
4
- data.tar.gz: bf138ca57419057dafdc088f076e3b632a623cd5d268012e6e67a9ce60372ea9
3
+ metadata.gz: e8bde32f3f60b62bfe1dfd5aef52ba6c56f21db3e2f8ac78743972c227ff1d2b
4
+ data.tar.gz: d0fe24cc62aa794dab07b04fa6ce943f85d015db045eb654cd66e9827e9b97fe
5
5
  SHA512:
6
- metadata.gz: 25e43ffa1f612ed39c3ddcee9a55b29ff8f81ed0f9e7d36651d605232aec4ef3ba55cf2e072bd99e72ce46fccc565591872bb58ddd982d65da86a7305f0f4784
7
- data.tar.gz: '095a38e7f8dc9e4ba2fbf9ffd40dfd2ac01fcd72b5475915266274a807c53b70054f823109648e73319557efc07c61ddfb95f99a5d4586855b3db1f0fed7573a'
6
+ metadata.gz: 0db637f28ba8d87d7e5e8a81b9642eeeb64f2c701d862c941e90ac0bdc5a2d15584f597df82420b736020dcd8f426c87e1e727cb9dcf081214e9aeecc07fdcd9
7
+ data.tar.gz: 134f36829793337b6d78a7a51dedddeadd2d4e2fc1353e5b0ae5962f7ddb72f08562c5ff9f5f93248b7ee80db14a987f4935c58eb152b0229bfae9f7ea9e128c
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2019 Moesif, Inc
1
+ Copyright (c) 2020 Moesif, Inc
2
2
 
3
3
  Licensed under the Apache License, Version 2.0 (the "License");
4
4
  you may not use this file except in compliance with the License.
@@ -19,7 +19,12 @@ class AppConfig
19
19
  puts 'Error getting application configuration, with status code:'
20
20
  puts e.response_code
21
21
  end
22
+ rescue => e
23
+ if debug
24
+ puts e.to_s
25
+ end
22
26
  end
27
+ rescue
23
28
  end
24
29
 
25
30
  def parse_configuration(config_api_response, debug)
@@ -1,8 +1,8 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){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*$/
6
6
  # We use !! to convert the return value to a boolean
7
7
  !!(value =~ ipv4 or value=~ ipv6)
8
8
  end
@@ -32,65 +32,29 @@ module MoesifRack
32
32
  @config = @app_config.get_config(@api_controller, @debug)
33
33
  @config_etag = nil
34
34
  @sampling_percentage = 100
35
- @last_updated_time = Time.now.utc
35
+ @last_config_download_time = Time.now.utc
36
+ @last_worker_run = Time.now.utc
36
37
  @config_dict = Hash.new
37
38
  @disable_transaction_id = options['disable_transaction_id'] || false
38
39
  @log_body = options.fetch('log_body', true)
39
40
  @batch_size = options['batch_size'] || 25
41
+ @batch_max_time = options['batch_max_time'] || 2
40
42
  @events_queue = Queue.new
41
43
  @event_response_config_etag = nil
42
-
43
- Thread::new do
44
- loop do
45
- begin
46
- until @events_queue.empty? do
47
- batch_events = []
48
- until batch_events.size == @batch_size || @events_queue.empty? do
49
- batch_events << @events_queue.pop
50
- end
51
- event_api_response = @api_controller.create_events_batch(batch_events)
52
- @event_response_config_etag = event_api_response[:x_moesif_config_etag]
53
- if @debug
54
- puts("Events successfully sent to Moesif")
55
- end
56
- end
57
-
58
- if @events_queue.empty?
59
- if @debug
60
- puts("No events to read from the queue")
61
- end
62
- end
63
-
64
- # Sleep for 5 seconds
65
- sleep 5
66
- rescue MoesifApi::APIException => e
67
- if e.response_code.between?(401, 403)
68
- puts "Unathorized accesss sending event to Moesif. Please verify your Application Id."
69
- end
70
- if @debug
71
- puts "Error sending event to Moesif, with status code: "
72
- puts e.response_code
73
- end
74
- end
75
- end
76
- end
44
+ start_worker()
77
45
 
78
46
  begin
79
47
  if !@config.nil?
80
- @config_etag, @sampling_percentage, @last_updated_time = @app_config.parse_configuration(@config, @debug)
48
+ @config_etag, @sampling_percentage, @last_config_download_time = @app_config.parse_configuration(@config, @debug)
81
49
  end
82
50
  rescue => exception
83
- if @debug
84
- puts 'Error while parsing application configuration on initialization'
85
- puts exception.to_s
86
- end
51
+ log_debug 'Error while parsing application configuration on initialization'
52
+ log_debug exception.to_s
87
53
  end
88
54
  @capture_outoing_requests = options['capture_outoing_requests']
89
55
  @capture_outgoing_requests = options['capture_outgoing_requests']
90
56
  if @capture_outoing_requests || @capture_outgoing_requests
91
- if @debug
92
- puts 'Start Capturing outgoing requests'
93
- end
57
+ log_debug 'Start Capturing outgoing requests'
94
58
  require_relative '../../moesif_capture_outgoing/httplog.rb'
95
59
  MoesifCaptureOutgoing.start_capture_outgoing(options)
96
60
  end
@@ -128,6 +92,12 @@ module MoesifRack
128
92
  return Base64.encode64(body), 'base64'
129
93
  end
130
94
 
95
+ def log_debug(message)
96
+ if @debug
97
+ puts("#{Time.now.to_s} [Moesif Middleware] PID #{Process.pid} TID #{Thread.current.object_id} #{message}")
98
+ end
99
+ end
100
+
131
101
  def parse_body(body, headers)
132
102
  begin
133
103
  if start_with_json(body)
@@ -145,12 +115,45 @@ module MoesifRack
145
115
  return parsed_body, transfer_encoding
146
116
  end
147
117
 
118
+ def start_worker
119
+ Thread::new do
120
+ @last_worker_run = Time.now.utc
121
+ loop do
122
+ begin
123
+ until @events_queue.empty? do
124
+ batch_events = []
125
+ until batch_events.size == @batch_size || @events_queue.empty? do
126
+ batch_events << @events_queue.pop
127
+ end
128
+ log_debug("Sending #{batch_events.size.to_s} events to Moesif")
129
+ event_api_response = @api_controller.create_events_batch(batch_events)
130
+ @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
+ end
134
+
135
+ if @events_queue.empty?
136
+ log_debug("No events to read from the queue")
137
+ end
138
+
139
+ sleep @batch_max_time
140
+ rescue MoesifApi::APIException => e
141
+ if e.response_code.between?(401, 403)
142
+ puts "Unathorized accesss sending event to Moesif. Please verify your Application Id."
143
+ log_debug(e.to_s)
144
+ end
145
+ log_debug("Error sending event to Moesif, with status code #{e.response_code.to_s}")
146
+ rescue => e
147
+ log_debug(e.to_s)
148
+ end
149
+ end
150
+ end
151
+ end
152
+
148
153
  def call env
149
154
  start_time = Time.now.utc.iso8601
150
155
 
151
- if @debug
152
- puts 'inside moesif middleware'
153
- end
156
+ log_debug('Calling Moesif middleware')
154
157
 
155
158
  status, headers, body = @app.call env
156
159
  end_time = Time.now.utc.iso8601
@@ -245,43 +248,31 @@ module MoesifRack
245
248
  event_model.direction = "Incoming"
246
249
 
247
250
  if @identify_user
248
- if @debug
249
- puts "calling identify user proc"
250
- end
251
+ log_debug "calling identify user proc"
251
252
  event_model.user_id = @identify_user.call(env, headers, body)
252
253
  end
253
254
 
254
255
  if @identify_company
255
- if @debug
256
- puts "calling identify company proc"
257
- end
256
+ log_debug "calling identify company proc"
258
257
  event_model.company_id = @identify_company.call(env, headers, body)
259
258
  end
260
259
 
261
260
  if @get_metadata
262
- if @debug
263
- puts "calling get_metadata proc"
264
- end
261
+ log_debug "calling get_metadata proc"
265
262
  event_model.metadata = @get_metadata.call(env, headers, body)
266
263
  end
267
264
 
268
265
  if @identify_session
269
- if @debug
270
- puts "calling identify session proc"
271
- end
266
+ log_debug "calling identify session proc"
272
267
  event_model.session_token = @identify_session.call(env, headers, body)
273
268
  end
274
269
  if @mask_data
275
- if @debug
276
- puts "calling mask_data proc"
277
- end
270
+ log_debug "calling mask_data proc"
278
271
  event_model = @mask_data.call(event_model)
279
272
  end
280
273
 
281
- if @debug
282
- puts "sending data to moesif"
283
- puts event_model.to_json
284
- end
274
+ log_debug "sending data to moesif"
275
+ log_debug event_model.to_json
285
276
  # Perform the API call through the SDK function
286
277
  begin
287
278
  @random_percentage = Random.rand(0.00..100.00)
@@ -289,10 +280,8 @@ module MoesifRack
289
280
  begin
290
281
  @sampling_percentage = @app_config.get_sampling_percentage(@config, event_model.user_id, event_model.company_id, @debug)
291
282
  rescue => exception
292
- if @debug
293
- puts 'Error while getting sampling percentage, assuming default behavior'
294
- puts exception.to_s
295
- end
283
+ log_debug 'Error while getting sampling percentage, assuming default behavior'
284
+ log_debug exception.to_s
296
285
  @sampling_percentage = 100
297
286
  end
298
287
 
@@ -300,31 +289,26 @@ module MoesifRack
300
289
  event_model.weight = @app_config.calculate_weight(@sampling_percentage)
301
290
  # Add Event to the queue
302
291
  @events_queue << event_model
303
- if @debug
304
- puts("Event added to the queue ")
292
+ log_debug("Event added to the queue ")
293
+ if Time.now.utc > (@last_config_download_time + 60)
294
+ start_worker()
305
295
  end
306
296
 
307
- if !@event_response_config_etag.nil? && !@config_etag.nil? && @config_etag != @event_response_config_etag && Time.now.utc > @last_updated_time + 300
297
+ if !@event_response_config_etag.nil? && !@config_etag.nil? && @config_etag != @event_response_config_etag && Time.now.utc > (@last_config_download_time + 300)
308
298
  begin
309
299
  @config = @app_config.get_config(@api_controller, @debug)
310
- @config_etag, @sampling_percentage, @last_updated_time = @app_config.parse_configuration(@config, @debug)
300
+ @config_etag, @sampling_percentage, @last_config_download_time = @app_config.parse_configuration(@config, @debug)
311
301
  rescue => exception
312
- if @debug
313
- puts 'Error while updating the application configuration'
314
- puts exception.to_s
315
- end
302
+ log_debug 'Error while updating the application configuration'
303
+ log_debug exception.to_s
316
304
  end
317
305
  end
318
306
  else
319
- if @debug
320
- puts("Skipped Event due to sampling percentage: " + @sampling_percentage.to_s + " and random percentage: " + @random_percentage.to_s)
321
- end
307
+ log_debug("Skipped Event due to sampling percentage: " + @sampling_percentage.to_s + " and random percentage: " + @random_percentage.to_s)
322
308
  end
323
309
  rescue => exception
324
- if @debug
325
- puts "Error adding event to the queue "
326
- puts exception.to_s
327
- end
310
+ log_debug "Error adding event to the queue "
311
+ log_debug exception.to_s
328
312
  end
329
313
 
330
314
  end
@@ -338,11 +322,7 @@ module MoesifRack
338
322
  end
339
323
 
340
324
  if !should_skip
341
- if @debug
342
- process_send.call
343
- else
344
- Thread.start(&process_send)
345
- end
325
+ process_send.call
346
326
  end
347
327
 
348
328
  [status, headers, body]
@@ -221,6 +221,10 @@ module MoesifCaptureOutgoing
221
221
  puts "Error sending event to Moesif, with status code: "
222
222
  puts e.response_code
223
223
  end
224
+ rescue => e
225
+ if debug
226
+ puts e.to_s
227
+ end
224
228
  end
225
229
  else
226
230
  if @debug
metadata CHANGED
@@ -1,28 +1,34 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moesif_rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moesif, Inc
8
8
  - Xing Wang
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-05-28 00:00:00.000000000 Z
12
+ date: 2020-06-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: test-unit
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
18
  - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.1'
21
+ - - ">="
19
22
  - !ruby/object:Gem::Version
20
23
  version: 3.1.5
21
- type: :runtime
24
+ type: :development
22
25
  prerelease: false
23
26
  version_requirements: !ruby/object:Gem::Requirement
24
27
  requirements:
25
28
  - - "~>"
29
+ - !ruby/object:Gem::Version
30
+ version: '3.1'
31
+ - - ">="
26
32
  - !ruby/object:Gem::Version
27
33
  version: 3.1.5
28
34
  - !ruby/object:Gem::Dependency
@@ -30,6 +36,9 @@ dependencies:
30
36
  requirement: !ruby/object:Gem::Requirement
31
37
  requirements:
32
38
  - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - - ">="
33
42
  - !ruby/object:Gem::Version
34
43
  version: 1.2.12
35
44
  type: :runtime
@@ -37,9 +46,12 @@ dependencies:
37
46
  version_requirements: !ruby/object:Gem::Requirement
38
47
  requirements:
39
48
  - - "~>"
49
+ - !ruby/object:Gem::Version
50
+ version: '1.2'
51
+ - - ">="
40
52
  - !ruby/object:Gem::Version
41
53
  version: 1.2.12
42
- description: Collection/Data Ingestion SDK for Rack (also Rails) Middleware / RoR
54
+ description: Rack/Rails middleware to log API calls to Moesif API analytics and monitoring
43
55
  email: xing@moesif.com
44
56
  executables: []
45
57
  extensions: []
@@ -61,7 +73,7 @@ homepage: https://moesif.com
61
73
  licenses:
62
74
  - Apache-2.0
63
75
  metadata: {}
64
- post_install_message:
76
+ post_install_message:
65
77
  rdoc_options: []
66
78
  require_paths:
67
79
  - lib
@@ -76,8 +88,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
88
  - !ruby/object:Gem::Version
77
89
  version: '0'
78
90
  requirements: []
79
- rubygems_version: 3.0.4
80
- signing_key:
91
+ rubyforge_project:
92
+ rubygems_version: 2.7.7
93
+ signing_key:
81
94
  specification_version: 4
82
95
  summary: moesif_rack
83
96
  test_files: []