Skip to content

Commit

Permalink
Display metrics in human friendly units (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
bedrock-adam authored Nov 17, 2024
1 parent 834d1a7 commit 677dec2
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bin/outboxer_sidekiq_publisher
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ Outboxer::Publisher.publish(
EventCreatedJob.perform_async({ 'id' => message[:messageable_id] })

logger.info "Outboxer published message id=#{message[:id]} "\
"messageable=#{message[:messageable_type]}/#{message[:messageable_id]}"\
"messageable=#{message[:messageable_type]}::#{message[:messageable_id]}"\
end
end
38 changes: 36 additions & 2 deletions lib/outboxer/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,41 @@ def outboxer_path(path)
"#{request.script_name}#{path}"
end

def human_readable_size(kilobytes)
def pretty_throughput(per_second: 0)
return "-" if per_second == 0

"#{per_second} /s"
end

def pretty_duration_from_period(start_time:, end_time: ::Process.clock_gettime(::Process::CLOCK_MONOTONIC))
pretty_duration(seconds: end_time - start_time)
end

def pretty_duration_from_seconds(seconds:)
return "-" if seconds <= 0

units = [
{ name: "y", scale: 1.0 / 31_536_000 }, # 1 year = 31,536,000 seconds (365 days)
{ name: "mo", scale: 1.0 / 2_592_000 }, # 1 month = 2,592,000 seconds (30 days)
{ name: "w", scale: 1.0 / 604_800 }, # 1 week = 604,800 seconds
{ name: "d", scale: 1.0 / 86_400 }, # 1 day = 86,400 seconds
{ name: "h", scale: 1.0 / 3_600 }, # 1 hour = 3,600 seconds
{ name: "min", scale: 1.0 / 60 }, # 1 minute = 60 seconds
{ name: "s", scale: 1 }, # 1 second = 1 second
{ name: "ms", scale: 1_000 }, # 1 millisecond = 1/1,000 second
{ name: "µs", scale: 1_000_000 }, # 1 microsecond = 1/1,000,000 second
{ name: "ns", scale: 1_000_000_000 } # 1 nanosecond = 1/1,000,000,000 second
].freeze

units.each do |unit|
value = seconds * unit[:scale]
if value >= 1 || unit[:name] == "ns"
return "#{value.round(0)} #{unit[:name]}"
end
end
end

def human_readable_size(kilobytes:)
units = ['KB', 'MB', 'GB', 'TB']
size = kilobytes.to_f
unit = units.shift
Expand All @@ -37,7 +71,7 @@ def human_readable_size(kilobytes)
unit = units.shift
end

"#{size.round(2)} #{unit}"
"#{size.round(0)} #{unit}"
end

def time_ago_in_words(time)
Expand Down
20 changes: 10 additions & 10 deletions lib/outboxer/web/views/home.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
<td><%= time_ago_in_words(publisher[:created_at]) %> ago</td>
<td><%= time_ago_in_words(publisher[:updated_at]) %> ago</td>
<td class="text-capitalize"><%= publisher[:status] %></td>
<td><%= publisher[:metrics]['throughput'] %></td>
<td><%= publisher[:metrics]['latency'] %></td>
<td><%= publisher[:metrics]['cpu'] %>%</td>
<td><%= human_readable_size(publisher[:metrics]['rss']) %></td>
<td><%= (publisher[:metrics]['rtt'] * 1000).round(2) %> ms</td>
<td><%= pretty_throughput(per_second: publisher[:metrics]['throughput']) %></td>
<td><%= pretty_duration_from_seconds(seconds: publisher[:metrics]['latency']) %></td>
<td><%= publisher[:metrics]['cpu'].round(0) %>%</td>
<td><%= human_readable_size(kilobytes: publisher[:metrics]['rss']) %></td>
<td><%= pretty_duration_from_seconds(seconds: publisher[:metrics]['rtt']) %></td>
<td>
<div class="d-flex gap-1">
<!-- Pause Button -->
Expand Down Expand Up @@ -114,11 +114,11 @@
<thead>
<tr>
<th scope="col">Status</th>
<th scope="col">Historic count</th>
<th scope="col">Current count</th>
<th scope="col">Total count</th>
<th scope="col">Throughput</th>
<th scope="col">Latency</th>
<th scope="col">Historic count</th>
<th scope="col">Total count</th>
</tr>
</thead>
<tbody>
Expand All @@ -129,11 +129,11 @@
<%= status %>
</a>
</td>
<td><%= messages_metrics[status.to_sym][:count][:historic] || '-' %></td>
<td><%= messages_metrics[status.to_sym][:count][:current] %></td>
<td><%= pretty_throughput(per_second: messages_metrics[status.to_sym][:throughput]) %></td>
<td><%= pretty_duration_from_seconds(seconds: messages_metrics[status.to_sym][:latency]) %></td>
<td><%= messages_metrics[status.to_sym][:count][:historic] || '-' %></td>
<td><%= messages_metrics[status.to_sym][:count][:total] || '-' %></td>
<td><%= messages_metrics[status.to_sym][:throughput] %></td>
<td><%= messages_metrics[status.to_sym][:latency] %></td>
</tr>
<% end %>
</tbody>
Expand Down
4 changes: 2 additions & 2 deletions lib/outboxer/web/views/message.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="container my-4">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h3 class="mb-0">Message <%= message[:id] %></h3>
<h3 class="mb-0">Message::<%= message[:id] %></h3>
<div class="card-header-buttons">
<!-- Requeue Form -->
<form action="<%= outboxer_path("/message/#{message[:id]}/requeue") %>" method="post" style="display: inline;">
Expand Down Expand Up @@ -43,7 +43,7 @@
</tr>
<tr>
<th scope="row">Status</th>
<td><%= message[:status] %></td>
<td class="text-capitalize"><%= message[:status] %></td>
</tr>
<tr>
<th scope="row">Created</th>
Expand Down
2 changes: 1 addition & 1 deletion lib/outboxer/web/views/messageable.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="container my-4">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h3 class="mb-0"><%= messageable.class.name %> <%= messageable.id %></h3>
<h3 class="mb-0"><%= messageable.class.name %>::<%= messageable.id %></h3>
</div>
<div class="card-body">
<table class="table">
Expand Down
4 changes: 2 additions & 2 deletions lib/outboxer/web/views/messages.erb
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@
<%= message[:id] %>
</a>
</td>
<td><%= message[:status] %></td>
<td class="text-capitalize"><%= message[:status] %></td>
<td>
<a href="<%= outboxer_path("/message/#{message[:id]}/messageable#{normalised_query_string}") %>" class="custom-link">
<%= message[:messageable_type] %>/<%= message[:messageable_id] %>
<%= message[:messageable_type] %>::<%= message[:messageable_id] %>
</a>
</td>
<td title="<%= message[:created_at].in_time_zone(denormalised_query_params[:time_zone]) %>">
Expand Down
12 changes: 6 additions & 6 deletions lib/outboxer/web/views/publisher.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="container my-4">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h3 class="mb-0">Publisher <%= publisher[:id] %></h3>
<h3 class="mb-0">Publisher::<%= publisher[:id] %></h3>
<div class="card-header-buttons">
<!-- Pause Button -->
<form action="<%= outboxer_path("/publisher/#{publisher[:id]}/signals") %>" method="post" style="display: inline;">
Expand Down Expand Up @@ -102,23 +102,23 @@
<tbody>
<tr>
<th scope="row">Throughput</th>
<td><%= publisher[:metrics]['throughput'] %></td>
<td><%= pretty_throughput(per_second: publisher[:metrics]['throughput']) %></td>
</tr>
<tr>
<th scope="row">Latency</th>
<td><%= publisher[:metrics]['latency'] %></td>
<td><%= pretty_duration_from_seconds(seconds: publisher[:metrics]['latency']) %></td>
</tr>
<tr>
<th scope="row">CPU</th>
<td><%= publisher[:metrics]['cpu'] %>%</td>
<td><%= publisher[:metrics]['cpu'].round(0) %>%</td>
</tr>
<tr>
<th scope="row">RSS</th>
<td><%= human_readable_size(publisher[:metrics]['rss']) %></td>
<td><%= human_readable_size(kilobytes: publisher[:metrics]['rss']) %></td>
</tr>
<tr>
<th scope="row">RTT</th>
<td><%= (publisher[:metrics]['rtt'] * 1000).round(2) %> ms</td>
<td><%= pretty_duration_from_seconds(seconds: publisher[:metrics]['rtt']) %></td>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit 677dec2

Please sign in to comment.