gruf-prometheus 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b2ac2951ece4595b35a0b951bbcd9d8d246238b755771554316fb22b97ab5ef
4
- data.tar.gz: db1793e8f126900fa981fc8cc2f36f665bbce8590f2d8cfab5912b5763180ec3
3
+ metadata.gz: 8c3aed24d2b55ffbf676b7a27acab6fdcdff134856f294ae1c7b5ffd3f6c39c9
4
+ data.tar.gz: d6221023c0bdbe602c00e46433aea36b746bcb5d37f107df92809f0590a6e61e
5
5
  SHA512:
6
- metadata.gz: 6766ed05abc90b57c8b46a587fe16c2f16c8aa46883a62f7493ae61938212a89d0c5b53a805949c1030dfe03a55dc9fd56e1f7972e12c50f7535f15e4397af04
7
- data.tar.gz: 6be2cc11e6d60caf4861196852a722c08ba1b20dc37a4e449033cf09ed41337aa66b6ba90d0bd73d66304b82b2d7acf49e3578f52eceb65781419054b1d41747
6
+ metadata.gz: f3df08b6a3032a196bf88999ca838f4a2bc6042913eaa0534ecdc72077ebfc7a0981878b98fcc1cafa847a4b4ba381eb890ad7dc7a2791d96da1dc4f85aefe45
7
+ data.tar.gz: d4df8e1ccd75da130d23bd894e6a088005fa7c7c90958b64fca22d486158d92d58df1f0848d2f43fa3fb1aa75c9b98c13dc8f9fdf8c013001335bd4a3471ee84
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@ Changelog for the gruf-prometheus gem.
2
2
 
3
3
  ### Pending Release
4
4
 
5
+ ### 1.0.0
6
+
7
+ - *Breaking Changes* Move all prometheus core dependencies to bc-prometheus-ruby
8
+
5
9
  ### 0.0.2
6
10
 
7
11
  - Cleaner starting of the gruf server and collectors
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # gruf-prometheus - Prometheus support for gruf
2
2
 
