2019-12-03 02:40:22 -06:00
|
|
|
import { LokiDatasource, LOKI_ENDPOINT, LEGACY_LOKI_ENDPOINT } from './datasource';
|
2019-10-31 04:48:05 -05:00
|
|
|
import { DataSourceSettings } from '@grafana/data';
|
2019-10-25 09:43:20 -05:00
|
|
|
import { LokiOptions } from './types';
|
|
|
|
import { createDatasourceSettings } from '../../../features/datasources/mocks';
|
2019-09-12 03:02:49 -05:00
|
|
|
|
2019-09-23 06:26:05 -05:00
|
|
|
export function makeMockLokiDatasource(labelsAndValues: { [label: string]: string[] }): LokiDatasource {
|
2019-12-03 02:40:22 -06:00
|
|
|
const legacyLokiLabelsAndValuesEndpointRegex = /^\/api\/prom\/label\/(\w*)\/values/;
|
|
|
|
const lokiLabelsAndValuesEndpointRegex = /^\/loki\/api\/v1\/label\/(\w*)\/values/;
|
|
|
|
|
|
|
|
const legacyLokiLabelsEndpoint = `${LEGACY_LOKI_ENDPOINT}/label`;
|
|
|
|
const lokiLabelsEndpoint = `${LOKI_ENDPOINT}/label`;
|
|
|
|
|
2019-09-12 03:02:49 -05:00
|
|
|
const labels = Object.keys(labelsAndValues);
|
|
|
|
return {
|
|
|
|
metadataRequest: (url: string) => {
|
|
|
|
let responseData;
|
2019-12-03 02:40:22 -06:00
|
|
|
if (url === legacyLokiLabelsEndpoint || url === lokiLabelsEndpoint) {
|
2019-09-12 03:02:49 -05:00
|
|
|
responseData = labels;
|
|
|
|
} else {
|
2019-12-03 02:40:22 -06:00
|
|
|
const match = url.match(legacyLokiLabelsAndValuesEndpointRegex) || url.match(lokiLabelsAndValuesEndpointRegex);
|
2019-09-12 03:02:49 -05:00
|
|
|
if (match) {
|
|
|
|
responseData = labelsAndValues[match[1]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (responseData) {
|
|
|
|
return {
|
|
|
|
data: {
|
|
|
|
data: responseData,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unexpected url error, ${url}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
} as any;
|
|
|
|
}
|
2019-10-25 09:43:20 -05:00
|
|
|
|
|
|
|
export function createDefaultConfigOptions(): DataSourceSettings<LokiOptions> {
|
|
|
|
return createDatasourceSettings<LokiOptions>({
|
|
|
|
maxLines: '531',
|
|
|
|
});
|
|
|
|
}
|