gruf-prometheus 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
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 Gruf
19
+ module Prometheus
20
+ ##
21
+ # Type Collector for prometheus and grpc instrumentation
22
+ #
23
+ class TypeCollector < Bigcommerce::Prometheus::TypeCollectors::Base
24
+ def type
25
+ 'grpc'
26
+ end
27
+
28
+ private
29
+
30
+ ##
31
+ # Initialize the collector
32
+ #
33
+ def build_metrics
34
+ {
35
+ pool_jobs_waiting_total: PrometheusExporter::Metric::Gauge.new('grpc_pool_jobs_waiting_total', 'Number jobs in the gRPC thread pool that are actively waiting'),
36
+ pool_ready_workers_total: PrometheusExporter::Metric::Gauge.new('grpc_pool_ready_workers_total', 'The amount of non-busy workers in the thread pool'),
37
+ pool_workers_total: PrometheusExporter::Metric::Gauge.new('grpc_pool_workers_total', 'Number of workers in the gRPC thread pool'),
38
+ pool_initial_size: PrometheusExporter::Metric::Gauge.new('grpc_pool_initial_size', 'Initial size of the gRPC thread pool'),
39
+ poll_period: PrometheusExporter::Metric::Gauge.new('grpc_poll_period', 'Polling period for the gRPC thread pool'),
40
+ thread_pool_exhausted: PrometheusExporter::Metric::Counter.new('grpc_thread_pool_exhausted', 'Times the gRPC thread pool has been exhausted')
41
+ }
42
+ end
43
+
44
+ ##
45
+ # Collect the object into the buffer
46
+ #
47
+ def collect_metrics(data: {}, labels: {})
48
+ metric(:pool_jobs_waiting_total)&.observe(data['pool_jobs_waiting_total'].to_i, labels)
49
+ metric(:pool_ready_workers_total)&.observe(data['pool_ready_workers_total'].to_i, labels)
50
+ metric(:pool_workers_total)&.observe(data['pool_workers_total'].to_i, labels)
51
+ metric(:pool_initial_size)&.observe(data['pool_initial_size'].to_i, labels)
52
+ metric(:poll_period)&.observe(data['poll_period'].to_i, labels)
53
+ metric(:thread_pool_exhausted)&.observe(data['thread_pool_exhausted'].to_i, labels)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -17,6 +17,6 @@
17
17
  #
18
18
  module Gruf
19
19
  module Prometheus
20
- VERSION = '1.0.1'
20
+ VERSION = '2.0.0'
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,155 +1,183 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gruf-prometheus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shaun McCormick
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-11 00:00:00.000000000 Z
11
+ date: 2020-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rake
14
+ name: bc-prometheus-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: gruf
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - ">="
18
32
  - !ruby/object:Gem::Version
19
- version: '10.0'
20
- type: :development
33
+ version: '2.7'
34
+ type: :runtime
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - ">="
25
39
  - !ruby/object:Gem::Version
26
- version: '10.0'
40
+ version: '2.7'
27
41
  - !ruby/object:Gem::Dependency
28
- name: rspec
42
+ name: bundler-audit
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: '3.8'
47
+ version: '0.6'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: '3.8'
54
+ version: '0.6'
41
55
  - !ruby/object:Gem::Dependency
42
- name: rspec_junit_formatter
56
+ name: pry
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - ">="
46
60
  - !ruby/object:Gem::Version
47
- version: '0.4'
61
+ version: '0.13'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - ">="
53
67
  - !ruby/object:Gem::Version
54
- version: '0.4'
68
+ version: '0.13'
55
69
  - !ruby/object:Gem::Dependency
56
- name: bundler-audit
70
+ name: rake
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - ">="
60
74
  - !ruby/object:Gem::Version
61
- version: '0.6'
75
+ version: '13.0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
80
  - - ">="
67
81
  - !ruby/object:Gem::Version
68
- version: '0.6'
82
+ version: '13.0'
69
83
  - !ruby/object:Gem::Dependency
70
- name: rubocop
84
+ name: rspec
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - ">="
74
88
  - !ruby/object:Gem::Version
75
- version: '0.68'
89
+ version: '3.10'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
94
  - - ">="
81
95
  - !ruby/object:Gem::Version
82
- version: '0.68'
96
+ version: '3.10'
83
97
  - !ruby/object:Gem::Dependency
84
- name: simplecov
98
+ name: rspec_junit_formatter
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
101
  - - ">="
88
102
  - !ruby/object:Gem::Version
89
- version: '0.16'
103
+ version: '0.4'
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
108
  - - ">="
95
109
  - !ruby/object:Gem::Version
96
- version: '0.16'
110
+ version: '0.4'
97
111
  - !ruby/object:Gem::Dependency
98
- name: null-logger
112
+ name: rubocop
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
115
  - - ">="
102
116
  - !ruby/object:Gem::Version
103
- version: '0.1'
117
+ version: '1.1'
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
122
  - - ">="
109
123
  - !ruby/object:Gem::Version
