moesif_rack 1.4.19 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,17 +3,14 @@ require 'rack'
3
3
  require 'moesif_api'
4
4
  require 'json'
5
5
  require 'base64'
6
- require_relative '../../lib/moesif_rack/app_config.rb'
6
+ require_relative '../../lib/moesif_rack/app_config'
7
7
 
8
8
  module MoesifCaptureOutgoing
9
-
10
9
  class << self
11
-
12
- def start_capture_outgoing(options)
10
+ def start_capture_outgoing(options, app_config_manager, events_queue)
13
11
  @moesif_options = options
14
- if not @moesif_options['application_id']
15
- raise 'application_id required for Moesif Middleware'
16
- end
12
+ raise 'application_id required for Moesif Middleware' unless @moesif_options['application_id']
13
+
17
14
  @api_client = MoesifApi::MoesifAPIClient.new(@moesif_options['application_id'])
18
15
  @api_controller = @api_client.api
19
16
  @debug = @moesif_options['debug']
@@ -24,31 +21,22 @@ module MoesifCaptureOutgoing
24
21
  @skip_outgoing = options['skip_outgoing']
25
22
  @mask_data_outgoing = options['mask_data_outgoing']
26
23
  @log_body_outgoing = options.fetch('log_body_outgoing', true)
27
- @app_config = AppConfig.new(@debug)
28
- @config_etag = nil
24
+
25
+ @app_config = app_config_manager
26
+ # @app_config and @events_queue should be shared instance from the middleware
27
+ # so that we can use the same queue and same loaded @app_config
28
+ @events_queue = events_queue
29
29
  @sampling_percentage = 100
30
30
  @last_updated_time = Time.now.utc
31
- @config_dict = Hash.new
32
- begin
33
- new_config = @app_config.get_config(@api_controller)
34
- if !new_config.nil?
35
- @config, @config_etag, @last_config_download_time = @app_config.parse_configuration(new_config)
36
- end
37
- rescue => exception
38
- if @debug
39
- puts 'Error while parsing application configuration on initialization'
40
- puts exception.to_s
41
- end
42
- end
43
31
  end
44
32
 
45
- def call (url, request, request_time, response, response_time)
33
+ def call(url, request, request_time, response, response_time)
46
34
  send_moesif_event(url, request, request_time, response, response_time)
47
35
  end
48
-
36
+
49
37
  def get_response_body(response)
50
38
  body = response.respond_to?(:body) ? response.body : response
51
- body = body.inject("") { |i, a| i << a } if body.respond_to?(:each)
39
+ body = body.inject('') { |i, a| i << a } if body.respond_to?(:each)
52
40
  body.to_s
53
41
  end
54
42
 
@@ -57,12 +45,9 @@ module MoesifCaptureOutgoing
57
45
  end
58
46
 
59
47
  def send_moesif_event(url, request, request_time, response, response_time)
60
-
61
- if url.downcase.include? "moesif"
62
- if @debug
63
- puts "Skip sending as it is moesif Event"
64
- end
65
- else
48
+ if url.downcase.include? 'moesif'
49
+ puts 'Skip sending as it is moesif Event' if @debug
50
+ else
66
51
  response.code = transform_response_code(response.code) if response.code.is_a?(Symbol)
67
52
 
68
53
  # Request Body
@@ -70,14 +55,12 @@ module MoesifCaptureOutgoing
70
55
  req_body_transfer_encoding = nil
71
56
  req_body = nil
72
57
 
73
- if @log_body_outgoing
74
- if req_body_string && req_body_string.length != 0
75
- begin
76
- req_body = JSON.parse(req_body_string)
77
- rescue
78
- req_body = Base64.encode64(req_body_string)
79
- req_body_transfer_encoding = 'base64'
80
- end
58
+ if @log_body_outgoing && (req_body_string && req_body_string.length != 0)
59
+ begin
60
+ req_body = JSON.parse(req_body_string)
61
+ rescue StandardError
62
+ req_body = Base64.encode64(req_body_string)
63
+ req_body_transfer_encoding = 'base64'
81
64
  end
