puma-newrelic-codeur 0.1.11 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/puma/newrelic/plugin.rb +1 -1
- data/lib/puma/newrelic/sampler.rb +46 -5
- data/lib/puma/newrelic/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: deeb0397b1f0915290df6b38eaf216ea5d890d925686f84b0750fb2d08307a3a
|
4
|
+
data.tar.gz: 8f85e4fc25e3b8ff0928363ef12c333be2281f046465c9a0c7b0e58b11e6a787
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7eb3de70d9831ca1b60e85e7a122659f3eb55ce54aa4e66fecb834881c0e8afdcf2914f4c9a6cadba5b450f44dfcf3ca3c7f9c7b921468ae4f32f843939345a2
|
7
|
+
data.tar.gz: 4472ba6ce624f43f89c63f8e9e39a9c2c622f56477606a45f5ac118e9c5411ff76fbfaf56e194eb5c8932454e2cca0c4e99bfb3a9c9ced023fb8c1563a7dd498
|
data/lib/puma/newrelic/plugin.rb
CHANGED
@@ -1,28 +1,41 @@
|
|
1
|
-
require "
|
1
|
+
require "yaml"
|
2
|
+
require "faraday"
|
2
3
|
|
3
4
|
module Puma
|
4
5
|
module NewRelic
|
5
6
|
class Sampler
|
6
7
|
def initialize(launcher)
|
7
|
-
|
8
|
+
@newrelic_config = YAML.load_file("config/newrelic.yml", aliases: true)[ENV["RAILS_ENV"]]
|
9
|
+
config = @newrelic_config[:puma] || {}
|
8
10
|
@launcher = launcher
|
9
11
|
@sample_rate = config.fetch("sample_rate", 23)
|
10
12
|
@keys = config.fetch("keys", %i[backlog running pool_capacity max_threads requests_count]).map(&:to_sym)
|
11
13
|
@last_sample_at = Time.now
|
14
|
+
@conn = Faraday.new(url: "https://metric-api.eu.newrelic.com/metric/v1") do |faraday|
|
15
|
+
faraday.headers["Content-Type"] = "application/json"
|
16
|
+
faraday.headers["Api-Key"] = @newrelic_config["license_key"]
|
17
|
+
faraday.adapter Faraday.default_adapter
|
18
|
+
end
|
12
19
|
end
|
13
20
|
|
14
21
|
def collect
|
22
|
+
return unless agent_enabled?
|
23
|
+
|
15
24
|
@running = true
|
16
25
|
while @running
|
17
26
|
sleep 1
|
18
27
|
|
19
28
|
if should_sample?
|
20
|
-
record_metrics(@launcher.stats)
|
21
29
|
@last_sample_at = Time.now
|
30
|
+
record_metrics(@launcher.stats)
|
22
31
|
end
|
23
32
|
end
|
24
33
|
end
|
25
34
|
|
35
|
+
def agent_enabled?
|
36
|
+
@newrelic_config["agent_enabled"]
|
37
|
+
end
|
38
|
+
|
26
39
|
def should_sample?
|
27
40
|
Time.now - @last_sample_at > @sample_rate
|
28
41
|
end
|
@@ -44,10 +57,38 @@ module Puma
|
|
44
57
|
stats.each { |key, value| metrics[key] += value if @keys.include?(key) }
|
45
58
|
end
|
46
59
|
|
60
|
+
payload = [
|
61
|
+
{
|
62
|
+
common: {
|
63
|
+
timestamp: Time.now.to_i,
|
64
|
+
"interval.ms": @sample_rate * 1000,
|
65
|
+
attributes: {
|
66
|
+
"app.name": @newrelic_config["app_name"]
|
67
|
+
}
|
68
|
+
},
|
69
|
+
metrics: []
|
70
|
+
}
|
71
|
+
]
|
47
72
|
metrics.each do |key, value|
|
48
|
-
|
49
|
-
|
73
|
+
payload[0]["metrics"] << {
|
74
|
+
name: "Custom/Puma/#{key}",
|
75
|
+
type: "count",
|
76
|
+
value: value
|
77
|
+
}
|
50
78
|
end
|
79
|
+
|
80
|
+
response = @conn.post do |req|
|
81
|
+
req.body = payload.to_json
|
82
|
+
end
|
83
|
+
|
84
|
+
if response.status != 202
|
85
|
+
@launcher.log_writer.write("Failed to send metrics to New Relic: #{response.status} - #{response.body}")
|
86
|
+
end
|
87
|
+
|
88
|
+
# metrics.each do |key, value|
|
89
|
+
# ::NewRelic::Agent.logger.info("Recorded metric: Custom/Puma/#{key}=#{value}")
|
90
|
+
# ::NewRelic::Agent.record_metric("Custom/Puma/#{key}", value)
|
91
|
+
# end
|
51
92
|
end
|
52
93
|
end
|
53
94
|
end
|