moesif_rack 1.3.0 → 1.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45a13b3f4b85664a0147ef4abe30bd73f1b11d21a96ce30b21d32f15185217e6
4
- data.tar.gz: 02c92b07b397124108dd0eba645b830b9da5b222a4058e9048c8b7bc380b6dd9
3
+ metadata.gz: c829d3004e26c40536debc186d0c45a8a3cf08d854c75f0cf76dce20d142e951
4
+ data.tar.gz: ac8c4d6c2c66dae5348b7421128fdd6b5ce0b2c9a48e1712aa632817f2ade291
5
5
  SHA512:
6
- metadata.gz: ccf6066387ea409cc74f7910bae06a33f2419442ddeba773b29329888e78ca61cd5ee8934168f2e38ea4ebc43fe58dbea8680716de3517d8d2a555ac52a690f7
7
- data.tar.gz: a53a8a807d6f4e2c244e9466743e9fbe2d8949d462a50cc9d82d85fbff6c1b89c852ddb088c4ca74042e3f3775ddbdfdf9955762341943821053aabe95e094af
6
+ metadata.gz: 7f10f8c2e8c4cb8af3d55a4d5aee69626ddb893405ba866578af506834516848b5367f5361a07ca5eb6acb3adccae2a8429432d8792cfb197391b42db310dab6
7
+ data.tar.gz: 3ee6984671ab33f20dcf4b6b640935e315cc84c68e39bb9093b77a9e632aa3683e0e213d197140baa93f76f14809e0d19f701710900f0edc1e9575182121c27f
data/README.md CHANGED
@@ -6,7 +6,9 @@
6
6
  [![Software License][ico-license]][link-license]
7
7
  [![Source Code][ico-source]][link-source]
8
8
 