82
65
  end
83
66
 
@@ -86,19 +69,17 @@ module MoesifCaptureOutgoing
86
69
  rsp_body_transfer_encoding = nil
87
70
  rsp_body = nil
88
71
 
89
- if @log_body_outgoing
90
- if rsp_body_string && rsp_body_string.length != 0
91
- begin
92
- rsp_body = JSON.parse(rsp_body_string)
93
- rescue
94
- rsp_body = Base64.encode64(rsp_body_string)
95
- rsp_body_transfer_encoding = 'base64'
96
- end
72
+ if @log_body_outgoing && (rsp_body_string && rsp_body_string.length != 0)
73
+ begin
74
+ rsp_body = JSON.parse(rsp_body_string)
75
+ rescue StandardError
76
+ rsp_body = Base64.encode64(rsp_body_string)
77
+ rsp_body_transfer_encoding = 'base64'
97
78
  end
98
79
  end
99
80
 
100
81
  # Event Request
101
- event_req = MoesifApi::EventRequestModel.new()
82
+ event_req = MoesifApi::EventRequestModel.new
102
83
  event_req.time = request_time
103
84
  event_req.uri = url
104
85
  event_req.verb = request.method.to_s.upcase
@@ -107,8 +88,8 @@ module MoesifCaptureOutgoing
107
88
  event_req.body = req_body
108
89
  event_req.transfer_encoding = req_body_transfer_encoding
109
90
 
110
- # Event Response
111
- event_rsp = MoesifApi::EventResponseModel.new()
91
+ # Event Response
92
+ event_rsp = MoesifApi::EventResponseModel.new
112
93
  event_rsp.time = response_time
113
94
  event_rsp.status = response.code.to_i
114
95
  event_rsp.headers = response.each_header.collect.to_h
@@ -116,71 +97,58 @@ module MoesifCaptureOutgoing
116
97
  event_rsp.transfer_encoding = rsp_body_transfer_encoding
117
98
 
118
99
  # Prepare Event Model
119
- event_model = MoesifApi::EventModel.new()
100
+ event_model = MoesifApi::EventModel.new
120
101
  event_model.request = event_req
121
102
  event_model.response = event_rsp
122
- event_model.direction = "Outgoing"
103
+ event_model.direction = 'Outgoing'
123
104
 
124
105
  # Metadata for Outgoing Request
125
106
  if @get_metadata_outgoing
126
- if @debug
127
- puts "calling get_metadata_outgoing proc"
128
- end
107
+ puts 'calling get_metadata_outgoing proc' if @debug
129
108
  event_model.metadata = @get_metadata_outgoing.call(request, response)
130
109
  end
131
110
 
132
111
  # Identify User
133
112
  if @identify_user_outgoing
134
- if @debug
135
- puts "calling identify_user_outgoing proc"
136
- end
113
+ puts 'calling identify_user_outgoing proc' if @debug
137
114
  event_model.user_id = @identify_user_outgoing.call(request, response)
138
115
  end
139
116
 
140
117
  # Identify Company
141
118
  if @identify_company_outgoing
142
- if @debug
143
- puts "calling identify_company_outgoing proc"
144
- end
119
+ puts 'calling identify_company_outgoing proc' if @debug
145
120
  event_model.company_id = @identify_company_outgoing.call(request, response)
146
121
  end
147
122
 
148
123
  # Session Token
149
124
  if @identify_session_outgoing
150
- if @debug
151
- puts "calling identify_session_outgoing proc"
152
- end
125
+ puts 'calling identify_session_outgoing proc' if @debug
153
126
  event_model.session_token = @identify_session_outgoing.call(request, response)
154
127
  end
155
128
 
156
129
  # Skip Outgoing Request
157
130
  should_skip = false
158
131
 
