speedshop-cloudwatch 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/README.md +57 -5
- data/lib/speedshop/cloudwatch/all.rb +1 -0
- data/lib/speedshop/cloudwatch/config.rb +4 -2
- data/lib/speedshop/cloudwatch/metrics.rb +44 -2
- data/lib/speedshop/cloudwatch/observations/solid_queue.rb +90 -0
- data/lib/speedshop/cloudwatch/observations.rb +1 -0
- data/lib/speedshop/cloudwatch/reporter.rb +77 -65
- data/lib/speedshop/cloudwatch/request_size.rb +31 -0
- data/lib/speedshop/cloudwatch/solid_queue.rb +59 -0
- data/lib/speedshop/cloudwatch/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f88cf52e836639196bffac94062d95b4ee9d6b8943b60baa48b79d8ed17e49d7
|
|
4
|
+
data.tar.gz: d0a9e0d7ccf1c4d045cb423efe7a3b7a606f9fd3cffeab4220140d6db434b830
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b740fa84be92541ef34432fbec47d3142a39cac97cfc2c40044393a2d9b5d79b0a1388f07cb562d6cd1e7f5941823320f06fb20d7b113fcf3e69578393a20f2c
|
|
7
|
+
data.tar.gz: 9b18ddf279f05614a701e6ff36788d57c32eafed40d4fd92dcc6a72e89e1160ad43701365307bee711b4d968f16b972eeeeeeee6de183ce3bd57883a3f18754a
|
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.3.0] - 2026-12-02
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- Added a Solid Queue integration that reports each supervisor's process metrics and per-queue metrics, in fork and async mode, and with Solid Queue's Puma plugin. Requires Solid Queue 1.6 or newer.
|
|
14
|
+
|
|
15
|
+
## [0.2.2] - 2026-09-07
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
- Preserve metric distributions with CloudWatch `Values` and `Counts` instead of reducing repeated observations to StatisticSets, enabling percentile statistics without changing application configuration.
|
|
19
|
+
|
|
8
20
|
## [0.2.1] - 2026-06-09
|
|
9
21
|
|
|
10
22
|
### Fixed
|
|
@@ -27,6 +39,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
27
39
|
### Added
|
|
28
40
|
- Initial public release.
|
|
29
41
|
|
|
42
|
+
[Unreleased]: https://github.com/speedshop/speedshop-cloudwatch/compare/v0.3.0...HEAD
|
|
43
|
+
[0.3.0]: https://github.com/speedshop/speedshop-cloudwatch/compare/v0.2.2...v0.3.0
|
|
44
|
+
[0.2.2]: https://github.com/speedshop/speedshop-cloudwatch/compare/v0.2.1...v0.2.2
|
|
30
45
|
[0.2.1]: https://github.com/speedshop/speedshop-cloudwatch/compare/v0.2.0...v0.2.1
|
|
31
46
|
[0.2.0]: https://github.com/speedshop/speedshop-cloudwatch/compare/v0.1.0...v0.2.0
|
|
32
47
|
[0.1.0]: https://github.com/speedshop/speedshop-cloudwatch/releases/tag/v0.1.0
|
data/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
<img src="https://github.com/user-attachments/assets/146ce110-311a-4acb-8000-66cd098f8b45" alt="Sit around and watch the clouds all day...">
|
|
5
5
|
</p>
|
|
6
6
|
|
|
7
|
-
This gem helps integrate your Ruby application with AWS CloudWatch for the purposes of auto-scaling. There are integrations for **Puma**, **Rack**, **Sidekiq** and **ActiveJob**.
|
|
7
|
+
This gem helps integrate your Ruby application with AWS CloudWatch for the purposes of auto-scaling. There are integrations for **Puma**, **Rack**, **Sidekiq**, **Solid Queue** and **ActiveJob**.
|
|
8
8
|
|
|
9
9
|
This gem is for **infrastructure and queue metrics**, not application performance metrics, like response times, job execution times, or error rates. Use your APM for that stuff.
|
|
10
10
|
|
|
@@ -12,7 +12,7 @@ CloudWatch is unusually difficult to integrate with properly in Ruby, because th
|
|
|
12
12
|
|
|
13
13
|
This library helps you avoid that latency by reporting to CloudWatch in a background thread.
|
|
14
14
|
|
|
15
|
-
This library supports **Ruby 2.7+, Sidekiq 7+, and Puma 6+**.
|
|
15
|
+
This library supports **Ruby 2.7+, Sidekiq 7+, Solid Queue 1.6+, and Puma 6+**.
|
|
16
16
|
|
|
17
17
|
## Metrics
|
|
18
18
|
|
|
@@ -23,6 +23,7 @@ By default, only essential queue metrics are enabled:
|
|
|
23
23
|
```ruby
|
|
24
24
|
config.metrics[:puma] = [] # Disabled by default
|
|
25
25
|
config.metrics[:sidekiq] = [:QueueLatency] # Per queue
|
|
26
|
+
config.metrics[:solid_queue] = [:QueueLatency] # Per queue
|
|
26
27
|
config.metrics[:rack] = [:RequestQueueTime]
|
|
27
28
|
config.metrics[:active_job] = [:QueueLatency] # Per queue
|
|
28
29
|
```
|
|
@@ -41,6 +42,12 @@ config.metrics[:sidekiq] = [
|
|
|
41
42
|
:DeadJobs, :Workers, :Processes, :DefaultQueueLatency, :Capacity,
|
|
42
43
|
:Utilization, :QueueLatency, :QueueSize
|
|
43
44
|
]
|
|
45
|
+
|
|
46
|
+
# Enable additional Solid Queue metrics
|
|
47
|
+
config.metrics[:solid_queue] = [
|
|
48
|
+
:Processes, :Workers, :Capacity, :Utilization,
|
|
49
|
+
:QueueLatency, :QueueSize
|
|
50
|
+
]
|
|
44
51
|
```
|
|
45
52
|
|
|
46
53
|
## Installation
|
|
@@ -78,9 +85,13 @@ Speedshop::Cloudwatch.configure do |config|
|
|
|
78
85
|
# Customize which Sidekiq queues to monitor (all queues by default)
|
|
79
86
|
config.sidekiq_queues = ["critical", "default", "low_priority"]
|
|
80
87
|
|
|
88
|
+
# Customize which Solid Queue queues to monitor (all queues by default)
|
|
89
|
+
config.solid_queue_queues = ["critical", "default", "low_priority"]
|
|
90
|
+
|
|
81
91
|
# Customize CloudWatch namespaces
|
|
82
92
|
config.namespaces[:puma] = "MyApp/Puma"
|
|
83
93
|
config.namespaces[:sidekiq] = "MyApp/Sidekiq"
|
|
94
|
+
config.namespaces[:solid_queue] = "MyApp/SolidQueue"
|
|
84
95
|
config.namespaces[:rack] = "MyApp/Rack"
|
|
85
96
|
config.namespaces[:active_job] = "MyApp/ActiveJob"
|
|
86
97
|
|
|
@@ -92,6 +103,16 @@ end
|
|
|
92
103
|
> [!WARNING]
|
|
93
104
|
> Setting `config.interval` to less than 60 seconds automatically enables [high-resolution storage](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html#high-resolution-metrics) (1-second granularity) in CloudWatch, which incurs additional costs.
|
|
94
105
|
|
|
106
|
+
### Percentiles and distribution statistics
|
|
107
|
+
|
|
108
|
+
When a metric has multiple observations in one reporting period, the reporter publishes them using CloudWatch `Values` and `Counts`. Repeated values are frequency-encoded, and distributions with more than CloudWatch's limit of 150 unique values are split across multiple metric datums. Requests retain the 20-datum cap and use a conservative serialized-size bound to stay within CloudWatch's uncompressed 1 MiB limit. An individual datum exceeding that bound is logged and discarded without preventing other datums from being sent.
|
|
109
|
+
|
|
110
|
+
This preserves the observed distribution, allowing CloudWatch to calculate `p50`, `p95`, `p99`, trimmed means, and other distribution statistics in addition to Average, Sum, Minimum, Maximum, and SampleCount. Observations discarded because `config.queue_max_size` was exceeded are not included.
|
|
111
|
+
|
|
112
|
+
CloudWatch does not provide percentile statistics for metrics containing negative values. Explicit `statistic_values:` input remains supported, but neither it nor historical StatisticSets recovers discarded distributions.
|
|
113
|
+
|
|
114
|
+
Explicit Yabeda gauge aggregation strategies retain their existing behavior: `most_recent` publishes the last value and `max` publishes the maximum value.
|
|
115
|
+
|
|
95
116
|
### Environment Control
|
|
96
117
|
|
|
97
118
|
**By default, the reporter only runs in production.** The environment is detected from `RAILS_ENV`, `RACK_ENV`, and defaults to `"development"`.
|
|
@@ -193,10 +214,40 @@ QueueSize - Size of each queue (Count) [per queue]
|
|
|
193
214
|
Metrics marked [per queue] include a QueueName dimension.
|
|
194
215
|
Utilization metrics include Tag and/or Hostname dimensions.
|
|
195
216
|
|
|
217
|
+
## Solid Queue
|
|
218
|
+
|
|
219
|
+
This integration registers Solid Queue lifecycle hooks automatically. Each supervisor reports process metrics for only its own workers, like Puma reports per server. Every supervisor also reports the queue metrics, so they're duplicated; aggregate them with Average or Maximum. Solid Queue 1.6 or newer is required.
|
|
220
|
+
|
|
221
|
+
The integration also works with Solid Queue's Puma plugin in either fork or async mode.
|
|
222
|
+
|
|
223
|
+
By default, only `QueueLatency` is reported. To enable additional metrics, configure them explicitly:
|
|
224
|
+
|
|
225
|
+
```ruby
|
|
226
|
+
Speedshop::Cloudwatch.configure do |config|
|
|
227
|
+
config.metrics[:solid_queue] = [
|
|
228
|
+
:Processes, :Workers, :Capacity, :Utilization,
|
|
229
|
+
:QueueLatency, :QueueSize
|
|
230
|
+
]
|
|
231
|
+
end
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
We report the following metrics:
|
|
235
|
+
|
|
236
|
+
```
|
|
237
|
+
Processes - Number of live worker processes for this supervisor (Count)
|
|
238
|
+
Workers - Number of worker threads processing jobs for this supervisor (Count)
|
|
239
|
+
Capacity - Total worker threads across this supervisor's workers (Count)
|
|
240
|
+
Utilization - Average utilization across this supervisor's workers (Percent)
|
|
241
|
+
QueueLatency - Latency for each queue (Seconds) [per queue]
|
|
242
|
+
QueueSize - Size of each queue (Count) [per queue]
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Metrics marked [per queue] include a QueueName dimension.
|
|
246
|
+
|
|
196
247
|
## ActiveJob
|
|
197
248
|
|
|
198
249
|
> [!WARNING]
|
|
199
|
-
> If you're using Sidekiq, just use
|
|
250
|
+
> If you're using Sidekiq or Solid Queue, just use those integrations and do not include the ActiveJob module.
|
|
200
251
|
|
|
201
252
|
In your ApplicationJob:
|
|
202
253
|
|
|
@@ -210,7 +261,7 @@ We report the following metrics:
|
|
|
210
261
|
QueueLatency - Time job spent waiting in queue before execution (Seconds)
|
|
211
262
|
```
|
|
212
263
|
|
|
213
|
-
This metric includes QueueName dimension and
|
|
264
|
+
This metric includes a QueueName dimension and preserves the per-interval distribution using CloudWatch `Values` and `Counts`.
|
|
214
265
|
|
|
215
266
|
## Yabeda
|
|
216
267
|
|
|
@@ -300,7 +351,7 @@ Speedshop::Cloudwatch.start!
|
|
|
300
351
|
|
|
301
352
|
## Disabling Automatic Integration
|
|
302
353
|
|
|
303
|
-
You can disable the auto-integration of Sidekiq and Puma by not requiring them:
|
|
354
|
+
You can disable the auto-integration of Sidekiq, Solid Queue and Puma by not requiring them:
|
|
304
355
|
|
|
305
356
|
```ruby
|
|
306
357
|
gem 'speedshop-cloudwatch', require: false
|
|
@@ -313,6 +364,7 @@ require 'speedshop/cloudwatch/puma'
|
|
|
313
364
|
require 'speedshop/cloudwatch/active_job'
|
|
314
365
|
require 'speedshop/cloudwatch/rack'
|
|
315
366
|
# require 'speedshop/cloudwatch/sidekiq'
|
|
367
|
+
# require 'speedshop/cloudwatch/solid_queue'
|
|
316
368
|
```
|
|
317
369
|
|
|
318
370
|
## Bibliography
|
|
@@ -9,7 +9,7 @@ module Speedshop
|
|
|
9
9
|
include Singleton
|
|
10
10
|
|
|
11
11
|
attr_accessor :interval, :metrics, :namespaces, :logger, :queue_max_size, :sidekiq_queues, :dimensions,
|
|
12
|
-
:collectors, :enabled_environments, :environment
|
|
12
|
+
:solid_queue_queues, :collectors, :enabled_environments, :environment
|
|
13
13
|
attr_writer :client
|
|
14
14
|
|
|
15
15
|
def initialize
|
|
@@ -27,11 +27,13 @@ module Speedshop
|
|
|
27
27
|
@metrics = {
|
|
28
28
|
puma: [],
|
|
29
29
|
sidekiq: [:QueueLatency],
|
|
30
|
+
solid_queue: [:QueueLatency],
|
|
30
31
|
rack: [:RequestQueueTime],
|
|
31
32
|
active_job: [:QueueLatency]
|
|
32
33
|
}
|
|
33
|
-
@namespaces = {puma: "Puma", sidekiq: "Sidekiq", rack: "Rack", active_job: "ActiveJob"}
|
|
34
|
+
@namespaces = {puma: "Puma", sidekiq: "Sidekiq", solid_queue: "SolidQueue", rack: "Rack", active_job: "ActiveJob"}
|
|
34
35
|
@sidekiq_queues = nil
|
|
36
|
+
@solid_queue_queues = nil
|
|
35
37
|
@dimensions = {}
|
|
36
38
|
@logger = (defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger) ? Rails.logger : Logger.new($stdout)
|
|
37
39
|
@collectors = [] # [:puma, :sidekiq]
|
|
@@ -155,6 +155,48 @@ module Speedshop
|
|
|
155
155
|
)
|
|
156
156
|
],
|
|
157
157
|
|
|
158
|
+
solid_queue: [
|
|
159
|
+
Metric.new(
|
|
160
|
+
name: :Processes,
|
|
161
|
+
unit: "Count",
|
|
162
|
+
description: "Number of live Solid Queue worker processes for this supervisor.",
|
|
163
|
+
source: "Live worker processes for this supervisor"
|
|
164
|
+
),
|
|
165
|
+
Metric.new(
|
|
166
|
+
name: :Workers,
|
|
167
|
+
unit: "Count",
|
|
168
|
+
description: "Number of worker threads currently processing jobs for this supervisor.",
|
|
169
|
+
source: "Sum of claimed executions across this supervisor's live worker processes"
|
|
170
|
+
),
|
|
171
|
+
Metric.new(
|
|
172
|
+
name: :Capacity,
|
|
173
|
+
unit: "Count",
|
|
174
|
+
description: "Number of worker threads available across this supervisor's live workers.",
|
|
175
|
+
source: "Sum of process.metadata['pool_size'] across this supervisor's live worker processes"
|
|
176
|
+
),
|
|
177
|
+
Metric.new(
|
|
178
|
+
name: :Utilization,
|
|
179
|
+
unit: "Percent",
|
|
180
|
+
description: "Average percentage of this supervisor's worker threads currently busy. 100% " \
|
|
181
|
+
"means all workers are busy.",
|
|
182
|
+
source: "Average of claimed executions / pool_size * 100 across this supervisor's live worker processes"
|
|
183
|
+
),
|
|
184
|
+
Metric.new(
|
|
185
|
+
name: :QueueLatency,
|
|
186
|
+
unit: "Seconds",
|
|
187
|
+
description: "Time the oldest job in this queue has been waiting. High " \
|
|
188
|
+
"latency indicates jobs are backing up.",
|
|
189
|
+
source: "SolidQueue::Queue#latency"
|
|
190
|
+
),
|
|
191
|
+
Metric.new(
|
|
192
|
+
name: :QueueSize,
|
|
193
|
+
unit: "Count",
|
|
194
|
+
description: "Number of jobs currently waiting in this queue. Growing " \
|
|
195
|
+
"size indicates jobs arriving faster than processing.",
|
|
196
|
+
source: "SolidQueue::Queue#size"
|
|
197
|
+
)
|
|
198
|
+
],
|
|
199
|
+
|
|
158
200
|
rack: [
|
|
159
201
|
Metric.new(
|
|
160
202
|
name: :RequestQueueTime,
|
|
@@ -171,8 +213,8 @@ module Speedshop
|
|
|
171
213
|
name: :QueueLatency,
|
|
172
214
|
unit: "Seconds",
|
|
173
215
|
description: "Time a job spent waiting in the queue before execution " \
|
|
174
|
-
"started. Values are
|
|
175
|
-
"
|
|
216
|
+
"started. Values are frequency-encoded without discarding " \
|
|
217
|
+
"the per-interval distribution.",
|
|
176
218
|
source: "Time.now.to_f - job.enqueued_at"
|
|
177
219
|
)
|
|
178
220
|
]
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Speedshop
|
|
4
|
+
module Cloudwatch
|
|
5
|
+
module Observations
|
|
6
|
+
module SolidQueue
|
|
7
|
+
PROCESS_METRICS = %i[Processes Workers Capacity Utilization].freeze
|
|
8
|
+
QUEUE_METRICS = %i[QueueLatency QueueSize].freeze
|
|
9
|
+
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def collect(supervisor_id:, solid_queue_queues: nil, metrics: PROCESS_METRICS + QUEUE_METRICS)
|
|
13
|
+
enabled = metrics.map(&:to_sym)
|
|
14
|
+
observations = []
|
|
15
|
+
observations.concat(process_observations(supervisor_id, metrics: enabled)) if (enabled & PROCESS_METRICS).any?
|
|
16
|
+
observations.concat(queue_observations(solid_queue_queues, metrics: enabled)) if (enabled & QUEUE_METRICS).any?
|
|
17
|
+
observations
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def process_observations(supervisor_id, metrics: PROCESS_METRICS)
|
|
21
|
+
enabled = metrics.map(&:to_sym)
|
|
22
|
+
return [] if (enabled & PROCESS_METRICS).empty?
|
|
23
|
+
|
|
24
|
+
include_claimed = (enabled & %i[Workers Utilization]).any?
|
|
25
|
+
processes = live_worker_processes(supervisor_id, include_claimed: include_claimed)
|
|
26
|
+
return [] if processes.empty?
|
|
27
|
+
|
|
28
|
+
observations = []
|
|
29
|
+
observations << {metric: :Processes, value: processes.size} if enabled.include?(:Processes)
|
|
30
|
+
observations << {metric: :Workers, value: processes.sum { |process| process.claimed_executions.size }} if enabled.include?(:Workers)
|
|
31
|
+
observations << {metric: :Capacity, value: processes.sum { |process| pool_size(process).to_i }} if enabled.include?(:Capacity)
|
|
32
|
+
|
|
33
|
+
if enabled.include?(:Utilization)
|
|
34
|
+
utilization = avg_utilization(processes) * 100.0
|
|
35
|
+
observations << {metric: :Utilization, value: utilization} unless utilization.nan?
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
observations
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def queue_observations(solid_queue_queues, metrics: QUEUE_METRICS)
|
|
42
|
+
enabled = metrics.map(&:to_sym)
|
|
43
|
+
include_latency = enabled.include?(:QueueLatency)
|
|
44
|
+
include_size = enabled.include?(:QueueSize)
|
|
45
|
+
return [] unless include_latency || include_size
|
|
46
|
+
|
|
47
|
+
queues_to_monitor(solid_queue_queues).flat_map do |queue|
|
|
48
|
+
observations = []
|
|
49
|
+
observations << {metric: :QueueLatency, value: queue.latency, dimensions: {QueueName: queue.name}} if include_latency
|
|
50
|
+
observations << {metric: :QueueSize, value: queue.size, dimensions: {QueueName: queue.name}} if include_size
|
|
51
|
+
observations
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def queues_to_monitor(solid_queue_queues)
|
|
56
|
+
if solid_queue_queues.nil? || solid_queue_queues.empty?
|
|
57
|
+
::SolidQueue::Queue.all
|
|
58
|
+
else
|
|
59
|
+
solid_queue_queues.map { |name| ::SolidQueue::Queue.find_by_name(name) }
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def live_worker_processes(supervisor_id, include_claimed: true)
|
|
64
|
+
scope = ::SolidQueue::Process.where(
|
|
65
|
+
kind: "Worker",
|
|
66
|
+
supervisor_id: supervisor_id,
|
|
67
|
+
last_heartbeat_at: ::SolidQueue.process_alive_threshold.ago..
|
|
68
|
+
)
|
|
69
|
+
scope = scope.includes(:claimed_executions) if include_claimed
|
|
70
|
+
scope.to_a
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def avg_utilization(processes)
|
|
74
|
+
utils = processes.filter_map do |process|
|
|
75
|
+
size = pool_size(process).to_f
|
|
76
|
+
next unless size > 0
|
|
77
|
+
|
|
78
|
+
process.claimed_executions.size / size
|
|
79
|
+
end
|
|
80
|
+
utils.sum / utils.size.to_f
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def pool_size(process)
|
|
84
|
+
metadata = process.metadata || {}
|
|
85
|
+
metadata["pool_size"] || metadata["thread_pool_size"]
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "singleton"
|
|
4
|
+
require_relative "request_size"
|
|
4
5
|
|
|
5
6
|
module Speedshop
|
|
6
7
|
module Cloudwatch
|
|
7
8
|
class Reporter
|
|
9
|
+
MAX_VALUES_PER_DATUM = 150
|
|
10
|
+
|
|
8
11
|
include Singleton
|
|
9
12
|
|
|
10
13
|
def initialize
|
|
@@ -20,13 +23,12 @@ module Speedshop
|
|
|
20
23
|
end
|
|
21
24
|
|
|
22
25
|
def start!
|
|
23
|
-
return
|
|
26
|
+
return unless config.environment_enabled?
|
|
24
27
|
|
|
25
28
|
@mutex.synchronize do
|
|
26
|
-
return if started?
|
|
27
|
-
|
|
28
29
|
reset_after_fork! if forked?
|
|
29
30
|
initialize_collectors
|
|
31
|
+
return if started?
|
|
30
32
|
|
|
31
33
|
Speedshop::Cloudwatch.log_info("Starting metric reporter (collectors: #{@collectors.map(&:class).join(", ")})")
|
|
32
34
|
@running = true
|
|
@@ -125,13 +127,16 @@ module Speedshop
|
|
|
125
127
|
end
|
|
126
128
|
|
|
127
129
|
def initialize_collectors
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
130
|
+
collectors = []
|
|
131
|
+
config.collectors.uniq.each do |integration|
|
|
132
|
+
collectors << Speedshop::Cloudwatch::Puma.new if integration == :puma
|
|
133
|
+
collectors << Speedshop::Cloudwatch::Sidekiq.new if integration == :sidekiq
|
|
134
|
+
collectors << Speedshop::Cloudwatch::SolidQueue.new if integration == :solid_queue
|
|
135
|
+
collectors << Speedshop::Cloudwatch::Yabeda::Collector.new if integration == :yabeda
|
|
132
136
|
rescue => e
|
|
133
137
|
Speedshop::Cloudwatch.log_error("Failed to initialize collector for #{integration}: #{e.message}", e)
|
|
134
138
|
end
|
|
139
|
+
@collectors = collectors
|
|
135
140
|
end
|
|
136
141
|
|
|
137
142
|
def run_loop
|
|
@@ -195,7 +200,10 @@ module Speedshop
|
|
|
195
200
|
timestamp: m[:timestamp],
|
|
196
201
|
dimensions: m[:dimensions]
|
|
197
202
|
}
|
|
198
|
-
if m[:
|
|
203
|
+
if m[:values]
|
|
204
|
+
datum[:values] = m[:values]
|
|
205
|
+
datum[:counts] = m[:counts]
|
|
206
|
+
elsif m[:statistic_values]
|
|
199
207
|
datum[:statistic_values] = m[:statistic_values]
|
|
200
208
|
else
|
|
201
209
|
datum[:value] = m[:value]
|
|
@@ -206,91 +214,95 @@ module Speedshop
|
|
|
206
214
|
end
|
|
207
215
|
|
|
208
216
|
def send_batches(namespace, metric_data)
|
|
209
|
-
|
|
210
|
-
|
|
217
|
+
overhead = RequestSize.request_overhead(namespace)
|
|
218
|
+
batch = []
|
|
219
|
+
size = overhead
|
|
220
|
+
metric_data.each do |datum|
|
|
221
|
+
datum_size = RequestSize.bound(datum)
|
|
222
|
+
if overhead + datum_size > RequestSize::LIMIT
|
|
223
|
+
Speedshop::Cloudwatch.log_error("Dropping oversized CloudWatch datum: #{datum[:metric_name]} (request size bound exceeds 1 MiB)")
|
|
224
|
+
next
|
|
225
|
+
end
|
|
226
|
+
if batch.size == 20 || size + datum_size > RequestSize::LIMIT
|
|
227
|
+
config.client.put_metric_data(namespace: namespace, metric_data: batch)
|
|
228
|
+
batch = []
|
|
229
|
+
size = overhead
|
|
230
|
+
end
|
|
231
|
+
batch << datum
|
|
232
|
+
size += datum_size
|
|
211
233
|
end
|
|
234
|
+
config.client.put_metric_data(namespace: namespace, metric_data: batch) unless batch.empty?
|
|
212
235
|
end
|
|
213
236
|
|
|
214
237
|
def aggregate_namespace_metrics(ns_metrics)
|
|
215
|
-
group_metrics(ns_metrics).
|
|
238
|
+
group_metrics(ns_metrics).flat_map { |items| aggregate_group(items) }
|
|
216
239
|
end
|
|
217
240
|
|
|
218
241
|
def group_metrics(ns_metrics)
|
|
219
242
|
groups = {}
|
|
220
243
|
ns_metrics.each do |m|
|
|
221
|
-
key = [
|
|
244
|
+
key = [
|
|
245
|
+
m[:metric_name],
|
|
246
|
+
m[:unit],
|
|
247
|
+
normalized_dimensions_key(m[:dimensions]),
|
|
248
|
+
m[:aggregation_strategy],
|
|
249
|
+
m.key?(:statistic_values),
|
|
250
|
+
timestamp_bucket(m[:timestamp])
|
|
251
|
+
]
|
|
222
252
|
(groups[key] ||= []) << m
|
|
223
253
|
end
|
|
224
254
|
groups.values
|
|
225
255
|
end
|
|
226
256
|
|
|
227
257
|
def aggregate_group(items)
|
|
228
|
-
return items
|
|
258
|
+
return items if items.size == 1
|
|
229
259
|
|
|
230
260
|
strategy = items.first[:aggregation_strategy]
|
|
231
|
-
return
|
|
232
|
-
return
|
|
261
|
+
return [items.last] if strategy == :most_recent
|
|
262
|
+
return [items.max_by { |item| item[:value].to_f }] if strategy == :max
|
|
263
|
+
return [merge_statistic_values_group(items)] if items.first[:statistic_values]
|
|
233
264
|
|
|
234
|
-
|
|
235
|
-
{
|
|
236
|
-
metric_name: items.first[:metric_name],
|
|
237
|
-
unit: items.first[:unit],
|
|
238
|
-
dimensions: items.first[:dimensions],
|
|
239
|
-
timestamp: Time.now,
|
|
240
|
-
statistic_values: build_statistic_values(sample_count, sum, minimum, maximum)
|
|
241
|
-
}
|
|
265
|
+
aggregate_distribution_group(items)
|
|
242
266
|
end
|
|
243
267
|
|
|
244
|
-
def
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
minimum = Float::INFINITY
|
|
248
|
-
maximum = -Float::INFINITY
|
|
249
|
-
|
|
250
|
-
items.each do |item|
|
|
251
|
-
if item[:statistic_values]
|
|
252
|
-
sv = item[:statistic_values]
|
|
253
|
-
sc = sv[:sample_count].to_f
|
|
254
|
-
sample_count += sc
|
|
255
|
-
sum += sv[:sum].to_f
|
|
256
|
-
minimum = [minimum, sv[:minimum].to_f].min
|
|
257
|
-
maximum = [maximum, sv[:maximum].to_f].max
|
|
258
|
-
elsif item.key?(:value)
|
|
259
|
-
v = item[:value].to_f
|
|
260
|
-
sample_count += 1.0
|
|
261
|
-
sum += v
|
|
262
|
-
minimum = [minimum, v].min
|
|
263
|
-
maximum = [maximum, v].max
|
|
264
|
-
end
|
|
268
|
+
def aggregate_distribution_group(items)
|
|
269
|
+
frequencies = items.each_with_object(Hash.new(0)) do |item, counts|
|
|
270
|
+
counts[item[:value].to_f] += 1
|
|
265
271
|
end
|
|
266
272
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
def item_value_for_max(item)
|
|
279
|
-
return item[:statistic_values][:maximum].to_f if item[:statistic_values]
|
|
280
|
-
return item[:value].to_f if item.key?(:value)
|
|
281
|
-
|
|
282
|
-
-Float::INFINITY
|
|
273
|
+
frequencies.sort_by(&:first).each_slice(MAX_VALUES_PER_DATUM).map do |slice|
|
|
274
|
+
{
|
|
275
|
+
metric_name: items.first[:metric_name],
|
|
276
|
+
unit: items.first[:unit],
|
|
277
|
+
dimensions: items.first[:dimensions],
|
|
278
|
+
timestamp: timestamp_bucket(items.first[:timestamp]),
|
|
279
|
+
values: slice.map(&:first),
|
|
280
|
+
counts: slice.map(&:last)
|
|
281
|
+
}
|
|
282
|
+
end
|
|
283
283
|
end
|
|
284
284
|
|
|
285
|
-
def
|
|
285
|
+
def merge_statistic_values_group(items)
|
|
286
|
+
statistic_values = items.map { |item| item[:statistic_values] }
|
|
286
287
|
{
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
288
|
+
metric_name: items.first[:metric_name],
|
|
289
|
+
unit: items.first[:unit],
|
|
290
|
+
dimensions: items.first[:dimensions],
|
|
291
|
+
timestamp: timestamp_bucket(items.first[:timestamp]),
|
|
292
|
+
statistic_values: {
|
|
293
|
+
sample_count: statistic_values.sum { |values| values[:sample_count].to_f },
|
|
294
|
+
sum: statistic_values.sum { |values| values[:sum].to_f },
|
|
295
|
+
minimum: statistic_values.map { |values| values[:minimum].to_f }.min,
|
|
296
|
+
maximum: statistic_values.map { |values| values[:maximum].to_f }.max
|
|
297
|
+
}
|
|
291
298
|
}
|
|
292
299
|
end
|
|
293
300
|
|
|
301
|
+
def timestamp_bucket(timestamp)
|
|
302
|
+
period = [config.interval.to_i, 1].max
|
|
303
|
+
Time.at((timestamp.to_f / period).floor * period)
|
|
304
|
+
end
|
|
305
|
+
|
|
294
306
|
def normalized_dimensions_key(dims)
|
|
295
307
|
(dims || []).sort_by { |d| d[:name].to_s }.map { |d| "#{d[:name]}=#{d[:value]}" }.join("|")
|
|
296
308
|
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Speedshop
|
|
4
|
+
module Cloudwatch
|
|
5
|
+
module RequestSize
|
|
6
|
+
LIMIT = 1024 * 1024
|
|
7
|
+
|
|
8
|
+
# Upper bound for uncompressed PutMetricData Query bodies, not Ruby object size.
|
|
9
|
+
# Every supported leaf's full Query key (including MetricData.member.20,
|
|
10
|
+
# Dimensions/Values/Counts indices), '=' and '&' fits within 128 bytes.
|
|
11
|
+
# Percent encoding expands each UTF-8 byte by at most 3. The 64-byte floor
|
|
12
|
+
# covers SDK numeric/timestamp formatting independently of Ruby's to_s.
|
|
13
|
+
# Container overhead is deliberately overcounted, including empty lists.
|
|
14
|
+
def self.bound(value)
|
|
15
|
+
case value
|
|
16
|
+
when Hash
|
|
17
|
+
128 + value.values.sum { |item| bound(item) }
|
|
18
|
+
when Array
|
|
19
|
+
128 + value.sum { |item| bound(item) }
|
|
20
|
+
else
|
|
21
|
+
128 + 3 * [value.to_s.bytesize, 64].max
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.request_overhead(namespace)
|
|
26
|
+
# Action, Version, Namespace key, and separators.
|
|
27
|
+
512 + bound(namespace)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "speedshop/cloudwatch/observations"
|
|
4
|
+
|
|
5
|
+
module Speedshop
|
|
6
|
+
module Cloudwatch
|
|
7
|
+
class SolidQueue
|
|
8
|
+
class << self
|
|
9
|
+
attr_accessor :supervisor
|
|
10
|
+
|
|
11
|
+
def setup_lifecycle_hooks
|
|
12
|
+
::SolidQueue.on_start do |supervisor|
|
|
13
|
+
Speedshop::Cloudwatch::SolidQueue.supervisor = supervisor
|
|
14
|
+
Speedshop::Cloudwatch.configure do |c|
|
|
15
|
+
c.collectors << :solid_queue unless c.collectors.include?(:solid_queue)
|
|
16
|
+
end
|
|
17
|
+
Speedshop::Cloudwatch.start!
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
::SolidQueue.on_exit { |supervisor| Speedshop::Cloudwatch.stop! if supervisor.standalone? }
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def collect
|
|
25
|
+
supervisor = self.class.supervisor
|
|
26
|
+
return unless supervisor&.pid == ::Process.pid
|
|
27
|
+
|
|
28
|
+
enabled_metrics = Config.instance.metrics[:solid_queue]
|
|
29
|
+
return if enabled_metrics.nil? || enabled_metrics.empty?
|
|
30
|
+
|
|
31
|
+
observations = ::SolidQueue::Record.connection_pool.with_connection do
|
|
32
|
+
Observations::SolidQueue.collect(
|
|
33
|
+
supervisor_id: supervisor.process_id,
|
|
34
|
+
solid_queue_queues: Config.instance.solid_queue_queues,
|
|
35
|
+
metrics: enabled_metrics
|
|
36
|
+
)
|
|
37
|
+
end
|
|
38
|
+
observations.each do |observation|
|
|
39
|
+
metric_mapper.report(
|
|
40
|
+
metric: observation[:metric],
|
|
41
|
+
value: observation[:value],
|
|
42
|
+
dimensions: observation[:dimensions] || {},
|
|
43
|
+
integration: :solid_queue
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
rescue => e
|
|
47
|
+
Speedshop::Cloudwatch.log_error("Failed to collect Solid Queue metrics: #{e.message}", e)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def metric_mapper
|
|
53
|
+
Speedshop::Cloudwatch.metric_mapper
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
ActiveSupport.on_load(:solid_queue) { Speedshop::Cloudwatch::SolidQueue.setup_lifecycle_hooks } if defined?(ActiveSupport)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: speedshop-cloudwatch
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Nate Berkopec
|
|
@@ -78,11 +78,14 @@ files:
|
|
|
78
78
|
- lib/speedshop/cloudwatch/observations/puma.rb
|
|
79
79
|
- lib/speedshop/cloudwatch/observations/rack.rb
|
|
80
80
|
- lib/speedshop/cloudwatch/observations/sidekiq.rb
|
|
81
|
+
- lib/speedshop/cloudwatch/observations/solid_queue.rb
|
|
81
82
|
- lib/speedshop/cloudwatch/puma.rb
|
|
82
83
|
- lib/speedshop/cloudwatch/rack.rb
|
|
83
84
|
- lib/speedshop/cloudwatch/railtie.rb
|
|
84
85
|
- lib/speedshop/cloudwatch/reporter.rb
|
|
86
|
+
- lib/speedshop/cloudwatch/request_size.rb
|
|
85
87
|
- lib/speedshop/cloudwatch/sidekiq.rb
|
|
88
|
+
- lib/speedshop/cloudwatch/solid_queue.rb
|
|
86
89
|
- lib/speedshop/cloudwatch/version.rb
|
|
87
90
|
- lib/speedshop/cloudwatch/yabeda.rb
|
|
88
91
|
- speedshop-cloudwatch.gemspec
|
|
@@ -107,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
107
110
|
- !ruby/object:Gem::Version
|
|
108
111
|
version: '0'
|
|
109
112
|
requirements: []
|
|
110
|
-
rubygems_version:
|
|
113
|
+
rubygems_version: 4.0.20
|
|
111
114
|
specification_version: 4
|
|
112
115
|
summary: Ruby application integration with AWS CloudWatch for Puma, Rack, Sidekiq,
|
|
113
116
|
and ActiveJob
|