9
- Rack Middleware that logs _incoming_ API calls to Moesif's AI-powered API analytics service.
9
+ Rack Middleware that logs API calls and sends
10
+ to [Moesif](https://www.moesif.com) for API analytics and log analysis.
11
+
10
12
  Supports Ruby on Rails apps and other Ruby frameworks built on Rack.
11
13
 
12
14
  [Source Code on GitHub](https://github.com/moesif/moesif-rack)
@@ -20,7 +22,7 @@ gem install moesif_rack
20
22
  and if you have a `Gemfile` in your project, please add this line to
21
23
 
22
24
  ```
23
- gem 'moesif_rack', '~> 1.2.6'
25
+ gem 'moesif_rack', '~> 1.3.4'
24
26
 
25
27
  ```
26
28
 
@@ -30,19 +32,40 @@ gem 'moesif_rack', '~> 1.2.6'
30
32
 
31
33
  ```ruby
32
34
  moesif_options = {
33
- 'application_id' => 'Your application Id'
35
+ 'application_id' => 'Your Moesif Application Id',
36
+ 'log_body' => true,
34
37
  }
35
38
  ```
36
39
 
37
- You can find your Application Id from [_Moesif Dashboard_](https://www.moesif.com/) -> _Top Right Menu_ -> _App Setup_
40
+ Your Moesif Application Id can be found in the [_Moesif Portal_](https://www.moesif.com/).
41
+ After signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps.
42
+
43
+ You can always find your Moesif Application Id at any time by logging
44
+ into the [_Moesif Portal_](https://www.moesif.com/), click on the top right menu,
45
+ and then clicking _Installation_.
38
46
 
39
47
  ### Add to middleware
40
48
 
41
- within `config/application.rb`
49
+ Using strings or symbols for middleware class names is deprecated for newer frameworks like Ruby 5.0,
50
+ so you should pass the class directly.
51
+
52
+ #### For Rails 5.0 or newer:
42
53
 
43
54
  ```ruby
55
+ class Application < Rails::Application
56
+ # snip
57
+
58
+ config.middleware.use MoesifRack::MoesifMiddleware, moesif_options
44
59
 
60
+ # snip
61
+ end
62
+ ```
63
+
64
+ #### For other frameworks:
45
65
 
66
+ within `config/application.rb`
67
+
68
+ ```ruby
46
69
  class Application < Rails::Application
47
70
  # snip
48
71
 
@@ -50,35 +73,37 @@ within `config/application.rb`
50
73
 
51
74
  # snip
52
75
  end
53
-
54
76
  ```
55
77
 
56
78
  #### Order of Middleware Matters
57
79
 
58
- Since Moesif Rack is basically a logging middleware, the ordering of middleware matters for accuracy and completeness.
59
- Many middlewares are installed by default by Rails.
60
-
61
- To see the list of middlewares that your system already have, type this into the bash.
80
+ Since Moesif Rack is a logging middleware, the ordering of middleware matters for accuracy and data collection.
81
+ Many middleware are installed by default by Rails.
62
82
 
63
- ```bash
64
- bin/rails middleware
65
- ```
66
-
67
- The best place for "MoesifRack::MoesifMidleware" is on top as possible (so it captures the data closest to the wire).
83
+ The best place for "MoesifRack::MoesifMidleware" is on top (so it captures the data closest to the wire).
68
84
  Typically, right above the default logger of Rails apps, "Rails::Rack::Logger" is a good spot.
69
85
  Or if you want to be as close as wire as possible, put it before "ActionDispatch::Static"
70
86
 
71
- You should use the following line of code to insert the middleware into the right spot.
87
+ To insert the Moesif middleware before "Rails::Rack::Logger", you can use the `insert_before` method instead of
88
+ `use`
72
89
 
73
90
  ```ruby
91
+ class Application < Rails::Application
92
+ # snip
74
93
 
75
- config.middleware.insert_before "Rails::Rack::Logger", "MoesifRack::MoesifMiddleware", moesif_options
94
+ config.middleware.insert_before Rails::Rack::Logger, MoesifRack::MoesifMiddleware, moesif_options
76
95
 
96
+ # snip
97
+ end
77
98
  ```
99
+ If you are using "Rack::Deflater" or other compression middleware, make sure Moesif is after
100
+ it, so it can capture the uncompressed data.
78
101
 
79
- Please note, if you are using "Rack::Deflater" please make sure that "MoesifRack::MoesifMiddlware"
80
- is below it, so it can capture uncompressed data.
102
+ To see your current list of middleware:
81
103
 
104
+ ```bash
105
+ bin/rails middleware
106
+ ```
82
107
 
83
108
  ## Configuration options
84
109
 
@@ -106,7 +131,23 @@ moesif_options['identify_user'] = Proc.new { |env, headers, body|
106
131
 
107
132
  #snip
108
133
 
109
- 'the_user_id'
134
+ 'my_user_id'
135
+ }
136
+
137
+ ```
138
+
139
+ #### __`identify_company`__
140
+
141
+ Optional.
142
+ identify_company is a Proc that takes env, headers, and body as arguments and returns a company_id string. This helps us attribute requests to unique company.
143
+
144
+ ```ruby
145
+
146
+ moesif_options['identify_company'] = Proc.new { |env, headers, body|
147
+
148
+ #snip
149
+
150
+ 'my_company_id'
110
151
  }
111
152
 
112
153
  ```
@@ -187,6 +228,10 @@ For details for the spec of event model, please see the [Moesif Ruby API Documen
187
228
 
188
229
  Optional. Boolean. Default false. If true, it will print out debug messages. In debug mode, the processing is not done in backend thread.
189
230
 
231
+ #### __`log_body`__
232
+
233
+ Optional. Boolean. Default true. If false, will not log request and response body to Moesif.
234
+
190
235
  #### __`capture_outoing_requests`__
191
236
  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.
192
237
 
@@ -207,6 +252,22 @@ moesif_options['identify_user_outgoing'] = Proc.new { |request, response|
207
252
 
208
253
  ```
209
254
 
255
+ ##### __`identify_company_outgoing`__
256
+
257
+ Optional.
258
+ identify_company_outgoing is a Proc that takes request and response as arguments and returns a company_id string. This helps us attribute requests to unique company.
259
+
260
+ ```ruby
261
+
262
+ moesif_options['identify_company_outgoing'] = Proc.new { |request, response|
263
+
264
+ #snip
265
+
266
+ 'the_company_id'
267
+ }
268
+
269
+ ```
270
+
210
271
  ##### __`get_metadata_outgoing`__
211
272
 
212
273
  Optional.
@@ -274,6 +335,10 @@ moesif_options['mask_data_outgoing'] = Proc.new { |event_model|
274
335
 
275
336
  ```
276
337
 
338
+ #### __`log_body_outgoing`__
339
+
340
+ Optional. Boolean. Default true. If false, will not log request and response body to Moesif.
341
+
277
342
  ## Update User
278
343
 
279
344
  ### update_user method
@@ -287,7 +352,8 @@ metadata = JSON.parse('{'\
287
352
  '"custom": "testdata"'\
288
353
  '}')
289
354
 
290
- user_model = { "user_id" => "testrubyapiuser",
355
+ user_model = { "user_id" => "12345",
356
+ "company_id" => "67890",
291
357
  "modified_time" => Time.now.utc.iso8601,
292
358
  "metadata" => metadata }
293
359
 
@@ -307,11 +373,13 @@ metadata = JSON.parse('{'\
307
373
 
308
374
  user_models = []
309
375
 
310
- user_model_A = { "user_id" => "testrubyapiuser",
376
+ user_model_A = { "user_id" => "12345",
377
+ "company_id" => "67890",
311
378
  "modified_time" => Time.now.utc.iso8601,
312
379
  "metadata" => metadata }
313
380
 
314
- user_model_B = { "user_id" => "testrubyapiuser1",
381
+ user_model_B = { "user_id" => "1234",
382
+ "company_id" => "6789",
315
383
  "modified_time" => Time.now.utc.iso8601,
316
384
  "metadata" => metadata }
317
385
 
@@ -332,7 +400,8 @@ metadata = JSON.parse('{'\
332
400
  '"custom": "testdata"'\
333
401
  '}')
334
402
 
335
- company_model = { "company_id" => "testrubyapicompany",
403
+ company_model = { "company_id" => "12345",
404
+ "company_domain" => "acmeinc.com",
336
405
  "metadata" => metadata }
337
406
 
338
407
  update_company = MoesifRack::MoesifMiddleware.new(@app, @options).update_company(company_model)
@@ -351,10 +420,12 @@ metadata = JSON.parse('{'\
351
420
 
352
421
  company_models = []
353
422
 
354
- company_model_A = { "company_id" => "testrubyapicompany",
423
+ company_model_A = { "company_id" => "12345",
424
+ "company_domain" => "nowhere.com",
355
425
  "metadata" => metadata }
356
426
 
357
- company_model_B = { "company_id" => "testrubyapicompany1",
427
+ company_model_B = { "company_id" => "67890",
428
+ "company_domain" => "acmeinc.com",
358
429
  "metadata" => metadata }
359
430
 
360
431
  company_models << company_model_A << company_model_B
@@ -19,6 +19,7 @@ module MoesifRack
19
19
 
20
20
  @api_version = options['api_version']
21
21
  @identify_user = options['identify_user']
22
+ @identify_company = options['identify_company']
22
23
  @get_metadata = options['get_metadata']
23
24
  @identify_session = options['identify_session']
24
25
  @mask_data = options['mask_data']
@@ -26,6 +27,7 @@ module MoesifRack
26
27
  @debug = options['debug']
27
28
  @config_dict = Hash.new
28
29
  @disable_transaction_id = options['disable_transaction_id'] || false
30
+ @log_body = options.fetch('log_body', true)
29
31
  @sampling_percentage = get_config(nil)
30
32
  if not @sampling_percentage.is_a? Numeric
31
33
  raise "Sampling Percentage should be a number"
@@ -109,13 +111,16 @@ module MoesifRack
109
111
  req_body_string = req.body.read
110
112
  req.body.rewind
111
113
  req_body_transfer_encoding = nil
112
-
113
- if req_body_string && req_body_string.length != 0
114
- begin
115
- req_body = JSON.parse(req_body_string)
116
- rescue
117
- req_body = Base64.encode64(req_body_string)
118
- req_body_transfer_encoding = 'base64'
114
+ req_body = nil
115
+
116
+ if @log_body
117
+ if req_body_string && req_body_string.length != 0
118
+ begin
119
+ req_body = JSON.parse(req_body_string)
120
+ rescue
121
+ req_body = Base64.encode64(req_body_string)
122
+ req_body_transfer_encoding = 'base64'
123
+ end
119
124
  end
120
125
  end
121
126
 
@@ -123,13 +128,16 @@ module MoesifRack
123
128
 
124
129
  rsp_body_string = get_response_body(body);
125
130
  rsp_body_transfer_encoding = nil
126
-
127
- if rsp_body_string && rsp_body_string.length != 0
128
- begin
129
- rsp_body = JSON.parse(rsp_body_string)
130
- rescue
131
- rsp_body = Base64.encode64(rsp_body_string)
132
- rsp_body_transfer_encoding = 'base64'
131
+ rsp_body = nil
132
+
133
+ if @log_body
134
+ if rsp_body_string && rsp_body_string.length != 0
135
+ begin
136
+ rsp_body = JSON.parse(rsp_body_string)
137
+ rescue
138
+ rsp_body = Base64.encode64(rsp_body_string)
139
+ rsp_body_transfer_encoding = 'base64'
140
+ end
133
141
  end
134
142
  end
135
143
 
@@ -194,6 +202,13 @@ module MoesifRack
194
202
  event_model.user_id = @identify_user.call(env, headers, body)
195
203
  end
196
204
 
205
+ if @identify_company
206
+ if @debug
207
+ puts "calling identify company proc"
208
+ end
209
+ event_model.company_id = @identify_company.call(env, headers, body)
210
+ end
211
+
197
212
  if @get_metadata
198
213
  if @debug
199
214
  puts "calling get_metadata proc"
@@ -270,7 +285,7 @@ module MoesifRack
270
285
 
271
286
  def get_response_body(response)
272
287
  body = response.respond_to?(:body) ? response.body : response
273
- body = body.inject("") { |i, a| i << a } if body.respond_to?(:each)
288
+ body = body.inject("") { |i, a| i << a } if (body.respond_to?(:each) && body.respond_to?(:inject))
274
289
  body.to_s
275
290
  end
276
291
 
@@ -18,9 +18,11 @@ module MoesifCaptureOutgoing
18
18
  @debug = @moesif_options['debug']
19
19
  @get_metadata_outgoing = @moesif_options['get_metadata_outgoing']
20
20
  @identify_user_outgoing = @moesif_options['identify_user_outgoing']
21
+ @identify_company_outgoing = @moesif_options['identify_company_outgoing']
21
22
  @identify_session_outgoing = @moesif_options['identify_session_outgoing']
22
23
  @skip_outgoing = options['skip_outgoing']
23
24
  @mask_data_outgoing = options['mask_data_outgoing']
25
+ @log_body_outgoing = options.fetch('log_body_outgoing', true)
24
26
  end
25
27
 
26
28
  def call (url, request, request_time, response, response_time)
@@ -49,25 +51,32 @@ module MoesifCaptureOutgoing
49
51
  # Request Body
50
52
  req_body_string = request.body.nil? || request.body.empty? ? nil : request.body
51
53
  req_body_transfer_encoding = nil
52
- if req_body_string && req_body_string.length != 0
53
- begin
54
- req_body = JSON.parse(req_body_string)
55
- rescue
56
- req_body = Base64.encode64(req_body_string)
57
- req_body_transfer_encoding = 'base64'
54
+ req_body = nil
55
+
56
+ if @log_body_outgoing
57
+ if req_body_string && req_body_string.length != 0
58
+ begin
59
+ req_body = JSON.parse(req_body_string)
60
+ rescue
61
+ req_body = Base64.encode64(req_body_string)
62
+ req_body_transfer_encoding = 'base64'
63
+ end
58
64
  end
59
65
  end
60
66
 
61
67
  # Response Body and encoding
62
68
  rsp_body_string = get_response_body(response.body)
63
69
  rsp_body_transfer_encoding = nil
64
-
65
- if rsp_body_string && rsp_body_string.length != 0
66
- begin
67
- rsp_body = JSON.parse(rsp_body_string)
68
- rescue
69
- rsp_body = Base64.encode64(rsp_body_string)
70
- rsp_body_transfer_encoding = 'base64'
70
+ rsp_body = nil
71
+
72
+ if @log_body_outgoing
73
+ if rsp_body_string && rsp_body_string.length != 0
74
+ begin
75
+ rsp_body = JSON.parse(rsp_body_string)
76
+ rescue
77
+ rsp_body = Base64.encode64(rsp_body_string)
78
+ rsp_body_transfer_encoding = 'base64'
79
+ end
71
80
  end
72
81
  end
73
82
 
@@ -78,7 +87,7 @@ module MoesifCaptureOutgoing
78
87
  event_req.verb = request.method.to_s.upcase
79
88
  event_req.headers = request.each_header.collect.to_h
80
89
  event_req.api_version = nil
81
- event_req.body = req_body_string
90
+ event_req.body = req_body
82
91
  event_req.transfer_encoding = req_body_transfer_encoding
83
92
 
84
93
  # Event Response
@@ -86,7 +95,7 @@ module MoesifCaptureOutgoing
86
95
  event_rsp.time = response_time
87
96
  event_rsp.status = response.code.to_i
88
97
  event_rsp.headers = response.each_header.collect.to_h
89
- event_rsp.body = rsp_body_string
98
+ event_rsp.body = rsp_body
90
99
  event_rsp.transfer_encoding = rsp_body_transfer_encoding
91
100
 
92
101
  # Prepare Event Model
@@ -110,6 +119,14 @@ module MoesifCaptureOutgoing
110
119
  event_model.user_id = @identify_user_outgoing.call(request, response)
111
120
  end
112
121
 
122
+ # Identify Company
123
+ if @identify_company_outgoing
124
+ if @debug
125
+ puts "calling identify_company_outgoing proc"
126
+ end
127
+ event_model.company_id = @identify_company_outgoing.call(request, response)
128
+ end
129
+
113
130
  # Session Token
114
131
  if @identify_session_outgoing
115
132
  if @debug
@@ -6,18 +6,33 @@ require_relative '../lib/moesif_rack'
6
6
  class MoesifRackTest < Test::Unit::TestCase
7
7
  def setup
8
8
  @app = ->(env) { [200, { "Content-Type" => "application/json" }, ["{ \"key\": \"value\"}"]]}
9
- @options = { 'application_id' => 'Your Application Id',
9
+ @options = { 'application_id' => 'Your Moesif Application Id',
10
10
  'debug' => true,
11
11
  'disable_transaction_id' => true,
12
12
  'capture_outoing_requests' => true,
13
+ 'get_metadata' => Proc.new {|request, response|
14
+ {
15
+ 'foo' => 'abc',
16
+ 'bar' => '123'
17
+ }
18
+ },
13
19
  'get_metadata_outgoing' => Proc.new {|request, response|
14
20
  {
15
21
  'foo' => 'abc',
16
22
  'bar' => '123'
17
23
  }
18
24
  },
25
+ 'identify_user' => Proc.new{|request, response|
26
+ 'my_user_id'
27
+ },
28
+ 'identify_company' => Proc.new{|request, response|
29
+ 'my_company_id'
30
+ },
19
31
  'identify_user_outgoing' => Proc.new{|request, response|
20
- 'outgoing_user'
32
+ 'outgoing_user_id'
33
+ },
34
+ 'identify_company_outgoing' => Proc.new{|request, response|
35
+ 'outgoing_company_id'
21
36
  },
22
37
  'identify_session_outgoing' => Proc.new{|request, response|
23
38
  'outgoing_session'
@@ -52,7 +67,8 @@ class MoesifRackTest < Test::Unit::TestCase
52
67
  '"custom": "testdata"'\
53
68
  '}')
54
69
 
55
- user_model = { "user_id" => "testrubyapiuser",
70
+ user_model = { "user_id" => "12345",
71
+ "company_id" => "67890",
56
72
  "modified_time" => Time.now.utc.iso8601,
57
73
  "metadata" => metadata }
58
74
 
@@ -69,11 +85,13 @@ class MoesifRackTest < Test::Unit::TestCase
69
85
 
70
86
  user_models = []
71
87
 
72
- user_model_A = { "user_id" => "testrubyapiuser",
88
+ user_model_A = { "user_id" => "12345",
89
+ "company_id" => "67890",
73
90
  "modified_time" => Time.now.utc.iso8601,
74
91
  "metadata" => metadata }
75
92
 
76
- user_model_B = { "user_id" => "testrubyapiuser1",
93
+ user_model_B = { "user_id" => "1234",
94
+ "company_id" => "6789",
77
95
  "modified_time" => Time.now.utc.iso8601,
78
96
  "metadata" => metadata }
79
97
 
@@ -98,7 +116,8 @@ class MoesifRackTest < Test::Unit::TestCase
98
116
  '"custom": "testdata"'\
99
117
  '}')
100
118
 
101
- company_model = { "company_id" => "testrubyapicompany",
119
+ company_model = { "company_id" => "12345",
120
+ "company_domain" => "acmeinc.com",
102
121
  "metadata" => metadata }
103
122
 
104
123
  response = @moesif_rack_app.update_company(company_model)
@@ -114,10 +133,12 @@ class MoesifRackTest < Test::Unit::TestCase
114
133
 
115
134
  company_models = []
116
135
 
117
- company_model_A = { "company_id" => "testrubyapicompany",
136
+ company_model_A = { "company_id" => "12345",
137
+ "company_domain" => "nowhere.com",
118
138
  "metadata" => metadata }
119
139
 
120
- company_model_B = { "company_id" => "testrubyapicompany1",
140
+ company_model_B = { "company_id" => "1234",
141
+ "company_domain" => "acmeinc.com",
121
142
  "metadata" => metadata }
122
143
 
123
144
  company_models << company_model_A << company_model_B
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.0
4
+ version: 1.3.5
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-05-22 00:00:00.000000000 Z
12
+ date: 2019-10-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: test-unit
@@ -31,14 +31,14 @@ dependencies:
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: 1.2.7
34
+ version: 1.2.8
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: 1.2.7
41
+ version: 1.2.8
42
42
  description: Collection/Data Ingestion SDK for Rack (also Rails) Middleware / RoR
43
43
  email: xing@moesif.com
44
44
  executables: []
@@ -75,7 +75,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  requirements: []
78
- rubygems_version: 3.0.1
78
+ rubyforge_project:
79
+ rubygems_version: 2.7.7
79
80
  signing_key:
80
81
  specification_version: 4
81
82
  summary: moesif_rack