moesif_rack 1.3.5 → 1.3.6

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: c829d3004e26c40536debc186d0c45a8a3cf08d854c75f0cf76dce20d142e951
4
- data.tar.gz: ac8c4d6c2c66dae5348b7421128fdd6b5ce0b2c9a48e1712aa632817f2ade291
3
+ metadata.gz: f40e229ac134210c6703c176f6ce81a6de3c1fd483f5430c88ebb93dd60ad67e
4
+ data.tar.gz: c7afe14c3db3328dfe93fa824a245d2bea5ac88e89e48bb29735e6a0696f14e0
5
5
  SHA512:
6
- metadata.gz: 7f10f8c2e8c4cb8af3d55a4d5aee69626ddb893405ba866578af506834516848b5367f5361a07ca5eb6acb3adccae2a8429432d8792cfb197391b42db310dab6
7
- data.tar.gz: 3ee6984671ab33f20dcf4b6b640935e315cc84c68e39bb9093b77a9e632aa3683e0e213d197140baa93f76f14809e0d19f701710900f0edc1e9575182121c27f
6
+ metadata.gz: ba2af16c43d3f9887b013edd3cfedac7cc032d713b51cc32c6250dfad367df96d7c1b848563aaf1889ce01ec2eb8d6b952ff08f12f40ebb210205570a7a59ace
7
+ data.tar.gz: 36acfefacd5edc2810185aa3b6096397e0ea2e0ee2b3003f7c3c9b99897d245be36dad84e45b35c3feded80fff6032d9704cc7edd2499a609354ded37e323ff5
@@ -0,0 +1,121 @@
1
+ require 'moesif_api'
2
+ require 'json'
3
+ require 'time'
4
+ require 'zlib'
5
+ require 'stringio'
6
+
7
+ class AppConfig
8
+
9
+ def get_config(api_controller, debug)
10
+ # Get Application Config
11
+ begin
12
+ config_api_response = api_controller.get_app_config()
13
+ return config_api_response
14
+ rescue MoesifApi::APIException => e
15
+ 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
21
+ end
22
+ end
23
+ end
24
+
25
+ def parse_configuration(config_api_response, debug)
26
+ # Parse configuration object and return Etag, sample rate and last updated time
27
+ begin
28
+ # Rails return gzipped compressed response body, so decompressing it and getting JSON response body
29
+ response_body = decompress_gzip_body(config_api_response, debug)
30
+
31
+ # Check if response body is not nil
32
+ if !response_body.nil? then
33
+ # Return Etag, sample rate and last updated time
34
+ return config_api_response.headers[:x_moesif_config_etag], response_body.fetch("sample_rate", 100), Time.now.utc
35
+ else
36
+ if debug
37
+ puts 'Response body is nil, assuming default behavior'
38
+ end
39
+ # Response body is nil, so assuming default behavior
40
+ return nil, 100, Time.now.utc
41
+ end
42
+ rescue => exception
43
+ if debug
44
+ puts 'Error while parsing the configuration object, assuming default behavior'
45
+ puts exception.to_s
46
+ end
47
+ # Assuming default behavior
48
+ return nil, 100, Time.now.utc
49
+ end
50
+ end
51
+
52
+ def get_sampling_percentage(config_api_response, user_id, company_id, debug)
53
+ # Get sampling percentage
54
+ begin
55
+ # Rails return gzipped compressed response body, so decompressing it and getting JSON response body
56
+ response_body = decompress_gzip_body(config_api_response, debug)
57
+
58
+ # Check if response body is not nil
59
+ if !response_body.nil? then
60
+
61
+ # Get user sample rate object
62
+ user_sample_rate = response_body.fetch('user_sample_rate', nil)
63
+
64
+ # Get company sample rate object
65
+ company_sample_rate = response_body.fetch('company_sample_rate', nil)
66
+
67
+ # Get sample rate for the user if exist
68
+ if !user_id.nil? && !user_sample_rate.nil? && user_sample_rate.key?(user_id)
69
+ return user_sample_rate.fetch(user_id)
70
+ end
71
+
72
+ # Get sample rate for the company if exist
73
+ if !company_id.nil? && !company_sample_rate.nil? && company_sample_rate.key?(company_id)
74
+ return company_sample_rate.fetch(company_id)
75
+ end
76
+
77
+ # Return sample rate
78
+ return response_body.fetch('sample_rate', 100)
79
+ else
80
+ if debug
81
+ puts 'Assuming default behavior as response body is nil - '
82
+ end
83
+ return 100
84
+ end
85
+ rescue => exception
86
+ if debug
87
+ puts 'Error while geting sampling percentage, assuming default behavior'
88
+ end
89
+ return 100
90
+ end
91
+ end
92
+
93
+ def decompress_gzip_body(config_api_response, debug)
94
+ # Decompress gzip response body
95
+ begin
96
+ # Check if the content-encoding header exist and is of type zip
97
+ if config_api_response.headers.key?(:content_encoding) && config_api_response.headers[:content_encoding].eql?( 'gzip' ) then
98
+
99
+ # Create a GZipReader object to read data
100
+ gzip_reader = Zlib::GzipReader.new(StringIO.new(config_api_response.raw_body.to_s))
101
+
102
+ # Read the body
103
+ uncompressed_string = gzip_reader.read
104
+
105
+ # Return the parsed body
106
+ return JSON.parse( uncompressed_string )
107
+ else
108
+ if debug
109
+ puts 'Content Encoding is of type other than gzip, returning nil'
110
+ end
111
+ return nil
112
+ end
113
+ rescue => exception
114
+ if debug
115
+ puts 'Error while decompressing the response body'
116
+ puts exception.to_s
117
+ end
118
+ return nil
119
+ end
120
+ end
121
+ end
@@ -3,6 +3,7 @@ require 'json'
3
3
  require 'time'
