prometheus_aggregator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +58 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +15 -0
- data/Rakefile +10 -0
- data/lib/prometheus_aggregator.rb +12 -0
- data/lib/prometheus_aggregator/client.rb +39 -0
- data/lib/prometheus_aggregator/exporter.rb +137 -0
- data/lib/prometheus_aggregator/rack_middleware.rb +51 -0
- data/lib/prometheus_aggregator/version.rb +5 -0
- data/prometheus_aggregator.gemspec +32 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 93d4677b2b500010a25dc614a0d0e539f6440c37e756555fbe0b432716253ea9
|
4
|
+
data.tar.gz: daa57b4c30c284887e2ace5a4159c0c2792c199e7d2accfaef66379ad519ab6b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3095f13baacd9c31432239547d1e6334219512c2f5affdb9de96fd8a8bf6d2395efdf99b02dbe70c94080ce212602d4babe54cf6272eb2a731b85ec2596931dc
|
7
|
+
data.tar.gz: 30c432202d312b10e1f5347ae6d705699b8dc3fb5d51ab5a796ad8669cd562da3fc096812b222abb5d9340261e7293c47cc798f039c4ec4ce35ba182d7c419c9
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisplayCopNames: true
|
3
|
+
TargetRubyVersion: 2.5
|
4
|
+
Exclude:
|
5
|
+
- vendor/**/*
|
6
|
+
- bin/**/*
|
7
|
+
- tmp/**/*
|
8
|
+
|
9
|
+
Layout/DotPosition:
|
10
|
+
EnforcedStyle: trailing
|
11
|
+
|
12
|
+
Layout/RescueEnsureAlignment:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Metrics/ClassLength:
|
16
|
+
Max: 250
|
17
|
+
|
18
|
+
Metrics/ModuleLength:
|
19
|
+
Max: 200
|
20
|
+
|
21
|
+
Metrics/CyclomaticComplexity:
|
22
|
+
Max: 7
|
23
|
+
|
24
|
+
Metrics/LineLength:
|
25
|
+
Max: 80
|
26
|
+
|
27
|
+
Metrics/AbcSize:
|
28
|
+
Max: 30
|
29
|
+
|
30
|
+
Metrics/MethodLength:
|
31
|
+
Max: 25
|
32
|
+
|
33
|
+
Metrics/BlockLength:
|
34
|
+
Exclude:
|
35
|
+
- spec/**/*
|
36
|
+
- dependabot-core.gemspec
|
37
|
+
|
38
|
+
Metrics/ParameterLists:
|
39
|
+
CountKeywordArgs: false
|
40
|
+
|
41
|
+
Naming/FileName:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
Style/StringLiterals:
|
45
|
+
EnforcedStyle: double_quotes
|
46
|
+
|
47
|
+
Style/SignalException:
|
48
|
+
EnforcedStyle: only_raise
|
49
|
+
|
50
|
+
Style/Documentation:
|
51
|
+
Enabled: false
|
52
|
+
|
53
|
+
Style/PercentLiteralDelimiters:
|
54
|
+
PreferredDelimiters:
|
55
|
+
'%i': ()
|
56
|
+
'%I': ()
|
57
|
+
'%w': ()
|
58
|
+
'%W': ()
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Dependabot
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# `prometheus_aggregator`
|
2
|
+
|
3
|
+
A Ruby client for https://github.com/peterbourgon/prometheus-aggregator.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'prometheus_aggregator'
|
11
|
+
```
|
12
|
+
|
13
|
+
## License
|
14
|
+
|
15
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "prometheus_aggregator/exporter"
|
4
|
+
|
5
|
+
module PrometheusAggregator
|
6
|
+
class Client
|
7
|
+
DEFAULT_BUCKETS = [0.01, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10].freeze
|
8
|
+
|
9
|
+
def initialize(host, port, opts = {})
|
10
|
+
@default_labels = opts.delete(:default_labels) || {}
|
11
|
+
@exporter = Exporter.new(host, port, opts)
|
12
|
+
end
|
13
|
+
|
14
|
+
def counter(name:, value:, help:, labels: {})
|
15
|
+
@exporter.enqueue(
|
16
|
+
type: "counter",
|
17
|
+
name: name,
|
18
|
+
value: value,
|
19
|
+
help: help,
|
20
|
+
labels: @default_labels.dup.merge(labels)
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
def histogram(name:, value:, help:, buckets: DEFAULT_BUCKETS, labels: {})
|
25
|
+
@exporter.enqueue(
|
26
|
+
type: "histogram",
|
27
|
+
name: name,
|
28
|
+
value: value,
|
29
|
+
help: help,
|
30
|
+
buckets: buckets,
|
31
|
+
labels: @default_labels.dup.merge(labels)
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
def stop
|
36
|
+
@exporter.stop
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "openssl"
|
4
|
+
require "json"
|
5
|
+
require "socket"
|
6
|
+
require "net_tcp_client"
|
7
|
+
require "prometheus_aggregator"
|
8
|
+
|
9
|
+
module PrometheusAggregator
|
10
|
+
class Exporter
|
11
|
+
CONNECTION_RETRY_INTERVAL = 1.0
|
12
|
+
QUEUE_CAPACITY = 100
|
13
|
+
LOOP_INTERVAL = 0.01
|
14
|
+
STALENESS_THRESHOLD = 5.0
|
15
|
+
|
16
|
+
def initialize(host, port, opts = {})
|
17
|
+
@host = host
|
18
|
+
@port = port
|
19
|
+
@ssl_params = opts[:ssl_params]
|
20
|
+
@staleness_threshold = opts[:staleness_threshold] || STALENESS_THRESHOLD
|
21
|
+
@connection_retry_interval =
|
22
|
+
opts[:connection_retry_interval] || CONNECTION_RETRY_INTERVAL
|
23
|
+
@registered = {}
|
24
|
+
|
25
|
+
@stop = false
|
26
|
+
@pid = nil
|
27
|
+
@mutex = Mutex.new
|
28
|
+
@queue = []
|
29
|
+
end
|
30
|
+
|
31
|
+
def enqueue(record)
|
32
|
+
@mutex.synchronize do
|
33
|
+
if Process.pid != @pid
|
34
|
+
PrometheusAggregator.logger.info("Pid mismatch, spawning new thread")
|
35
|
+
@thread&.kill
|
36
|
+
@thread = nil
|
37
|
+
end
|
38
|
+
|
39
|
+
if @thread.nil?
|
40
|
+
@thread = Thread.new { write_loop }
|
41
|
+
@pid = Process.pid
|
42
|
+
end
|
43
|
+
|
44
|
+
@queue << [Time.now, record]
|
45
|
+
@queue.shift while @queue.length > QUEUE_CAPACITY
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def backlog
|
50
|
+
@queue.length
|
51
|
+
end
|
52
|
+
|
53
|
+
def stop
|
54
|
+
@stop = true
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def write_loop
|
60
|
+
loop do
|
61
|
+
break if @stop
|
62
|
+
|
63
|
+
connect unless connection_ok?
|
64
|
+
unless connection_ok?
|
65
|
+
PrometheusAggregator.logger.warn(
|
66
|
+
"Not connected to prometheus agggregator (#{@host}:#{@port})"
|
67
|
+
)
|
68
|
+
|
69
|
+
sleep(@connection_retry_interval)
|
70
|
+
next
|
71
|
+
end
|
72
|
+
|
73
|
+
event = @mutex.synchronize do
|
74
|
+
@queue.shift while !@queue.empty? && stale?(@queue.first)
|
75
|
+
@queue.shift
|
76
|
+
end
|
77
|
+
|
78
|
+
if event.nil?
|
79
|
+
sleep(LOOP_INTERVAL)
|
80
|
+
next
|
81
|
+
end
|
82
|
+
|
83
|
+
record = event[1]
|
84
|
+
register(record)
|
85
|
+
emit_value(record)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def stale?(record)
|
90
|
+
record[0] < Time.now - @staleness_threshold
|
91
|
+
end
|
92
|
+
|
93
|
+
def connection_ok?
|
94
|
+
!@socket.nil? && @socket.alive?
|
95
|
+
end
|
96
|
+
|
97
|
+
def connect
|
98
|
+
@registered = {}
|
99
|
+
|
100
|
+
@socket&.close
|
101
|
+
|
102
|
+
@socket = Net::TCPClient.new(
|
103
|
+
server: "#{@host}:#{@port}",
|
104
|
+
ssl: @ssl_params,
|
105
|
+
connect_timeout: 3.0,
|
106
|
+
write_timeout: 3.0,
|
107
|
+
read_timeout: 3.0,
|
108
|
+
connect_retry_count: 0
|
109
|
+
)
|
110
|
+
rescue Net::TCPClient::ConnectionFailure => err
|
111
|
+
PrometheusAggregator.logger.debug(err)
|
112
|
+
@socket&.close
|
113
|
+
@socket = nil
|
114
|
+
end
|
115
|
+
|
116
|
+
def register(record)
|
117
|
+
declaration = record.reject { |k| k == :value }
|
118
|
+
json_declaration = JSON.fast_generate(declaration)
|
119
|
+
return if @registered[json_declaration]
|
120
|
+
|
121
|
+
send_line(json_declaration)
|
122
|
+
@registered[json_declaration] = true
|
123
|
+
end
|
124
|
+
|
125
|
+
def emit_value(record)
|
126
|
+
send_line(JSON.fast_generate(record.slice(:name, :value, :labels)))
|
127
|
+
end
|
128
|
+
|
129
|
+
def send_line(line)
|
130
|
+
@socket.write(line + "\n")
|
131
|
+
rescue Net::TCPClient::ConnectionFailure => err
|
132
|
+
PrometheusAggregator.logger.debug(err)
|
133
|
+
@socket&.close
|
134
|
+
@socket = nil
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "prometheus_aggregator"
|
4
|
+
|
5
|
+
module PrometheusAggregator
|
6
|
+
class RackMiddleware
|
7
|
+
def initialize(app, options = {})
|
8
|
+
@app = app
|
9
|
+
@client = options[:client]
|
10
|
+
raise ArgumentError, ":client option is required" unless @client
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
start_time = Time.now
|
15
|
+
response = @app.call(env)
|
16
|
+
duration = Time.now - start_time
|
17
|
+
|
18
|
+
begin
|
19
|
+
@client.counter(
|
20
|
+
name: "http_server_requests_total",
|
21
|
+
help: "The total number of HTTP requests handled by the Rack app",
|
22
|
+
value: 1,
|
23
|
+
labels: labels(env).merge(code: response.first.to_s)
|
24
|
+
)
|
25
|
+
|
26
|
+
@client.histogram(
|
27
|
+
name: "http_server_request_duration_seconds",
|
28
|
+
help: "The HTTP response duration of the Rack application",
|
29
|
+
value: duration,
|
30
|
+
labels: labels(env)
|
31
|
+
)
|
32
|
+
rescue => err # rubocop:disable Style/RescueStandardError
|
33
|
+
# Let's be ultra defensive. Metrics should never break the app.
|
34
|
+
PrometheusAggregator.logger.error("RackMiddleware: #{err}")
|
35
|
+
end
|
36
|
+
|
37
|
+
response
|
38
|
+
end
|
39
|
+
|
40
|
+
def labels(env)
|
41
|
+
{
|
42
|
+
method: env["REQUEST_METHOD"].downcase,
|
43
|
+
path: clean_path(env["PATH_INFO"])
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
def clean_path(path)
|
48
|
+
path.gsub(%r{/\d+/}, "/:id/").gsub(%r{/\d+$}, "/:id")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "prometheus_aggregator/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "prometheus_aggregator"
|
8
|
+
spec.version = PrometheusAggregator::VERSION
|
9
|
+
spec.authors = ["Harry Marr"]
|
10
|
+
spec.email = ["support@dependabot.com"]
|
11
|
+
|
12
|
+
spec.summary = "Client for https://github.com/peterbourgon/prometheus-aggregator"
|
13
|
+
spec.homepage = "https://github.com/dependabot/prometheus-aggregator-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_runtime_dependency "net_tcp_client", "~> 2.0"
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
28
|
+
spec.add_development_dependency "excon", "~> 0.62"
|
29
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
30
|
+
spec.add_development_dependency "rack", "~> 2.0"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prometheus_aggregator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Harry Marr
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: net_tcp_client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: excon
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.62'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.62'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- support@dependabot.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rubocop.yml"
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- lib/prometheus_aggregator.rb
|
111
|
+
- lib/prometheus_aggregator/client.rb
|
112
|
+
- lib/prometheus_aggregator/exporter.rb
|
113
|
+
- lib/prometheus_aggregator/rack_middleware.rb
|
114
|
+
- lib/prometheus_aggregator/version.rb
|
115
|
+
- prometheus_aggregator.gemspec
|
116
|
+
homepage: https://github.com/dependabot/prometheus-aggregator-ruby
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata: {}
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.7.6
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: Client for https://github.com/peterbourgon/prometheus-aggregator
|
140
|
+
test_files: []
|