gruf-prometheus 1.0.2 → 2.1.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.2'
20
+ VERSION = '2.1.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.2
4
+ version: 2.1.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-12 00:00:00.000000000 Z
11
+ date: 2021-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rake
14
+ name: bc-prometheus-ruby
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '10.0'
20
- type: :development
19
+ version: 0.5.1
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '10.0'
26
+ version: 0.5.1
27
27
  - !ruby/object:Gem::Dependency
28
- name: rspec
28
+ name: gruf
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '3.8'
34
- type: :development
33
+ version: '2.7'
34
+ type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '3.8'
40
+ version: '2.7'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec_junit_formatter
42
+ name: bundler-audit
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0.4'
47
+ version: '0.6'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0.4'
54
+ version: '0.6'
55
55
  - !ruby/object:Gem::Dependency
56
- name: bundler-audit
56
+ name: pry
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0.6'
61
+ version: '0.13'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '0.6'
68
+ version: '0.13'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rubocop
70
+ name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '0.68'
75
+ version: '13.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '0.68'
82
+ version: '13.0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: simplecov
84
+ name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: '0.16'
89
+ version: '3.10'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: '0.16'
96
+ version: '3.10'
97
97
  - !ruby/object:Gem::Dependency
98
- name: null-logger
98
+ name: rspec_junit_formatter
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: '0.1'
103
+ version: '0.4'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
- version: '0.1'
110
+ version: '0.4'
111
111
  - !ruby/object:Gem::Dependency
112
- name: pry
112
+ name: rubocop
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: '0.12'
117
+ version: '1.1'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
- version: '0.12'
124
+ version: '1.1'
125
125
  - !ruby/object:Gem::Dependency
126
- name: gruf
126
+ name: rubocop-packaging
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - ">="
129
+ - - "~>"
130
130
  - !ruby/object:Gem::Version
131
- version: '2.7'
132
- type: :runtime
131
+ version: '0.5'
132
+ type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - ">="
136
+ - - "~>"
137
137
  - !ruby/object:Gem::Version
138
- version: '2.7'
138
+ version: '0.5'
139
139
  - !ruby/object:Gem::Dependency
140
- name: bc-prometheus-ruby
140
+ name: rubocop-performance
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: 0.1.5
146
- type: :runtime
145
+ version: '1.8'
146
+ type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: 0.1.5
152
+ version: '1.8'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-thread_safety
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.3'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
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,15 +214,18 @@ 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'
218
+ - - "<"
219
+ - !ruby/object:Gem::Version
220
+ version: '3.1'
183
221
  required_rubygems_version: !ruby/object:Gem::Requirement
184
222
  requirements:
185
223
  - - ">="
186
224
  - !ruby/object:Gem::Version
187
225
  version: '0'
188
226
  requirements: []
189
- rubygems_version: 3.0.6
190
- signing_key:
227
+ rubygems_version: 3.2.3
228
+ signing_key:
191
229
  specification_version: 4
192
230
  summary: Prometheus support for gruf
193
231
  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