@dash0hq/codemirror-promql

中文文档
0.302.1-dash03发布于 2026/03/30Apache-2.0TypeScriptESM + CJS近 30 天下载 1.1万

a CodeMirror mode for the PromQL language

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. Default setup
  4. Deactivate autocompletion - linter
  5. Linter
  6. Autocompletion
  7. Example
  8. License

CodeMirror-promql

This project provides a mode for CodeMirror that handles syntax highlighting, linting and autocompletion for PromQL (Prometheus Query Language).

preview

Installation

This mode is available as a npm package:

npm install --save @dash0hq/codemirror-promql

Note: You will have to manually install different packages that are part of CodeMirror, as they are a peer dependency to this package. Here are the different packages you need to install:

  • @codemirror/autocomplete
  • @codemirror/language
  • @codemirror/lint
  • @codemirror/state
  • @codemirror/view
  • @lezer/common
npm install --save @codemirror/autocomplete @codemirror/language @codemirror/lint @codemirror/state @codemirror/view @lezer/common

Note 2: that's the minimum required to install the lib. You would probably need to install as well the dependency @codemirror/basic-setup to ease the setup of codeMirror itself:

npm install --save @codemirror/basic-setup

Usage

As the setup of the PromQL language can a bit tricky in CMN, this lib provides a class PromQLExtension which is here to help you to configure the different extensions we aim to provide.

Default setup

If you want to enjoy about the different features provided without taking too much time to understand how to configure them, then the easiest way is this one:

import {PromQLExtension} from '@dash0hq/codemirror-promql';
import {basicSetup} from '@codemirror/basic-setup';
import {EditorState} from '@codemirror/state';
import {EditorView} from '@codemirror/view';

const promQL = new PromQLExtension()
new EditorView({
    state: EditorState.create({
        extensions: [basicSetup, promQL.asExtension()],
    }),
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    // tslint:disable-next-line:no-non-null-assertion
    parent: document.getElementById('editor')!,
});

Using the default setup will activate:

  • syntax highlighting
  • an offline autocompletion that will suggest PromQL keywords such as functions / aggregations, depending on the context.
  • an offline linter that will display PromQL syntax errors (which is closer to what Prometheus returns)

Deactivate autocompletion - linter

In case you would like to deactivate the linter and/or the autocompletion it's simple as that:

const promQL = new PromQLExtension().activateLinter(false).activateCompletion(false) // here the linter and the autocomplete are deactivated

Linter

There is no particular configuration today for the linter. Feel free to file an issue if you have some use cases that would require configurability.

Autocompletion

The autocompletion feature provides multiple different parameters that can be used to adapt this lib to your environment.

maxMetricsMetadata

maxMetricsMetadata is the maximum number of metrics in Prometheus for which metadata is fetched. If the number of metrics exceeds this limit, no metric metadata is fetched at all.

By default, the limit is 10 000 metrics.

Use it cautiously. A high value of this limit can cause a crash of your browser due to too many data fetched.

const promQL = new PromQLExtension().setComplete({maxMetricsMetadata: 10000})

Connect the autocompletion extension to a remote Prometheus server

Connecting the autocompletion extension to a remote Prometheus server will provide autocompletion of metric names, label names, and label values.

Use the default Prometheus client
Prometheus URL

If you want to use the default Prometheus client provided by this lib, you have to provide the url used to contact the Prometheus server.

Note: this is the only mandatory parameter in case you want to use the default Prometheus client. Without this parameter, the rest of the config will be ignored, and the Prometheus client won't be initialized.

const promQL = new PromQLExtension().setComplete({remote: {url: 'https://prometheus.land'}})
Override FetchFn

In case your Prometheus server is protected and requires a special HTTP client, you can override the function fetchFn that is used to perform any required HTTP request.

const promQL = new PromQLExtension().setComplete({remote: {fetchFn: myHTTPClient}})
Duration to use for looking back when retrieving metrics / labels

If you are a bit familiar with the Prometheus API, you are aware that you can pass a time interval that is used to tell to Prometheus which period of time you are looking for when retrieving metadata (like metrics / labels).

In case you would like to provide your own duration, you can override the variable lookbackInterval. By default, the value is 12 * 60 * 60 * 1000 (12h). The value must be defined in milliseconds.