110
- version: '0.1'
124
+ version: '1.1'
111
125
  - !ruby/object:Gem::Dependency
112
- name: pry
126
+ name: rubocop-packaging
113
127
  requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
- - - ">="
129
+ - - "~>"
116
130
  - !ruby/object:Gem::Version
117
- version: '0.12'
131
+ version: '0.5'
118
132
  type: :development
119
133
  prerelease: false
120
134
  version_requirements: !ruby/object:Gem::Requirement
121
135
  requirements:
122
- - - ">="
136
+ - - "~>"
123
137
  - !ruby/object:Gem::Version
124
- version: '0.12'
138
+ version: '0.5'
125
139
  - !ruby/object:Gem::Dependency
126
- name: gruf
140
+ name: rubocop-performance
127
141
  requirement: !ruby/object:Gem::Requirement
128
142
  requirements:
129
- - - ">="
143
+ - - "~>"
130
144
  - !ruby/object:Gem::Version
131
- version: '2.7'
132
- type: :runtime
145
+ version: '1.8'
146
+ type: :development
133
147
  prerelease: false
134
148
  version_requirements: !ruby/object:Gem::Requirement
135
149
  requirements:
136
- - - ">="
150
+ - - "~>"
137
151
  - !ruby/object:Gem::Version
138
- version: '2.7'
152
+ version: '1.8'
139
153
  - !ruby/object:Gem::Dependency
140
- name: bc-prometheus-ruby
154
+ name: rubocop-thread_safety
141
155
  requirement: !ruby/object:Gem::Requirement
142
156
  requirements:
143
157
  - - "~>"
144
158
  - !ruby/object:Gem::Version
145
- version: 0.1.4
146
- type: :runtime
159
+ version: '0.3'
160
+ type: :development
147
161
  prerelease: false
148
162
  version_requirements: !ruby/object:Gem::Requirement
149
163
  requirements:
150
164
  - - "~>"
151
165
  - !ruby/object:Gem::Version
152
- version: 0.1.4
166
+ version: '0.3'
167
+ - !ruby/object:Gem::Dependency
168
+ name: simplecov
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0.19'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0.19'
153
181
  description: Prometheus support for gruf
154
182
  email:
155
183
  - shaun.mccormick@bigcommerce.com
@@ -162,16 +190,23 @@ files:
162
190
  - README.md
163
191
  - gruf-prometheus.gemspec
164
192
  - lib/gruf/prometheus.rb
165
- - lib/gruf/prometheus/collectors/grpc.rb
193
+ - lib/gruf/prometheus/client/collector.rb
194
+ - lib/gruf/prometheus/client/interceptor.rb
195
+ - lib/gruf/prometheus/client/type_collector.rb
196
+ - lib/gruf/prometheus/collector.rb
166
197
  - lib/gruf/prometheus/configuration.rb
167
198
  - lib/gruf/prometheus/hook.rb
168
- - lib/gruf/prometheus/type_collectors/grpc.rb
199
+ - lib/gruf/prometheus/request_types.rb
200
+ - lib/gruf/prometheus/server/collector.rb
201
+ - lib/gruf/prometheus/server/interceptor.rb
202
+ - lib/gruf/prometheus/server/type_collector.rb
203
+ - lib/gruf/prometheus/type_collector.rb
169
204
  - lib/gruf/prometheus/version.rb
170
205
  homepage: https://github.com/bigcommerce/gruf-prometheus
171
206
  licenses:
172
207
  - MIT
173
208
  metadata: {}
174
- post_install_message:
209
+ post_install_message:
175
210
  rdoc_options: []
176
211
  require_paths:
177
212
  - lib
@@ -179,7 +214,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
179
214
  requirements:
180
215
  - - ">="
181
216
  - !ruby/object:Gem::Version
182
- version: '0'
217
+ version: '2.6'
183
218
  required_rubygems_version: !ruby/object:Gem::Requirement
184
219
  requirements:
185
220
  - - ">="
@@ -187,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
187
222
  version: '0'
188
223
  requirements: []
189
224
  rubygems_version: 3.0.6
190
- signing_key:
225
+ signing_key:
191
226
  specification_version: 4
192
227
  summary: Prometheus support for gruf
193
228
  test_files: []
