-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathprometheus-client.codemirror-adapter.ts
More file actions
81 lines (75 loc) · 3.12 KB
/
prometheus-client.codemirror-adapter.ts
File metadata and controls
81 lines (75 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2025 The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { PrometheusClient as CodeMirrorPrometheusClient } from '@prometheus-io/codemirror-promql';
import { LabelNamesRequestParameters, LabelValuesRequestParameters, SeriesRequestParameters } from './api-types';
import { Matcher } from '@prometheus-io/codemirror-promql/dist/esm/types';
import { PrometheusClient } from './prometheus-client';
import { MetricMetadata } from '@prometheus-io/codemirror-promql/dist/esm/client/prometheus';
export const createCodeMirrorPromClient = (
prometheusClient?: PrometheusClient
): CodeMirrorPrometheusClient | undefined => {
if (prometheusClient) {
return {
labelNames: async (metricNames: string | undefined) => {
let params: LabelNamesRequestParameters = {};
if (metricNames) {
params['match[]'] = metricNames.split(',');
}
const { data } = (await prometheusClient?.labelNames(params)) || {};
return data || [];
},
labelValues: async (labelName: string, metricName?: string, matchers?: Matcher[]) => {
let params: LabelValuesRequestParameters = {
labelName,
};
if (metricName) {
params['match[]'] = [
`${metricName}{${matchers
?.reduce((acc: Matcher[], curr: Matcher) => {
if (!curr.matchesEmpty()) {
acc.push(curr);
}
return acc;
}, [])
.map((m) => `${m.name}${m.type}"${m.value}"`)
.join(',')}}`,
];
}
const { data } = (await prometheusClient?.labelValues(params)) || {};
return data || [];
},
metricNames: async (prefix?: string) => {
let params: LabelValuesRequestParameters = {
labelName: '__name__',
};
const { data } = (await prometheusClient?.labelValues(params)) || {};
return data || [];
},
metricMetadata: async (): Promise<Record<string, MetricMetadata[]>> => {
const { data } = (await prometheusClient?.metricMetadata({})) || {};
return data || {};
},
series: async (metricName: string, matchers?: Matcher[], labelName?: string): Promise<Map<string, string>[]> => {
const params: SeriesRequestParameters = {
'match[]': [],
};
const { data } = (await prometheusClient?.series(params)) || {};
return (data || []).map((item: any) => new Map(Object.entries(item.metric)));
},
flags: async () => {
const { data } = (await prometheusClient?.flags()) || {};
return data || {};
},
};
}
};