puma-plugin-statsd 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e2328d05d42da5a0259a1db34e06d577b80640c055601c4689581a9ffdc3d66
4
- data.tar.gz: e69455947064fb8fd3ce30af4d5a97853e0511f0998d11f424924b0fcfe2452f
3
+ metadata.gz: c327637905c572e3ed22e9936b440597f77d032abe2b155e73776bd845c9588c
4
+ data.tar.gz: 5e57068f3dfaa9a5f9334825ff5bf7493a8286a2b46feb6da5b9fca36e605a92
5
5
  SHA512:
6
- metadata.gz: 885de8a9ccc13c7f1bb95fe160b18c27e204fe71d68fc13e75024cba27ff83f235b1a7876e4b37bb3482987583962eccf7d2cef3d051fba3b48a7c452145bfce
7
- data.tar.gz: 7560d10e03f50ad938408e735026ecec85b8542c79a3e394d1cc8c5250521875c16b5e56e6401776262a6e2ea9aeaafb3b589792b49562c92b0744044a3f8392
6
+ metadata.gz: 7b5cd14ef13b733fe0488cb61dd63a74811566a467d45ec7f9a661515a61cd7b7a0cde4e4ff1e4f71f64d5f52e7c76a5a52e4addbc7085ec7159c90e88e62231
7
+ data.tar.gz: 91c439d68efdf850d777bc380ee9ef3ca3c1233d04bb8891c1a204bbd038ff75f436c7bf3fb09caf8fa4576fed37385979aa94cd39f9288125c7d2eeccd7f1c2
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.2.0 2020-02-29
4
+
5
+ * Added option to prefix stats metric (via STATSD_METRIC_PREFIX env var)
6
+
3
7
  ## 0.1.0 2019-07-06
4
8
 
5
9
  * The statsd port is now configurable
@@ -7,6 +7,7 @@ require 'socket'
7
7
  class StatsdConnector
8
8
  ENV_NAME = "STATSD_HOST"
9
9
  STATSD_TYPES = { count: 'c', gauge: 'g' }
10
+ METRIC_DELIMETER = ".".freeze
10
11
 
11
12
  attr_reader :host, :port
12
13
 
@@ -40,46 +41,46 @@ class PumaStats
40
41
  end
41
42
 
42
43
  def clustered?
43
- @stats.has_key? "workers"
44
+ @stats.has_key?(:workers)
44
45
  end
45
46
 
46
47
  def workers
47
- @stats.fetch("workers", 1)
48
+ @stats.fetch(:workers, 1)
48
49
  end
49
50
 
50
51
  def booted_workers
51
- @stats.fetch("booted_workers", 1)
52
+ @stats.fetch(:booted_workers, 1)
52
53
  end
53
54
 
54
55
  def running
55
56
  if clustered?
56
- @stats["worker_status"].map { |s| s["last_status"].fetch("running", 0) }.inject(0, &:+)
57
+ @stats[:worker_status].map { |s| s[:last_status].fetch(:running, 0) }.inject(0, &:+)
57
58
  else
58
- @stats.fetch("running", 0)
59
+ @stats.fetch(:running, 0)
59
60
  end
60
61
  end
61
62
 
62
63
  def backlog
63
64
  if clustered?
64
- @stats["worker_status"].map { |s| s["last_status"].fetch("backlog", 0) }.inject(0, &:+)
65
+ @stats[:worker_status].map { |s| s[:last_status].fetch(:backlog, 0) }.inject(0, &:+)
65
66
  else
66
- @stats.fetch("backlog", 0)
67
+ @stats.fetch(:backlog, 0)
67
68
  end
68
69
  end
69
70
 
70
71
  def pool_capacity
71
72
  if clustered?
72
- @stats["worker_status"].map { |s| s["last_status"].fetch("pool_capacity", 0) }.inject(0, &:+)
73
+ @stats[:worker_status].map { |s| s[:last_status].fetch(:pool_capacity, 0) }.inject(0, &:+)
73
74
  else
74
- @stats.fetch("pool_capacity", 0)
75
+ @stats.fetch(:pool_capacity, 0)
75
76
  end
76
77
  end
77
78
 
78
79
  def max_threads
79
80
  if clustered?
