Commit 7d447cc5 authored by Sarah Yasonik's avatar Sarah Yasonik Committed by Sean McGivern

Accept user-defined dashboard uids in Grafana embeds

parent 41884a8b
...@@ -133,7 +133,7 @@ module Metrics ...@@ -133,7 +133,7 @@ module Metrics
def uid_regex def uid_regex
base_url = @project.grafana_integration.grafana_url.chomp('/') base_url = @project.grafana_integration.grafana_url.chomp('/')
%r{(#{Regexp.escape(base_url)}\/d\/(?<uid>\w+)\/)}x %r{^(#{Regexp.escape(base_url)}\/d\/(?<uid>.+)\/)}x
end end
end end
......
---
title: Accept user-defined dashboard uids in Grafana embeds
merge_request: 20486
author:
type: fixed
...@@ -175,3 +175,64 @@ describe Metrics::Dashboard::GrafanaMetricEmbedService do ...@@ -175,3 +175,64 @@ describe Metrics::Dashboard::GrafanaMetricEmbedService do
end end
end end
end end
describe Metrics::Dashboard::GrafanaUidParser do
let_it_be(:grafana_integration) { create(:grafana_integration) }
let_it_be(:project) { grafana_integration.project }
subject { described_class.new(grafana_url, project).parse }
context 'with a Grafana-defined uid' do
let(:grafana_url) { grafana_integration.grafana_url + '/d/XDaNK6amz/?panelId=1' }
it { is_expected.to eq 'XDaNK6amz' }
end
context 'with a user-defined uid' do
let(:grafana_url) { grafana_integration.grafana_url + '/d/pgbouncer-main/pgbouncer-overview?panelId=1' }
it { is_expected.to eq 'pgbouncer-main' }
end
context 'when a uid is not present' do
let(:grafana_url) { grafana_integration.grafana_url }
it { is_expected.to be nil }
end
context 'when the url starts with unrelated content' do
let(:grafana_url) { 'js:' + grafana_integration.grafana_url }
it { is_expected.to be nil }
end
end
describe Metrics::Dashboard::DatasourceNameParser do
include GrafanaApiHelpers
let(:grafana_url) { valid_grafana_dashboard_link('https://gitlab.grafana.net') }
let(:grafana_dashboard) { JSON.parse(fixture_file('grafana/dashboard_response.json'), symbolize_names: true) }
subject { described_class.new(grafana_url, grafana_dashboard).parse }
it { is_expected.to eq 'GitLab Omnibus' }
context 'when the panelId is missing from the url' do
let(:grafana_url) { 'https:/gitlab.grafana.net/d/jbdbks/' }
it { is_expected.to be nil }
end
context 'when the panel is not present' do
# We're looking for panelId of 8, but only 6 is present
let(:grafana_dashboard) { { dashboard: { panels: [{ id: 6 }] } } }
it { is_expected.to be nil }
end
context 'when the dashboard panel does not have a datasource' do
let(:grafana_dashboard) { { dashboard: { panels: [{ id: 8 }] } } }
it { is_expected.to be nil }
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