3
- [![CircleCI](https://circleci.com/gh/bigcommerce/gruf-prometheus/tree/master.svg?style=svg)](https://circleci.com/gh/bigcommerce/gruf-prometheus/tree/master)
3
+ [![CircleCI](https://circleci.com/gh/bigcommerce/gruf-prometheus/tree/master.svg?style=svg)](https://circleci.com/gh/bigcommerce/gruf-prometheus/tree/master) [![Gem Version](https://badge.fury.io/rb/gruf-prometheus.svg)](https://badge.fury.io/rb/gruf-prometheus) [![Documentation](https://inch-ci.org/github/bigcommerce/gruf-prometheus.svg?branch=master)](https://inch-ci.org/github/bigcommerce/gruf-prometheus?branch=master)
4
4
 
5
5
  Adds Prometheus support for [gruf](https://github.com/bigcommerce/gruf) 2.7.0+.
6
6
 
@@ -20,24 +20,19 @@ Gruf.configure do |c|
20
20
  end
21
21
  ```
22
22
 
23
- Then `bundle exec gruf` and you'll automatically have prometheus metrics for gruf servers.
23
+ Then `bundle exec gruf` and you'll automatically have prometheus metrics for your gruf server.
24
+
25
+ The gruf server will by default run on port 9394, and can be scraped at `/metrics`.
24
26
 
25
27
  ## Configuration
26
28
 
27
- You can further configure with:
29
+ You can further configure via bc-prometheus-ruby with:
28
30
 
29
31
  | Option | Description | Default |
30
32
  | ------ | ----------- | ------- |
31
- | client_custom_labels | A hash of custom labels to send with each client request | `{}` |
32
- | client_max_queue_size | The max amount of metrics to send before flushing | 10000 |
33
- | client_thread_sleep | How often to sleep the worker thread that manages the client buffer (seconds) | 0.5 |
34
33
  | process_label | The label to use for metric prefixing | grpc |
35
- | process_name | Label to use for process name in logging | grpc |
36
- | collection_frequency | How often to poll collection metrics (seconds) | 15 |
37
- | server_host | The host to run the collector on | '0.0.0.0' |
38
- | server_port | The port to run the collector on | 9394 |
39
- | server_prefix | The prefix for all collected metrics | ruby_ |
40
- | server_timeout | Timeout when exporting metrics (seconds) | 2 |
34
+ | process_name | Label to use for process name in logging | grpc |
35
+ | collection_frequency | The period in seconds in which to collect metrics | 30 |
41
36
 
42
37
  ## License
43
38
 
@@ -42,5 +42,5 @@ Gem::Specification.new do |spec|
42
42
  spec.add_development_dependency 'pry', '>= 0.12'
43
43
 
44
44
  spec.add_runtime_dependency 'gruf', '>= 2.7'
45
- spec.add_runtime_dependency 'prometheus_exporter', '~> 0.4'
45
+ spec.add_runtime_dependency 'bc-prometheus-ruby', '~> 0.1.3'
46
46
  end
@@ -21,11 +21,12 @@ require 'prometheus_exporter/client'
21
21
  require 'prometheus_exporter/middleware'
22
22
  require 'prometheus_exporter/instrumentation'
23
23
  require 'gruf'
24
+ require 'net/http'
25
+ require 'logger'
26
+ require 'bigcommerce/prometheus'
24
27
 
25
28
  require_relative 'prometheus/version'
26
29
  require_relative 'prometheus/configuration'
27
- require_relative 'prometheus/client'
28
- require_relative 'prometheus/server'
29
30
  require_relative 'prometheus/collectors/grpc'
30
31
  require_relative 'prometheus/type_collectors/grpc'
31
32
  require_relative 'prometheus/hook'
@@ -38,7 +39,7 @@ module Gruf
38
39
  extend Configuration
39
40
 
40
41
  def self.client
41
- Gruf::Prometheus::Client.instance
42
+ Bigcommerce::Prometheus::Client.instance
42
43
  end
43
44
  end
44
45
  end
@@ -36,24 +36,38 @@ module Gruf
36
36
  end
37
37
 
38
38
  ##
39
- # Start the instrumentor
39
+ # Start the collector
40
40
  #
41
41
  # @param [Gruf::Server] server
42
42
  # @param [Gruf::Prometheus::Client] client
43
43
  # @param [Integer] frequency
44
44
  #
45
45
  def self.start(server:, client: nil, frequency: nil)
46
+ stop if @thread
47
+
48
+ client ||= Bigcommerce::Prometheus::Client.instance
46
49
  collector = new(server: server, client: client, frequency: frequency)
47
- Thread.new do
50
+ @thread = Thread.new do
48
51
  loop do
49
52
  collector.run
50
53
  end
51
54
  end
52
55
  end
53
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
+
54
68
  def run
55
69
  metric = collect
56
- logger.debug "[gruf-prometheus] Pushing metrics to collector: #{metric.inspect}"
70
+ logger.debug "[gruf-prometheus] Pushing gruf metrics to type collector: #{metric.inspect}"
57
71
  @client.send_json metric
58
72
  rescue StandardError => e
59
73
  logger.error "[gruf-prometheus] Failed to collect gruf-prometheus stats: #{e.message}"
@@ -22,16 +22,9 @@ module Gruf
22
22
  #
23
23
  module Configuration
24
24
  VALID_CONFIG_KEYS = {
25
- client_custom_labels: nil,
26
- client_max_queue_size: 10_000,
27
- client_thread_sleep: 0.5,
28
25
  process_label: 'grpc',
29
26
  process_name: 'grpc',
30
- collection_frequency: 15,
31
- server_host: '0.0.0.0',
32
- server_port: ::PrometheusExporter::DEFAULT_PORT,
33
- server_prefix: ::PrometheusExporter::DEFAULT_PREFIX,
34
- server_timeout: ::PrometheusExporter::DEFAULT_TIMEOUT
27
+ collection_frequency: 30
35
28
  }.freeze
36
29
 
37
30
  attr_accessor *VALID_CONFIG_KEYS.keys
@@ -75,13 +68,9 @@ module Gruf
75
68
  VALID_CONFIG_KEYS.each do |k, v|
76
69
  send("#{k}=".to_sym, v)
77
70
  end
78
- self.client_max_queue_size = ENV.fetch('PROMETHEUS_CLIENT_MAX_QUEUE_SIZE', 10_000).to_i
79
- self.client_thread_sleep = ENV.fetch('PROMETHEUS_CLIENT_THREAD_SLEEP', 0.5).to_i
80
71
  self.process_label = ENV.fetch('PROMETHEUS_PROCESS_LABEL', 'grpc').to_s
72
+ self.process_name = ENV.fetch('PROMETHEUS_PROCESS_NAME', 'grpc').to_s
81
73
  self.collection_frequency = ENV.fetch('PROMETHEUS_COLLECTION_FREQUENCY', 30).to_i
82
- self.server_host = ENV.fetch('PROMETHEUS_SERVER_HOST', '0.0.0.0').to_s
83
- self.server_port = ENV.fetch('PROMETHEUS_SERVER_PORT', ::PrometheusExporter::DEFAULT_PORT).to_i
84
- self.server_timeout = ENV.fetch('PROMETHEUS_SERVER_TIMEOUT', ::PrometheusExporter::DEFAULT_TIMEOUT).to_i
85
74
  end
86
75
 
87
76
  ##
@@ -27,9 +27,12 @@ module Gruf
27
27
  # @param [Gruf::Server] server
28
28
  #
29
29
  def before_server_start(server:)
30
- start_collectors(server: server)
31
- prometheus_server.add_type_collector(Gruf::Prometheus::TypeCollectors::Grpc)
30
+ logger.info "[gruf-prometheus][#{::Gruf::Prometheus.process_name}] Starting #{server.class}"
31
+ prometheus_server.add_type_collector(::Gruf::Prometheus::TypeCollectors::Grpc.new)
32
+ prometheus_server.add_type_collector(::PrometheusExporter::Server::ActiveRecordCollector.new)
32
33
  prometheus_server.start
34
+ sleep 2 unless ENV['RACK_ENV'] == 'test' # wait for server to come online
35
+ start_collectors(server: server)
33
36
  rescue StandardError => e
34
37
  logger.error "[gruf-prometheus][#{::Gruf::Prometheus.process_name}] Failed to start gruf instrumentation - #{e.message} - #{e.backtrace[0..4].join("\n")}"
35
38
  end
@@ -37,8 +40,8 @@ module Gruf
37
40
  ##
38
41
  # Handle proper shutdown of the prometheus server
39
42
  #
40
- def after_server_stop(server:)
41
- logger.debug "[gruf-prometheus][#{::Gruf::Prometheus.process_name}] Stopping #{server.class}"
43
+ def after_server_stop(*)
44
+ stop_collectors
42
45
  prometheus_server.stop
43
46
  rescue StandardError => e
44
47
  logger.error "[gruf-prometheus][#{::Gruf::Prometheus.process_name}] Failed to stop gruf instrumentation - #{e.message} - #{e.backtrace[0..4].join("\n")}"
@@ -52,24 +55,34 @@ module Gruf
52
55
  def start_collectors(server:)
53
56
  ::PrometheusExporter::Instrumentation::Process.start(
54
57
  type: ::Gruf::Prometheus.process_label,
55
- client: ::Gruf::Prometheus.client,
58
+ client: ::Bigcommerce::Prometheus.client,
56
59
  frequency: ::Gruf::Prometheus.collection_frequency
57
60
  )
58
61
  ::Gruf::Prometheus::Collectors::Grpc.start(
59
62
  server: server,
60
- client: ::Gruf::Prometheus.client,
63
+ client: ::Bigcommerce::Prometheus.client,
61
64
  frequency: ::Gruf::Prometheus.collection_frequency
62
65
  )
63
66
  end
64
67
 
68
+ ##
69
+ # Stop collectors for the gRPC process
70
+ #
71
+ def stop_collectors
72
+ ::PrometheusExporter::Instrumentation::Process.stop
73
+ ::Gruf::Prometheus::Collectors::Grpc.stop
74
+ end
75
+
65
76
  ##
66
77
  # @return [Gruf::Prometheus::Server]
67
78
  #
68
79
  def prometheus_server
69
- @prometheus_server ||= ::Gruf::Prometheus::Server.new(
70
- port: Gruf::Prometheus.server_port,
71
- timeout: Gruf::Prometheus.server_timeout,
72
- prefix: Gruf::Prometheus.server_prefix
80
+ @prometheus_server ||= ::Bigcommerce::Prometheus::Server.new(
81
+ host: Bigcommerce::Prometheus.server_host,
82
+ port: Bigcommerce::Prometheus.server_port,
83
+ timeout: Bigcommerce::Prometheus.server_timeout,
84
+ prefix: Bigcommerce::Prometheus.server_prefix,
85
+ logger: logger
73
86
  )
74
87
  end
75
88
  end
@@ -67,12 +67,12 @@ module Gruf
67
67
  custom_labels = obj['custom_labels'] || {}
68
68
  labels = custom_labels.nil? ? default_labels : default_labels.merge(custom_labels)
69
69
 
70
- @pool_jobs_waiting_total.observe(obj['pool_jobs_waiting_total'], labels)
71
- @pool_ready_workers_total.observe(obj['pool_ready_workers_total'], labels)
72
- @pool_workers_total.observe(obj['pool_workers_total'], labels)
73
- @pool_initial_size.observe(obj['pool_initial_size'], labels)
74
- @poll_period.observe(obj['poll_period'], labels)
75
- @thread_pool_exhausted.observe(obj['thread_pool_exhausted'], labels)
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
76
  end
77
77
  end
78
78
  end
@@ -17,6 +17,6 @@
17
17
  #
18
18
  module Gruf
19
19
  module Prometheus
20
- VERSION = '0.0.2'
20
+ VERSION = '1.0.0'
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gruf-prometheus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shaun McCormick
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-19 00:00:00.000000000 Z
11
+ date: 2019-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -137,19 +137,19 @@ dependencies:
137
137
  - !ruby/object:Gem::Version
138
138
  version: '2.7'
139
139
  - !ruby/object:Gem::Dependency
140
- name: prometheus_exporter
140
+ name: bc-prometheus-ruby
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: '0.4'
145
+ version: 0.1.3
146
146
  type: :runtime
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.4'
152
+ version: 0.1.3
153
153
  description: Prometheus support for gruf
154
154
  email:
155
155
  - shaun.mccormick@bigcommerce.com
@@ -162,11 +162,9 @@ files:
162
162
  - README.md
163
163
  - gruf-prometheus.gemspec
164
164
  - lib/gruf/prometheus.rb
165
- - lib/gruf/prometheus/client.rb
166
165
  - lib/gruf/prometheus/collectors/grpc.rb
167
166
  - lib/gruf/prometheus/configuration.rb
168
167
  - lib/gruf/prometheus/hook.rb
169
- - lib/gruf/prometheus/server.rb
170
168
  - lib/gruf/prometheus/type_collectors/grpc.rb
171
169
  - lib/gruf/prometheus/version.rb
172
170
  homepage: https://github.com/bigcommerce/gruf-prometheus
@@ -188,8 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
188
186
  - !ruby/object:Gem::Version
189
187
  version: '0'
190
188
  requirements: []
191
- rubyforge_project:
192
- rubygems_version: 2.7.9
189
+ rubygems_version: 3.0.6
193
190
  signing_key:
194
191
  specification_version: 4
195
192
  summary: Prometheus support for gruf
@@ -1,37 +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
- ##
21
- # Client implementation for Prometheus
22
- #
23
- class Client < ::PrometheusExporter::Client
24
- include Singleton
25
-
26
- def initialize
27
- super(
28
- host: ::Gruf::Prometheus.server_host,
29
- port: ::Gruf::Prometheus.server_port,
30
- max_queue_size: ::Gruf::Prometheus.client_max_queue_size,
31
- thread_sleep: ::Gruf::Prometheus.client_thread_sleep,
32
- custom_labels: ::Gruf::Prometheus.client_custom_labels
33
- )
34
- end
35
- end
36
- end
37
- end
@@ -1,124 +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
- ##
21
- # Prometheus server that runs with gruf hooks
22
- #
23
- class Server
24
- include Gruf::Loggable
25
-
26
- ##
27
- # @param [Integer] port
28
- # @param [Integer] timeout
29
- # @param [String] prefix
30
- # @param [Boolean] verbose
31
- # @param [Class] server_class
32
- #
33
- def initialize(
34
- port:,
35
- timeout:,
36
- prefix: nil,
37
- verbose: false,
38
- server_class: nil
39
- )
40
- @port = (port || ::PrometheusExporter::DEFAULT_PORT).to_i
41
- @timeout = (timeout || ::PrometheusExporter::DEFAULT_TIMEOUT).to_i
42
- @prefix = (prefix || ::PrometheusExporter::DEFAULT_PREFIX).to_s
43
- @verbose = verbose
44
- @server_class = server_class || ::PrometheusExporter::Server::WebServer
45
- @running = false
46
- @process_name = ::Gruf::Prometheus.process_name
47
- end
48
-
49
- def start
50
- logger.info "[gruf-prometheus][#{@process_name}] Starting prometheus exporter on port #{@port}"
51
- server.start
52
- logger.info "[gruf-prometheus][#{@process_name}] Prometheus exporter started on port #{@port}"
53
-
54
- @running = true
55
- server
56
- rescue StandardError => e
57
- logger.error "[gruf-prometheus][#{@process_name}] Failed to start exporter: #{e.message}"
58
- end
59
-
60
- ##
61
- # Stop the server
62
- #
63
- def stop
64
- logger.info "[gruf-prometheus][#{@process_name}] Shutting down prometheus exporter"
65
- server.stop
66
- logger.info "[gruf-prometheus][#{@process_name}] Prometheus exporter cleanly shut down"
67
- rescue StandardError => e
68
- logger.error "[gruf-prometheus][#{@process_name}] Failed to stop exporter: #{e.message}"
69
- end
70
-
71
- ##
72
- # Whether or not the server is running
73
- #
74
- # @return [Boolean]
75
- #
76
- def running?
77
- @running
78
- end
79
-
80
- ##
81
- # Add a type collector to this server
82
- #
83
- # @param [Class] collector A type collector for the prometheus server
84
- #
85
- def add_type_collector(collector)
86
- runner.type_collectors = runner.type_collectors.push(collector)
87
- end
88
-
89
- private
90
-
91
- ##
92
- # @return [::PrometheusExporter::Server::Runner]
93
- #
94
- def runner
95
- unless @runner
96
- @runner = ::PrometheusExporter::Server::Runner.new(
97
- timeout: @timeout,
98
- port: @port,
99
- prefix: @prefix,
100
- verbose: @verbose,
101
- server_class: @server_class
102
- )
103
- PrometheusExporter::Metric::Base.default_prefix = @runner.prefix
104
- end
105
- @runner
106
- end
107
-
108
- ##
109
- # @return [PrometheusExporter::Server::WebServer]
110
- #
111
- def server
112
- @server ||= begin
113
- runner.send(:register_type_collectors)
114
- runner.server_class.new(
115
- port: runner.port,
116
- collector: runner.collector,
117
- timeout: runner.timeout,
118
- verbose: runner.verbose
119
- )
120
- end
121
- end
122
- end
123
- end
124
- end