grafana-pcp/SOURCES/000-redis-support-wildcards-in-metric-names.patch
2021-09-09 18:02:52 +00:00

93 lines
4.5 KiB
Diff

diff --git a/src/datasources/redis/datasource.ts b/src/datasources/redis/datasource.ts
index 6076585..aea11fc 100644
--- a/src/datasources/redis/datasource.ts
+++ b/src/datasources/redis/datasource.ts
@@ -96,6 +96,7 @@ export class PCPRedisDatasource {
}
async handleTarget(instancesValuesGroupedBySeries: Record<string, MetricValue[]>,
+ metricNames: Record<string, string>,
descriptions: any, labels: any, target: QueryTarget): Promise<TargetResult> {
const metrics: Metric<number | string>[] = [];
@@ -125,7 +126,7 @@ export class PCPRedisDatasource {
}
metrics.push({
- name: target.expr, // TODO: metric, not expression
+ name: metricNames[series],
instances: metricInstances
});
}
@@ -179,10 +180,11 @@ export class PCPRedisDatasource {
const instances = await this.pmSeriesSrv.getValues(seriesList, { start, finish, samples });
const descriptions = await this.pmSeriesSrv.getDescriptions(seriesList);
+ const metricNames = await this.pmSeriesSrv.getMetricNames(seriesList);
const instanceValuesGroupedBySeries = _.groupBy(instances, "series");
const labels = this.pmSeriesSrv.getMetricAndInstanceLabels(seriesList);
const targetResults = await Promise.all(targets.map(target => this.handleTarget(
- _.pick(instanceValuesGroupedBySeries, seriesByExpr[target.expr]), descriptions, labels, target
+ _.pick(instanceValuesGroupedBySeries, seriesByExpr[target.expr]), metricNames, descriptions, labels, target
)));
const panelData = this.transformations.transform(query, targetResults, PCPRedisDatasource.defaultLegendFormatter);
return {
diff --git a/src/datasources/redis/pmseries_srv.ts b/src/datasources/redis/pmseries_srv.ts
index e3c59c6..f3686a5 100644
--- a/src/datasources/redis/pmseries_srv.ts
+++ b/src/datasources/redis/pmseries_srv.ts
@@ -8,6 +8,11 @@ export interface LabelsResponse {
labels: Labels;
}
+export interface MetricNamesResponse {
+ series: string;
+ name: string;
+}
+
class PmSeriesApi {
constructor(private datasourceRequest: DatasourceRequestFn, private url: string) {
@@ -67,6 +72,15 @@ class PmSeriesApi {
return _.isArray(metrics) ? metrics : []; // TODO: on error (no metrics found), pmproxy returns an object (should be an empty array)
}
+ async metricsSeries(series: string[]): Promise<MetricNamesResponse[]> {
+ const response = await this.datasourceRequest({
+ url: `${this.url}/series/metrics`,
+ params: { series: series.join(',') }
+ });
+ const metricNames = response.data;
+ return _.isArray(metricNames) ? metricNames : []; // TODO: on error (no metrics found), pmproxy returns an object (should be an empty array)
+ }
+
async labels(series: string[]): Promise<LabelsResponse[]> {
const response = await this.datasourceRequest({
url: `${this.url}/series/labels`,
@@ -84,6 +98,7 @@ export class PmSeriesSrv {
private instanceCache: Record<string, Record<string, Instance>> = {}; // instanceCache[series][instance] = instance;
private labelCache: Record<string, Labels> = {}; // labelCache[series_or_instance] = labels;
private metricNamesCache: Record<string, string[]> = {}; // metricNamesCache[prefix] = name[];
+ private metricNameOfSeriesCache: Record<string, string> = {};
constructor(datasourceRequest: DatasourceRequestFn, url: string) {
this.pmSeriesApi = new PmSeriesApi(datasourceRequest, url);
@@ -108,6 +123,17 @@ export class PmSeriesSrv {
return _.pick(this.descriptionCache, series);
}
+ async getMetricNames(series: string[]): Promise<Record<string, string>> {
+ const requiredSeries = _.difference(series, Object.keys(this.metricNameOfSeriesCache));
+ if (requiredSeries.length > 0) {
+ const metricNames = await this.pmSeriesApi.metricsSeries(requiredSeries);
+ for (const metricName of metricNames) {
+ this.metricNameOfSeriesCache[metricName.series] = metricName.name;
+ }
+ }
+ return _.pick(this.metricNameOfSeriesCache, series);
+ }
+
async getInstances(series: string[], ignoreCache = false): Promise<Record<string, Record<string, Instance>>> {
const requiredSeries = ignoreCache ? series : _.difference(series, Object.keys(this.instanceCache));
if (requiredSeries.length > 0) {