bc-prometheus-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +30 -0
  3. data/CODE_OF_CONDUCT.md +49 -0
  4. data/README.md +67 -0
  5. data/bc-prometheus-ruby.gemspec +46 -0
  6. data/lib/bigcommerce/prometheus.rb +62 -0
  7. data/lib/bigcommerce/prometheus/client.rb +80 -0
  8. data/lib/bigcommerce/prometheus/collectors/resque.rb +85 -0
  9. data/lib/bigcommerce/prometheus/configuration.rb +127 -0
  10. data/lib/bigcommerce/prometheus/instrumentors/hutch.rb +72 -0
  11. data/lib/bigcommerce/prometheus/instrumentors/resque.rb +72 -0
  12. data/lib/bigcommerce/prometheus/instrumentors/web.rb +84 -0
  13. data/lib/bigcommerce/prometheus/integrations/puma.rb +55 -0
  14. data/lib/bigcommerce/prometheus/integrations/railtie.rb +31 -0
  15. data/lib/bigcommerce/prometheus/integrations/resque.rb +41 -0
  16. data/lib/bigcommerce/prometheus/loggable.rb +32 -0
  17. data/lib/bigcommerce/prometheus/server.rb +117 -0
  18. data/lib/bigcommerce/prometheus/servers/thin/controllers/base_controller.rb +63 -0
  19. data/lib/bigcommerce/prometheus/servers/thin/controllers/error_controller.rb +36 -0
  20. data/lib/bigcommerce/prometheus/servers/thin/controllers/metrics_controller.rb +87 -0
  21. data/lib/bigcommerce/prometheus/servers/thin/controllers/not_found_controller.rb +36 -0
  22. data/lib/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller.rb +92 -0
  23. data/lib/bigcommerce/prometheus/servers/thin/rack_app.rb +88 -0
  24. data/lib/bigcommerce/prometheus/servers/thin/server.rb +48 -0
  25. data/lib/bigcommerce/prometheus/servers/thin/server_metrics.rb +98 -0
  26. data/lib/bigcommerce/prometheus/type_collectors/resque.rb +82 -0
  27. data/lib/bigcommerce/prometheus/version.rb +22 -0
  28. metadata +209 -0
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2019-present, BigCommerce Pty. Ltd. All rights reserved
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6
+ # documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7
+ # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ # persons to whom the Software is furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11
+ # Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
+ # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
+ #
18
+ require 'timeout'
19
+ require 'zlib'
20
+ require 'stringio'
21
+
22
+ module Bigcommerce
23
+ module Prometheus
24
+ module Servers
25
+ module Thin
26
+ ##
27
+ # Server metrics for the collector
28
+ #
29
+ class ServerMetrics
30
+ ##
31
+ # @param [::Logger] logger
32
+ #
33
+ def initialize(logger: nil)
34
+ @logger = logger || ::Bigcommerce::Prometheus.logger
35
+ @metrics_total = ::PrometheusExporter::Metric::Counter.new('collector_metrics_total', 'Total metrics processed by exporter.')
36
+ @sessions_total = ::PrometheusExporter::Metric::Counter.new('collector_sessions_total', 'Total send_metric sessions processed by exporter.')
37
+ @bad_metrics_total = ::PrometheusExporter::Metric::Counter.new('collector_bad_metrics_total', 'Total mis-handled metrics by collector.')
38
+ @collector_working_gauge = ::PrometheusExporter::Metric::Gauge.new('collector_working', 'Is the master process collector able to collect metrics')
39
+ @collector_rss_gauge = ::PrometheusExporter::Metric::Gauge.new('collector_rss', 'total memory used by collector process')
40
+ end
41
+
42
+ def add_session
43
+ @sessions_total.observe
44
+ end
45
+
46
+ def add_metric
47
+ @metrics_total.observe
48
+ end
49
+
50
+ def add_bad_metric
51
+ @bad_metrics_total.observe
52
+ end
53
+
54
+ ##
55
+ # @param [Boolean] working
56
+ # @return [String]
57
+ #
58
+ def to_prometheus_text(working: true)
59
+ collect(working: working).map(&:to_prometheus_text).join("\n\n")
60
+ end
61
+
62
+ private
63
+
64
+ ##
65
+ # Collect server metrics
66
+ #
67
+ # @param [Boolean] working
68
+ #
69
+ def collect(working: true)
70
+ @collector_working_gauge.observe(working ? 1 : 0)
71
+ @collector_rss_gauge.observe(rss)
72
+
73
+ [
74
+ @metrics_total,
75
+ @sessions_total,
76
+ @bad_metrics_total,
77
+ @collector_working_gauge,
78
+ @collector_rss_gauge
79
+ ]
80
+ end
81
+
82
+ ##
83
+ # Get RSS size of the current process
84
+ #
85
+ # @return [Integer]
86
+ #
87
+ def rss
88
+ _pid, size = `ps ax -o pid,rss | grep -E "^[[:space:]]*#{::Process.pid}"`.strip.split.map(&:to_i)
89
+ size
90
+ rescue StandardError => e
91
+ @logger.error "Failed to get RSS size: #{e.message}"
92
+ 0
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2019-present, BigCommerce Pty. Ltd. All rights reserved
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6
+ # documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7
+ # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ # persons to whom the Software is furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11
+ # Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
+ # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
+ #
18
+ module Bigcommerce
19
+ module Prometheus
20
+ module TypeCollectors
21
+ ##
22
+ # Collect resque data from collectors and parse them into metrics
23
+ #
24
+ class Resque < PrometheusExporter::Server::TypeCollector
25
+ ##
26
+ # Initialize the collector
27
+ #
28
+ def initialize
29
+ @workers_total = PrometheusExporter::Metric::Gauge.new('resque_workers_total', 'Number of active workers')
30
+ @jobs_failed_total = PrometheusExporter::Metric::Gauge.new('jobs_failed_total', 'Number of failed jobs')
31
+ @jobs_pending_total = PrometheusExporter::Metric::Gauge.new('jobs_pending_total', 'Number of pending jobs')
32
+ @jobs_processed_total = PrometheusExporter::Metric::Gauge.new('jobs_processed_total', 'Number of processed jobs')
33
+ @queues_total = PrometheusExporter::Metric::Gauge.new('queues_total', 'Number of total queues')
34
+ @queue_sizes = PrometheusExporter::Metric::Gauge.new('queue_sizes', 'Size of each queue')
35
+ end
36
+
37
+ ##
38
+ # @return [String]
39
+ #
40
+ def type
41
+ 'resque'
42
+ end
43
+
44
+ ##
45
+ # @return [Array]
46
+ #
47
+ def metrics
48
+ return [] unless @workers_total
49
+
50
+ [
51
+ @workers_total,
52
+ @jobs_failed_total,
53
+ @jobs_pending_total,
54
+ @jobs_processed_total,
55
+ @queues_total,
56
+ @queue_sizes
57
+ ]
58
+ end
59
+
60
+ ##
61
+ # Collect resque metrics from input data
62
+ #
63
+ def collect(obj)
64
+ default_labels = { environment: obj['environment'] }
65
+
66
+ custom_labels = obj['custom_labels']
67
+ labels = custom_labels.nil? ? default_labels : default_labels.merge(custom_labels)
68
+
69
+ @workers_total.observe(obj['workers_total'], labels)
70
+ @jobs_failed_total.observe(obj['jobs_failed_total'], labels)
71
+ @jobs_pending_total.observe(obj['jobs_pending_total'], labels)
72
+ @jobs_processed_total.observe(obj['jobs_processed_total'], labels)
73
+ @queues_total.observe(obj['queues_total'], labels)
74
+
75
+ obj['queues'].each do |name, size|
76
+ @queue_sizes.observe(size, labels.merge(queue: name))
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2019-present, BigCommerce Pty. Ltd. All rights reserved
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6
+ # documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7
+ # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ # persons to whom the Software is furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11
+ # Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
+ # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
+ #
18
+ module Bigcommerce
19
+ module Prometheus
20
+ VERSION = '0.1.0'
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,209 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bc-prometheus-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shaun McCormick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec_junit_formatter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler-audit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: null-logger
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0.12'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0.12'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0.74'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0.74'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0.16'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0.16'
125
+ - !ruby/object:Gem::Dependency
126
+ name: prometheus_exporter
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.4'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.4'
139
+ - !ruby/object:Gem::Dependency
140
+ name: thin
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '1.7'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '1.7'
153
+ description: Simple integration of ruby and puma servers with prometheus
154
+ email:
155
+ - shaun.mccormick@bigcommerce.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - CHANGELOG.md
161
+ - CODE_OF_CONDUCT.md
162
+ - README.md
163
+ - bc-prometheus-ruby.gemspec
164
+ - lib/bigcommerce/prometheus.rb
165
+ - lib/bigcommerce/prometheus/client.rb
166
+ - lib/bigcommerce/prometheus/collectors/resque.rb
167
+ - lib/bigcommerce/prometheus/configuration.rb
168
+ - lib/bigcommerce/prometheus/instrumentors/hutch.rb
169
+ - lib/bigcommerce/prometheus/instrumentors/resque.rb
170
+ - lib/bigcommerce/prometheus/instrumentors/web.rb
171
+ - lib/bigcommerce/prometheus/integrations/puma.rb
172
+ - lib/bigcommerce/prometheus/integrations/railtie.rb
173
+ - lib/bigcommerce/prometheus/integrations/resque.rb
174
+ - lib/bigcommerce/prometheus/loggable.rb
175
+ - lib/bigcommerce/prometheus/server.rb
176
+ - lib/bigcommerce/prometheus/servers/thin/controllers/base_controller.rb
177
+ - lib/bigcommerce/prometheus/servers/thin/controllers/error_controller.rb
178
+ - lib/bigcommerce/prometheus/servers/thin/controllers/metrics_controller.rb
179
+ - lib/bigcommerce/prometheus/servers/thin/controllers/not_found_controller.rb
180
+ - lib/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller.rb
181
+ - lib/bigcommerce/prometheus/servers/thin/rack_app.rb
182
+ - lib/bigcommerce/prometheus/servers/thin/server.rb
183
+ - lib/bigcommerce/prometheus/servers/thin/server_metrics.rb
184
+ - lib/bigcommerce/prometheus/type_collectors/resque.rb
185
+ - lib/bigcommerce/prometheus/version.rb
186
+ homepage: https://github.com/bigcommerce/bc-prometheus-ruby
187
+ licenses:
188
+ - MIT
189
+ metadata: {}
190
+ post_install_message:
191
+ rdoc_options: []
192
+ require_paths:
193
+ - lib
194
+ required_ruby_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ required_rubygems_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ requirements: []
205
+ rubygems_version: 3.0.6
206
+ signing_key:
207
+ specification_version: 4
208
+ summary: Simple integration of ruby and puma servers with prometheus
209
+ test_files: []