Skip to content

Commit 8c82411

Browse files
Merge pull request #150 from igorkasyanchuk/deploy_events
Deployment events
2 parents 7bc8327 + 5a741b2 commit 8c82411

File tree

16 files changed

+163
-12
lines changed

16 files changed

+163
-12
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ gemspec
1515
# gem 'byebug', group: [:development, :test]
1616
gem "rails", "8.0.0"
1717

18-
gem "pry"
18+
gem "debug"
1919
gem "sqlite3"
2020

2121
gem "simplecov", require: false, group: :test

Gemfile.lock

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ GEM
8787
bigdecimal (3.1.8)
8888
browser (5.3.1)
8989
builder (3.3.0)
90-
coderay (1.1.3)
9190
concurrent-ruby (1.3.4)
9291
connection_pool (2.4.1)
9392
crass (1.0.6)
9493
csv (3.3.0)
9594
daemons (1.4.1)
9695
date (3.4.0)
96+
debug (1.11.0)
97+
irb (~> 1.10)
98+
reline (>= 0.3.8)
9799
delayed_job (4.1.13)
98100
activesupport (>= 3.0, < 9.0)
99101
delayed_job_active_record (4.1.11)
@@ -155,7 +157,6 @@ GEM
155157
net-pop
156158
net-smtp
157159
marcel (1.0.4)
158-
method_source (1.1.0)
159160
mimemagic (0.4.3)
160161
nokogiri (~> 1)
161162
rake
@@ -185,9 +186,6 @@ GEM
185186
parser (3.3.6.0)
186187
ast (~> 2.4.1)
187188
racc
188-
pry (0.15.0)
189-
coderay (~> 1.1)
190-
method_source (~> 1.0)
191189
psych (5.2.0)
192190
stringio
193191
puma (6.5.0)
@@ -324,13 +322,13 @@ DEPENDENCIES
324322
activestorage
325323
csv
326324
daemons
325+
debug
327326
delayed_job_active_record
328327
devise
329328
get_process_mem
330329
grape
331330
mimemagic (= 0.4.3)
332331
ostruct
333-
pry
334332
puma
335333
rails (= 8.0.0)
336334
rails_performance!

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ It allows you to track:
3434
- total duration of time spent per request, views rendering, DB
3535
- SQL queries, rendering logs in "Recent Requests" section
3636
- simple 500-crashes reports
37+
- deployment events (or custom events)
3738
- Sidekiq jobs
3839
- Delayed Job jobs
3940
- Grape API inside Rails app
@@ -237,6 +238,31 @@ More information here: `lib/rails_performance/engine.rb`.
237238

238239
PS: right now it can only distinguish between web app servers and the sidekiq servers.
239240

241+
#### Deployment Events + Custom Events on the Charts
242+
243+
![Deployments](docs/deploys.png)
244+
245+
#### For Kamal
246+
247+
- edit `.kamal/hooks/post-deploy` (rename .sample if needed)
248+
- add `kamal app exec -p './bin/rails runner "RailsPerformance.create_event(name: \"Deploy\")"'`
249+
- kamal deploy
250+
251+
#### Custom Events on the Charts
252+
253+
You can specify colors, orientation for the event label.
254+
255+
```ruby
256+
RailsPerformance.create_event(name: "Deploy", options: {
257+
borderColor: "#00E396",
258+
label: {
259+
borderColor: "#00E396",
260+
orientation: "horizontal",
261+
text: "Deploy"
262+
}
263+
})
264+
```
265+
240266
### Custom events
241267

