Commit 88e90782 authored by Sean McGivern's avatar Sean McGivern

Merge branch '220338-remove-newlines-in-prom-queries' into 'master'

Remove newlines when generating prometheus_endpoint_path

See merge request gitlab-org/gitlab!34508
parents 9a49e425 36f04307
---
title: Allow multiline Prometheus queries in metrics dashboards yaml
merge_request: 34508
author:
type: changed
...@@ -45,7 +45,9 @@ module Gitlab ...@@ -45,7 +45,9 @@ module Gitlab
raise Errors::MissingQueryError.new('Each "metric" must define one of :query or :query_range') unless query raise Errors::MissingQueryError.new('Each "metric" must define one of :query or :query_range') unless query
query # We need to remove any newlines since our UrlBlocker does not allow
# multiline URLs.
query.squish
end end
end end
end end
......
...@@ -41,7 +41,17 @@ panel_groups: ...@@ -41,7 +41,17 @@ panel_groups:
max_value: 1 max_value: 1
metrics: metrics:
- id: metric_a1 - id: metric_a1
query_range: 'query' query_range: |+
avg(
sum(
container_memory_usage_bytes{
container_name!="POD",
pod_name=~"^{{ci_environment_slug}}-(.*)",
namespace="{{kube_namespace}}"
}
) by (job)
) without (job)
/1024/1024/1024
unit: unit unit: unit
label: Legend Label label: Legend Label
- title: "Super Chart A2" - title: "Super Chart A2"
......
...@@ -26,12 +26,6 @@ RSpec.describe Gitlab::Metrics::Dashboard::Processor do ...@@ -26,12 +26,6 @@ RSpec.describe Gitlab::Metrics::Dashboard::Processor do
let(:process_params) { [project, dashboard_yml, sequence, { environment: environment }] } let(:process_params) { [project, dashboard_yml, sequence, { environment: environment }] }
let(:dashboard) { described_class.new(*process_params).process } let(:dashboard) { described_class.new(*process_params).process }
it 'includes a path for the prometheus endpoint with each metric' do
expect(all_metrics).to satisfy_all do |metric|
metric[:prometheus_endpoint_path] == prometheus_path(metric[:query_range])
end
end
it 'includes an id for each dashboard panel' do it 'includes an id for each dashboard panel' do
expect(all_panels).to satisfy_all do |panel| expect(all_panels).to satisfy_all do |panel|
panel[:id].present? panel[:id].present?
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Metrics::Dashboard::Stages::MetricEndpointInserter do
include MetricsDashboardHelpers
let(:project) { build_stubbed(:project) }
let(:environment) { build_stubbed(:environment, project: project) }
describe '#transform!' do
subject(:transform!) { described_class.new(project, dashboard, environment: environment).transform! }
let(:dashboard) { load_sample_dashboard.deep_symbolize_keys }
it 'generates prometheus_endpoint_path without newlines' do
query = 'avg( sum( container_memory_usage_bytes{ container_name!="POD", '\
'pod_name=~"^{{ci_environment_slug}}-(.*)", namespace="{{kube_namespace}}" } ) '\
'by (job) ) without (job) /1024/1024/1024'
transform!
expect(all_metrics[0][:prometheus_endpoint_path]).to eq(prometheus_path(query))
end
it 'includes a path for the prometheus endpoint with each metric' do
transform!
expect(all_metrics).to satisfy_all do |metric|
metric[:prometheus_endpoint_path] == prometheus_path(metric[:query_range].squish)
end
end
end
private
def all_metrics
dashboard[:panel_groups].flat_map do |group|
group[:panels].flat_map { |panel| panel[:metrics] }
end
end
def prometheus_path(query)
Gitlab::Routing.url_helpers.prometheus_api_project_environment_path(
project,
environment,
proxy_path: :query_range,
query: query
)
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