Commit 333632f1 authored by Stan Hu's avatar Stan Hu

Merge branch '30357-http_request_duration_seconds' into 'master'

Initialize http_request_duration_seconds metric

Closes #30357

See merge request gitlab-org/gitlab!17377
parents 7b345c06 2c3207d8
...@@ -54,5 +54,7 @@ if !Rails.env.test? && Gitlab::Metrics.prometheus_metrics_enabled? ...@@ -54,5 +54,7 @@ if !Rails.env.test? && Gitlab::Metrics.prometheus_metrics_enabled?
elsif defined?(::Puma) elsif defined?(::Puma)
Gitlab::Metrics::Samplers::PumaSampler.instance(Settings.monitoring.puma_sampler_interval).start Gitlab::Metrics::Samplers::PumaSampler.instance(Settings.monitoring.puma_sampler_interval).start
end end
Gitlab::Metrics::RequestsRackMiddleware.initialize_http_request_duration_seconds
end end
end end
...@@ -3,6 +3,18 @@ ...@@ -3,6 +3,18 @@
module Gitlab module Gitlab
module Metrics module Metrics
class RequestsRackMiddleware class RequestsRackMiddleware
HTTP_METHODS = {
"delete" => %w(200 202 204 303 400 401 403 404 410 422 500 503),
"get" => %w(200 204 301 302 303 304 307 400 401 403 404 410 412 422 429 500 503),
"head" => %w(200 204 301 302 303 304 400 401 403 404 410 429 500 503),
"options" => %w(200 404),
"patch" => %w(200 202 204 400 403 404 409 416 422 500),
"post" => %w(200 201 202 204 301 302 303 304 400 401 403 404 406 409 410 412 413 415 422 429 500 503),
"propfind" => %w(404),
"put" => %w(200 202 204 400 401 403 404 405 406 409 410 415 422 500),
"report" => %w(404)
}.freeze
def initialize(app) def initialize(app)
@app = app @app = app
end end
...@@ -20,6 +32,14 @@ module Gitlab ...@@ -20,6 +32,14 @@ module Gitlab
{}, [0.05, 0.1, 0.25, 0.5, 0.7, 1, 2.5, 5, 10, 25]) {}, [0.05, 0.1, 0.25, 0.5, 0.7, 1, 2.5, 5, 10, 25])
end end
def self.initialize_http_request_duration_seconds
HTTP_METHODS.each do |method, statuses|
statuses.each do |status|
http_request_duration_seconds.get({ method: method, status: status })
end
end
end
def call(env) def call(env)
method = env['REQUEST_METHOD'].downcase method = env['REQUEST_METHOD'].downcase
started = Time.now.to_f started = Time.now.to_f
......
...@@ -63,5 +63,19 @@ describe Gitlab::Metrics::RequestsRackMiddleware do ...@@ -63,5 +63,19 @@ describe Gitlab::Metrics::RequestsRackMiddleware do
expect { subject.call(env) }.to raise_error(StandardError) expect { subject.call(env) }.to raise_error(StandardError)
end end
end end
describe '.initialize_http_request_duration_seconds' do
it "sets labels" do
expected_labels = []
described_class::HTTP_METHODS.each do |method, statuses|
statuses.each do |status|
expected_labels << { method: method, status: status }
end
end
described_class.initialize_http_request_duration_seconds
expect(described_class.http_request_duration_seconds.values.keys).to include(*expected_labels)
end
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment