bc-prometheus-ruby 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/CHANGELOG.md +30 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/README.md +67 -0
- data/bc-prometheus-ruby.gemspec +46 -0
- data/lib/bigcommerce/prometheus.rb +62 -0
- data/lib/bigcommerce/prometheus/client.rb +80 -0
- data/lib/bigcommerce/prometheus/collectors/resque.rb +85 -0
- data/lib/bigcommerce/prometheus/configuration.rb +127 -0
- data/lib/bigcommerce/prometheus/instrumentors/hutch.rb +72 -0
- data/lib/bigcommerce/prometheus/instrumentors/resque.rb +72 -0
- data/lib/bigcommerce/prometheus/instrumentors/web.rb +84 -0
- data/lib/bigcommerce/prometheus/integrations/puma.rb +55 -0
- data/lib/bigcommerce/prometheus/integrations/railtie.rb +31 -0
- data/lib/bigcommerce/prometheus/integrations/resque.rb +41 -0
- data/lib/bigcommerce/prometheus/loggable.rb +32 -0
- data/lib/bigcommerce/prometheus/server.rb +117 -0
- data/lib/bigcommerce/prometheus/servers/thin/controllers/base_controller.rb +63 -0
- data/lib/bigcommerce/prometheus/servers/thin/controllers/error_controller.rb +36 -0
- data/lib/bigcommerce/prometheus/servers/thin/controllers/metrics_controller.rb +87 -0
- data/lib/bigcommerce/prometheus/servers/thin/controllers/not_found_controller.rb +36 -0
- data/lib/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller.rb +92 -0
- data/lib/bigcommerce/prometheus/servers/thin/rack_app.rb +88 -0
- data/lib/bigcommerce/prometheus/servers/thin/server.rb +48 -0
- data/lib/bigcommerce/prometheus/servers/thin/server_metrics.rb +98 -0
- data/lib/bigcommerce/prometheus/type_collectors/resque.rb +82 -0
- data/lib/bigcommerce/prometheus/version.rb +22 -0
- metadata +209 -0
@@ -0,0 +1,72 @@
|
|
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 Instrumentors
|
21
|
+
##
|
22
|
+
# Instrumentors for hutch process
|
23
|
+
#
|
24
|
+
class Hutch
|
25
|
+
include Bigcommerce::Prometheus::Loggable
|
26
|
+
|
27
|
+
def initialize(app:)
|
28
|
+
@app = app
|
29
|
+
@enabled = Bigcommerce::Prometheus.enabled
|
30
|
+
@process_name = Bigcommerce::Prometheus.process_name
|
31
|
+
@server_port = Bigcommerce::Prometheus.server_port
|
32
|
+
@server_timeout = Bigcommerce::Prometheus.server_timeout
|
33
|
+
@server_prefix = Bigcommerce::Prometheus.server_prefix
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# Start the web instrumentor
|
38
|
+
#
|
39
|
+
def start
|
40
|
+
unless @enabled
|
41
|
+
logger.debug "[bigcommerce-prometheus][#{@process_name}] Prometheus disabled, skipping hutch start..."
|
42
|
+
return
|
43
|
+
end
|
44
|
+
|
45
|
+
server.add_type_collector(PrometheusExporter::Server::ActiveRecordCollector.new)
|
46
|
+
server.add_type_collector(PrometheusExporter::Server::HutchCollector.new)
|
47
|
+
server.start
|
48
|
+
setup_middleware
|
49
|
+
rescue StandardError => e
|
50
|
+
logger.error "[bigcommerce-prometheus][#{@process_name}] Failed to start hutch instrumentation - #{e.message} - #{e.backtrace[0..4].join("\n")}"
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def server
|
56
|
+
@server ||= ::Bigcommerce::Prometheus::Server.new(
|
57
|
+
port: @server_port,
|
58
|
+
timeout: @server_timeout,
|
59
|
+
prefix: @server_prefix
|
60
|
+
)
|
61
|
+
end
|
62
|
+
|
63
|
+
def setup_middleware
|
64
|
+
logger.info "[bigcommerce-prometheus][#{@process_name}] Setting up hutch prometheus middleware"
|
65
|
+
require 'hutch'
|
66
|
+
::Hutch::Config.set(:tracer, PrometheusExporter::Instrumentation::Hutch)
|
67
|
+
@app.middleware.unshift(PrometheusExporter::Middleware, client: Bigcommerce::Prometheus.client)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,72 @@
|
|
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 Instrumentors
|
21
|
+
##
|
22
|
+
# Instrumentors for resque process
|
23
|
+
#
|
24
|
+
class Resque
|
25
|
+
include Bigcommerce::Prometheus::Loggable
|
26
|
+
|
27
|
+
def initialize(app:)
|
28
|
+
@app = app
|
29
|
+
@enabled = Bigcommerce::Prometheus.enabled
|
30
|
+
@process_name = Bigcommerce::Prometheus.process_name
|
31
|
+
@server_port = Bigcommerce::Prometheus.server_port
|
32
|
+
@server_timeout = Bigcommerce::Prometheus.server_timeout
|
33
|
+
@server_prefix = Bigcommerce::Prometheus.server_prefix
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# Start the web instrumentor
|
38
|
+
#
|
39
|
+
def start
|
40
|
+
unless @enabled
|
41
|
+
logger.debug "[bigcommerce-prometheus][#{@process_name}] Prometheus disabled, skipping resque start..."
|
42
|
+
return
|
43
|
+
end
|
44
|
+
|
45
|
+
server.add_type_collector(PrometheusExporter::Server::ActiveRecordCollector.new)
|
46
|
+
server.add_type_collector(Bigcommerce::Prometheus::TypeCollectors::Resque.new)
|
47
|
+
server.start
|
48
|
+
setup_middleware
|
49
|
+
rescue StandardError => e
|
50
|
+
logger.error "[bigcommerce-prometheus][#{@process_name}] Failed to start resque instrumentation - #{e.message} - #{e.backtrace[0..4].join("\n")}"
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def server
|
56
|
+
@server ||= ::Bigcommerce::Prometheus::Server.new(
|
57
|
+
port: @server_port,
|
58
|
+
timeout: @server_timeout,
|
59
|
+
prefix: @server_prefix
|
60
|
+
)
|
61
|
+
end
|
62
|
+
|
63
|
+
def setup_middleware
|
64
|
+
logger.info "[bigcommerce-prometheus][#{@process_name}] Setting up resque prometheus middleware"
|
65
|
+
::Resque.before_first_fork do
|
66
|
+
::Bigcommerce::Prometheus::Integrations::Resque.start
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,84 @@
|
|
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 Instrumentors
|
21
|
+
##
|
22
|
+
# Instrumentors for web process
|
23
|
+
#
|
24
|
+
class Web
|
25
|
+
include Bigcommerce::Prometheus::Loggable
|
26
|
+
|
27
|
+
def initialize(app:)
|
28
|
+
@app = app
|
29
|
+
@enabled = Bigcommerce::Prometheus.enabled
|
30
|
+
@server_port = Bigcommerce::Prometheus.server_port
|
31
|
+
@server_timeout = Bigcommerce::Prometheus.server_timeout
|
32
|
+
@server_prefix = Bigcommerce::Prometheus.server_prefix
|
33
|
+
@process_name = Bigcommerce::Prometheus.process_name
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# Start the web instrumentor
|
38
|
+
#
|
39
|
+
def start
|
40
|
+
unless @enabled
|
41
|
+
logger.debug "[bigcommerce-prometheus][#{@process_name}] Prometheus disabled, skipping web start..."
|
42
|
+
return
|
43
|
+
end
|
44
|
+
|
45
|
+
setup_before_fork
|
46
|
+
setup_after_fork
|
47
|
+
setup_middleware
|
48
|
+
rescue StandardError => e
|
49
|
+
logger.error "[bigcommerce-prometheus][#{@process_name}] Failed to start web instrumentation - #{e.message} - #{e.backtrace[0..4].join("\n")}"
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def server
|
55
|
+
@server ||= ::Bigcommerce::Prometheus::Server.new(
|
56
|
+
port: @server_port,
|
57
|
+
timeout: @server_timeout,
|
58
|
+
prefix: @server_prefix
|
59
|
+
)
|
60
|
+
end
|
61
|
+
|
62
|
+
def setup_before_fork
|
63
|
+
@app.config.before_fork_callbacks = [] unless @app.config.before_fork_callbacks
|
64
|
+
@app.config.before_fork_callbacks << lambda do
|
65
|
+
server.add_type_collector(PrometheusExporter::Server::ActiveRecordCollector.new)
|
66
|
+
server.add_type_collector(PrometheusExporter::Server::WebCollector.new)
|
67
|
+
server.start
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def setup_after_fork
|
72
|
+
@app.config.after_fork_callbacks = [] unless @app.config.after_fork_callbacks
|
73
|
+
@app.config.after_fork_callbacks << lambda do
|
74
|
+
::Bigcommerce::Prometheus::Integrations::Puma.start
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def setup_middleware
|
79
|
+
@app.middleware.unshift(PrometheusExporter::Middleware, client: Bigcommerce::Prometheus.client)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,55 @@
|
|
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 Integrations
|
21
|
+
##
|
22
|
+
# Plugin for puma
|
23
|
+
#
|
24
|
+
class Puma
|
25
|
+
##
|
26
|
+
# Start the puma collector
|
27
|
+
#
|
28
|
+
def self.start
|
29
|
+
::PrometheusExporter::Instrumentation::Puma.start(
|
30
|
+
client: ::Bigcommerce::Prometheus.client,
|
31
|
+
frequency: ::Bigcommerce::Prometheus.puma_collection_frequency
|
32
|
+
)
|
33
|
+
if active_record_enabled?
|
34
|
+
::PrometheusExporter::Instrumentation::ActiveRecord.start(
|
35
|
+
client: ::Bigcommerce::Prometheus.client,
|
36
|
+
frequency: ::Bigcommerce::Prometheus.puma_collection_frequency
|
37
|
+
)
|
38
|
+
end
|
39
|
+
::PrometheusExporter::Instrumentation::Process.start(
|
40
|
+
client: ::Bigcommerce::Prometheus.client,
|
41
|
+
type: ::Bigcommerce::Prometheus.puma_process_label,
|
42
|
+
frequency: ::Bigcommerce::Prometheus.puma_collection_frequency
|
43
|
+
)
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# @return [Boolean]
|
48
|
+
#
|
49
|
+
def self.active_record_enabled?
|
50
|
+
defined?(ActiveRecord) && ::ActiveRecord::Base.connection_pool.respond_to?(:stat)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,31 @@
|
|
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 Integrations
|
21
|
+
##
|
22
|
+
# Railtie for automatic configuration of Rails environments
|
23
|
+
#
|
24
|
+
class Railtie < ::Rails::Railtie
|
25
|
+
initializer 'zzz_bigcommerce.prometheus.configure_rails_initialization' do |app|
|
26
|
+
Bigcommerce::Prometheus::Instrumentors::Web.new(app: app).start
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,41 @@
|
|
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 Integrations
|
21
|
+
##
|
22
|
+
# Plugin for resque
|
23
|
+
#
|
24
|
+
class Resque
|
25
|
+
##
|
26
|
+
# Start the resque integration
|
27
|
+
#
|
28
|
+
def self.start
|
29
|
+
::PrometheusExporter::Instrumentation::Process.start(
|
30
|
+
client: ::Bigcommerce::Prometheus.client,
|
31
|
+
type: ::Bigcommerce::Prometheus.resque_process_label
|
32
|
+
)
|
33
|
+
::Bigcommerce::Prometheus::Collectors::Resque.start(
|
34
|
+
client: ::Bigcommerce::Prometheus.client,
|
35
|
+
frequency: ::Bigcommerce::Prometheus.resque_collection_frequency
|
36
|
+
)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,32 @@
|
|
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
|
+
##
|
21
|
+
# Module for logging support
|
22
|
+
#
|
23
|
+
module Loggable
|
24
|
+
##
|
25
|
+
# @return [::Logger]
|
26
|
+
#
|
27
|
+
def logger
|
28
|
+
Bigcommerce::Prometheus.logger
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,117 @@
|
|
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
|
+
##
|
21
|
+
# Server implementation for Prometheus
|
22
|
+
#
|
23
|
+
class Server
|
24
|
+
##
|
25
|
+
# @param [String] host
|
26
|
+
# @param [Integer] port
|
27
|
+
# @param [Integer] timeout
|
28
|
+
# @param [String] prefix
|
29
|
+
#
|
30
|
+
def initialize(host: nil, port: nil, timeout: nil, prefix: nil, logger: nil)
|
31
|
+
@host = host || ::Bigcommerce::Prometheus.server_host
|
32
|
+
@port = (port || ::Bigcommerce::Prometheus.server_port).to_i
|
33
|
+
@timeout = (timeout || ::Bigcommerce::Prometheus.server_timeout).to_i
|
34
|
+
@prefix = (prefix || ::PrometheusExporter::DEFAULT_PREFIX).to_s
|
35
|
+
@process_name = ::Bigcommerce::Prometheus.process_name
|
36
|
+
@logger = logger || ::Bigcommerce::Prometheus.logger
|
37
|
+
@server = ::Bigcommerce::Prometheus::Servers::Thin::Server.new(port: @port, timeout: @timeout, logger: @logger)
|
38
|
+
@running = false
|
39
|
+
::PrometheusExporter::Metric::Base.default_prefix = @prefix
|
40
|
+
setup_signal_handlers
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Start the server
|
45
|
+
#
|
46
|
+
def start
|
47
|
+
@logger.info "[bigcommerce-prometheus][#{@process_name}] Starting prometheus exporter on port #{@host}:#{@port}"
|
48
|
+
|
49
|
+
@run_thread = ::Thread.start do
|
50
|
+
@server.start
|
51
|
+
end
|
52
|
+
@running = true
|
53
|
+
|
54
|
+
@logger.info "[bigcommerce-prometheus][#{@process_name}] Prometheus exporter started on #{@host}:#{@port}"
|
55
|
+
|
56
|
+
@server
|
57
|
+
rescue ::StandardError => e
|
58
|
+
@logger.error "[bigcommerce-prometheus][#{@process_name}] Failed to start exporter: #{e.message}"
|
59
|
+
stop
|
60
|
+
end
|
61
|
+
|
62
|
+
##
|
63
|
+
# Start the server and run it until stopped
|
64
|
+
#
|
65
|
+
def start_until_stopped
|
66
|
+
start
|
67
|
+
yield
|
68
|
+
@run_thread.join
|
69
|
+
rescue StandardError => e
|
70
|
+
@logger.error "[bigcommerce-prometheus] Server crashed: #{e.message}"
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# Stop the server
|
75
|
+
#
|
76
|
+
def stop
|
77
|
+
@server.stop!
|
78
|
+
@run_thread.kill
|
79
|
+
@running = false
|
80
|
+
@logger.info "[bigcommerce-prometheus][#{@process_name}] Prometheus exporter cleanly shut down"
|
81
|
+
rescue ::StandardError => e
|
82
|
+
@logger.error "[bigcommerce-prometheus][#{@process_name}] Failed to stop exporter: #{e.message}"
|
83
|
+
end
|
84
|
+
|
85
|
+
##
|
86
|
+
# Whether or not the server is running
|
87
|
+
#
|
88
|
+
# @return [Boolean]
|
89
|
+
#
|
90
|
+
def running?
|
91
|
+
@running
|
92
|
+
end
|
93
|
+
|
94
|
+
##
|
95
|
+
# Add a type collector to this server
|
96
|
+
#
|
97
|
+
# @param [PrometheusExporter::Server::TypeCollector] collector
|
98
|
+
#
|
99
|
+
def add_type_collector(collector)
|
100
|
+
@logger.info "[bigcommerce-prometheus][#{@process_name}] Registering collector #{collector&.type}"
|
101
|
+
@server.add_type_collector(collector)
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
##
|
107
|
+
# Register signal handlers
|
108
|
+
#
|
109
|
+
# :nocov:
|
110
|
+
def setup_signal_handlers
|
111
|
+
::Signal.trap('INT', &method(:stop))
|
112
|
+
::Signal.trap('TERM', &method(:stop))
|
113
|
+
end
|
114
|
+
# :nocov:
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|