wor-prof 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/wor-prof.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'wprof/version'
2
+ require 'wprof/supports/style_support'
3
+ require 'wprof/conf_wprof'
4
+ require 'wprof/reporters/db_report'
5
+ require 'wprof/reporters/external_report'
6
+ require 'wprof/reporters/file_report'
7
+ require 'wprof/wprof_reporter'
8
+ require 'wprof/gen_prof'
9
+ require 'wprof/wprof_subscriptors'
10
+
11
+
12
+ module Wprof
13
+ def self.included(base)
14
+ load 'wprof/wprof_modules.rb'
15
+ base.extend(ForHttparty) if base.include?(HTTParty)
16
+ base.prepend(CustomMethods) unless WProf::Config.get_value(:custom_methods).nil?
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ module WProf
2
+ class Config
3
+ DEFAULTS_CONFIGS = {
4
+ db_runtime: true,
5
+ reporter_type: 'LOGGER',
6
+ csv_type: 'SPLIT',
7
+ async: false,
8
+ httparty_methods_to_trace: %w[get put delete post patch].freeze,
9
+ external_url: nil,
10
+ external_headers: nil,
11
+ custom_methods: nil,
12
+ file_path: Rails.root.to_s
13
+ }.freeze
14
+
15
+ class << self
16
+ def get_value(param)
17
+ unless Rails.application.nil?
18
+ value = Rails.configuration.x.wprof[param]
19
+ return value unless value.nil?
20
+ end
21
+ DEFAULTS_CONFIGS[param]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,73 @@
1
+ class GenProf
2
+ include Wprof::Supports::StyleSupport
3
+
4
+ def initialize(event, rec_type)
5
+ @event = event
6
+ @db_runtime = WProf::Config.get_value(:db_runtime)
7
+ @async_report = WProf::Config.get_value(:async)
8
+ @rec_type = rec_type
9
+ generate_profiling
10
+ end
11
+
12
+ def generate_profiling
13
+ generate_common_params
14
+ generate_custom_params
15
+ deploy_reporter
16
+ end
17
+
18
+ private
19
+
20
+ def generate_common_params
21
+ @params = {
22
+ transaction_id: @event.transaction_id,
23
+ total_time: format_time(@event.duration),
24
+ start_dt: @event.time,
25
+ end_dt: @event.end
26
+ }
27
+ end
28
+
29
+ def generate_custom_params
30
+ case @rec_type
31
+ when :standard
32
+ app_params
33
+ when :service
34
+ service_params
35
+ when :custom
36
+ custom_params
37
+ end
38
+ end
39
+
40
+ def app_params
41
+ for_app_only = {
42
+ code: @event.payload[:status],
43
+ controller: @event.payload[:controller],
44
+ url: @event.payload[:path]
45
+ }
46
+ for_app_only[:db_runtime] = format_time(@event.payload[:db_runtime]) if @db_runtime
47
+ @params.merge!(for_app_only)
48
+ end
49
+
50
+ def service_params
51
+ for_service_only = {
52
+ code: @event.payload[:status],
53
+ service_hostname: @event.payload[:service_hostname],
54
+ request_uri: @event.payload[:request_uri]
55
+ }
56
+ @params.merge!(for_service_only)
57
+ end
58
+
59
+ def custom_params
60
+ for_customs_only = {
61
+ method: @event.payload[:method]
62
+ }
63
+ @params.merge!(for_customs_only)
64
+ end
65
+
66
+ def deploy_reporter
67
+ if @async_report
68
+ WprofReporter.perform_async(@params, @rec_type)
69
+ else
70
+ WprofReporter.new.perform(@params, @rec_type)
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,16 @@
1
+ module Wprof
2
+ module Reporters
3
+ module DatabaseReport
4
+ def db_report
5
+ case @rec_type
6
+ when :service
7
+ WprofService.create(@data)
8
+ when :standard
9
+ WprofController.create(@data)
10
+ when :custom
11
+ WprofMethod.create(@data)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ module Wprof
2
+ module Reporters
3
+ module ExternalReport
4
+ def generate_external_report
5
+ options = { body: @data }
6
+ headers = WProf::Config.get_value(:external_headers)
7
+ options.merge!(headers) unless headers.nil?
8
+ HTTParty.post(WProf::Config.get_value(:external_url),
9
+ options)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,59 @@
1
+ module Wprof
2
+ module Reporters
3
+ module FileReport
4
+ def generate_file_report
5
+ require 'csv'
6
+ csv_type = WProf::Config.get_value(:csv_type) # rubocop:disable Lint/UselessAssignment
7
+ path = WProf::Config.get_value(:file_path)
8
+ send('write_#{csv_type.undercase}_file', path)
9
+ end
10
+
11
+ def write_mix_file(path)
12
+ CSV.open("#{path}/wprof.csv", 'ab') do |csv|
13
+ @data.to_a.each do |elem|
14
+ csv << elem
15
+ end
16
+ end
17
+ end
18
+
19
+ def write_split_file(path)
20
+ make_files(path)
21
+ case @rec_type
22
+ when :service
23
+ CSV.open("#{path}/wprofservice.csv", 'ab') do |csv|
24
+ csv << @data.values
25
+ end
26
+ when :standard
27
+ CSV.open("#{path}/wprofcontroller.csv", 'ab') do |csv|
28
+ csv << @data.values
29
+ end
30
+ when :custom
31
+ CSV.open("#{path}/wprofmethods.csv", 'ab') do |csv|
32
+ csv << @data.values
33
+ end
34
+ end
35
+ end
36
+
37
+ def make_files(path)
38
+ unless File.exist?("#{path}/wprofservice.csv")
39
+ params = %w[transaction_id total_time start_dt end_dt code service_hostname request_uri]
40
+ CSV.open("#{path}/wprofservice.csv", 'wb') do |csv|
41
+ csv << params
42
+ end
43
+ end
44
+ unless File.exist?("#{path}/wprofcontroller.csv")
45
+ params = %w[transaction_id total_time start_dt end_dt code controller url db_runtime]
46
+ CSV.open("#{path}/wprofcontroller.csv", 'wb') do |csv|
47
+ csv << params
48
+ end
49
+ end
50
+ unless File.exist?("#{path}/wprofmethods.csv") # rubocop:disable Style/GuardClause
51
+ params = %w[transaction_id total_time start_dt end_dt method]
52
+ CSV.open("#{path}/wprofmethods.csv", 'wb') do |csv|
53
+ csv << params
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,9 @@
1
+ module Wprof
2
+ module Supports
3
+ module StyleSupport
4
+ def format_time(time)
5
+ time.round(4)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Wprof
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ module ForHttparty
2
+ httparty_methods_to_trace = WProf::Config.get_value(:httparty_methods_to_trace)
3
+ httparty_methods_to_trace.each do |name_method|
4
+ define_method(name_method.to_s) do |*arg|
5
+ ActiveSupport::Notifications.instrument 'wprof.service', data = {} do
6
+ rest = super(*arg)
7
+ begin
8
+ data[:status] = rest&.code
9
+ data[:service_hostname] = rest&.request&.path&.hostname
10
+ data[:request_uri] = rest&.request&.path&.request_uri
11
+ rescue StandardError => e
12
+ err_message = "WProf ERROR when try parsing service response: #{e}"
13
+ Rails.logger.warn(err_message)
14
+ end
15
+ rest
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ module CustomMethods
22
+ custom_methods = WProf::Config.get_value(:custom_methods)
23
+ unless custom_methods.nil?
24
+ custom_methods.each do |name_method|
25
+ define_method(name_method.to_s) do |*arg|
26
+ ActiveSupport::Notifications.instrument 'wprof.custom', method: name_method.to_s do
27
+ super(*arg)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,34 @@
1
+ class WprofReporter
2
+ include Sidekiq::Worker
3
+ include Wprof::Reporters::FileReport
4
+ include Wprof::Reporters::DatabaseReport
5
+ include Wprof::Reporters::ExternalReport
6
+
7
+ def perform(data, rec_type)
8
+ @data = data
9
+ @rec_type = rec_type.to_sym
10
+ reporter
11
+ rescue StandardError => error
12
+ Rails.logger.warn("An error was raised when WProf tried to send data to reporter: #{error}")
13
+ end
14
+
15
+ def reporter
16
+ reporter_type = WProf::Config.get_value(:reporter_type)
17
+ case reporter_type
18
+ when 'LOGGER'
19
+ logger_report
20
+ when 'FILE'
21
+ generate_file_report
22
+ when 'DATABASE'
23
+ db_report
24
+ when 'EXTERNAL'
25
+ generate_external_report
26
+ else
27
+ logger_report
28
+ end
29
+ end
30
+
31
+ def logger_report
32
+ Rails.logger.info(@data)
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ module WprofSubscriptors
2
+ ActiveSupport::Notifications.subscribe 'process_action.action_controller' do |*args|
3
+ event = ActiveSupport::Notifications::Event.new(*args)
4
+ GenProf.new(event, :standard)
5
+ end
6
+
7
+ ActiveSupport::Notifications.subscribe 'wprof.service' do |*args|
8
+ event = ActiveSupport::Notifications::Event.new(*args)
9
+ GenProf.new(event, :service)
10
+ end
11
+
12
+ ActiveSupport::Notifications.subscribe 'wprof.custom' do |*args|
13
+ event = ActiveSupport::Notifications::Event.new(*args)
14
+ GenProf.new(event, :custom)
15
+ end
16
+ end
data/wor-prof.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ require File.expand_path("../lib/wprof/version", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'wor-prof'
5
+ s.version = Wprof::VERSION
6
+ s.date = '2019-04-05'
7
+ s.summary = 'Wor-Profiling Gem for Rails'
8
+ s.description = "Wor-prof (Wprof) is a gem for Ruby On Rails which its only purpose is to measure a RoR app's performance through a profile with different times of response.
9
+ Catch all request and save them into a database, csv file or send to external service, it's easy to configure and use. Wprof can take measure of HTTParty requests and your own methods."
10
+ s.authors = ['mcolombo87']
11
+ s.author = 'Maximiliano Colombo'
12
+ s.email = ['maximiliano.colombo@wolox.com.ar', 'mcolombo87@gmail.com']
13
+ s.require_paths = ['lib']
14
+ s.files = `git ls-files`.split("\n").reject { |f| f.match(%r{^(test|spec)/}) }
15
+
16
+ s.homepage =
17
+ 'https://github.com/Wolox/wor-prof'
18
+ s.license = 'MIT'
19
+ s.extra_rdoc_files = ['README.md', 'doc/user-guide_en.md','doc/user-guide_es.md' ]
20
+ s.required_ruby_version = '>= 2.4'
21
+
22
+ s.add_dependency 'rails', '>= 4.0.2'
23
+ s.add_dependency 'sidekiq', '~> 5.2'
24
+ s.add_dependency 'httparty', '~> 0.15'
25
+ s.add_development_dependency "bundler", "~> 1.17"
26
+ s.add_development_dependency "byebug"
27
+ s.add_development_dependency 'factory_bot_rails'
28
+ s.add_development_dependency 'faker'
29
+ s.add_development_dependency 'rails', '>= 4.0.2'
30
+ s.add_development_dependency "rake", "~> 10.0"
31
+ s.add_development_dependency "rspec", "~> 3.0"
32
+ s.add_development_dependency 'sidekiq', '~> 5.2'
33
+ s.add_development_dependency 'webmock'
34
+ s.add_development_dependency 'simplecov'
35
+ s.add_development_dependency 'simplecov-console'
36
+ end
metadata ADDED
@@ -0,0 +1,271 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wor-prof
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Maximiliano Colombo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: sidekiq
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.15'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.17'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.17'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: factory_bot_rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: faker
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 4.0.2
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 4.0.2
125
+ - !ruby/object:Gem::Dependency
126
+ name: rake
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '10.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '10.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '3.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: sidekiq
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '5.2'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '5.2'
167
+ - !ruby/object:Gem::Dependency
168
+ name: webmock
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: simplecov
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: simplecov-console
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ description: |-
210
+ Wor-prof (Wprof) is a gem for Ruby On Rails which its only purpose is to measure a RoR app's performance through a profile with different times of response.
211
+ Catch all request and save them into a database, csv file or send to external service, it's easy to configure and use. Wprof can take measure of HTTParty requests and your own methods.
212
+ email:
213
+ - maximiliano.colombo@wolox.com.ar
214
+ - mcolombo87@gmail.com
215
+ executables: []
216
+ extensions: []
217
+ extra_rdoc_files:
218
+ - README.md
219
+ - doc/user-guide_en.md
220
+ - doc/user-guide_es.md
221
+ files:
222
+ - ".gitignore"
223
+ - ".rubocop.yml"
224
+ - ".travis.yml"
225
+ - Gemfile
226
+ - LICENSE
227
+ - README.md
228
+ - Rakefile
229
+ - bin/console
230
+ - bin/setup
231
+ - doc/user-guide_en.md
232
+ - doc/user-guide_es.md
233
+ - lib/generators/wprof_generator.rb
234
+ - lib/generators/wprof_model_generator.rb
235
+ - lib/wor-prof.rb
236
+ - lib/wprof/conf_wprof.rb
237
+ - lib/wprof/gen_prof.rb
238
+ - lib/wprof/reporters/db_report.rb
239
+ - lib/wprof/reporters/external_report.rb
240
+ - lib/wprof/reporters/file_report.rb
241
+ - lib/wprof/supports/style_support.rb
242
+ - lib/wprof/version.rb
243
+ - lib/wprof/wprof_modules.rb
244
+ - lib/wprof/wprof_reporter.rb
245
+ - lib/wprof/wprof_subscriptors.rb
246
+ - wor-prof.gemspec
247
+ homepage: https://github.com/Wolox/wor-prof
248
+ licenses:
249
+ - MIT
250
+ metadata: {}
251
+ post_install_message:
252
+ rdoc_options: []
253
+ require_paths:
254
+ - lib
255
+ required_ruby_version: !ruby/object:Gem::Requirement
256
+ requirements:
257
+ - - ">="
258
+ - !ruby/object:Gem::Version
259
+ version: '2.4'
260
+ required_rubygems_version: !ruby/object:Gem::Requirement
261
+ requirements:
262
+ - - ">="
263
+ - !ruby/object:Gem::Version
264
+ version: '0'
265
+ requirements: []
266
+ rubyforge_project:
267
+ rubygems_version: 2.7.6
268
+ signing_key:
269
+ specification_version: 4
270
+ summary: Wor-Profiling Gem for Rails
271
+ test_files: []