159
- if @skip_outgoing
160
- if @skip_outgoing.call(request, response)
161
- should_skip = true;
162
- end
163
- end
132
+ should_skip = true if @skip_outgoing && @skip_outgoing.call(request, response)
164
133
 
165
134
  if !should_skip
166
135
 
167
136
  # Mask outgoing Event
168
137
  if @mask_data_outgoing
169
- if @debug
170
- puts "calling mask_data_outgoing proc"
171
- end
138
+ puts 'calling mask_data_outgoing proc' if @debug
172
139
  event_model = @mask_data_outgoing.call(event_model)
173
140
  end
174
141
 
175
142
  # Send Event to Moesif
176
143
  begin
177
144
  @random_percentage = Random.rand(0.00..100.00)
178
- begin
179
- @sampling_percentage = @app_config.get_sampling_percentage(event_model, @config, event_model.user_id, event_model.company_id)
180
- rescue => exception
145
+ begin
146
+ @sampling_percentage = @app_config.get_sampling_percentage(event_model, event_model.user_id,
147
+ event_model.company_id)
148
+ rescue StandardError => e
181
149
  if @debug
182
150
  puts 'Error while getting sampling percentage, assuming default behavior'
183
- puts exception.to_s
151
+ puts e
184
152
  end
185
153
  @sampling_percentage = 100
186
154
  end
@@ -191,47 +159,29 @@ module MoesifCaptureOutgoing
191
159
  puts 'Sending Outgoing Request Data to Moesif'
192
160
  puts event_model.to_json
193
161
  end
194
- event_api_response = @api_controller.create_event(event_model)
195
- event_response_config_etag = event_api_response[:x_moesif_config_etag]
196
-
197
- if !event_response_config_etag.nil? && !@config_etag.nil? && @config_etag != event_response_config_etag && Time.now.utc > @last_updated_time + 300
198
- begin
199
- new_config = @app_config.get_config(@api_controller)
200
- if !new_config.nil?
201
- @config, @config_etag, @last_config_download_time = @app_config.parse_configuration(new_config)
202
- end
203
- rescue => exception
204
- if @debug
205
- puts 'Error while updating the application configuration'
206
- puts exception.to_s
207
- end
208
- end
209
- end
210
- if @debug
211
- puts("Event successfully sent to Moesif")
212
- end
213
- else
214
- if @debug
215
- puts("Skipped outgoing Event due to sampling percentage: " + @sampling_percentage.to_s + " and random percentage: " + @random_percentage.to_s)
162
+
163
+ # we put in the queue and format abot it.
164
+ unless @events_queue.nil?
165
+ @events_queue << event_model
166
+ puts('Outgoing Event successfully added to event queue') if @debug
167
+ return
216
168
  end
169
+ elsif @debug
170
+ puts('Skipped outgoing Event due to sampling percentage: ' + @sampling_percentage.to_s + ' and random percentage: ' + @random_percentage.to_s)
217
171
  end
218
172
  rescue MoesifApi::APIException => e
219
173
  if e.response_code.between?(401, 403)
220
- puts "Unathorized accesss sending event to Moesif. Please verify your Application Id."
174
+ puts 'Unathorized accesss sending event to Moesif. Please verify your Application Id.'
221
175
  end
222
176
  if @debug
223
- puts "Error sending event to Moesif, with status code: "
177
+ puts 'Error sending event to Moesif, with status code: '
224
178
  puts e.response_code
225
179
  end
226
- rescue => e
227
- if @debug
228
- puts e.to_s
229
- end
180
+ rescue StandardError => e
181
+ puts e if @debug
230
182
  end
231
- else
232
- if @debug
233
- puts 'Skip sending outgoing request'
234
- end
183
+ elsif @debug
184
+ puts 'Skip sending outgoing request'
235
185
  end
236
186
  end
237
187
  end
@@ -1,2 +1,2 @@
1
- require_relative 'httplog/http_log.rb'
2
- require_relative 'httplog/adapters/net_http.rb'
1
+ require_relative 'httplog/http_log'
2
+ require_relative 'httplog/adapters/net_http'