moesif_rack 1.4.5 → 1.4.10

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: 2366969ea63a6f2aeeb37498dc5bbfad99d4ea43753085237af1326689050cc4
4
- data.tar.gz: 2ac98efe04dc86f79db515805ed5f45e7beff707c87c82e42fc7adc2eacbc883
3
+ metadata.gz: e9862b7c7e3a113f37599bff78bf6fbca9bd1edd439dac103f26ec54354dc862
4
+ data.tar.gz: 507fe6d85365f8588063e656d3085e34c8b6b7b5a9e17ec7b993dc8f31af60ac
5
5
  SHA512:
6
- metadata.gz: f18420e49b9dbd3f6d8a1cba791a4c235a06d3568e8ed47d5be9059e93b09acdd6a71a7a7382f500daafe1d5d1b6dd3c3eb87b7d99612db042a41a3a881eb65c
7
- data.tar.gz: a1a3174c9e701e6ccd809ee0ea113878ca53b0ed8194ca612ba5491f0aaa9aa2761e3b586b3acaf23f6317244700111026e6ae9ce84f212a87dd4a25bdda7619
6
+ metadata.gz: ef060aca2101ec875908456975537dba773fbbf4d721ed0a0797c0a03c23604e750a98fff8ec4d9b836b4aba6ba5b090ffa4aed1866d25ef0d49d36d5cad94fd
7
+ data.tar.gz: c7a845885a3fc2171e904e08c05f553b025037e72027e9dfea2c52a2b186266ea93d26ce51d091f14a9d514c8afe084e3dddab5886d7d70400669418a083cba8
data/README.md CHANGED
@@ -7,33 +7,37 @@
7
7
  [![Source Code][ico-source]][link-source]
8
8
 
9
9
  Rack Middleware that logs API calls and sends