const promQL = new PromQLExtension().setComplete({remote: {lookbackInterval: 12 * 60 * 60 * 1000}})
Error Handling

You can set up your own error handler to catch any HTTP error that can occur when the PrometheusClient is contacting Prometheus.

const promQL = new PromQLExtension().setComplete({remote: {httpErrorHandler: (error: any) => console.error(error)}})
HTTP method used

By default, the Prometheus client will use the HTTP method POST when contacting Prometheus for the endpoints /api/v1/labels and /api/v1/series.

You can change it to use the HTTP method GET if you prefer.

const promQL = new PromQLExtension().setComplete({remote: {httpMethod: 'GET'}})
HTTP request headers

If you need to send specific HTTP headers along with the requests to Prometheus, you can adjust that as follows:

const customHeaders = new Headers({'header-name': 'test-value'});
const promql = new PromQLExtension().setComplete({remote: {requestHeaders: customHeaders}})
Override the API Prefix

The default Prometheus Client, when building the query to get data from Prometheus, is using an API prefix which is by default /api/v1.

You can override this value like this:

const promql = new PromQLExtension().setComplete({remote: {apiPrefix: '/my/api/prefix'}})
Cache

The default client has an embedded cache that is used to store the different metrics and labels retrieved from a remote Prometheus server.

Max Age

The data are stored in the cache for a limited amount of time defined by the variable maxAge which is by default 5 minutes. The value must be defined in milliseconds.

const promQL = new PromQLExtension().setComplete({remote: {cache: {maxAge: 5 * 60 * 1000}}})
Initial Metric List

The cache can be initialized with a list of metric names. It is useful when you already have the list of the metrics somewhere else in your application, and you would like to share this list with the embedded Prometheus client of codemirror-promql.

Note: keep in mind that this list will be kept into the embedded Prometheus client until the time defined by maxAge.

const promQL = new PromQLExtension().setComplete({
    remote: {
        cache: {
            initialMetricList: [
                'ALERTS',
                'ALERTS_FOR_STATE',
                'alertmanager_alerts',
                'alertmanager_alerts_invalid_total',
                'alertmanager_alerts_received_total',
            ]
        }
    }
})
Override the default Prometheus client

In case you are not satisfied by our default Prometheus client, you can still provide your own. It has to implement the interface PrometheusClient .

const promQL = new PromQLExtension().setComplete({remote: MyPrometheusClient})

Provide your own implementation of the autocompletion

In case you would like to provide you own implementation of the autocompletion, you can simply do it like that:

const promQL = new PromQLExtension().setComplete({completeStrategy: myCustomImpl})

Note: In case this parameter is provided, then the rest of the configuration is ignored.

Example

License

The code is licensed under an Apache 2.0 license.

版本与发布记录

@dash0hq/codemirror-promql 共发布 4 个版本,首次发布于 2025/04/26;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.302.1-dash03latest2026/03/307 个月前
0.302.1-dash022025/12/2610 个月前
0.302.1-dash012025/05/081.5 年前
0.302.12025/04/261.5 年前

关于 @dash0hq/codemirror-promql 的常见问题

@dash0hq/codemirror-promql 是什么?

a CodeMirror mode for the PromQL language

如何安装 @dash0hq/codemirror-promql?

在项目目录运行 npm install @dash0hq/codemirror-promql,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@codemirror/autocomplete(^6.4.0)、@codemirror/language(^6.3.0)、@codemirror/lint(^6.0.0)、@codemirror/state(^6.1.1)、@codemirror/view(^6.4.0)、@lezer/common(^1.0.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@dash0hq/codemirror-promql 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dash0hq/codemirror-promql 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dash0hq/codemirror-promql 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dash0hq/codemirror-promql 可以商用吗?

@dash0hq/codemirror-promql 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@dash0hq/codemirror-promql 的下载量有多少?

根据 npm 官方下载统计,@dash0hq/codemirror-promql 在 2026/10/12 – 2026/11/10 这 30 天内被下载 10,789 次,日均约 360 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dash0hq/codemirror-promql 还在维护吗?

最新版本 0.302.1-dash03 发布于 2026/03/30(7 个月前);至今共发布 4 个版本,其中过去 12 个月发布了 2 个;首次发布于 2025/04/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。