242268
```ruby

app/views/rails_performance/javascripts/app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ function showTIRChart(element_id, data, addon, name) {
7979
series: [{
8080
name: name,
8181
data: data
82-
}]
82+
}],
83+
annotations: window?.annotationsData || {}
8384
});
8485
}
8586

@@ -90,6 +91,7 @@ function showRTChart(element_id, data) {
9091
name: 'Response Time',
9192
data: data,
9293
}],
94+
annotations: window?.annotationsData || {}
9395
});
9496
}
9597

app/views/rails_performance/layouts/rails_performance.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
<%= csp_meta_tag if ::Rails::VERSION::STRING.to_f >= 5.2 %>
99
<%= render '/rails_performance/stylesheets/stylesheets' %>
1010
<link rel="shortcut icon" href="/favicon.ico">
11+
<script>
12+
window.annotationsData = <%= raw RailsPerformance::Reports::AnnotationsReport.new.data.to_json %>;
13+
</script>
1114
</head>
1215

1316
<body class="has-sticky-footer">

docs/deploys.png

200 KB
Loading

lib/rails_performance.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@
2626
require_relative "rails_performance/reports/trace_report"
2727
require_relative "rails_performance/reports/percentile_report"
2828
require_relative "rails_performance/reports/resources_report"
29+
require_relative "rails_performance/reports/annotations_report"
30+
require_relative "rails_performance/events/record"
2931
require_relative "rails_performance/extensions/trace"
3032
require_relative "rails_performance/thread/current_request"
33+
require_relative "rails_performance/interface"
3134

3235
module RailsPerformance
36+
extend RailsPerformance::Interface
37+
3338
FORMAT = "%Y%m%dT%H%M"
3439

3540
mattr_accessor :redis

lib/rails_performance/data_source.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def initialize(type:, q: {}, days: RailsPerformance::Utils.days(RailsPerformance
2222

2323
def db
2424
result = RailsPerformance::Models::Collection.new
25-
now = RailsPerformance::Utils.time
25+
now = RailsPerformance::Utils.kind_of_now
2626
(0..days).to_a.reverse_each do |e|
2727
RailsPerformance::DataSource.new(q: q.merge({on: (now - e.days).to_date}), type: type).add_to(result)
2828
end
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module RailsPerformance
2+
module Events
3+
class Record
4+
attr_reader :name, :datetimei, :options
5+
6+
DEFAULT_COLOR = "#FF00FF"
7+
DEFAULT_LABEL_COLOR = "#FF00FF"
8+
DEFAULT_LABEL_ORIENTATION = "horizontal"
9+
10+
class << self
11+
def create(name:, datetimei: Time.now.to_i, options: {})
12+
instance = new(name: name, datetimei: datetimei, options: options)
13+
instance.save
14+
instance
15+
end
16+
17+
def all
18+
_, values = RailsPerformance::Utils.fetch_from_redis("rails_performance:records:events:*")
19+
Array(values).map do |value|
20+
json = JSON.parse(value)
21+
new(name: json["name"], datetimei: json["datetimei"], options: Hash(json["options"]))
22+
end
23+
end
24+
end
25+
26+
def initialize(name:, datetimei:, options: {})
27+
@name = name
28+
@datetimei = datetimei
29+
@options = options
30+
end
31+
32+
def save
33+
RailsPerformance::Utils.save_to_redis(rails_performance_key, value)
34+
end
35+
36+
def rails_performance_key
37+
"rails_performance:records:events:#{datetimei}|#{RailsPerformance::EVENTS_SCHEMA}"
38+
end
39+
40+
def value
41+
{
42+
name: name,
43+
datetime: RailsPerformance::Utils.from_datetimei(datetimei.to_i),
44+
datetimei: datetimei,
45+
options: options
46+
}
47+
end
48+
49+
def to_annotation
50+
{
51+
x: datetimei * 1000,
52+
borderColor: options.dig("borderColor") || DEFAULT_COLOR,
53+
label: {
54+
borderColor: options.dig("label", "borderColor") || DEFAULT_LABEL_COLOR,
55+
orientation: options.dig("label", "orientation") || DEFAULT_LABEL_ORIENTATION,
56+
text: options.dig("label", "text") || name
57+
}
58+
}
59+
end
60+
end
61+
end
62+
end

lib/rails_performance/interface.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module RailsPerformance
2+
module Interface
3+
def create_event(name:, datetime: RailsPerformance::Utils.time, options: {})
4+
RailsPerformance::Events::Record.create(name: name, datetimei: datetime.to_i, options: options)
5+
end
6+
end
7+
end

0 commit comments

Comments
 (0)