From f486efbb9a017d416f95de41a2e433afdfff9773 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Wed, 3 Aug 2022 14:15:32 +0200 Subject: [PATCH] codemirror-promql: add method to fetch flags in prometheus client (#11090) Signed-off-by: Augustin Husson --- .../src/client/prometheus.ts | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/web/ui/module/codemirror-promql/src/client/prometheus.ts b/web/ui/module/codemirror-promql/src/client/prometheus.ts index 5c620faf9..0055247c0 100644 --- a/web/ui/module/codemirror-promql/src/client/prometheus.ts +++ b/web/ui/module/codemirror-promql/src/client/prometheus.ts @@ -36,6 +36,8 @@ export interface PrometheusClient { // Note that the returned list can be a superset of those suggestions for the prefix (i.e., including ones without the // prefix), as codemirror will filter these out when displaying suggestions to the user. metricNames(prefix?: string): Promise; + // flags returns flag values that prometheus was configured with. + flags(): Promise>; } export interface CacheConfig { @@ -209,6 +211,15 @@ export class HTTPPrometheusClient implements PrometheusClient { return this.labelValues('__name__'); } + flags(): Promise> { + return this.fetchAPI>(this.flagsEndpoint()).catch((error) => { + if (this.errorHandler) { + this.errorHandler(error); + } + return {}; + }); + } + private fetchAPI(resource: string, init?: RequestInit): Promise { return this.fetchFn(this.url + resource, init) .then((res) => { @@ -254,6 +265,10 @@ export class HTTPPrometheusClient implements PrometheusClient { private metricMetadataEndpoint(): string { return `${this.apiPrefix}/metadata`; } + + private flagsEndpoint(): string { + return `${this.apiPrefix}/status/flags`; + } } class Cache { @@ -263,6 +278,7 @@ class Cache { private metricMetadata: Record; private labelValues: LRUCache; private labelNames: string[]; + private flags: Record; constructor(config?: CacheConfig) { const maxAge = config && config.maxAge ? config.maxAge : 5 * 60 * 1000; @@ -270,6 +286,7 @@ class Cache { this.metricMetadata = {}; this.labelValues = new LRUCache(maxAge); this.labelNames = []; + this.flags = {}; if (config?.initialMetricList) { this.setLabelValues('__name__', config.initialMetricList); } @@ -297,6 +314,14 @@ class Cache { }); } + setFlags(flags: Record): void { + this.flags = flags; + } + + getFlags(): Record { + return this.flags; + } + setMetricMetadata(metadata: Record): void { this.metricMetadata = metadata; } @@ -388,7 +413,7 @@ export class CachedPrometheusClient implements PrometheusClient { return this.client.metricMetadata().then((metadata) => { this.cache.setMetricMetadata(metadata); - return this.cache.getMetricMetadata(); + return metadata; }); } @@ -402,4 +427,16 @@ export class CachedPrometheusClient implements PrometheusClient { metricNames(): Promise { return this.labelValues('__name__'); } + + flags(): Promise> { + const cachedFlags = this.cache.getFlags(); + if (cachedFlags && Object.keys(cachedFlags).length > 0) { + return Promise.resolve(cachedFlags); + } + + return this.client.flags().then((flags) => { + this.cache.setFlags(flags); + return flags; + }); + } }