prom_multi_proc_rb 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 +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +119 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/prom_multi_proc/base.rb +103 -0
- data/lib/prom_multi_proc/collector.rb +60 -0
- data/lib/prom_multi_proc/counter.rb +11 -0
- data/lib/prom_multi_proc/gauge.rb +27 -0
- data/lib/prom_multi_proc/histogram.rb +7 -0
- data/lib/prom_multi_proc/proxy.rb +66 -0
- data/lib/prom_multi_proc/rails.rb +39 -0
- data/lib/prom_multi_proc/summary.rb +7 -0
- data/lib/prom_multi_proc/version.rb +3 -0
- data/lib/prom_multi_proc/writer.rb +78 -0
- data/lib/prom_multi_proc.rb +30 -0
- data/prom_multi_proc_rb.gemspec +39 -0
- metadata +145 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aaa90b86011843d393312165dd5bf5d6682a675d
|
4
|
+
data.tar.gz: f5b1bd68707e154fe475a8d8d7be6264e42e27f1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7797fc3804567bc937f086906185de812ad03ea16a04eb8a6b9e61a3bf639298d285e2c4961abe885505e57a055f65c32b4d14390f33320ca00b2c4f65043f7d
|
7
|
+
data.tar.gz: d6a74316d992ddeec905b4227e8bfb18af04c7be824f440ace6c7dfcbd8995b1652b53741b7dcfcac16ea08f05374bb9f53bbb0f6ac7b96416f082648dd770f4
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at atongen@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Andrew Tongen
|
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,119 @@
|
|
1
|
+
# prom_multi_proc_rb
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/atongen/prom_multi_proc_rb.svg?branch=master)](https://travis-ci.org/atongen/prom_multi_proc_rb)
|
4
|
+
|
5
|
+
Ruby client library for collecting prometheus metrics.
|
6
|
+
Designed for use in applications running under forking servers (unicorn, puma).
|
7
|
+
Writes metrics in json format to unix socket being listened to
|
8
|
+
by [prom_multi_proc](https://github.com/atongen/prom_multi_proc).
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'prom_multi_proc_rb', require: 'prom_multi_proc'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install prom_multi_proc_rb
|
25
|
+
|
26
|
+
## General Usage
|
27
|
+
|
28
|
+
### Define metrics
|
29
|
+
|
30
|
+
Create a json file to define the prometheus metrics that your application will track, for example:
|
31
|
+
|
32
|
+
```json
|
33
|
+
[
|
34
|
+
{
|
35
|
+
"type": "counter",
|
36
|
+
"name": "app_test_counter_total",
|
37
|
+
"help": "A test counter",
|
38
|
+
"labels": [
|
39
|
+
"label1",
|
40
|
+
]
|
41
|
+
},
|
42
|
+
{
|
43
|
+
"type": "gauge",
|
44
|
+
"name": "app_test_gauge_total",
|
45
|
+
"help": "A test gauge",
|
46
|
+
"labels": [
|
47
|
+
"label2",
|
48
|
+
]
|
49
|
+
},
|
50
|
+
{
|
51
|
+
"type": "histogram",
|
52
|
+
"name": "app_test_histogram_seconds",
|
53
|
+
"help": "A test histogram",
|
54
|
+
"labels": [
|
55
|
+
"label3",
|
56
|
+
]
|
57
|
+
},
|
58
|
+
{
|
59
|
+
"type": "summary",
|
60
|
+
"name": "app_test_summary_seconds",
|
61
|
+
"help": "A test summary",
|
62
|
+
"labels": [
|
63
|
+
"label4",
|
64
|
+
]
|
65
|
+
}
|
66
|
+
]
|
67
|
+
```
|
68
|
+
|
69
|
+
This file is intened to be shared by both the aggregator process and this ruby library.
|
70
|
+
|
71
|
+
### Install and start the aggregator process
|
72
|
+
|
73
|
+
Download, install, and start the [prom_multi_proc](https://github.com/atongen/prom_multi_proc)
|
74
|
+
aggregator application using the metrics json definition file created earlier.
|
75
|
+
Make note of the socket location.
|
76
|
+
|
77
|
+
Note that in development, the ruby client will funtion normally if there is no aggregator process listening
|
78
|
+
on the socket.
|
79
|
+
|
80
|
+
### Collect metrics in ruby app
|
81
|
+
|
82
|
+
Create a `PromMultiProc::Base` object for collecting metrics
|
83
|
+
and begin collecting metrics:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
metrics = PromMultiProc::Base.new(
|
87
|
+
prefix: "app_",
|
88
|
+
socket: "path/to/aggregator/socket.sock",
|
89
|
+
metrics: "path/to/metrics/definition.json",
|
90
|
+
batch_size: 10,
|
91
|
+
validate: true
|
92
|
+
)
|
93
|
+
|
94
|
+
metrics.test_counter_total.inc(label1: "my-label-value")
|
95
|
+
metrics.test_histogram_seconds.observe(2.3, label3: "my-other-label-value")
|
96
|
+
```
|
97
|
+
|
98
|
+
## Rails Usage
|
99
|
+
|
100
|
+
This helper class can be used to simplify a rails initializer, for example, in `config/initializers/prom_multi_proc.rb`:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
$prom_multi_proc = PromMultiProc::Rails.init
|
104
|
+
|
105
|
+
$prom_multi_proc.test_counter_total.inc(label1: "my-label-value")
|
106
|
+
$prom_multi_proc.test_histogram_seconds.observe(2.3, label3: "my-other-label-value")
|
107
|
+
```
|
108
|
+
|
109
|
+
## Contributing
|
110
|
+
|
111
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/atongen/prom_multi_proc_rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
112
|
+
|
113
|
+
## License
|
114
|
+
|
115
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
116
|
+
|
117
|
+
## Code of Conduct
|
118
|
+
|
119
|
+
Everyone interacting in the prom_multi_proc_rb project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/atongen/prom_multi_proc_rb/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "prom_multi_proc"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require "logger"
|
2
|
+
require "concurrent"
|
3
|
+
|
4
|
+
module PromMultiProc
|
5
|
+
class Base
|
6
|
+
attr_reader :logger, :prefix, :writer
|
7
|
+
|
8
|
+
def initialize(socket:, metrics:, batch_size: 1, logger: nil, validate: false, prefix: "")
|
9
|
+
@prefix = prefix
|
10
|
+
@logger = logger || ::Logger.new(STDOUT)
|
11
|
+
|
12
|
+
unless File.socket?(socket)
|
13
|
+
@logger.warn("Socket does not exist: #{socket}")
|
14
|
+
end
|
15
|
+
|
16
|
+
@metric_objects = Concurrent::Map.new
|
17
|
+
@writer = Writer.new(socket: socket, batch_size: batch_size, validate: validate)
|
18
|
+
@multi_lock = Mutex.new
|
19
|
+
|
20
|
+
specs = get_specs(metrics)
|
21
|
+
process_specs!(specs)
|
22
|
+
end
|
23
|
+
|
24
|
+
def metric(name)
|
25
|
+
@metric_objects[name]
|
26
|
+
end
|
27
|
+
|
28
|
+
def metric?(name)
|
29
|
+
@metric_objects.key?(name)
|
30
|
+
end
|
31
|
+
|
32
|
+
def metrics
|
33
|
+
@metric_objects.keys
|
34
|
+
end
|
35
|
+
|
36
|
+
def multi
|
37
|
+
return unless block_given?
|
38
|
+
result = @multi_lock.synchronize do
|
39
|
+
Proxy.new(self).tap do |proxy|
|
40
|
+
yield(proxy)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
@writer.write_multi(result.multis)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def valid_metric?(name)
|
49
|
+
!!METRIC_RE.match(name)
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_specs(file)
|
53
|
+
unless File.file?(file)
|
54
|
+
raise PromMultiProcError.new("Metric definition file not found: #{file}")
|
55
|
+
end
|
56
|
+
|
57
|
+
begin
|
58
|
+
JSON.parse(File.read(file))
|
59
|
+
rescue JSON::ParserError => e
|
60
|
+
raise PromMultiProcError.new("Metric definition file (#{file}) is not valid json: #{e}")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def process_specs!(specs)
|
65
|
+
specs.each { |spec| process_spec!(spec) }
|
66
|
+
end
|
67
|
+
|
68
|
+
def process_spec!(spec)
|
69
|
+
klazz = TYPES[spec["type"].to_sym]
|
70
|
+
unless klazz
|
71
|
+
raise PromMultiProcError.new("Unkown type: #{spec.inspect}")
|
72
|
+
end
|
73
|
+
|
74
|
+
unless valid_metric?(spec["name"])
|
75
|
+
raise PromMultiProcError.new("Invalid name: #{spec.inspect}")
|
76
|
+
end
|
77
|
+
|
78
|
+
unless spec["name"].start_with?(prefix)
|
79
|
+
raise PromMultiProcError.new("Metric '#{spec['name']}' must start with prefix '#{prefix}'")
|
80
|
+
end
|
81
|
+
name = spec["name"].sub(/\A#{prefix}/, "").to_sym
|
82
|
+
|
83
|
+
labels = (spec["labels"] || []).map(&:to_sym)
|
84
|
+
unless labels.all? { |l| valid_metric?(l) }
|
85
|
+
raise PromMultiProcError.new("Invalid label: #{spec.inspect}")
|
86
|
+
end
|
87
|
+
|
88
|
+
if self.class.instance_methods(false).include?(name) || methods(false).include?(name)
|
89
|
+
raise PromMultiProcError.new("Metric method exists: #{name}")
|
90
|
+
end
|
91
|
+
|
92
|
+
if @metric_objects.key?(name)
|
93
|
+
raise PromMultiProcError.new("Metric already exists: #{name}")
|
94
|
+
end
|
95
|
+
|
96
|
+
@metric_objects[name] = klazz.new(spec["name"], labels, @writer)
|
97
|
+
|
98
|
+
define_singleton_method(name) do
|
99
|
+
@metric_objects[name]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module PromMultiProc
|
2
|
+
class Collector
|
3
|
+
attr_reader :name, :metric_methods
|
4
|
+
|
5
|
+
def initialize(name, label_keys, writer)
|
6
|
+
@name = name
|
7
|
+
@label_keys = label_keys
|
8
|
+
@writer = writer
|
9
|
+
@metric_methods = (public_methods(false) - %i(
|
10
|
+
validate!
|
11
|
+
valid_method? valid_label_keys? valid_label_values? valid_value?
|
12
|
+
to_msg
|
13
|
+
)).map(&:to_s)
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate!(method, value, labels)
|
17
|
+
unless valid_method?(method)
|
18
|
+
raise PromMultiProcError.new("Invalid metric method (#{method}): try: #{metric_methods.inspect}")
|
19
|
+
end
|
20
|
+
unless valid_label_keys?(labels)
|
21
|
+
raise PromMultiProcError.new("Invalid label cardinality (#{name}): #{labels.keys.inspect}, need keys: #{@label_keys.inspect}")
|
22
|
+
end
|
23
|
+
unless valid_label_values?(labels)
|
24
|
+
raise PromMultiProcError.new("Invalid label values (#{name}): #{labels.values.inspect}")
|
25
|
+
end
|
26
|
+
unless valid_value?(value)
|
27
|
+
raise PromMultiProcError.new("Invalid value (#{name}): #{value.inspect} (must be numeric)")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def valid_method?(method)
|
32
|
+
metric_methods.include?(method)
|
33
|
+
end
|
34
|
+
|
35
|
+
def valid_label_keys?(labels)
|
36
|
+
labels.keys == @label_keys
|
37
|
+
end
|
38
|
+
|
39
|
+
def valid_label_values?(labels)
|
40
|
+
labels.values.all? { |v| v.is_a?(String) || v.is_a?(Symbol) }
|
41
|
+
end
|
42
|
+
|
43
|
+
def valid_value?(value)
|
44
|
+
value.is_a?(Numeric)
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_msg(method, value, labels)
|
48
|
+
{ "name" => name,
|
49
|
+
"method" => method,
|
50
|
+
"value" => value.to_f,
|
51
|
+
"label_values" => labels.values }
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def write(method, value, labels)
|
57
|
+
@writer.write(self, method, value, labels)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module PromMultiProc
|
2
|
+
class Gauge < Collector
|
3
|
+
def set(value, labels = {})
|
4
|
+
write("set".freeze, value, labels)
|
5
|
+
end
|
6
|
+
|
7
|
+
def inc(labels = {})
|
8
|
+
write("inc".freeze, 1, labels)
|
9
|
+
end
|
10
|
+
|
11
|
+
def dec(labels = {})
|
12
|
+
write("dec".freeze, 1, labels)
|
13
|
+
end
|
14
|
+
|
15
|
+
def add(value, labels = {})
|
16
|
+
write("add".freeze, value, labels)
|
17
|
+
end
|
18
|
+
|
19
|
+
def sub(value, labels = {})
|
20
|
+
write("sub".freeze, value, labels)
|
21
|
+
end
|
22
|
+
|
23
|
+
def set_to_current_time(labels = {})
|
24
|
+
write("set_to_current_time".freeze, 1, labels)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module PromMultiProc
|
2
|
+
class Proxy
|
3
|
+
attr_reader :multis
|
4
|
+
|
5
|
+
def initialize(base)
|
6
|
+
@base = base
|
7
|
+
@proxies = {}
|
8
|
+
@multis = []
|
9
|
+
|
10
|
+
add_proxy_methods
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_multi(collector, method, value, labels)
|
14
|
+
@multis << [collector, method, value, labels]
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def add_proxy_methods
|
20
|
+
@base.metrics.each do |name|
|
21
|
+
@proxies[name] = ProxyCollector.new(self, @base.metric(name))
|
22
|
+
define_singleton_method(name) do
|
23
|
+
@proxies[name]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class ProxyCollector
|
30
|
+
def initialize(proxy, collector)
|
31
|
+
@proxy = proxy
|
32
|
+
@collector = collector
|
33
|
+
|
34
|
+
add_proxy_methods
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def add_proxy_methods
|
40
|
+
@collector.metric_methods.each do |meth|
|
41
|
+
define_singleton_method(meth) do |*args|
|
42
|
+
case args.length
|
43
|
+
when 0
|
44
|
+
value = 1.0
|
45
|
+
labels = {}
|
46
|
+
when 1
|
47
|
+
if args[0].is_a?(Hash)
|
48
|
+
value = 1.0
|
49
|
+
labels = args[0]
|
50
|
+
else
|
51
|
+
value = args[0]
|
52
|
+
labels = {}
|
53
|
+
end
|
54
|
+
when 2
|
55
|
+
value = args[0]
|
56
|
+
labels = args[1]
|
57
|
+
else
|
58
|
+
raise PromMultiProcError.new("Invalid number of arguments")
|
59
|
+
end
|
60
|
+
|
61
|
+
@proxy.add_multi(@collector, meth, value, labels)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module PromMultiProc
|
2
|
+
module Rails
|
3
|
+
def self.init(prefix = nil)
|
4
|
+
metrics = ENV.fetch("PROM_MULTI_PROC_DEFINITION_FILE", ::Rails.root.join("config/metrics.json").to_s)
|
5
|
+
socket = ENV.fetch("PROM_MULTI_PROC_SOCKET", ::Rails.root.join("tmp/sockets/metrics.sock").to_s)
|
6
|
+
|
7
|
+
program_name = File.basename($PROGRAM_NAME)
|
8
|
+
app_name = ::Rails.application.class.name.underscore.split("/").first
|
9
|
+
prefix ||= "#{app_name}_"
|
10
|
+
|
11
|
+
if ENV.key?("PROM_MULTI_PROC_BATCH_SIZE")
|
12
|
+
batch_size = ENV["PROM_MULTI_PROC_BATCH_SIZE"].to_i
|
13
|
+
elsif %w(rails rake).include?(name) || ::Rails.env.development? || ::Rails.env.test?
|
14
|
+
batch_size = 1
|
15
|
+
elsif ::Rails.env.production?
|
16
|
+
batch_size = 100
|
17
|
+
else
|
18
|
+
batch_size = 5
|
19
|
+
end
|
20
|
+
|
21
|
+
if ::Rails.env.development? || ::Rails.env.test?
|
22
|
+
validate = true
|
23
|
+
else
|
24
|
+
validate = false
|
25
|
+
end
|
26
|
+
|
27
|
+
::Rails.logger.error("Setting up prom_multi_proc for #{app_name}-#{program_name}, batch size: #{batch_size}, validate: #{validate}")
|
28
|
+
|
29
|
+
Base.new(
|
30
|
+
prefix: prefix,
|
31
|
+
socket: socket,
|
32
|
+
metrics: metrics,
|
33
|
+
batch_size: batch_size,
|
34
|
+
validate: validate,
|
35
|
+
logger: ::Rails.logger
|
36
|
+
)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module PromMultiProc
|
2
|
+
class Writer
|
3
|
+
attr_reader :socket, :batch_size
|
4
|
+
|
5
|
+
def initialize(socket:, batch_size: 1, validate: false)
|
6
|
+
if !batch_size.is_a?(Fixnum) || batch_size <= 0
|
7
|
+
raise PromMultiProcError.new("Invalid batch size: #{batch_size}")
|
8
|
+
end
|
9
|
+
@batch_size = batch_size
|
10
|
+
@validate = !!validate
|
11
|
+
|
12
|
+
@lock = Mutex.new
|
13
|
+
@messages = []
|
14
|
+
|
15
|
+
@socket = socket
|
16
|
+
end
|
17
|
+
|
18
|
+
def validate?
|
19
|
+
@validate
|
20
|
+
end
|
21
|
+
|
22
|
+
def write(metric, method, value, labels)
|
23
|
+
@lock.synchronize do
|
24
|
+
metric.validate!(method, value, labels) if validate?
|
25
|
+
@messages << metric.to_msg(method, value, labels)
|
26
|
+
end
|
27
|
+
|
28
|
+
flush
|
29
|
+
end
|
30
|
+
|
31
|
+
# array of arrays where inner array is length 4 matching arguments
|
32
|
+
# for signature of #write
|
33
|
+
def write_multi(metrics)
|
34
|
+
@lock.synchronize do
|
35
|
+
if validate?
|
36
|
+
metrics.each do |m, method, value, labels|
|
37
|
+
m.validate!(method, value, labels)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
metrics.each do |m, method, value, labels|
|
42
|
+
@messages << m.to_msg(method, value, labels)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
flush
|
47
|
+
end
|
48
|
+
|
49
|
+
def flush
|
50
|
+
@lock.synchronize do
|
51
|
+
if @messages.length >= batch_size
|
52
|
+
begin
|
53
|
+
write_socket(JSON.generate(@messages))
|
54
|
+
ensure
|
55
|
+
@messages.clear
|
56
|
+
end
|
57
|
+
else
|
58
|
+
true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def socket?
|
64
|
+
!!write_socket("\n")
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def write_socket(msg)
|
70
|
+
s = UNIXSocket.new(@socket)
|
71
|
+
s.send(msg, 0)
|
72
|
+
s.close
|
73
|
+
true
|
74
|
+
rescue StandardError
|
75
|
+
false
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "socket"
|
2
|
+
require "json"
|
3
|
+
require "thread"
|
4
|
+
|
5
|
+
require "prom_multi_proc/version"
|
6
|
+
|
7
|
+
require "prom_multi_proc/collector"
|
8
|
+
require "prom_multi_proc/counter"
|
9
|
+
require "prom_multi_proc/gauge"
|
10
|
+
require "prom_multi_proc/histogram"
|
11
|
+
require "prom_multi_proc/summary"
|
12
|
+
|
13
|
+
require "prom_multi_proc/writer"
|
14
|
+
require "prom_multi_proc/proxy"
|
15
|
+
require "prom_multi_proc/base"
|
16
|
+
|
17
|
+
module PromMultiProc
|
18
|
+
class PromMultiProcError < StandardError; end
|
19
|
+
|
20
|
+
TYPES = {
|
21
|
+
counter: Counter,
|
22
|
+
gauge: Gauge,
|
23
|
+
histogram: Histogram,
|
24
|
+
summary: Summary
|
25
|
+
}
|
26
|
+
|
27
|
+
METRIC_RE = /\A[a-z]+[0-9a-z_]+\Z/
|
28
|
+
end
|
29
|
+
|
30
|
+
require "prom_multi_proc/rails" if defined?(::Rails)
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "prom_multi_proc/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "prom_multi_proc_rb"
|
8
|
+
spec.version = PromMultiProc::VERSION
|
9
|
+
spec.authors = ["Andrew Tongen"]
|
10
|
+
spec.email = ["atongen@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A ruby library for collecting prometheus metrics within forking servers}
|
13
|
+
spec.description = %q{A ruby library for collecting prometheus metrics within forking servers}
|
14
|
+
spec.homepage = "https://github.com/atongen/prom_multi_proc_rb"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "bin"
|
30
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
36
|
+
spec.add_development_dependency "rspec-collection_matchers", "~> 1.0"
|
37
|
+
|
38
|
+
spec.add_dependency "concurrent-ruby", "~> 1.0", ">= 1.0.2"
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prom_multi_proc_rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Tongen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-collection_matchers
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: concurrent-ruby
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 1.0.2
|
79
|
+
type: :runtime
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - "~>"
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '1.0'
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 1.0.2
|
89
|
+
description: A ruby library for collecting prometheus metrics within forking servers
|
90
|
+
email:
|
91
|
+
- atongen@gmail.com
|
92
|
+
executables:
|
93
|
+
- console
|
94
|
+
- setup
|
95
|
+
extensions: []
|
96
|
+
extra_rdoc_files: []
|
97
|
+
files:
|
98
|
+
- ".gitignore"
|
99
|
+
- ".rspec"
|
100
|
+
- ".travis.yml"
|
101
|
+
- CODE_OF_CONDUCT.md
|
102
|
+
- Gemfile
|
103
|
+
- LICENSE.txt
|
104
|
+
- README.md
|
105
|
+
- Rakefile
|
106
|
+
- bin/console
|
107
|
+
- bin/setup
|
108
|
+
- lib/prom_multi_proc.rb
|
109
|
+
- lib/prom_multi_proc/base.rb
|
110
|
+
- lib/prom_multi_proc/collector.rb
|
111
|
+
- lib/prom_multi_proc/counter.rb
|
112
|
+
- lib/prom_multi_proc/gauge.rb
|
113
|
+
- lib/prom_multi_proc/histogram.rb
|
114
|
+
- lib/prom_multi_proc/proxy.rb
|
115
|
+
- lib/prom_multi_proc/rails.rb
|
116
|
+
- lib/prom_multi_proc/summary.rb
|
117
|
+
- lib/prom_multi_proc/version.rb
|
118
|
+
- lib/prom_multi_proc/writer.rb
|
119
|
+
- prom_multi_proc_rb.gemspec
|
120
|
+
homepage: https://github.com/atongen/prom_multi_proc_rb
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata:
|
124
|
+
allowed_push_host: https://rubygems.org
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 2.5.2
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: A ruby library for collecting prometheus metrics within forking servers
|
145
|
+
test_files: []
|