OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
expose reduced set of metrics used for auto scaling
Markus Kahl068a4d8620e3
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rbindex ad7f2b5dbe2..818d56ca3f0 100644--- a/config/constants/settings/definition.rb+++ b/config/constants/settings/definition.rb@@ -1145,6 +1145,17 @@ module Settings},writable: false},+ metrics: {+ description: "+ Publish a reduced set of puma metrics on a separate port for Prometheus consumption,+ providing autoscaling hints+ ".squish,+ default: {+ "enabled" => false,+ "port" => 9394+ },+ writable: false+ },sys_api_enabled: {description: "Enable internal system API for setting up managed repositories",default: falsediff --git a/config/puma.rb b/config/puma.rbindex 13c2d1bc940..a440f67650a 100644--- a/config/puma.rb+++ b/config/puma.rb@@ -90,3 +90,32 @@ if OpenProject::Configuration.statsd_host.present?plugin :statsdend++metrics_enabled = OpenProject::Configuration.metrics["enabled"]++if metrics_enabled+ def start_metrics_server!+ require "open_project/metrics/metrics_app"++ Thread.new do+ require "webrick"++ port = OpenProject::Configuration.metrics["port"]+ # we silence the logs because lots of 'GET /metrics HTTP/1.1' logs are not particularly useful+ server = WEBrick::HTTPServer.new Port: port, BindAddress: "0.0.0.0", AccessLog: []++ Rails.logger.info "Starting metrics server on port #{port} under /metrics"++ server.mount "/", Rack::Handler::WEBrick, OpenProject::Metrics::MetricsApp.new+ server.start+ end+ end++ if OpenProject::Configuration.web["workers"] > 0+ before_fork do+ start_metrics_server!+ end+ else+ start_metrics_server!+ end+enddiff --git a/lib/open_project/metrics/metrics_app.rb b/lib/open_project/metrics/metrics_app.rbnew file mode 100644index 00000000000..f7c3f9f253e--- /dev/null+++ b/lib/open_project/metrics/metrics_app.rb@@ -0,0 +1,114 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++# frozen_string_literal: true++require "socket"++##+# Returns metrics about the running server using the Prometheus format.+# This has to be started in the puma master process before forking+# since otherwise it will not yield useful results.+module OpenProject+ module Metrics+ class MetricsApp+ def call(env)+ if metrics_endpoint?(env)+ [200, { "Content-Type" => content_type }, [metrics]]+ else+ [404, { "Content-Type" => content_type }, ["Not found. We only support /metrics."]]+ end+ end++ def collected_puma_metrics+ @collected_puma_metrics ||= {+ backlog: [:puma_request_backlog, "Number of requests waiting to be processed by a puma thread"],+ running: [:puma_running_threads, "Number of puma threads currently running"],+ pool_capacity: [:puma_thread_pool_capacity, "Number of puma threads available at current scale"],+ requests_count: [:puma_requests_count, "The number of requests processed"],+ busy_threads: [+ :puma_busy_threads,+ "Wholistic stat reflecting the overall current state of work to be done and the capacity to do it"+ ]+ }+ end++ def puma_metrics+ metrics = collected_puma_metrics.keys+ values = puma_metric_values metrics+ stats = metrics.zip(values).to_h+ text = +""++ stats.each do |metric, value|+ name, help = collected_puma_metrics[metric]++ text << "# HELP #{name} #{help}\n"+ text << "# TYPE #{name} gauge\n"+ text << "#{name}{hostname=\"#{hostname}\"} #{value}\n\n"+ end++ text+ end++ def puma_metric_values(metrics)+ status = Array(Puma.stats_hash[:worker_status])++ if status.empty? # puma not running in clustered mode+ status = [{ last_status: Puma.stats_hash }]+ end++ status+ .map { |status| get_stats status, metrics }+ .transpose+ .map(&:sum)+ end++ def get_stats(status, metrics)+ stats = Hash(status[:last_status])++ metrics.map { |metric| stats[metric] || 0 }+ end++ def metrics_endpoint?(env)+ env["REQUEST_PATH"] == "/metrics"+ end++ def hostname+ @hostname ||= Socket.gethostname+ end++ def metrics+ puma_metrics+ end++ def content_type+ "text/plain; charset=utf-8"+ end+ end+ end+end