10
- to [Moesif](https://www.moesif.com) for API analytics and log analysis.
10
+ to [Moesif](https://www.moesif.com) for API analytics and monitoring.
11
11
 
12
- Supports Ruby on Rails apps and other Ruby frameworks built on Rack.
12
+ Supports Ruby on Rails, Grape, and other Ruby frameworks built on Rack.
13
13
 
14
14
  [Source Code on GitHub](https://github.com/moesif/moesif-rack)
15
15
 
16
16
  ## How to install
17
17
 
18
+ Install the Moesif gem.
19
+
18
20
  ```bash
19
21
  gem install moesif_rack
20
22
  ```
21
23
 
22
- and if you have a `Gemfile` in your project, please add this line to
24
+ If you're using Bundler, add the gem to your `Gemfile`.
23
25
 
24
- ```
26
+ ```ruby
25
27
  gem 'moesif_rack'
26
-
27
28
  ```
28
29
 
30
+ Then, run `bundle install`
31
+
29
32
  ## How to use
30
33
 
31
- ### Create the options
34
+ ### 1. Enter Moesif Application Id
35
+
36
+ Create an options hash containing application_id and any other options.
32
37
 
33
38
  ```ruby
34
39
  moesif_options = {
35
- 'application_id' => 'Your Moesif Application Id',
36
- 'log_body' => true,
40
+ 'application_id' => 'Your Moesif Application Id'
37
41
  }
38
42
  ```
39
43
 
@@ -44,43 +48,67 @@ You can always find your Moesif Application Id at any time by logging
44
48
  into the [_Moesif Portal_](https://www.moesif.com/), click on the top right menu,
45
49
  and then clicking _Installation_.
46
50
 
47
- ### Add to middleware
51
+ ### 2. Add the middleware
52
+
53
+ #### For Rails 5.0 or newer:
48
54
 
49
55
  Using strings or symbols for middleware class names is deprecated for newer frameworks like Ruby 5.0,
50
56
  so you should pass the class directly.
51
57
 
52
- #### For Rails 5.0 or newer:
53
-
54
58
  ```ruby
55
59
  class Application < Rails::Application
56
- # snip
60
+ moesif_options = {
61
+ 'application_id' => 'Your Moesif Application Id'
62
+ }
57
63
 
58
64
  config.middleware.use MoesifRack::MoesifMiddleware, moesif_options
59
-
60
- # snip
61
65
  end
62
66
  ```
63
67
 
64
- #### For other frameworks:
68
+ #### For Rails 4.0 and other frameworks:
69
+
70
+ For most rack-based frameworks including Rails 4.x or older, add the middleware `MoesifRack::MoesifMiddleware`.
65
71
 
66
72
  within `config/application.rb`
67
73
 
68
74
  ```ruby
69
75
  class Application < Rails::Application
70
- # snip
76
+ moesif_options = {
77
+ 'application_id' => 'Your Moesif Application Id'
78
+ }
71
79
 
72
80
  config.middleware.use "MoesifRack::MoesifMiddleware", moesif_options
81
+ end
82
+ ```
73
83
 
74
- # snip
84
+ #### For Grape API:
85
+
86
+ For [Grape APIs](https://github.com/ruby-grape/grape), we can add the middleware after any custom parsers or formatters.
87
+
88
+ ```ruby
89
+ module Acme
90
+ class Ping < Grape::API
91
+ format :json
92
+
93
+ moesif_options = {
94
+ 'application_id' => 'Your Moesif Application Id'
95
+ }
96
+
97
+ insert_after Grape::Middleware::Formatter, MoesifRack::MoesifMiddleware, moesif_options
98
+
99
+ get '/ping' do
100
+ { ping: 'pong' }
101
+ end
75
102
  end
103
+ end
76
104
  ```
77
105
 
78
106
  #### Order of Middleware Matters
79
107
 
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.
108
+ Since Moesif Rack is a logging middleware, the ordering of middleware matters.
109
+ The best place for "MoesifRack::MoesifMidleware" is near the top (so it captures the data closest to the wire), but after
110
+ any body parsers or authentication middleware.
82
111
 
83
- The best place for "MoesifRack::MoesifMidleware" is on top (so it captures the data closest to the wire).
84
112
  Typically, right above the default logger of Rails apps, "Rails::Rack::Logger" is a good spot.
85
113
  Or if you want to be as close as wire as possible, put it before "ActionDispatch::Static"
86
114
 
@@ -107,7 +135,7 @@ To see your current list of middleware:
107
135
 
108
136
  ## Configuration options
109
137
 
110
- The options is a hash with these possible key value pairs.
138
+ Options is a hash with these possible key/value pairs.
111
139
 
112
140
  #### __`application_id`__
113
141
 
@@ -206,7 +234,12 @@ Optional. A Proc that takes env, headers, body and returns a boolean.
206
234
 
207
235
  moesif_options['skip'] = Proc.new { |env, headers, body|
208
236
  # Add your custom code that returns true to skip logging the API call
209
- false
237
+ if env.key?("REQUEST_URI")
238
+ # Skip probes to health page
239
+ env["REQUEST_URI"].include? "/health"
240
+ else
241
+ false
242
+ end
210
243
  }
211
244
 
212
245
  ```
@@ -2,10 +2,8 @@
2
2
  def is_ip?(value)
3
3
  ipv4 = /^(?:(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/
4
4
  ipv6 = /^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!! to convert the return value to a boolean
7
- !!(value =~ ipv4 or value=~ ipv6)
8
- end
5
+ # We use !! to convert the return value to a boolean
6
+ !!(value =~ ipv4 or value=~ ipv6)
9
7
  end
10
8
 
11
9
  def get_client_ip_from_x_forwarded_for(value)
@@ -102,7 +102,10 @@ module MoesifRack
102
102
 
103
103
  def parse_body(body, headers)
104
104
  begin
105
- if start_with_json(body)
105
+ if (body.instance_of?(Hash) || body.instance_of?(Array))
106
+ parsed_body = body
107
+ transfer_encoding = 'json'
108
+ elsif start_with_json(body)
106
109
  parsed_body = JSON.parse(body)
107
110
  transfer_encoding = 'json'
108
111
  elsif headers.key?('content-encoding') && ((headers['content-encoding'].downcase).include? "gzip")
@@ -293,7 +296,7 @@ module MoesifRack
293
296
  # Add Event to the queue
294
297
  @events_queue << event_model
295
298
  @helpers.log_debug("Event added to the queue ")
296
- if Time.now.utc > (@last_config_download_time + 60)
299
+ if Time.now.utc > (@last_worker_run + 60)
297
300
  start_worker()
298
301
  end
299
302
 
@@ -328,7 +331,12 @@ module MoesifRack
328
331
  end
329
332
 
330
333
  if !should_skip
331
- process_send.call
334
+ begin
335
+ process_send.call
336
+ rescue => exception
337
+ @helpers.log_debug 'Error while logging event - '
338
+ @helpers.log_debug exception.to_s
339
+ end
332
340
  end
333
341
 
334
342
  [status, headers, body]
@@ -336,6 +344,9 @@ module MoesifRack
336
344
 
337
345
  def get_response_body(response)
338
346
  body = response.respond_to?(:body) ? response.body : response
347
+ if (body.instance_of?(Hash) || body.instance_of?(Array))
348
+ return body
349
+ end
339
350
  body = body.inject("") { |i, a| i << a } if (body.respond_to?(:each) && body.respond_to?(:inject))
340
351
  body.to_s
341
352
  end
@@ -24,15 +24,15 @@ module MoesifCaptureOutgoing
24
24
  @skip_outgoing = options['skip_outgoing']
25
25
  @mask_data_outgoing = options['mask_data_outgoing']
26
26
  @log_body_outgoing = options.fetch('log_body_outgoing', true)
27
- @app_config = AppConfig.new
28
- @config = @app_config.get_config(@api_controller, @debug)
27
+ @app_config = AppConfig.new(@debug)
29
28
  @config_etag = nil
30
29
  @sampling_percentage = 100
31
30
  @last_updated_time = Time.now.utc
32
31
  @config_dict = Hash.new
33
32
  begin
34
- if !@config.nil?
35
- @config_etag, @sampling_percentage, @last_updated_time = @app_config.parse_configuration(@config, @debug)
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
36
  end
37
37
  rescue => exception
38
38
  if @debug
@@ -176,7 +176,7 @@ module MoesifCaptureOutgoing
176
176
  begin
177
177
  @random_percentage = Random.rand(0.00..100.00)
178
178
  begin
179
- @sampling_percentage = @app_config.get_sampling_percentage(@config, event_model.user_id, event_model.company_id, @debug)
179
+ @sampling_percentage = @app_config.get_sampling_percentage(@config, event_model.user_id, event_model.company_id)
180
180
  rescue => exception
181
181
  if @debug
182
182
  puts 'Error while getting sampling percentage, assuming default behavior'
@@ -196,8 +196,10 @@ module MoesifCaptureOutgoing
196
196
 
197
197
  if !event_response_config_etag.nil? && !@config_etag.nil? && @config_etag != event_response_config_etag && Time.now.utc > @last_updated_time + 300
198
198
  begin
199
- @config = @app_config.get_config(@api_controller, @debug)
200
- @config_etag, @sampling_percentage, @last_updated_time = @app_config.parse_configuration(@config, @debug)
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
201
203
  rescue => exception
202
204
  if @debug
203
205
  puts 'Error while updating the application configuration'
@@ -222,7 +224,7 @@ module MoesifCaptureOutgoing
222
224
  puts e.response_code
223
225
  end
224
226
  rescue => e
225
- if debug
227
+ if @debug
226
228
  puts e.to_s
227
229
  end
228
230
  end
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.4.5
4
+ version: 1.4.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moesif, Inc
@@ -9,10 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-06-19 00:00:00.000000000 Z
12
+ date: 2021-06-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: test-unit
16
15
  requirement: !ruby/object:Gem::Requirement
17
16
  requirements:
18
17
  - - "~>"
@@ -21,8 +20,9 @@ dependencies:
21
20
  - - ">="
22
21
  - !ruby/object:Gem::Version
23
22
  version: 3.1.5
24
- type: :development
23
+ name: test-unit
25
24
  prerelease: false
25
+ type: :development
26
26
  version_requirements: !ruby/object:Gem::Requirement
27
27
  requirements:
28
28
  - - "~>"
@@ -32,7 +32,6 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: 3.1.5
34
34
  - !ruby/object:Gem::Dependency
35
- name: moesif_api
36
35
  requirement: !ruby/object:Gem::Requirement
37
36
  requirements:
38
37
  - - "~>"
@@ -41,8 +40,9 @@ dependencies:
41
40
  - - ">="
42
41
  - !ruby/object:Gem::Version
43
42
  version: 1.2.12
44
- type: :runtime
43
+ name: moesif_api
45
44
  prerelease: false
45
+ type: :runtime
46
46
  version_requirements: !ruby/object:Gem::Requirement
47
47
  requirements:
48
48
  - - "~>"
@@ -96,7 +96,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
98
  requirements: []
99
- rubygems_version: 3.1.4
99
+ rubyforge_project:
100
+ rubygems_version: 2.7.10
100
101
  signing_key:
101
102
  specification_version: 4
102
103
  summary: moesif_rack