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