Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions prometheus/src/model/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,5 @@ export interface ParseQueryRequestParameters {
}

export type ParseQueryResponse = ApiResponse<ASTNode>;

export type FlagsResponse = ApiResponse<Record<string, string>>;
81 changes: 81 additions & 0 deletions prometheus/src/model/prometheus-client.codemirror-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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';

Check failure on line 16 in prometheus/src/model/prometheus-client.codemirror-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

`@prometheus-io/codemirror-promql/dist/esm/types` import should occur before import of `./api-types`
import { PrometheusClient } from './prometheus-client';
import { MetricMetadata } from '@prometheus-io/codemirror-promql/dist/esm/client/prometheus';

Check failure on line 18 in prometheus/src/model/prometheus-client.codemirror-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

`@prometheus-io/codemirror-promql/dist/esm/client/prometheus` import should occur before import of `./api-types`

export const createCodeMirrorPromClient = (
prometheusClient?: PrometheusClient
): CodeMirrorPrometheusClient | undefined => {
if (prometheusClient) {
return {
labelNames: async (metricNames: string | undefined) => {
let params: LabelNamesRequestParameters = {};

Check failure on line 26 in prometheus/src/model/prometheus-client.codemirror-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

'params' is never reassigned. Use 'const' instead

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 = {

Check failure on line 35 in prometheus/src/model/prometheus-client.codemirror-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

'params' is never reassigned. Use 'const' instead
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) => {

Check failure on line 55 in prometheus/src/model/prometheus-client.codemirror-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

'prefix' is defined but never used. Allowed unused args must match /^_/u
let params: LabelValuesRequestParameters = {

Check failure on line 56 in prometheus/src/model/prometheus-client.codemirror-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

'params' is never reassigned. Use 'const' instead
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>[]> => {

Check failure on line 67 in prometheus/src/model/prometheus-client.codemirror-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

'metricName' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 67 in prometheus/src/model/prometheus-client.codemirror-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

'matchers' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 67 in prometheus/src/model/prometheus-client.codemirror-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

'labelName' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 67 in prometheus/src/model/prometheus-client.codemirror-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

Array type using 'T[]' is forbidden for non-simple types. Use 'Array<T>' instead
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 || {};
},
};
}
};
6 changes: 6 additions & 0 deletions prometheus/src/model/prometheus-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import { fetch, fetchJson, RequestHeaders } from '@perses-dev/core';
import { DatasourceClient } from '@perses-dev/plugin-system';
import {
FlagsResponse,
InstantQueryRequestParameters,
InstantQueryResponse,
LabelNamesRequestParameters,
Expand Down Expand Up @@ -45,6 +46,7 @@ export interface PrometheusClient extends DatasourceClient {
metricMetadata(params: MetricMetadataRequestParameters, headers?: RequestHeaders): Promise<MetricMetadataResponse>;
series(params: SeriesRequestParameters, headers?: RequestHeaders): Promise<SeriesResponse>;
parseQuery(params: ParseQueryRequestParameters, headers?: RequestHeaders): Promise<ParseQueryResponse>;
flags(headers?: RequestHeaders): Promise<FlagsResponse>;
}

export interface QueryOptions {
Expand Down Expand Up @@ -149,6 +151,10 @@ export function parseQuery(
return fetchWithPost<ParseQueryRequestParameters, ParseQueryResponse>(apiURI, params, queryOptions);
}

export function flags(queryOptions: QueryOptions): Promise<FlagsResponse> {
return fetchWithGet('/api/v1/status/flags', {}, queryOptions);
}

function fetchWithGet<T extends RequestParams<T>, TResponse>(
apiURI: string,
params: T,
Expand Down
2 changes: 2 additions & 0 deletions prometheus/src/plugins/prometheus-datasource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
metricMetadata,
series,
parseQuery,
flags,
} from '../model';
import { PrometheusDatasourceSpec } from './types';
import { PrometheusDatasourceEditor } from './PrometheusDatasourceEditor';
Expand Down Expand Up @@ -55,6 +56,7 @@ const createClient: DatasourcePlugin<PrometheusDatasourceSpec, PrometheusClient>
metricMetadata: (params, headers) => metricMetadata(params, { datasourceUrl, headers: headers ?? specHeaders }),
series: (params, headers) => series(params, { datasourceUrl, headers: headers ?? specHeaders }),
parseQuery: (params, headers) => parseQuery(params, { datasourceUrl, headers: headers ?? specHeaders }),
flags: (headers) => flags({ datasourceUrl, headers: headers ?? specHeaders }),
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
useFormatState,
useMinStepState,
} from './query-editor-model';
import { createCodeMirrorPromClient } from '../../model/prometheus-client.codemirror-adapter';

/**
* The options editor component for editing a PrometheusTimeSeriesQuery's spec.
Expand All @@ -42,7 +43,7 @@ export function PrometheusTimeSeriesQueryEditor(props: PrometheusTimeSeriesQuery
const datasourceSelectLabelID = `prom-datasource-label-${selectedDatasource.name || 'default'}`;

const { data: client } = useDatasourceClient<PrometheusClient>(selectedDatasource);
const promURL = client?.options.datasourceUrl;
const codeMirrorPromClient = createCodeMirrorPromClient(client);
const { data: datasourceResource } = useDatasource(selectedDatasource);

const { handleQueryChange, handleQueryBlur } = useQueryState(props);
Expand Down Expand Up @@ -81,7 +82,9 @@ export function PrometheusTimeSeriesQueryEditor(props: PrometheusTimeSeriesQuery
/>
</FormControl>
<PromQLEditor
completeConfig={{ remote: { url: promURL } }}
completeConfig={{
remote: codeMirrorPromClient,
}}
value={value.query} // here we are passing `value.query` and not `query` from useQueryState in order to get updates only on onBlur events
datasource={selectedDatasource}
onChange={handleQueryChange}
Expand Down
5 changes: 3 additions & 2 deletions prometheus/src/plugins/prometheus-variables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
PrometheusLabelValuesVariableOptions,
PrometheusPromQLVariableOptions,
} from './types';
import { createCodeMirrorPromClient } from '../model/prometheus-client.codemirror-adapter';

export function PrometheusLabelValuesVariableEditor(
props: OptionsEditorProps<PrometheusLabelValuesVariableOptions>
Expand Down Expand Up @@ -146,7 +147,7 @@ export function PrometheusPromQLVariableEditor(
const selectedDatasource = datasource ?? DEFAULT_PROM;

const { data: client } = useDatasourceClient<PrometheusClient>(selectedDatasource);
const promURL = client?.options.datasourceUrl;
const codeMirrorPromClient = createCodeMirrorPromClient(client);

const handleDatasourceChange: DatasourceSelectProps['onChange'] = (next) => {
if (isPrometheusDatasourceSelector(next)) {
Expand Down Expand Up @@ -176,7 +177,7 @@ export function PrometheusPromQLVariableEditor(
/>
</FormControl>
<PromQLEditor
completeConfig={{ remote: { url: promURL } }}
completeConfig={{ remote: codeMirrorPromClient }}
value={value.expr}
datasource={selectedDatasource}
onBlur={(event) => {
Expand Down
Loading