Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more configs and better readme for system monitoring tool #115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- 1.4.0 (unreleased)
- track CPU, memory, storage: https://github.com/igorkasyanchuk/rails_performance/pull/111
- use UTC time: https://github.com/igorkasyanchuk/rails_performance/pull/114
- custom expiration time for system monitoring report: https://github.com/igorkasyanchuk/rails_performance/pull/115/files

- 1.3.3
- little improvements and bug fixes
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rails_performance (1.4.0.alpha4)
rails_performance (1.4.0.alpha5)
browser
railties
redis
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ RailsPerformance.setup do |config|

# To monitor custom events with `RailsPerformance.measure` block
# config.include_custom_events = true

# To monitor system resources (CPU, memory, disk)
# to enabled add required gems (see README)
# config.system_monitor_duration = 24.hours
end if defined?(RailsPerformance)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def index
end

def resources
@datasource = RailsPerformance::DataSource.new(**prepare_query(params), type: :resources)
@datasource = RailsPerformance::DataSource.new(**prepare_query(params), type: :resources, days: RailsPerformance::Utils.days(RailsPerformance.system_monitor_duration))
db = @datasource.db

@resources_report = RailsPerformance::Reports::ResourcesReport.new(db)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,9 @@
config.skipable_rake_tasks = ["webpacker:compile"]
config.include_rake_tasks = false
config.include_custom_events = true

# If enabled, the system monitor will be displayed on the dashboard
# to enabled add required gems (see README)
# config.system_monitor_duration = 24.hours
end
end
8 changes: 8 additions & 0 deletions lib/rails_performance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ def self.ignored_paths=(paths)
mattr_accessor :ignore_trace_headers
@@ignore_trace_headers = ["datetimei"]

# System monitor duration (expiration time)
mattr_accessor :system_monitor_duration
@@system_monitor_duration = 24.hours

# -- internal usage --
#
#
# to store the resource monitor instance
mattr_accessor :_resource_monitor
@@_resource_monitor = nil

Expand Down
7 changes: 4 additions & 3 deletions lib/rails_performance/data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ class DataSource
resources: RailsPerformance::Models::ResourceRecord
}

attr_reader :q, :klass, :type
attr_reader :q, :klass, :type, :days

def initialize(type:, q: {})
def initialize(type:, q: {}, days: RailsPerformance::Utils.days(RailsPerformance.duration))
@type = type
@klass = KLASSES[type]
q[:on] ||= Date.today
@q = q
@days = days
end

def db
result = RailsPerformance::Models::Collection.new
now = RailsPerformance::Utils.time
(0..(RailsPerformance::Utils.days)).to_a.reverse_each do |e|
(0..days).to_a.reverse_each do |e|
RailsPerformance::DataSource.new(q: q.merge({on: (now - e.days).to_date}), type: type).add_to(result)
end
result
Expand Down
3 changes: 2 additions & 1 deletion lib/rails_performance/models/resource_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def record_hash

def save
key = "resource|server|#{server}|context|#{context}|role|#{role}|datetime|#{datetime}|datetimei|#{datetimei}|END|#{RailsPerformance::SCHEMA}"
Utils.save_to_redis(key, json)
# with longer expiration time
Utils.save_to_redis(key, json, RailsPerformance.system_monitor_duration.to_i)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/rails_performance/reports/base_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ def calculate_data
# 1732125550000 => 0,
# ....
# }
def nil_data
def nil_data(duration = RailsPerformance.duration)
@nil_data ||= begin
result = {}
now = RailsPerformance::Utils.time
now = now.change(sec: 0, usec: 0)
stop = now # Time.at(60 * (now.to_i / 60))
offset = 0 # RailsPerformance::Reports::BaseReport.time_in_app_time_zone(now).utc_offset
current = stop - RailsPerformance.duration
current = stop - duration

while current <= stop
current.strftime(RailsPerformance::FORMAT)
Expand All @@ -86,8 +86,8 @@ def nil_data
# 1732125540000 => 1,
# 1732125550000 => 0,
# }
def nullify_data(input)
nil_data.merge(input).sort
def nullify_data(input, duration = RailsPerformance.duration)
nil_data(duration).merge(input).sort
end
end
end
Expand Down
14 changes: 10 additions & 4 deletions lib/rails_performance/reports/resources_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,39 @@ def data
@data ||= db.data
.collect { |e| e.record_hash }
.group_by { |e| e[:server] + "///" + e[:context] + "///" + e[:role] }
.transform_values { |v| v.sort { |a, b| b[sort] <=> a[sort] } }
# .transform_values { |v| v.sort { |a, b| b[sort] <=> a[sort] } }
.transform_values { |v| v.map { |e| e.merge({datetimei: e[:datetimei].to_i}) } }
end

def cpu
@cpu ||= data.transform_values do |v|
nullify_data(v.each_with_object({}) do |e, res|
prepare_report(v.each_with_object({}) do |e, res|
res[e[:datetimei] * 1000] = e[:cpu]["one_min"].to_f.round(2)
end)
end
end

def memory
@memory ||= data.transform_values do |v|
nullify_data(v.each_with_object({}) do |e, res|
prepare_report(v.each_with_object({}) do |e, res|
res[e[:datetimei] * 1000] = e[:memory].to_f.round(2)
end)
end
end

def disk
@disk ||= data.transform_values do |v|
nullify_data(v.each_with_object({}) do |e, res|
prepare_report(v.each_with_object({}) do |e, res|
res[e[:datetimei] * 1000] = e[:disk]["available"].to_f.round(2)
end)
end
end

private

def prepare_report(input)
nullify_data(input, RailsPerformance.system_monitor_duration)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/rails_performance/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def self.save_to_redis(key, value, expire = RailsPerformance.duration.to_i)
RailsPerformance.redis.set(key, value.to_json, ex: expire.to_i)
end

def self.days
(RailsPerformance.duration / 1.day) + 1
def self.days(duration = RailsPerformance.duration)
(duration / 1.day) + 1
end

def self.median(array)
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_performance/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module RailsPerformance
VERSION = "1.4.0.alpha4"
VERSION = "1.4.0.alpha5"
SCHEMA = "1.0.2"
end
4 changes: 4 additions & 0 deletions test/dummy/config/initializers/rails_performance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@

config.include_rake_tasks = true
config.include_custom_events = true

# If enabled, the system monitor will be displayed on the dashboard
# to enabled add required gems (see README)
config.system_monitor_duration = 24.hours
end
end