io_monitor 0.2.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/README.md +47 -6
- data/lib/io_monitor/adapters/active_record_adapter.rb +1 -1
- data/lib/io_monitor/aggregator.rb +6 -0
- data/lib/io_monitor/configuration.rb +15 -11
- data/lib/io_monitor/controller.rb +29 -6
- data/lib/io_monitor/patches/action_controller_base_patch.rb +1 -1
- data/lib/io_monitor/patches/net_http_adapter_patch.rb +2 -2
- data/lib/io_monitor/publishers/base_publisher.rb +3 -3
- data/lib/io_monitor/publishers/prometheus_publisher.rb +40 -0
- data/lib/io_monitor/railtie.rb +1 -1
- data/lib/io_monitor/version.rb +1 -1
- data/lib/io_monitor.rb +2 -1
- metadata +21 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6ce81228730f5e3d3da830ae05d879dda4a15d0573cb8a7485990935a1d4d04
|
4
|
+
data.tar.gz: 0e090281b4e74cacd24ea4abd888d53236d7d7d8b4a96dc166d46186b9aeae57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87d1a4ba93ef5b254da0f38e567909b29fe5d5788fb7acfd429fddb05896f6922b0c334491f42f072adb2e2ffc690a6c6acc58fd97edae0e8c5847f8a58ea1d8
|
7
|
+
data.tar.gz: c4928efeba47b7996b996606c4181fb34acf04b40a541563a088d1e4fe83a456cf9338729361574e7d5d256bd705ff630d6b4699feaf742487ec316e93a30e8d
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,18 @@
|
|
2
2
|
|
3
3
|
## main
|
4
4
|
|
5
|
+
## 1.1.0 (2025-01-21)
|
6
|
+
|
7
|
+
- [PR#23](https://github.com/DmitryTsepelev/io_monitor/pull/23) Fix net/http adapter and an issue with nil responses ([@SSDany])
|
8
|
+
|
9
|
+
## 1.0.0 (2023-05-06)
|
10
|
+
|
11
|
+
- [PR#22](https://github.com/DmitryTsepelev/io_monitor/pull/22) Handle zero payload ([@DmitryTsepelev])
|
12
|
+
- [PR#17](https://github.com/DmitryTsepelev/io_monitor/pull/17) Prometheus publisher ([@maxshend])
|
13
|
+
- [PR#10](https://github.com/DmitryTsepelev/io_monitor/pull/10) Per–action monitoring ([@DmitryTsepelev])
|
14
|
+
- [PR#15](https://github.com/DmitryTsepelev/io_monitor/pull/15) Allow configure more than one publisher ([@DmitryTsepelev])
|
15
|
+
- [PR#9](https://github.com/DmitryTsepelev/io_monitor/pull/9) Restrict minimum Rails version to 6.1, adjust test matrix, and related changes ([@Envek])
|
16
|
+
|
5
17
|
## 0.2.0 (2022-05-29)
|
6
18
|
|
7
19
|
- [PR#8](https://github.com/DmitryTsepelev/io_monitor/pull/8) Add Redis adapter ([@DmitryTsepelev])
|
@@ -17,3 +29,5 @@
|
|
17
29
|
[@prog-supdex]: https://github.com/prog-supdex
|
18
30
|
[@maxshend]: https://github.com/maxshend
|
19
31
|
[@DmitryTsepelev]: https://github.com/DmitryTsepelev
|
32
|
+
[@Envek]: https://github.com/Envek
|
33
|
+
[@SSDany]: https://github.com/SSDany
|
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# IoMonitor
|
2
2
|
|
3
|
+
[](https://rubygems.org/gems/io_monitor)
|
4
|
+
[](https://github.com/DmitryTsepelev/io_monitor/actions/workflows/test.yml)
|
5
|
+

|
6
|
+
|
3
7
|
A gem that helps to detect potential memory bloats.
|
4
8
|
|
5
9
|
When your controller loads a lot of data to the memory but returns a small response to the client it might mean that you're using the IO in the non–optimal way. In this case, you'll see the following message in your logs:
|
@@ -8,11 +12,7 @@ When your controller loads a lot of data to the memory but returns a small respo
|
|
8
12
|
Completed 200 OK in 349ms (Views: 2.1ms | ActiveRecord: 38.7ms | ActiveRecord Payload: 866.00 B | Response Payload: 25.00 B | Allocations: 72304)
|
9
13
|
```
|
10
14
|
|
11
|
-
|
12
|
-
<a href="https://evilmartians.com/?utm_source=io_monitor">
|
13
|
-
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
|
14
|
-
</a>
|
15
|
-
</p>
|
15
|
+
You can support my open–source work [here](https://boosty.to/dmitry_tsepelev).
|
16
16
|
|
17
17
|
## Usage
|
18
18
|
|
@@ -28,7 +28,7 @@ Change configuration in an initializer if you need:
|
|
28
28
|
|
29
29
|
```ruby
|
30
30
|
IoMonitor.configure do |config|
|
31
|
-
config.publish = :notifications # defaults to :logs
|
31
|
+
config.publish = [:logs, :notifications, :prometheus] # defaults to :logs
|
32
32
|
config.warn_threshold = 0.8 # defaults to 0
|
33
33
|
config.adapters = [:active_record, :net_http, :redis] # defaults to [:active_record]
|
34
34
|
end
|
@@ -47,6 +47,31 @@ Depending on configuration when IO payload size to response payload size ratio r
|
|
47
47
|
```
|
48
48
|
ActiveRecord I/O to response payload ratio is 0.1, while threshold is 0.8
|
49
49
|
```
|
50
|
+
Prometheus metrics example:
|
51
|
+
```
|
52
|
+
...
|
53
|
+
# TYPE io_monitor_ratio histogram
|
54
|
+
# HELP io_monitor_ratio IO payload size to response payload size ratio
|
55
|
+
io_monitor_ratio_bucket{adapter="active_record",le="0.01"} 0.0
|
56
|
+
io_monitor_ratio_bucket{adapter="active_record",le="5"} 2.0
|
57
|
+
io_monitor_ratio_bucket{adapter="active_record",le="10"} 2.0
|
58
|
+
io_monitor_ratio_bucket{adapter="active_record",le="+Inf"} 2.0
|
59
|
+
io_monitor_ratio_sum{adapter="active_record"} 0.15779381908414167
|
60
|
+
io_monitor_ratio_count{adapter="active_record"} 2.0
|
61
|
+
...
|
62
|
+
```
|
63
|
+
If you want to customize Prometheus publisher you can pass it as object:
|
64
|
+
```ruby
|
65
|
+
IoMonitor.configure do |config|
|
66
|
+
config.publish = [
|
67
|
+
IoMonitor::PrometheusPublisher.new(
|
68
|
+
registry: custom_registry, # defaults to Prometheus::Client.registry
|
69
|
+
aggregation: :max, # defaults to nil
|
70
|
+
buckets: [0.1, 5, 10] # defaults to Prometheus::Client::Histogram::DEFAULT_BUCKETS
|
71
|
+
)
|
72
|
+
]
|
73
|
+
end
|
74
|
+
```
|
50
75
|
|
51
76
|
In addition, if `publish` is set to logs, additional data will be logged on each request:
|
52
77
|
|
@@ -62,6 +87,18 @@ ActiveSupport::Notifications.subscribe("process_action.action_controller") do |n
|
|
62
87
|
end
|
63
88
|
```
|
64
89
|
|
90
|
+
## Per–action monitoring
|
91
|
+
|
92
|
+
Since this approach can lead to false–positives or other things you don't want or cannot fix, there is a way to configure monitoring only for specific actions:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
class MyController < ApplicationController
|
96
|
+
include IoMonitor::Controller
|
97
|
+
|
98
|
+
monitor_io_for :index, :show
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
65
102
|
## Custom publishers
|
66
103
|
|
67
104
|
Implement your custom publisher by inheriting from `BasePublisher`:
|
@@ -110,6 +147,10 @@ end
|
|
110
147
|
|
111
148
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
|
112
149
|
|
150
|
+
## Credits
|
151
|
+
|
152
|
+
Initially sponsored by [Evil Martians](http://evilmartians.com).
|
153
|
+
|
113
154
|
## Contributing
|
114
155
|
|
115
156
|
Bug reports and pull requests are welcome on GitHub at https://github.com/DmitryTsepelev/io_monitor.
|
@@ -5,22 +5,15 @@ module IoMonitor
|
|
5
5
|
DEFAULT_WARN_THRESHOLD = 0.0
|
6
6
|
|
7
7
|
def initialize
|
8
|
-
@
|
8
|
+
@publishers = [LogsPublisher.new]
|
9
9
|
@adapters = [ActiveRecordAdapter.new]
|
10
10
|
@warn_threshold = DEFAULT_WARN_THRESHOLD
|
11
11
|
end
|
12
12
|
|
13
|
-
attr_reader :
|
13
|
+
attr_reader :publishers, :adapters, :warn_threshold
|
14
14
|
|
15
|
-
def publish=(
|
16
|
-
|
17
|
-
@publisher = value
|
18
|
-
elsif (publisher_type = resolve(IoMonitor::PUBLISHERS, value))
|
19
|
-
@publisher = publisher_type.new
|
20
|
-
else
|
21
|
-
supported = IoMonitor::PUBLISHERS.map(&:kind)
|
22
|
-
raise ArgumentError, "Only the following publishers are supported: #{supported}."
|
23
|
-
end
|
15
|
+
def publish=(values)
|
16
|
+
@publishers = [*values].map { |value| value_to_publisher(value) }
|
24
17
|
end
|
25
18
|
|
26
19
|
def adapters=(value)
|
@@ -50,5 +43,16 @@ module IoMonitor
|
|
50
43
|
def resolve(list, kind)
|
51
44
|
list.find { |p| p.kind == kind }
|
52
45
|
end
|
46
|
+
|
47
|
+
def value_to_publisher(value)
|
48
|
+
if value.is_a?(BasePublisher)
|
49
|
+
value
|
50
|
+
elsif (publisher_type = resolve(IoMonitor::PUBLISHERS, value))
|
51
|
+
publisher_type.new
|
52
|
+
else
|
53
|
+
supported = IoMonitor::PUBLISHERS.map(&:kind)
|
54
|
+
raise ArgumentError, "Only the following publishers are supported: #{supported}."
|
55
|
+
end
|
56
|
+
end
|
53
57
|
end
|
54
58
|
end
|
@@ -4,24 +4,47 @@ module IoMonitor
|
|
4
4
|
module Controller
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
|
8
|
-
IoMonitor.aggregator.start!
|
7
|
+
delegate :aggregator, to: IoMonitor
|
9
8
|
|
10
|
-
|
9
|
+
ALL_ACTIONS = Object.new
|
10
|
+
|
11
|
+
class_methods do
|
12
|
+
def monitor_io_for(*actions_to_monitor_io)
|
13
|
+
@actions_to_monitor_io = actions_to_monitor_io
|
14
|
+
end
|
11
15
|
|
12
|
-
|
16
|
+
def actions_to_monitor_io
|
17
|
+
@actions_to_monitor_io || ALL_ACTIONS
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def process_action(*)
|
22
|
+
if monitors_action?(action_name)
|
23
|
+
aggregator.collect { super }
|
24
|
+
else
|
25
|
+
super
|
26
|
+
end
|
13
27
|
end
|
14
28
|
|
15
29
|
def append_info_to_payload(payload)
|
16
30
|
super
|
17
31
|
|
32
|
+
return unless monitors_action?(action_name)
|
33
|
+
|
18
34
|
data = payload[IoMonitor::NAMESPACE] = {}
|
19
35
|
|
20
|
-
|
21
|
-
data[source] =
|
36
|
+
aggregator.sources.each do |source|
|
37
|
+
data[source] = aggregator.get(source)
|
22
38
|
end
|
23
39
|
|
24
40
|
data[:response] = payload[:response]&.body&.bytesize || 0
|
25
41
|
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def monitors_action?(action_name)
|
46
|
+
actions = self.class.actions_to_monitor_io
|
47
|
+
actions == ALL_ACTIONS || actions.include?(action_name.to_sym)
|
48
|
+
end
|
26
49
|
end
|
27
50
|
end
|
@@ -4,7 +4,7 @@ module IoMonitor
|
|
4
4
|
module ActionControllerBasePatch
|
5
5
|
def log_process_action(payload)
|
6
6
|
super.tap do |messages|
|
7
|
-
next unless IoMonitor.config.publisher.is_a?(LogsPublisher)
|
7
|
+
next unless IoMonitor.config.publishers.any? { |publisher| publisher.is_a?(LogsPublisher) }
|
8
8
|
|
9
9
|
data = payload[IoMonitor::NAMESPACE]
|
10
10
|
next unless data
|
@@ -3,8 +3,8 @@
|
|
3
3
|
module IoMonitor
|
4
4
|
module NetHttpAdapterPatch
|
5
5
|
def request(*args, &block)
|
6
|
-
super do |response|
|
7
|
-
if response
|
6
|
+
super.tap do |response|
|
7
|
+
if response&.body && IoMonitor.aggregator.active?
|
8
8
|
IoMonitor.aggregator.increment(NetHttpAdapter.kind, response.body.bytesize)
|
9
9
|
end
|
10
10
|
end
|
@@ -16,15 +16,15 @@ module IoMonitor
|
|
16
16
|
(payload.keys - [:response]).each do |source|
|
17
17
|
ratio = ratio(payload[:response], payload[source])
|
18
18
|
|
19
|
-
if ratio < IoMonitor.config.warn_threshold
|
20
|
-
publish(source, ratio)
|
21
|
-
end
|
19
|
+
publish(source, ratio) if ratio < IoMonitor.config.warn_threshold
|
22
20
|
end
|
23
21
|
end
|
24
22
|
|
25
23
|
private
|
26
24
|
|
27
25
|
def ratio(response_size, io_size)
|
26
|
+
return 0 if io_size.to_f.zero?
|
27
|
+
|
28
28
|
response_size.to_f / io_size.to_f
|
29
29
|
end
|
30
30
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module IoMonitor
|
4
|
+
class PrometheusPublisher < BasePublisher
|
5
|
+
HELP_MESSAGE = "IO payload size to response payload size ratio"
|
6
|
+
|
7
|
+
def initialize(registry: nil, aggregation: nil, buckets: nil)
|
8
|
+
registry ||= ::Prometheus::Client.registry
|
9
|
+
@metric = registry.histogram(
|
10
|
+
"#{IoMonitor::NAMESPACE}_ratio".to_sym,
|
11
|
+
labels: %i[adapter],
|
12
|
+
buckets: buckets || ::Prometheus::Client::Histogram::DEFAULT_BUCKETS,
|
13
|
+
store_settings: store_settings(aggregation),
|
14
|
+
docstring: HELP_MESSAGE
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.kind
|
19
|
+
:prometheus
|
20
|
+
end
|
21
|
+
|
22
|
+
def publish(source, ratio)
|
23
|
+
metric.observe(ratio, labels: {adapter: source})
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
attr_reader :metric
|
29
|
+
|
30
|
+
# From https://github.com/yabeda-rb/yabeda-prometheus/blob/v0.8.0/lib/yabeda/prometheus/adapter.rb#L101
|
31
|
+
def store_settings(aggregation)
|
32
|
+
case ::Prometheus::Client.config.data_store
|
33
|
+
when ::Prometheus::Client::DataStores::Synchronized, ::Prometheus::Client::DataStores::SingleThreaded
|
34
|
+
{} # Default synchronized store doesn't allow to pass any options
|
35
|
+
when ::Prometheus::Client::DataStores::DirectFileStore, ::Object # Anything else
|
36
|
+
{aggregation: aggregation}.compact
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/io_monitor/railtie.rb
CHANGED
data/lib/io_monitor/version.rb
CHANGED
data/lib/io_monitor.rb
CHANGED
@@ -12,6 +12,7 @@ require "io_monitor/adapters/net_http_adapter"
|
|
12
12
|
require "io_monitor/publishers/base_publisher"
|
13
13
|
require "io_monitor/publishers/logs_publisher"
|
14
14
|
require "io_monitor/publishers/notifications_publisher"
|
15
|
+
require "io_monitor/publishers/prometheus_publisher"
|
15
16
|
|
16
17
|
require "io_monitor/railtie"
|
17
18
|
|
@@ -26,7 +27,7 @@ module IoMonitor
|
|
26
27
|
end
|
27
28
|
ADAPTERS = adapters.freeze
|
28
29
|
|
29
|
-
PUBLISHERS = [LogsPublisher, NotificationsPublisher].freeze
|
30
|
+
PUBLISHERS = [LogsPublisher, NotificationsPublisher, PrometheusPublisher].freeze
|
30
31
|
|
31
32
|
class << self
|
32
33
|
def aggregator
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: io_monitor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- baygeldin
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2025-01-21 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rails
|
@@ -19,14 +19,14 @@ dependencies:
|
|
19
19
|
requirements:
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7.0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
29
|
+
version: '7.0'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: redis
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -41,6 +41,20 @@ dependencies:
|
|
41
41
|
- - ">="
|
42
42
|
- !ruby/object:Gem::Version
|
43
43
|
version: '4.0'
|
44
|
+
- !ruby/object:Gem::Dependency
|
45
|
+
name: prometheus-client
|
46
|
+
requirement: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
44
58
|
description:
|
45
59
|
email:
|
46
60
|
- dmitry.a.tsepelev@gmail.com
|
@@ -67,6 +81,7 @@ files:
|
|
67
81
|
- lib/io_monitor/publishers/base_publisher.rb
|
68
82
|
- lib/io_monitor/publishers/logs_publisher.rb
|
69
83
|
- lib/io_monitor/publishers/notifications_publisher.rb
|
84
|
+
- lib/io_monitor/publishers/prometheus_publisher.rb
|
70
85
|
- lib/io_monitor/railtie.rb
|
71
86
|
- lib/io_monitor/version.rb
|
72
87
|
homepage: https://github.com/DmitryTsepelev/io_monitor
|
@@ -86,14 +101,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
101
|
requirements:
|
87
102
|
- - ">="
|
88
103
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
104
|
+
version: 3.1.0
|
90
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
106
|
requirements:
|
92
107
|
- - ">="
|
93
108
|
- !ruby/object:Gem::Version
|
94
109
|
version: '0'
|
95
110
|
requirements: []
|
96
|
-
rubygems_version: 3.2.
|
111
|
+
rubygems_version: 3.2.15
|
97
112
|
signing_key:
|
98
113
|
specification_version: 4
|
99
114
|
summary: A gem that helps to detect potential memory bloats
|