80
- @stats["worker_status"].map { |s| s["last_status"].fetch("max_threads", 0) }.inject(0, &:+)
81
+ @stats[:worker_status].map { |s| s[:last_status].fetch(:max_threads, 0) }.inject(0, &:+)
81
82
  else
82
- @stats.fetch("max_threads", 0)
83
+ @stats.fetch(:max_threads, 0)
83
84
  end
84
85
  end
85
86
  end
@@ -97,6 +98,13 @@ Puma::Plugin.create do
97
98
  @statsd = ::StatsdConnector.new
98
99
  if @statsd.enabled?
99
100
  @launcher.events.debug "statsd: enabled (host: #{@statsd.host})"
101
+
102
+ # Fetch global metric prefix from env variable
103
+ @metric_prefix = ENV.fetch("STATSD_METRIC_PREFIX", nil)
104
+ if @metric_prefix && !@metric_prefix.end_with?(::StatsdConnector::METRIC_DELIMETER)
105
+ @metric_prefix += ::StatsdConnector::METRIC_DELIMETER
106
+ end
107
+
100
108
  register_hooks
101
109
  else
102
110
  @launcher.events.debug "statsd: not enabled (no #{StatsdConnector::ENV_NAME} env var found)"
@@ -110,7 +118,7 @@ Puma::Plugin.create do
110
118
  end
111
119
 
112
120
  def fetch_stats
113
- JSON.parse(Puma.stats)
121
+ JSON.parse(Puma.stats, symbolize_names: true)
114
122
  end
115
123
 
116
124
  def tags
@@ -124,6 +132,10 @@ Puma::Plugin.create do
124
132
  tags
125
133
  end
126
134
 
135
+ def prefixed_metric_name(puma_metric)
136
+ "#{@metric_prefix}#{puma_metric}"
137
+ end
138
+
127
139
  # Send data to statsd every few seconds
128
140
  def stats_loop
129
141
  sleep 5
@@ -131,12 +143,12 @@ Puma::Plugin.create do
131
143
  @launcher.events.debug "statsd: notify statsd"
132
144
  begin
133
145
  stats = ::PumaStats.new(fetch_stats)
134
- @statsd.send(metric_name: "puma.workers", value: stats.workers, type: :gauge, tags: tags)
135
- @statsd.send(metric_name: "puma.booted_workers", value: stats.booted_workers, type: :gauge, tags: tags)
136
- @statsd.send(metric_name: "puma.running", value: stats.running, type: :gauge, tags: tags)
137
- @statsd.send(metric_name: "puma.backlog", value: stats.backlog, type: :gauge, tags: tags)
138
- @statsd.send(metric_name: "puma.pool_capacity", value: stats.pool_capacity, type: :gauge, tags: tags)
139
- @statsd.send(metric_name: "puma.max_threads", value: stats.max_threads, type: :gauge, tags: tags)
146
+ @statsd.send(metric_name: prefixed_metric_name("puma.workers"), value: stats.workers, type: :gauge, tags: tags)
147
+ @statsd.send(metric_name: prefixed_metric_name("puma.booted_workers"), value: stats.booted_workers, type: :gauge, tags: tags)
148
+ @statsd.send(metric_name: prefixed_metric_name("puma.running"), value: stats.running, type: :gauge, tags: tags)
149
+ @statsd.send(metric_name: prefixed_metric_name("puma.backlog"), value: stats.backlog, type: :gauge, tags: tags)
150
+ @statsd.send(metric_name: prefixed_metric_name("puma.pool_capacity"), value: stats.pool_capacity, type: :gauge, tags: tags)
151
+ @statsd.send(metric_name: prefixed_metric_name("puma.max_threads"), value: stats.max_threads, type: :gauge, tags: tags)
140
152
  rescue StandardError => e
141
153
  @launcher.events.error "! statsd: notify stats failed:\n #{e.to_s}\n #{e.backtrace.join("\n ")}"
142
154
  ensure
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puma-plugin-statsd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Healy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-06 00:00:00.000000000 Z
11
+ date: 2020-02-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puma
@@ -143,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  - !ruby/object:Gem::Version
144
144
  version: '0'
145
145
  requirements: []
146
- rubygems_version: 3.0.1
146
+ rubygems_version: 3.0.3
147
147
  signing_key:
148
148
  specification_version: 4
149
149
  summary: Send puma metrics to statsd via a background thread