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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -10
  3. data/lib/ezmetrics.rb +42 -21
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 55610b1357968e974ca2e525a6d09bd2245c8f5e
4
- data.tar.gz: efef7c5d22cc0c4d9eb27e91b2149a23e3eb4832
3
+ metadata.gz: 1bd4e2f35cc2cf1b50efbac72d107041dc68129e
4
+ data.tar.gz: 179ed567aac638d1ab0d3a4ed86eaad458c55fd1
5
5
  SHA512:
6
- metadata.gz: a161d7d0715d82958a89131e31c3344dcd4553d78909406bedd0041c0d91e05486be64b2c7c382783b540ca837a57bc97763cc9fc1a88a821f7e19083b22e0ce
7
- data.tar.gz: 32073d20b467a1c2e98b0efe561f8513d7e2de23dd75740c42cc3628a62112b1ee69658d533f2b91c99453c375556815dda4515d159b90d7ccf1b7b9cba602e8
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
- - `queries`
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(status: 200, db: 300.45, duration: 320.45, queries: 5)
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(status: 200, db: 300.45, duration: 320.45, queries: 5)
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
- status: event.payload[:status].to_i || 500
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
@@ -8,36 +8,39 @@ class EZmetrics
8
8
  @storage_key = "ez-metrics"
9
9
  end
10
10
 
11
- def log(payload={db: 0.0, duration: 0.0, queries: 0, status: 200})
12
- payload = {
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 = Time.now.to_i
20
- status_group = "#{payload[:status].to_s[0]}xx"
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
- this_second_metrics["db_sum"] += payload[:db]
26
- this_second_metrics["queries_sum"] += payload[:queries]
27
- this_second_metrics["duration_sum"] += payload[:duration]
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
- "db_sum" => payload[:db],
36
- "db_max" => payload[:db],
37
- "queries_sum" => payload[:queries],
38
- "queries_max" => payload[:queries],
39
- "duration_sum" => payload[:duration],
40
- "duration_max" => payload[:duration],
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ezmetrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolae Rotaru