codemirror-promql: add method to fetch flags in prometheus client (#11090)

Signed-off-by: Augustin Husson <husson.augustin@gmail.com>
This commit is contained in:
Augustin Husson 2022-08-03 14:15:32 +02:00 committed by GitHub
parent d943f39f22
commit f486efbb9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 38 additions and 1 deletions

View File

@ -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 // 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. // prefix), as codemirror will filter these out when displaying suggestions to the user.
metricNames(prefix?: string): Promise<string[]>; metricNames(prefix?: string): Promise<string[]>;
// flags returns flag values that prometheus was configured with.
flags(): Promise<Record<string, string>>;
} }
export interface CacheConfig { export interface CacheConfig {
@ -209,6 +211,15 @@ export class HTTPPrometheusClient implements PrometheusClient {
return this.labelValues('__name__'); return this.labelValues('__name__');
} }
flags(): Promise<Record<string, string>> {
return this.fetchAPI<Record<string, string>>(this.flagsEndpoint()).catch((error) => {
if (this.errorHandler) {
this.errorHandler(error);
}
return {};
});
}
private fetchAPI<T>(resource: string, init?: RequestInit): Promise<T> { private fetchAPI<T>(resource: string, init?: RequestInit): Promise<T> {
return this.fetchFn(this.url + resource, init) return this.fetchFn(this.url + resource, init)
.then((res) => { .then((res) => {
@ -254,6 +265,10 @@ export class HTTPPrometheusClient implements PrometheusClient {
private metricMetadataEndpoint(): string { private metricMetadataEndpoint(): string {
return `${this.apiPrefix}/metadata`; return `${this.apiPrefix}/metadata`;
} }
private flagsEndpoint(): string {
return `${this.apiPrefix}/status/flags`;
}
} }
class Cache { class Cache {
@ -263,6 +278,7 @@ class Cache {
private metricMetadata: Record<string, MetricMetadata[]>; private metricMetadata: Record<string, MetricMetadata[]>;
private labelValues: LRUCache<string, string[]>; private labelValues: LRUCache<string, string[]>;
private labelNames: string[]; private labelNames: string[];
private flags: Record<string, string>;
constructor(config?: CacheConfig) { constructor(config?: CacheConfig) {
const maxAge = config && config.maxAge ? config.maxAge : 5 * 60 * 1000; const maxAge = config && config.maxAge ? config.maxAge : 5 * 60 * 1000;
@ -270,6 +286,7 @@ class Cache {
this.metricMetadata = {}; this.metricMetadata = {};
this.labelValues = new LRUCache<string, string[]>(maxAge); this.labelValues = new LRUCache<string, string[]>(maxAge);
this.labelNames = []; this.labelNames = [];
this.flags = {};
if (config?.initialMetricList) { if (config?.initialMetricList) {
this.setLabelValues('__name__', config.initialMetricList); this.setLabelValues('__name__', config.initialMetricList);
} }
@ -297,6 +314,14 @@ class Cache {
}); });
} }
setFlags(flags: Record<string, string>): void {
this.flags = flags;
}
getFlags(): Record<string, string> {
return this.flags;
}
setMetricMetadata(metadata: Record<string, MetricMetadata[]>): void { setMetricMetadata(metadata: Record<string, MetricMetadata[]>): void {
this.metricMetadata = metadata; this.metricMetadata = metadata;
} }
@ -388,7 +413,7 @@ export class CachedPrometheusClient implements PrometheusClient {
return this.client.metricMetadata().then((metadata) => { return this.client.metricMetadata().then((metadata) => {
this.cache.setMetricMetadata(metadata); this.cache.setMetricMetadata(metadata);
return this.cache.getMetricMetadata(); return metadata;
}); });
} }
@ -402,4 +427,16 @@ export class CachedPrometheusClient implements PrometheusClient {
metricNames(): Promise<string[]> { metricNames(): Promise<string[]> {
return this.labelValues('__name__'); return this.labelValues('__name__');
} }
flags(): Promise<Record<string, string>> {
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;
});
}
} }