@@ -1,111 +0,0 @@
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 Gruf
19
- module Prometheus
20
- module Collectors
21
- ##
22
- # Prometheus instrumentor for gRPC servers
23
- #
24
- class Grpc
25
- include Gruf::Loggable
26
-
27
- ##
28
- # @param [Gruf::Server] server
29
- # @param [Gruf::Prometheus::Client] client
30
- # @param [Integer] frequency
31
- #
32
- def initialize(server:, client:, frequency: nil)
33
- @server = server
34
- @client = client
35
- @frequency = frequency || 15
36
- end
37
-
38
- ##
39
- # Start the collector
40
- #
41
- # @param [Gruf::Server] server
42
- # @param [Gruf::Prometheus::Client] client
43
- # @param [Integer] frequency
44
- #
45
- def self.start(server:, client: nil, frequency: nil)
46
- stop if @thread
47
-
48
- client ||= Bigcommerce::Prometheus::Client.instance
49
- collector = new(server: server, client: client, frequency: frequency)
50
- @thread = Thread.new do
51
- loop do
52
- collector.run
53
- end
54
- end
55
- end
56
-
57
- ##
58
- # Stop the collector
59
- #
60
- def self.stop
61
- t = @thread
62
- return unless t
63
-
64
- t.kill
65
- @thread = nil
66
- end
67
-
68
- def run
69
- metric = collect
70
- logger.debug "[gruf-prometheus] Pushing gruf metrics to type collector: #{metric.inspect}"
71
- @client.send_json metric
72
- rescue StandardError => e
73
- logger.error "[gruf-prometheus] Failed to collect gruf-prometheus stats: #{e.message}"
74
- ensure
75
- sleep @frequency
76
- end
77
-
78
- private
79
-
80
- def collect
81
- metric = {}
82
- metric[:type] = 'grpc'
83
- rpc_server = @server.server
84
- rpc_server.instance_variable_get(:@run_mutex).synchronize do
85
- collect_server_metrics(rpc_server, metric)
86
- end
87
- metric
88
- end
89
-
90
- ##
91
- # @param [GRPC::RpcServer] rpc_server
92
- #
93
- def collect_server_metrics(rpc_server, metric)
94
- pool = rpc_server.instance_variable_get(:@pool)
95
- metric[:pool_jobs_waiting_total] = pool.jobs_waiting.to_i
96
- metric[:pool_ready_workers_total] = pool.instance_variable_get(:@ready_workers).size
97
- metric[:pool_workers_total] = pool.instance_variable_get(:@workers)&.size
98
- metric[:pool_initial_size] = rpc_server.instance_variable_get(:@pool_size).to_i
99
- metric[:poll_period] = rpc_server.instance_variable_get(:@poll_period).to_i
100
- end
101
-
102
- ##
103
- # @return [GRPC::RpcServer]
104
- #
105
- def grpc_server
106
- @server.server
107
- end
108
- end
109
- end
110
- end
111
- end
@@ -1,80 +0,0 @@
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 Gruf
19
- module Prometheus
20
- module TypeCollectors
21
- ##
22
- # Type Collector for prometheus and grpc instrumentation
23
- #
24
- class Grpc < PrometheusExporter::Server::TypeCollector
25
- ##
26
- # Initialize the collector
27
- #
28
- def initialize
29
- @pool_jobs_waiting_total = PrometheusExporter::Metric::Gauge.new('grpc_pool_jobs_waiting_total', 'Number jobs in the gRPC thread pool that are actively waiting')
30
- @pool_ready_workers_total = PrometheusExporter::Metric::Gauge.new('grpc_pool_ready_workers_total', 'The amount of non-busy workers in the thread pool')
31
- @pool_workers_total = PrometheusExporter::Metric::Gauge.new('grpc_pool_workers_total', 'Number of workers in the gRPC thread pool')
32
- @pool_initial_size = PrometheusExporter::Metric::Gauge.new('grpc_pool_initial_size', 'Initial size of the gRPC thread pool')
33
- @poll_period = PrometheusExporter::Metric::Gauge.new('grpc_poll_period', 'Polling period for the gRPC thread pool')
34
- @thread_pool_exhausted = PrometheusExporter::Metric::Counter.new('grpc_thread_pool_exhausted', 'Times the gRPC thread pool has been exhausted')
35
- end
36
-
37
- ##
38
- # @return [String]
39
- #
40
- def type
41
- 'grpc'
42
- end
43
-
44
- ##
45
- # @return [Array]
46
- #
47
- def metrics
48
- return [] unless @pool_jobs_waiting_total
49
-
50
- [
51
- @pool_jobs_waiting_total,
52
- @pool_ready_workers_total,
53
- @pool_workers_total,
54
- @pool_initial_size,
55
- @poll_period,
56
- @thread_pool_exhausted
57
- ]
58
- end
59
-
60
- ##
61
- # Collect the object into the buffer
62
- #
63
- def collect(obj)
64
- default_labels = {}
65
- default_labels['environment'] = obj['environment'] if obj['environment']
66
-
67
- custom_labels = obj['custom_labels'] || {}
68
- labels = custom_labels.nil? ? default_labels : default_labels.merge(custom_labels)
69
-
70
- @pool_jobs_waiting_total.observe(obj['pool_jobs_waiting_total'].to_i, labels)
71
- @pool_ready_workers_total.observe(obj['pool_ready_workers_total'].to_i, labels)
72
- @pool_workers_total.observe(obj['pool_workers_total'].to_i, labels)
73
- @pool_initial_size.observe(obj['pool_initial_size'].to_i, labels)
74
- @poll_period.observe(obj['poll_period'].to_i, labels)
75
- @thread_pool_exhausted.observe(obj['thread_pool_exhausted'].to_i, labels)
76
- end
77
- end
78
- end
79
- end
80
- end