Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PE-27794) add a puma status app #1452

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions config/transport_service_config.rb
Expand Up @@ -47,3 +47,24 @@
end

app impl

#### Metrics/Status

control_token = 'none'
control_url_https = bind_addr.sub(config['port'].to_s, config['status-port'].to_s).sub('force_peer', 'none')

# Start either the default Puma Control/Status app (if it supports our 'auth_actions' feature)
# or start our custom Puma Status app via our custom 'stats' app/plugin (shipped in the Bolt gem).

if defined? auth_actions
control_actions = 'gc-stats,stats'
activate_control_app control_url_https, auth_token: control_token, auth_actions: control_actions
else
begin
plugin 'stats'
stats_url control_url_https
stats_token control_token
rescue UnknownPlugin
puts '* Status endpoint disabled'
end
end
5 changes: 3 additions & 2 deletions lib/bolt_server/base_config.rb
Expand Up @@ -6,8 +6,9 @@
module BoltServer
class BaseConfig
def config_keys
%w[host port ssl-cert ssl-key ssl-ca-cert
ssl-cipher-suites loglevel logfile whitelist]
%w[host port status-port
ssl-cert ssl-key ssl-ca-cert ssl-cipher-suites
loglevel logfile whitelist]
end

def env_keys
Expand Down
1 change: 1 addition & 0 deletions lib/bolt_server/config.rb
Expand Up @@ -21,6 +21,7 @@ def int_keys
def defaults
super.merge(
'port' => 62658,
'status-port' => 62657,
'concurrency' => 100,
'cache-dir' => "/opt/puppetlabs/server/data/bolt-server/cache",
'file-server-conn-timeout' => 120
Expand Down
5 changes: 5 additions & 0 deletions lib/puma/README.md
@@ -0,0 +1,5 @@
#### puma-stats

Add the plugin from: https://github.com/tkishel/puma-stats
Remove if/after https://github.com/puma/puma/pull/2083 is merged.
Requires Puma 4.3.x or newer.
43 changes: 43 additions & 0 deletions lib/puma/plugin/stats.rb
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require 'puma/stats/dsl'

Puma::Plugin.create do
def start(launcher)
str = launcher.options[:stats_url] || 'tcp://0.0.0.0:51209'

require 'puma/stats/app'
require 'puma/minissl/context_builder'
require 'puma/util'

app = Puma::Stats::App.new launcher
uri = URI.parse str

stats = Puma::Server.new app, launcher.events
stats.min_threads = 0
stats.max_threads = 1

case uri.scheme
when 'ssl'
optional_token = launcher.options[:stats_token] ? "with auth token: #{launcher.options[:stats_token]}" : ''
launcher.events.log "* Starting stats server on URI: #{str} #{optional_token}"
params = Puma::Util.parse_query uri.query
ctx = Puma::MiniSSL::ContextBuilder.new(params, launcher.events).context
stats.add_ssl_listener uri.host, uri.port, ctx
when 'tcp'
optional_token = launcher.options[:stats_token] ? "with auth token: #{launcher.options[:stats_token]}" : ''
launcher.events.log "* Starting stats server on URI: #{str} #{optional_token}"
stats.add_tcp_listener uri.host, uri.port
else
launcher.events.error "Invalid stats server URI: #{str}"
end

launcher.events.register(:state) do |state|
if %i[halt restart stop].include?(state)
stats.stop(true) unless stats.shutting_down?
end
end

stats.run
end
end
45 changes: 45 additions & 0 deletions lib/puma/stats/app.rb
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require 'json'

module Puma
module Stats
class App
def initialize(launcher)
@launcher = launcher
@auth_token = launcher.options[:stats_token]
end

def call(env)
return rack_response(403, 'Invalid stats auth token', 'text/plain') unless authenticate(env)

case env['PATH_INFO']
when %r{/puma-stats-gc$}
rack_response(200, GC.stat.to_json)

when %r{/puma-stats$}
rack_response(200, @launcher.stats)

else
rack_response 404, 'Unsupported request', 'text/plain'
end
end

private

def authenticate(env)
return true unless @auth_token

env['QUERY_STRING'].to_s.split(/&;/).include?("token=#{@auth_token}")
end

def rack_response(status, body, content_type = 'application/json')
headers = {
'Content-Type' => content_type,
'Content-Length' => body.bytesize.to_s
}
[status, headers, [body]]
end
end
end
end
13 changes: 13 additions & 0 deletions lib/puma/stats/dsl.rb
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Puma
class DSL
def stats_url(url = nil)
@options[:stats_url] = url
end

def stats_token(token = nil)
@options[:stats_token] = token
end
end
end
7 changes: 7 additions & 0 deletions lib/puma/stats/version.rb
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Puma
module Stats
VERSION = '1.0.2'
end
end