Prometheus: Refactor. Use data source UID rather than the deprecated ID (#62795)

This commit is contained in:
Andres Martinez Gotor 2023-02-06 11:02:02 +01:00 committed by GitHub
parent 5205cfb11c
commit 32e6af966f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 20 deletions

View File

@ -109,7 +109,7 @@ const setPrometheusVersion = (
onUpdate(options)
.then((updatedOptions) => {
getBackendSrv()
.get(`/api/datasources/${updatedOptions.id}/resources/version-detect`)
.get(`/api/datasources/uid/${updatedOptions.uid}/resources/version-detect`)
.then((rawResponse: PromBuildInfoResponse) => {
const rawVersionStringFromApi = rawResponse.data?.version ?? '';
if (rawVersionStringFromApi && semver.valid(rawVersionStringFromApi)) {

View File

@ -61,6 +61,7 @@ describe('PrometheusDatasource', () => {
const instanceSettings = {
url: 'proxied',
id: 1,
uid: 'ABCDEF',
directUrl: 'direct',
user: 'test',
password: 'mupp',
@ -202,7 +203,7 @@ describe('PrometheusDatasource', () => {
it('added to metadata request', () => {
promDs.metadataRequest('/foo');
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0].url).toBe('/api/datasources/1/resources/foo?customQuery=123');
expect(fetchMock.mock.calls[0][0].url).toBe('/api/datasources/uid/ABCDEF/resources/foo?customQuery=123');
});
it('adds params to timeseries query', () => {
@ -237,13 +238,13 @@ describe('PrometheusDatasource', () => {
it('added to metadata request with non-POST endpoint', () => {
promDs.metadataRequest('/foo');
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0].url).toBe('/api/datasources/1/resources/foo?customQuery=123');
expect(fetchMock.mock.calls[0][0].url).toBe('/api/datasources/uid/ABCDEF/resources/foo?customQuery=123');
});
it('added to metadata request with POST endpoint', () => {
promDs.metadataRequest('/api/v1/labels');
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0].url).toBe('/api/datasources/1/resources/api/v1/labels');
expect(fetchMock.mock.calls[0][0].url).toBe('/api/datasources/uid/ABCDEF/resources/api/v1/labels');
expect(fetchMock.mock.calls[0][0].data.customQuery).toBe('123');
});
@ -752,6 +753,7 @@ describe('PrometheusDatasource2', () => {
const instanceSettings = {
url: 'proxied',
id: 1,
uid: 'ABCDEF',
directUrl: 'direct',
user: 'test',
password: 'mupp',
@ -914,7 +916,7 @@ describe('PrometheusDatasource2', () => {
describe('When querying prometheus with one target and instant = true', () => {
let results: any;
const urlExpected = `/api/datasources/1/resources/api/v1/query?query=${encodeURIComponent(
const urlExpected = `/api/datasources/uid/ABCDEF/resources/api/v1/query?query=${encodeURIComponent(
'test{job="testjob"}'
)}&time=123`;
const query = {

View File

@ -213,7 +213,7 @@ export class PrometheusDatasource
}
let queryUrl = this.url + url;
if (url.startsWith(`/api/datasources/${this.id}`)) {
if (url.startsWith(`/api/datasources/uid/${this.uid}`)) {
// This url is meant to be a replacement for the whole URL. Replace the entire URL
queryUrl = url;
}
@ -263,7 +263,7 @@ export class PrometheusDatasource
if (GET_AND_POST_METADATA_ENDPOINTS.some((endpoint) => url.includes(endpoint))) {
try {
return await lastValueFrom(
this._request<T>(`/api/datasources/${this.id}/resources${url}`, params, {
this._request<T>(`/api/datasources/uid/${this.uid}/resources${url}`, params, {
method: this.httpMethod,
hideFromInspector: true,
showErrorAlert: false,
@ -281,7 +281,7 @@ export class PrometheusDatasource
}
return await lastValueFrom(
this._request<T>(`/api/datasources/${this.id}/resources${url}`, params, {
this._request<T>(`/api/datasources/uid/${this.uid}/resources${url}`, params, {
method: 'GET',
hideFromInspector: true,
...options,
@ -702,7 +702,7 @@ export class PrometheusDatasource
}
return this._request<PromDataSuccessResponse<PromVectorData | PromScalarData>>(
`/api/datasources/${this.id}/resources${url}`,
`/api/datasources/uid/${this.uid}/resources${url}`,
data,
{
requestId: query.requestId,

View File

@ -21,6 +21,7 @@ const fetchMock = jest.spyOn(backendSrv, 'fetch');
const instanceSettings = {
url: 'proxied',
id: 1,
uid: 'ABCDEF',
directUrl: 'direct',
user: 'test',
password: 'mupp',
@ -86,7 +87,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `/api/datasources/1/resources/api/v1/labels?start=${raw.from.unix()}&end=${raw.to.unix()}`,
url: `/api/datasources/uid/ABCDEF/resources/api/v1/labels?start=${raw.from.unix()}&end=${raw.to.unix()}`,
hideFromInspector: true,
showErrorAlert: false,
headers: {},
@ -106,7 +107,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `/api/datasources/1/resources/api/v1/label/resource/values?start=${raw.from.unix()}&end=${raw.to.unix()}`,
url: `/api/datasources/uid/ABCDEF/resources/api/v1/label/resource/values?start=${raw.from.unix()}&end=${raw.to.unix()}`,
hideFromInspector: true,
headers: {},
});
@ -130,7 +131,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `/api/datasources/1/resources/api/v1/series?match${encodeURIComponent(
url: `/api/datasources/uid/ABCDEF/resources/api/v1/series?match${encodeURIComponent(
'[]'
)}=metric&start=${raw.from.unix()}&end=${raw.to.unix()}`,
hideFromInspector: true,
@ -156,7 +157,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: '/api/datasources/1/resources/api/v1/series?match%5B%5D=metric%7Blabel1%3D%22foo%22%2C%20label2%3D%22bar%22%2C%20label3%3D%22baz%22%7D&start=1524650400&end=1524654000',
url: '/api/datasources/uid/ABCDEF/resources/api/v1/series?match%5B%5D=metric%7Blabel1%3D%22foo%22%2C%20label2%3D%22bar%22%2C%20label3%3D%22baz%22%7D&start=1524650400&end=1524654000',
hideFromInspector: true,
showErrorAlert: false,
headers: {},
@ -182,7 +183,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `/api/datasources/1/resources/api/v1/series?match${encodeURIComponent(
url: `/api/datasources/uid/ABCDEF/resources/api/v1/series?match${encodeURIComponent(
'[]'
)}=metric&start=${raw.from.unix()}&end=${raw.to.unix()}`,
hideFromInspector: true,
@ -205,7 +206,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `/api/datasources/1/resources/api/v1/label/__name__/values?start=${raw.from.unix()}&end=${raw.to.unix()}`,
url: `/api/datasources/uid/ABCDEF/resources/api/v1/label/__name__/values?start=${raw.from.unix()}&end=${raw.to.unix()}`,
hideFromInspector: true,
headers: {},
});
@ -233,7 +234,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `/api/datasources/1/resources/api/v1/query?query=metric&time=${raw.to.unix()}`,
url: `/api/datasources/uid/ABCDEF/resources/api/v1/query?query=metric&time=${raw.to.unix()}`,
requestId: undefined,
headers: {},
});
@ -255,7 +256,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `/api/datasources/1/resources/api/v1/query?query=1%2B1&time=${raw.to.unix()}`,
url: `/api/datasources/uid/ABCDEF/resources/api/v1/query?query=1%2B1&time=${raw.to.unix()}`,
requestId: undefined,
headers: {},
});
@ -281,7 +282,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `/api/datasources/1/resources/api/v1/series?match${encodeURIComponent('[]')}=${encodeURIComponent(
url: `/api/datasources/uid/ABCDEF/resources/api/v1/series?match${encodeURIComponent('[]')}=${encodeURIComponent(
'up{job="job1"}'
)}&start=${raw.from.unix()}&end=${raw.to.unix()}`,
hideFromInspector: true,
@ -313,7 +314,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `/api/datasources/1/resources/api/v1/label/${resourceName}/values?match${encodeURIComponent(
url: `/api/datasources/uid/ABCDEF/resources/api/v1/label/${resourceName}/values?match${encodeURIComponent(
'[]'
)}=${metricName}&start=${raw.from.unix()}&end=${raw.to.unix()}`,
hideFromInspector: true,
@ -341,7 +342,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `/api/datasources/1/resources/api/v1/label/${resourceName}/values?match%5B%5D=${metricName}%7B${label1Name}%3D%22${label1Value}%22%7D&start=1524650400&end=1524654000`,
url: `/api/datasources/uid/ABCDEF/resources/api/v1/label/${resourceName}/values?match%5B%5D=${metricName}%7B${label1Name}%3D%22${label1Value}%22%7D&start=1524650400&end=1524654000`,
hideFromInspector: true,
headers: {},
});