90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# File 'lib/moesif_rack/moesif_middleware.rb', line 90
def call env
start_time = Time.now.utc.iso8601
if @debug
puts 'inside moesif middleware'
end
status, , body = @app.call env
end_time = Time.now.utc.iso8601
process_send = lambda do
req = Rack::Request.new(env)
complex_copy = env.dup
= {}
complex_copy.select {|k,v| k.start_with? 'HTTP_'}.each do |key, val|
new_key = key.sub(/^HTTP_/, '')
new_key = new_key.sub('_', '-')
[new_key] = val
end
req_body_string = req.body.read
req.body.rewind
req_body_transfer_encoding = nil
req_body = nil
if @log_body
if req_body_string && req_body_string.length != 0
begin
req_body = JSON.parse(req_body_string)
rescue
req_body = Base64.encode64(req_body_string)
req_body_transfer_encoding = 'base64'
end
end
end
= .dup
rsp_body_string = get_response_body(body);
rsp_body_transfer_encoding = nil
rsp_body = nil
if @log_body
if rsp_body_string && rsp_body_string.length != 0
begin
rsp_body = JSON.parse(rsp_body_string)
rescue
rsp_body = Base64.encode64(rsp_body_string)
rsp_body_transfer_encoding = 'base64'
end
end
end
event_req = MoesifApi::EventRequestModel.new()
event_req.time = start_time
event_req.uri = req.url
event_req.verb = req.request_method
if @api_version
event_req.api_version = @api_version
end
if !@disable_transaction_id
req_trans_id = ["X-MOESIF_TRANSACTION_ID"]
if !req_trans_id.nil?
transaction_id = req_trans_id
if transaction_id.strip.empty?
transaction_id = SecureRandom.uuid
end
else
transaction_id = SecureRandom.uuid
end
["X-Moesif-Transaction-Id"] = transaction_id
if .key?("X-MOESIF_TRANSACTION_ID")
= .except("X-MOESIF_TRANSACTION_ID")
end
end
if !transaction_id.nil?
["X-Moesif-Transaction-Id"] = transaction_id
end
if !transaction_id.nil?
["X-Moesif-Transaction-Id"] = transaction_id
end
event_req.ip_address = get_client_address(req.env)
event_req. =
event_req.body = req_body
event_req.transfer_encoding = req_body_transfer_encoding
event_rsp = MoesifApi::EventResponseModel.new()
event_rsp.time = end_time
event_rsp.status = status
event_rsp. =
event_rsp.body = rsp_body
event_rsp.transfer_encoding = rsp_body_transfer_encoding
event_model = MoesifApi::EventModel.new()
event_model.request = event_req
event_model.response = event_rsp
if @identify_user
if @debug
puts "calling identify user proc"
end
event_model.user_id = @identify_user.call(env, , body)
end
if @identify_company
if @debug
puts "calling identify company proc"
end
event_model.company_id = @identify_company.call(env, , body)
end
if @get_metadata
if @debug
puts "calling get_metadata proc"
end
event_model.metadata = @get_metadata.call(env, , body)
end
if @identify_session
if @debug
puts "calling identify session proc"
end
event_model.session_token = @identify_session.call(env, , body)
end
if @mask_data
if @debug
puts "calling mask_data proc"
end
event_model = @mask_data.call(event_model)
end
if @debug
puts "sending data to moesif"
puts event_model.to_json
end
begin
@random_percentage = Random.rand(0.00..100.00)
if @sampling_percentage > @random_percentage
event_api_response = @api_controller.create_event(event_model)
cached_config_etag = @config_dict.keys[0]
event_response_config_etag = event_api_response[:x_moesif_config_etag]
if !event_response_config_etag.nil? && cached_config_etag != event_response_config_etag && Time.now.utc > @last_updated_time + 30
@sampling_percentage = get_config(cached_config_etag)
end
if @debug
puts("Event successfully sent to Moesif")
end
else
if @debug
puts("Skipped Event due to sampling percentage: " + @sampling_percentage.to_s + " and random percentage: " + @random_percentage.to_s)
end
end
rescue MoesifApi::APIException => e
if e.response_code.between?(401, 403)
puts "Unathorized accesss sending event to Moesif. Please verify your Application Id."
end
if @debug
puts "Error sending event to Moesif, with status code: "
puts e.response_code
end
end
end
should_skip = false
if @skip
if @skip.call(env, , body)
should_skip = true;
end
end
if !should_skip
if @debug
process_send.call
else
Thread.start(&process_send)
end
end
[status, , body]
end
|