4
4
  require 'base64'
5
5
  require_relative './client_ip.rb'
6
+ require_relative './app_config.rb'
6
7
  require_relative './update_user.rb'
7
8
  require_relative './update_company.rb'
8
9
 
@@ -12,7 +13,7 @@ module MoesifRack
12
13
  def initialize app, options = {}
13
14
  @app = app
14
15
  if not options['application_id']
15
- raise 'application_id requird for Moesif Middleware'
16
+ raise 'application_id required for Moesif Middleware'
16
17
  end
17
18
  @api_client = MoesifApi::MoesifAPIClient.new(options['application_id'])
18
19
  @api_controller = @api_client.api
@@ -25,12 +26,23 @@ module MoesifRack
25
26
  @mask_data = options['mask_data']
26
27
  @skip = options['skip']
27
28
  @debug = options['debug']
29
+ @app_config = AppConfig.new
30
+ @config = @app_config.get_config(@api_controller, @debug)
31
+ @config_etag = nil
32
+ @sampling_percentage = 100
33
+ @last_updated_time = Time.now.utc
28
34
  @config_dict = Hash.new
29
35
  @disable_transaction_id = options['disable_transaction_id'] || false
30
36
  @log_body = options.fetch('log_body', true)
31
- @sampling_percentage = get_config(nil)
32
- if not @sampling_percentage.is_a? Numeric
33
- raise "Sampling Percentage should be a number"
37
+ begin
38
+ if !@config.nil?
39
+ @config_etag, @sampling_percentage, @last_updated_time = @app_config.parse_configuration(@config, @debug)
40
+ end
41
+ rescue => exception
42
+ if @debug
43
+ puts 'Error while parsing application configuration on initialization'
44
+ puts exception.to_s
45
+ end
34
46
  end
35
47
  @capture_outoing_requests = options['capture_outoing_requests']
36
48
  if @capture_outoing_requests
@@ -42,35 +54,6 @@ module MoesifRack
42
54
  end
43
55
  end
44
56
 
45
- def get_config(cached_config_etag)
46
- sample_rate = 100
47
- begin
48
- # Calling the api
49
- config_api_response = @api_controller.get_app_config()
50
- # Fetch the response ETag
51
- response_config_etag = JSON.parse( config_api_response.to_json )["headers"]["x_moesif_config_etag"]
52
- # Remove ETag from the global dict if exist
53
- if !cached_config_etag.nil? && @config_dict.key?(cached_config_etag)
54
- @config_dict.delete(cached_config_etag)
55
- end
56
- # Fetch the response body
57
- @config_dict[response_config_etag] = JSON.parse(JSON.parse( config_api_response.to_json )["raw_body"])
58
- #
59
- app_config = @config_dict[response_config_etag]
60
- # Fetch the sample rate
61
- if !app_config.nil?
62
- sample_rate = app_config.fetch("sample_rate", 100)
63
- end
64
- # Set the last updated time
65
- @last_updated_time = Time.now.utc
66
- rescue
67
- # Set the last updated time
68
- @last_updated_time = Time.now.utc
69
- end
70
- # Return the sample rate
71
- return sample_rate
72
- end
73
-
74
57
  def update_user(user_profile)
