prometheus-collector 0.0.1
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 +7 -0
- data/Gemfile +3 -0
- data/README.md +47 -0
- data/bin/prometheus-collector +15 -0
- data/config.ru +6 -0
- data/lib/prometheus/collector/application.rb +53 -0
- data/lib/prometheus/collector/extensions/http_requests.rb +9 -0
- data/lib/prometheus/collector/extensions.rb +76 -0
- data/lib/prometheus/collector/version.rb +5 -0
- data/lib/prometheus/collector.rb +27 -0
- data/prometheus-collector.gemspec +28 -0
- data/template/Gemfile +3 -0
- data/template/app.rb +18 -0
- metadata +188 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e0b9ae81681b71f64ad5b6689d4ffb5cd3544ec774ea928b04c240635938d843
|
4
|
+
data.tar.gz: 8ff03517b8f2cebfd84e56fee1f16ac4426933535f9d89995b72303a4d4d8bda
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 983dc30a1ab55e8cf6a872b3302d0894d801e89830042df4c3a57bd5b30f15b7f44e0eeb9db21fc8bc5c313fd87a188b1a1b0d9a2970a3334f2be632563379e5
|
7
|
+
data.tar.gz: 5ed27c398bd5df4a3fc5512285e55bc925ae33056567bfd367ea92ce16e36c854bd147c350f167b471e512c132c717251601f7cc1439076fb3ea37ba78be85cd
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
Application to gather prometheus style metrics
|
2
|
+
|
3
|
+
# Usage
|
4
|
+
|
5
|
+
Install the gem into your gemfile
|
6
|
+
|
7
|
+
```gem prometheus-collector```
|
8
|
+
|
9
|
+
Install your gemset
|
10
|
+
|
11
|
+
```bundle install```
|
12
|
+
|
13
|
+
Consume the program.
|
14
|
+
|
15
|
+
|
16
|
+
```
|
17
|
+
require 'prometheus/collector'
|
18
|
+
|
19
|
+
class Guage < Prometheus::Collector::Extensions::Base
|
20
|
+
install
|
21
|
+
|
22
|
+
def run
|
23
|
+
# Do some things that would be collected in Prometheus::Client Objects
|
24
|
+
end
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
Mount the Prometheus::Collector::Application application, or start it from your app.rb
|
29
|
+
|
30
|
+
```
|
31
|
+
Prometheus::Collector::Application.start
|
32
|
+
```
|
33
|
+
|
34
|
+
# How it works
|
35
|
+
|
36
|
+
The collector app makes use of the Prometheus client collector and exporter middleware to allow you to write custom applications that export prometheus style metrics.
|
37
|
+
|
38
|
+
It is designed as a bare-bones scaffold to get you off the ground with a ruby applet to get some statistics.
|
39
|
+
|
40
|
+
It utilizes rack and its middleware.
|
41
|
+
|
42
|
+
The interface is fairly straightforward: Your Metric Executing code needs to extend Prometheus::Collector::Extensions::Base for 'repeatedly-runbable' operations and Prometheus::Collector::Extensions::Once for something that should only be executed Once.
|
43
|
+
|
44
|
+
Your class should implement an instance level `run` function, and may optionally implement a class level `schedule` function: This must return a `cron` style string to tell the application when to invoke your `run` code. By default, `schedule` is set to `* * * * *` which would allow the code to be executed every minute.
|
45
|
+
|
46
|
+
### Scheduling
|
47
|
+
Scheduling is implemented via em-cron. Thus the re-scheduling of a task should occur within the parameters of the `schedule` string but is evaluated upon completion. Thus in normal operation, the code should not execute more than one `run` of a given worker definition at a time.
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'fileutils'
|
3
|
+
def generate(path)
|
4
|
+
FileUtils.copy_entry 'template', path
|
5
|
+
Dir.chdir(path) do
|
6
|
+
system 'bundle install'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
case ARGV[0]
|
11
|
+
when 'generate'
|
12
|
+
generate ARGV[1]
|
13
|
+
else
|
14
|
+
puts 'unimplemented'
|
15
|
+
end
|
data/config.ru
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'prometheus/middleware/collector'
|
2
|
+
require 'prometheus/middleware/exporter'
|
3
|
+
require 'rack'
|
4
|
+
module Prometheus
|
5
|
+
module Collector
|
6
|
+
class Application
|
7
|
+
def self.app
|
8
|
+
Rack::Builder.app do
|
9
|
+
use Rack::CommonLogger, Prometheus::Collector.logger
|
10
|
+
use Rack::Deflater
|
11
|
+
use Prometheus::Middleware::Collector
|
12
|
+
use Prometheus::Middleware::Exporter
|
13
|
+
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['OK']] }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.start
|
18
|
+
initialize_static_extensions
|
19
|
+
initialize_runnable_extensions
|
20
|
+
Rack::Server.start(
|
21
|
+
app: app, Port: 9292
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.initialize_static_extensions
|
26
|
+
Prometheus::Collector.logger.info "Initializing Static Extensions"
|
27
|
+
Extensions.values.each do |o|
|
28
|
+
Prometheus::Collector.logger.info "Initializing #{o.class.name}"
|
29
|
+
begin
|
30
|
+
o.callback.call o.call
|
31
|
+
rescue => e
|
32
|
+
o.errback.call e
|
33
|
+
end
|
34
|
+
end
|
35
|
+
Prometheus::Collector.logger.info "Done initializing Static Extensions"
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.initialize_runnable_extensions
|
39
|
+
Prometheus::Collector.logger.info "Initializing Runnable Extensions"
|
40
|
+
Thread.new do
|
41
|
+
EM.run do
|
42
|
+
Extensions::Runnable.values.each do |o|
|
43
|
+
Prometheus::Collector.logger.info "Initializing #{o.class.name}"
|
44
|
+
EM::Cron.schedule(o.class.schedule) do |time|
|
45
|
+
EM.defer(o, o.callback, o.errback)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class HTTPRequests < Prometheus::Collector::Extensions::Once
|
2
|
+
install
|
3
|
+
|
4
|
+
def run
|
5
|
+
return if @requests
|
6
|
+
@requests = Prometheus::Client::Counter.new(:http_requests, docstring: 'A counter of HTTP Requests')
|
7
|
+
# Prometheus::Client.registry.register(@requests)
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'register'
|
2
|
+
|
3
|
+
module Prometheus
|
4
|
+
module Collector
|
5
|
+
module Extensions
|
6
|
+
include Register
|
7
|
+
module Runnable
|
8
|
+
include Register
|
9
|
+
end
|
10
|
+
|
11
|
+
class Base
|
12
|
+
class << self
|
13
|
+
attr_writer :logger, :schedule
|
14
|
+
|
15
|
+
def logger
|
16
|
+
@logger ||= Prometheus::Collector.logger
|
17
|
+
end
|
18
|
+
|
19
|
+
def schedule
|
20
|
+
@schedule ||= '* * * * *'
|
21
|
+
end
|
22
|
+
|
23
|
+
def install
|
24
|
+
logger.info "Install #{name.to_sym}"
|
25
|
+
Prometheus::Collector::Extensions::Runnable.register name.to_sym, new
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def log(msg, level: :info)
|
30
|
+
self.class.logger.send(level, self.class) { msg } if self.class.logger
|
31
|
+
end
|
32
|
+
|
33
|
+
def call
|
34
|
+
log 'Starting call'
|
35
|
+
run
|
36
|
+
end
|
37
|
+
|
38
|
+
def run
|
39
|
+
raise 'Not Implemented - please implement a run function'
|
40
|
+
end
|
41
|
+
|
42
|
+
def callback
|
43
|
+
proc { log 'Finished call' }
|
44
|
+
end
|
45
|
+
|
46
|
+
def errback
|
47
|
+
proc do |error|
|
48
|
+
log "Exception in call: '#{error}'", level: :error
|
49
|
+
log 'Backtrace:', level: :debug
|
50
|
+
error.backtrace.each do |l|
|
51
|
+
log l, level: :debug
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class Once < Base
|
58
|
+
class << self
|
59
|
+
def install
|
60
|
+
logger.info "Install #{name.to_sym}"
|
61
|
+
Prometheus::Collector::Extensions.register name.to_sym, new
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def call
|
66
|
+
log 'Installing Runner Once'
|
67
|
+
run
|
68
|
+
end
|
69
|
+
|
70
|
+
def callback
|
71
|
+
proc { log 'Finished Installing Runner Once' }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'prometheus/client'
|
2
|
+
|
3
|
+
require 'register'
|
4
|
+
require 'em/cron'
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
|
8
|
+
require 'prometheus/collector/extensions'
|
9
|
+
require 'prometheus/collector/application'
|
10
|
+
|
11
|
+
module Prometheus
|
12
|
+
module Collector
|
13
|
+
class << self
|
14
|
+
attr_writer :logger
|
15
|
+
|
16
|
+
def logger
|
17
|
+
@logger ||= Logger.new ENV.fetch('PROMETHEUS_COLLECTOR_LOGGER', STDOUT)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
Dir.glob("#{File.dirname(__FILE__)}/collector/extensions/*").each do |f|
|
25
|
+
require "prometheus/collector/extensions/#{File.basename(f)}"
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'prometheus/collector/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'prometheus-collector'
|
7
|
+
spec.version = Prometheus::Collector::VERSION
|
8
|
+
spec.authors = ['Stuart Harland']
|
9
|
+
spec.email = ['essjayhch@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Scaffold to collect prometheus style metrics from an application via a RACK interface'
|
12
|
+
spec.description = File.read('README.md')
|
13
|
+
|
14
|
+
spec.homepage = 'https://github.com/essjayhch/prometheus-collector'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files =`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
spec.executables << 'prometheus-collector'
|
20
|
+
|
21
|
+
spec.add_dependency 'prometheus-client', '> 0'
|
22
|
+
spec.add_dependency 'puma', '> 0'
|
23
|
+
spec.add_dependency 'rack', '> 0'
|
24
|
+
spec.add_dependency 'register', '> 0'
|
25
|
+
spec.add_dependency 'em-cron', '> 0'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'fileutils'
|
28
|
+
end
|
data/template/Gemfile
ADDED
data/template/app.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'prometheus/collector'
|
2
|
+
|
3
|
+
class Guage < Prometheus::Collector::Extensions::Base
|
4
|
+
install
|
5
|
+
|
6
|
+
def run
|
7
|
+
counter.increment(labels: { service: 'foo'})
|
8
|
+
end
|
9
|
+
|
10
|
+
def counter
|
11
|
+
return @counter if @counter
|
12
|
+
@counter = Prometheus::Client::Counter.new(:test_counter, docstring: 'Rolling Counter', labels: [:service])
|
13
|
+
Prometheus::Client.registry.register(@counter)
|
14
|
+
@counter
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Prometheus::Collector::Application.start
|
metadata
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prometheus-collector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stuart Harland
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-12-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: prometheus-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: puma
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rack
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: register
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: em-cron
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: fileutils
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: |
|
98
|
+
Application to gather prometheus style metrics
|
99
|
+
|
100
|
+
# Usage
|
101
|
+
|
102
|
+
Install the gem into your gemfile
|
103
|
+
|
104
|
+
```gem prometheus-collector```
|
105
|
+
|
106
|
+
Install your gemset
|
107
|
+
|
108
|
+
```bundle install```
|
109
|
+
|
110
|
+
Consume the program.
|
111
|
+
|
112
|
+
|
113
|
+
```
|
114
|
+
require 'prometheus/collector'
|
115
|
+
|
116
|
+
class Guage < Prometheus::Collector::Extensions::Base
|
117
|
+
install
|
118
|
+
|
119
|
+
def run
|
120
|
+
# Do some things that would be collected in Prometheus::Client Objects
|
121
|
+
end
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
Mount the Prometheus::Collector::Application application, or start it from your app.rb
|
126
|
+
|
127
|
+
```
|
128
|
+
Prometheus::Collector::Application.start
|
129
|
+
```
|
130
|
+
|
131
|
+
# How it works
|
132
|
+
|
133
|
+
The collector app makes use of the Prometheus client collector and exporter middleware to allow you to write custom applications that export prometheus style metrics.
|
134
|
+
|
135
|
+
It is designed as a bare-bones scaffold to get you off the ground with a ruby applet to get some statistics.
|
136
|
+
|
137
|
+
It utilizes rack and its middleware.
|
138
|
+
|
139
|
+
The interface is fairly straightforward: Your Metric Executing code needs to extend Prometheus::Collector::Extensions::Base for 'repeatedly-runbable' operations and Prometheus::Collector::Extensions::Once for something that should only be executed Once.
|
140
|
+
|
141
|
+
Your class should implement an instance level `run` function, and may optionally implement a class level `schedule` function: This must return a `cron` style string to tell the application when to invoke your `run` code. By default, `schedule` is set to `* * * * *` which would allow the code to be executed every minute.
|
142
|
+
|
143
|
+
### Scheduling
|
144
|
+
Scheduling is implemented via em-cron. Thus the re-scheduling of a task should occur within the parameters of the `schedule` string but is evaluated upon completion. Thus in normal operation, the code should not execute more than one `run` of a given worker definition at a time.
|
145
|
+
email:
|
146
|
+
- essjayhch@gmail.com
|
147
|
+
executables:
|
148
|
+
- prometheus-collector
|
149
|
+
extensions: []
|
150
|
+
extra_rdoc_files: []
|
151
|
+
files:
|
152
|
+
- Gemfile
|
153
|
+
- README.md
|
154
|
+
- bin/prometheus-collector
|
155
|
+
- config.ru
|
156
|
+
- lib/prometheus/collector.rb
|
157
|
+
- lib/prometheus/collector/application.rb
|
158
|
+
- lib/prometheus/collector/extensions.rb
|
159
|
+
- lib/prometheus/collector/extensions/http_requests.rb
|
160
|
+
- lib/prometheus/collector/version.rb
|
161
|
+
- prometheus-collector.gemspec
|
162
|
+
- template/Gemfile
|
163
|
+
- template/app.rb
|
164
|
+
homepage: https://github.com/essjayhch/prometheus-collector
|
165
|
+
licenses:
|
166
|
+
- MIT
|
167
|
+
metadata: {}
|
168
|
+
post_install_message:
|
169
|
+
rdoc_options: []
|
170
|
+
require_paths:
|
171
|
+
- lib
|
172
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
173
|
+
requirements:
|
174
|
+
- - ">="
|
175
|
+
- !ruby/object:Gem::Version
|
176
|
+
version: '0'
|
177
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
178
|
+
requirements:
|
179
|
+
- - ">="
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
182
|
+
requirements: []
|
183
|
+
rubygems_version: 3.1.4
|
184
|
+
signing_key:
|
185
|
+
specification_version: 4
|
186
|
+
summary: Scaffold to collect prometheus style metrics from an application via a RACK
|
187
|
+
interface
|
188
|
+
test_files: []
|