ezmetrics 1.0.4 → 1.0.5
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/README.md +16 -10
- data/lib/ezmetrics.rb +42 -21
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1bd4e2f35cc2cf1b50efbac72d107041dc68129e
|
4
|
+
data.tar.gz: 179ed567aac638d1ab0d3a4ed86eaad458c55fd1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e805073975368174e1ce2fd62ce3ddeece44a50bb0141f4a2d7fb9a1b8392c1083667656f29217a1beef6f9add360af0bbba3fe4016263dfa20421af247e1bbf
|
7
|
+
data.tar.gz: 7364788497b995a02cb3385220a7f9e861cc4d7db6377f29850c6d22e25d99b25b6fdbaaa00a6ffb224573739262219136d9739e3676f65af6b4ba03a72c6606
|
data/README.md
CHANGED
@@ -15,32 +15,33 @@ gem 'ezmetrics'
|
|
15
15
|
|
16
16
|
### Getting started
|
17
17
|
|
18
|
-
This tool captures and aggregates metrics such as
|
19
|
-
- `status`
|
18
|
+
This tool captures and aggregates metrics such as
|
20
19
|
- `duration`
|
21
|
-
- `
|
20
|
+
- `views`
|
22
21
|
- `db`
|
22
|
+
- `queries`
|
23
|
+
- `status`
|
23
24
|
|
24
25
|
for a 60 seconds timeframe by default.
|
25
26
|
|
26
|
-
You can change the timeframe according to your needs and save the metrics by calling `log` method:
|
27
|
+
You can change the timeframe according to your needs and save the metrics by calling `log` method:
|
27
28
|
|
28
29
|
```ruby
|
29
30
|
# Store the metrics for 60 seconds (default behaviour)
|
30
|
-
EZmetrics.new.log(
|
31
|
+
EZmetrics.new.log(duration: 100.5, views: 40.7, db: 59.8, queries: 4, status: 200)
|
31
32
|
```
|
32
33
|
or
|
33
34
|
|
34
35
|
```ruby
|
35
36
|
# Store the metrics for 10 minutes
|
36
|
-
EZmetrics.new(10.minutes).log(
|
37
|
+
EZmetrics.new(10.minutes).log(duration: 100.5, views: 40.7, db: 59.8, queries: 4, status: 200)
|
37
38
|
```
|
38
39
|
|
39
40
|
For displaying metrics you need call `show` method:
|
40
41
|
|
41
42
|
```ruby
|
42
43
|
# Aggregate and show metrics for last 60 seconds (default behaviour)
|
43
|
-
EZmetrics.new.show
|
44
|
+
EZmetrics.new.show
|
44
45
|
```
|
45
46
|
|
46
47
|
or
|
@@ -69,10 +70,11 @@ end
|
|
69
70
|
ActiveSupport::Notifications.subscribe("process_action.action_controller") do |*args|
|
70
71
|
event = ActiveSupport::Notifications::Event.new(*args)
|
71
72
|
EZmetrics.new.log(
|
72
|
-
queries: Thread.current[:queries].to_i,
|
73
|
-
db: event.payload[:db_runtime].to_f,
|
74
73
|
duration: event.duration.to_f,
|
75
|
-
|
74
|
+
views: event.payload[:view_runtime].to_f,
|
75
|
+
db: event.payload[:db_runtime].to_f,
|
76
|
+
status: event.payload[:status].to_i || 500,
|
77
|
+
queries: Thread.current[:queries].to_i,
|
76
78
|
)
|
77
79
|
end
|
78
80
|
```
|
@@ -91,6 +93,10 @@ This will return a hash with the following structure:
|
|
91
93
|
avg: 5569,
|
92
94
|
max: 9675
|
93
95
|
},
|
96
|
+
views: {
|
97
|
+
avg: 12,
|
98
|
+
max: 240
|
99
|
+
},
|
94
100
|
db: {
|
95
101
|
avg: 155,
|
96
102
|
max: 4382
|
data/lib/ezmetrics.rb
CHANGED
@@ -8,36 +8,39 @@ class EZmetrics
|
|
8
8
|
@storage_key = "ez-metrics"
|
9
9
|
end
|
10
10
|
|
11
|
-
def log(payload={
|
12
|
-
|
11
|
+
def log(payload={duration: 0.0, views: 0.0, db: 0.0, queries: 0, status: 200})
|
12
|
+
@safe_payload = {
|
13
|
+
duration: payload[:duration].to_f,
|
14
|
+
views: payload[:views].to_f,
|
13
15
|
db: payload[:db].to_f,
|
14
16
|
queries: payload[:queries].to_i,
|
15
|
-
duration: payload[:duration].to_f,
|
16
17
|
status: payload[:status].to_i
|
17
18
|
}
|
18
19
|
|
19
|
-
this_second
|
20
|
-
status_group
|
21
|
-
this_second_metrics = redis.get("#{storage_key}:#{this_second}")
|
20
|
+
this_second = Time.now.to_i
|
21
|
+
status_group = "#{payload[:status].to_s[0]}xx"
|
22
|
+
@this_second_metrics = redis.get("#{storage_key}:#{this_second}")
|
22
23
|
|
23
24
|
if this_second_metrics
|
24
|
-
this_second_metrics = JSON.parse(this_second_metrics)
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
@this_second_metrics = JSON.parse(this_second_metrics)
|
26
|
+
|
27
|
+
[:duration, :views, :db, :queries].each do |metrics_type|
|
28
|
+
update_sum(metrics_type)
|
29
|
+
update_max(metrics_type)
|
30
|
+
end
|
31
|
+
|
28
32
|
this_second_metrics["statuses"]["all"] += 1
|
29
33
|
this_second_metrics["statuses"][status_group] += 1
|
30
|
-
this_second_metrics["db_max"] = [payload[:db], this_second_metrics["db_max"]].max
|
31
|
-
this_second_metrics["queries_max"] = [payload[:queries], this_second_metrics["queries_max"]].max
|
32
|
-
this_second_metrics["duration_max"] = [payload[:duration], this_second_metrics["duration_max"]].max
|
33
34
|
else
|
34
|
-
this_second_metrics = {
|
35
|
-
"
|
36
|
-
"
|
37
|
-
"
|
38
|
-
"
|
39
|
-
"
|
40
|
-
"
|
35
|
+
@this_second_metrics = {
|
36
|
+
"duration_sum" => safe_payload[:duration],
|
37
|
+
"duration_max" => safe_payload[:duration],
|
38
|
+
"views_sum" => safe_payload[:views],
|
39
|
+
"views_max" => safe_payload[:views],
|
40
|
+
"db_sum" => safe_payload[:db],
|
41
|
+
"db_max" => safe_payload[:db],
|
42
|
+
"queries_sum" => safe_payload[:queries],
|
43
|
+
"queries_max" => safe_payload[:queries],
|
41
44
|
"statuses" => { "2xx" => 0, "3xx" => 0, "4xx" => 0, "5xx" => 0, "all" => 1 }
|
42
45
|
}
|
43
46
|
|
@@ -67,7 +70,17 @@ class EZmetrics
|
|
67
70
|
|
68
71
|
private
|
69
72
|
|
70
|
-
attr_reader :redis, :interval_seconds, :interval_metrics, :requests, :storage_key
|
73
|
+
attr_reader :redis, :interval_seconds, :interval_metrics, :requests, :storage_key,
|
74
|
+
:safe_payload, :this_second_metrics
|
75
|
+
|
76
|
+
def update_sum(metrics)
|
77
|
+
this_second_metrics["#{metrics}_sum"] += safe_payload[metrics.to_sym]
|
78
|
+
end
|
79
|
+
|
80
|
+
def update_max(metrics)
|
81
|
+
max_value = [safe_payload[metrics.to_sym], this_second_metrics["#{metrics}_max"]].max
|
82
|
+
this_second_metrics["#{metrics}_max"] = max_value
|
83
|
+
end
|
71
84
|
|
72
85
|
def avg(metrics)
|
73
86
|
(interval_metrics.map { |h| h[metrics.to_s] }.sum.to_f / requests).round
|
@@ -95,6 +108,10 @@ class EZmetrics
|
|
95
108
|
avg: avg(:duration_sum),
|
96
109
|
max: max(:duration_max)
|
97
110
|
},
|
111
|
+
views: {
|
112
|
+
avg: avg(:views_sum),
|
113
|
+
max: max(:views_max)
|
114
|
+
},
|
98
115
|
db: {
|
99
116
|
avg: avg(:db_sum),
|
100
117
|
max: max(:db_max)
|
@@ -121,6 +138,10 @@ class EZmetrics
|
|
121
138
|
avg: 0,
|
122
139
|
max: 0
|
123
140
|
},
|
141
|
+
views: {
|
142
|
+
avg: 0,
|
143
|
+
max: 0
|
144
|
+
},
|
124
145
|
db: {
|
125
146
|
avg: 0,
|
126
147
|
max: 0
|