75
58
  UserHelper.new.update_user(@api_controller, @debug, user_profile)
76
59
  end
@@ -236,13 +219,28 @@ module MoesifRack
236
219
  # Perform the API call through the SDK function
237
220
  begin
238
221
  @random_percentage = Random.rand(0.00..100.00)
222
+
223
+ # Event UserId
224
+ user_id = !event_model.user_id.nil? ? event_model.user_id : nil
225
+ # Event CompanyId
226
+ company_id = !event_model.company_id.nil? ? event_model.company_id : nil
227
+ # Get sampling percentage
228
+ @sampling_percentage = @app_config.get_sampling_percentage(@config, user_id, company_id, @debug)
229
+
239
230
  if @sampling_percentage > @random_percentage
240
231
  event_api_response = @api_controller.create_event(event_model)
241
- cached_config_etag = @config_dict.keys[0]
242
232
  event_response_config_etag = event_api_response[:x_moesif_config_etag]
243
233
 
244
- if !event_response_config_etag.nil? && cached_config_etag != event_response_config_etag && Time.now.utc > @last_updated_time + 30
245
- @sampling_percentage = get_config(cached_config_etag)
234
+ if !event_response_config_etag.nil? && !@config_etag.nil? && @config_etag != event_response_config_etag && Time.now.utc > @last_updated_time + 300
235
+ begin
236
+ @config = @app_config.get_config(@api_controller, @debug)
237
+ @config_etag, @sampling_percentage, @last_updated_time = @app_config.parse_configuration(@config, @debug)
238
+ rescue => exception
239
+ if @debug
240
+ puts 'Error while updating the application configuration'
241
+ puts exception.to_s
242
+ end
243
+ end
246
244
  end
247
245
  if @debug
248
246
  puts("Event successfully sent to Moesif")
@@ -1,6 +1,7 @@
1
1
  require 'test/unit'
2
2
  require 'rack'
3
3
  require 'net/http'
4
+ require_relative '../lib/moesif_rack/app_config.rb'
4
5
  require_relative '../lib/moesif_rack'
5
6
 
6
7
  class MoesifRackTest < Test::Unit::TestCase
@@ -45,6 +46,7 @@ class MoesifRackTest < Test::Unit::TestCase
45
46
  }
46
47
  }
47
48
  @moesif_rack_app = MoesifRack::MoesifMiddleware.new(@app, @options)
49
+ @app_config = AppConfig.new
48
50
  end
49
51
 
50
52
  def test_capture_outgoing
@@ -106,7 +108,11 @@ class MoesifRackTest < Test::Unit::TestCase
106
108
  end
107
109
 
108
110
  def test_get_config
109
- assert_operator 100, :>=, @moesif_rack_app.get_config(nil)
111
+ @api_client = MoesifApi::MoesifAPIClient.new(@options['application_id'])
112
+ @api_controller = @api_client.api
113
+ @config = @app_config.get_config(@api_controller, @debug)
114
+ @config_etag, @sampling_percentage, @last_updated_time = @app_config.parse_configuration(@config, @debug)
115
+ assert_operator 100, :>=, @sampling_percentage
110
116
  end
111
117
 
112
118
  def test_update_company
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.3.5
4
+ version: 1.3.6
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: 2019-10-11 00:00:00.000000000 Z
12
+ date: 2019-10-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: test-unit
@@ -48,6 +48,7 @@ files:
48
48
  - LICENSE
49
49
  - README.md
50
50
  - lib/moesif_rack.rb
51
+ - lib/moesif_rack/app_config.rb
51
52
  - lib/moesif_rack/client_ip.rb
52
53
  - lib/moesif_rack/moesif_middleware.rb
53
54
  - lib/moesif_rack/update_company.rb
@@ -75,8 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
76
  - !ruby/object:Gem::Version
76
77
  version: '0'
77
78
  requirements: []
78
- rubyforge_project:
79
- rubygems_version: 2.7.7
79
+ rubygems_version: 3.0.1
80
80
  signing_key:
81
81
  specification_version: 4
82
82
  summary: moesif_rack