puma-newrelic-codeur 0.1.11 → 0.1.13
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/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: '0195325b801d6caeb6d91cb7f4bed411816254c595e409946f2b9213722082d0'
|
4
|
+
data.tar.gz: 3593843db70aae24a4471b5b17b806bf13e7fbc5a99636438c119f619efbe8b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ac929fb8eec76aa714264a8c77e169a940c108c75ea795fe5c8e9cc9a5ea386a045c2aabbf4feac1ba7ea81ab8f67bb670c3a52dea07b553efbfdf325177d43
|
7
|
+
data.tar.gz: e29a3722c3ff9c5d10af3dc28be114ee87cb268294048dd255ddcaebeeaaaecbb5f6c9724c5fdca19766c8d02a8d079b4047a259c700e4f2ee96d7bf9b9a5afa
|
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
|