• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..05-Oct-2021-

src/H05-Oct-2021-6,4375,603

.eslintrc.jsonH A D05-Oct-2021789 3635

.gitignoreH A D05-Oct-2021152 128

.npmignoreH A D05-Oct-2021162 1413

CHANGELOG.mdH A D05-Oct-20215.3 KiB10078

README.mdH A D05-Oct-20219.6 KiB266181

build.shH A D05-Oct-20211 KiB309

package-lock.jsonH A D05-Oct-2021806.3 KiB22,16822,167

package.jsonH A D05-Oct-20213 KiB9493

tsconfig.jsonH A D05-Oct-2021469 2726

webpack.config.cjsH A D05-Oct-20211.1 KiB4139

README.md

1CodeMirror-promql
2=================
3[![CircleCI](https://circleci.com/gh/prometheus-community/codemirror-promql.svg?style=shield)](https://circleci.com/gh/prometheus-community/codemirror-promql) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
4[![NPM version](https://img.shields.io/npm/v/codemirror-promql.svg)](https://www.npmjs.org/package/codemirror-promql) [![codecov](https://codecov.io/gh/prometheus-community/codemirror-promql/branch/master/graph/badge.svg?token=1OSVPBDKZC)](https://codecov.io/gh/prometheus-community/codemirror-promql)
5
6## Overview
7
8This project provides a mode for [CodeMirror Next](https://codemirror.net/6) that handles syntax highlighting, linting
9and autocompletion for PromQL ([Prometheus Query Language](https://prometheus.io/docs/introduction/overview/)).
10
11### Installation
12
13This mode is available as a npm package:
14
15```bash
16npm install --save codemirror-promql
17```
18
19**Note:** You will have to manually install different packages that are part
20of [CodeMirror Next](https://codemirror.net/6), as they are a peer dependency to this package. Here are the different
21packages you need to install:
22
23* **@codemirror/autocomplete**
24* **@codemirror/highlight**
25* **@codemirror/language**
26* **@codemirror/lint**
27* **@codemirror/state**
28* **@codemirror/view**
29
30```bash
31npm install --save @codemirror/autocomplete @codemirror/highlight @codemirror/language @codemirror/lint @codemirror/state @codemirror/view
32```
33
34**Note 2**: that's the minimum required to install the lib. You would probably need to install as well the dependency
35**@codemirror/basic-setup** to ease the setup of codeMirror itself:
36
37```bash
38npm install --save @codemirror/basic-setup
39```
40
41### Playground
42
43[Here](https://codemirror-promql.netlify.app/) you have a playground available that is deployed from the latest commit
44available on the `master` branch.
45
46Here is a short preview of it looks like currently:
47
48![preview](https://user-images.githubusercontent.com/4548045/95660829-d5e4b680-0b2a-11eb-9ecb-41dca6396273.gif)
49
50## Usage
51
52As the setup of the PromQL language can a bit tricky in CMN, this lib provides a class `PromQLExtension`
53which is here to help you to configure the different extensions we aim to provide.
54
55### Default setup
56
57If you want to enjoy about the different features provided without taking too much time to understand how to configure
58them, then the easiest way is this one:
59
60```typescript
61import { PromQLExtension } from 'codemirror-promql';
62import { basicSetup } from '@codemirror/basic-setup';
63import { EditorState } from '@codemirror/state';
64import { EditorView } from '@codemirror/view';
65
66const promQL = new PromQLExtension()
67new EditorView({
68    state: EditorState.create({
69        extensions: [basicSetup, promQL.asExtension()],
70    }),
71    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
72    // tslint:disable-next-line:no-non-null-assertion
73    parent: document.getElementById('editor')!,
74});
75```
76
77Using the default setup will activate:
78
79* syntax highlighting
80* an offline autocompletion that will suggest PromQL keywords such as functions / aggregations, depending on the
81  context.
82* an offline linter that will display PromQL syntax errors (which is closer to what Prometheus returns)
83
84### Deactivate autocompletion - linter
85
86In case you would like to deactivate the linter and/or the autocompletion it's simple as that:
87
88```typescript
89const promQL = new PromQLExtension().activateLinter(false).activateCompletion(false) // here the linter and the autocomplete are deactivated
90```
91
92### Linter
93
94There is no particular configuration today for the linter. Feel free to file an issue if you have some use cases that
95would require configurability.
96
97### Autocompletion
98
99The autocompletion feature provides multiple different parameters that can be used to adapt this lib to your
100environment.
101
102#### maxMetricsMetadata
103
104`maxMetricsMetadata` is the maximum number of metrics in Prometheus for which metadata is fetched. If the number of
105metrics exceeds this limit, no metric metadata is fetched at all.
106
107By default, the limit is 10 000 metrics.
108
109Use it cautiously. A high value of this limit can cause a crash of your browser due to too many data fetched.
110
111```typescript
112const promQL = new PromQLExtension().setComplete({ maxMetricsMetadata: 10000 })
113```
114
115#### Connect the autocompletion extension to a remote Prometheus server
116
117Connecting the autocompletion extension to a remote Prometheus server will provide autocompletion of metric names, label
118names, and label values.
119
120##### Use the default Prometheus client
121
122###### Prometheus URL
123
124If you want to use the default Prometheus client provided by this lib, you have to provide the url used to contact the
125Prometheus server.
126
127Note: this is the only mandatory parameter in case you want to use the default Prometheus client. Without this
128parameter, the rest of the config will be ignored, and the Prometheus client won't be initialized.
129
130```typescript
131const promQL = new PromQLExtension().setComplete({ remote: { url: 'https://prometheus.land' } })
132```
133
134###### Override FetchFn
135
136In case your Prometheus server is protected and requires a special HTTP client, you can override the function `fetchFn`
137that is used to perform any required HTTP request.
138
139```typescript
140const promQL = new PromQLExtension().setComplete({ remote: { fetchFn: myHTTPClient } })
141```
142
143###### Duration to use for looking back when retrieving metrics / labels
144
145If you are a bit familiar with the Prometheus API, you are aware that you can pass a time interval that is used to tell
146to Prometheus which period of time you are looking for when retrieving metadata (like metrics / labels).
147
148In case you would like to provide your own duration, you can override the variable `lookbackInterval`. By default, the
149value is `12 * 60 * 60 * 1000` (12h). The value must be defined in **milliseconds**.
150
151```typescript
152const promQL = new PromQLExtension().setComplete({ remote: { lookbackInterval: 12 * 60 * 60 * 1000 } })
153```
154
155###### Error Handling
156
157You can set up your own error handler to catch any HTTP error that can occur when the PrometheusClient is contacting
158Prometheus.
159
160```typescript
161const promQL = new PromQLExtension().setComplete({ remote: { httpErrorHandler: (error: any) => console.error(error) } })
162```
163
164###### HTTP method used
165
166By default, the Prometheus client will use the HTTP method `POST` when contacting Prometheus for the
167endpoints `/api/v1/labels` and `/api/v1/series`.
168
169You can change it to use the HTTP method `GET` if you prefer.
170
171```typescript
172const promQL = new PromQLExtension().setComplete({ remote: { httpMethod: 'GET' } })
173```
174
175###### Cache
176
177The default client has an embedded cache that is used to store the different metrics and labels retrieved from a remote
178Prometheus server.
179
180###### Max Age
181
182The data are stored in the cache for a limited amount of time defined by the variable `maxAge` which is by default 5
183minutes. The value must be defined in **milliseconds**.
184
185```typescript
186const promQL = new PromQLExtension().setComplete({ remote: { cache: { maxAge: 5 * 60 * 1000 } } })
187```
188
189###### Initial Metric List
190
191The cache can be initialized with a list of metric names. It is useful when you already have the list of the metrics
192somewhere else in your application, and you would like to share this list with the embedded Prometheus client
193of `codemirror-promql`.
194
195Note: keep in mind that this list will be kept into the embedded Prometheus client until the time defined by `maxAge`.
196
197```typescript
198const promQL = new PromQLExtension().setComplete({
199    remote: {
200        cache: {
201            initialMetricList: [
202                'ALERTS',
203                'ALERTS_FOR_STATE',
204                'alertmanager_alerts',
205                'alertmanager_alerts_invalid_total',
206                'alertmanager_alerts_received_total',
207            ]
208        }
209    }
210})
211```
212
213##### Override the default Prometheus client
214
215In case you are not satisfied by our default Prometheus client, you can still provide your own. It has to implement the
216interface [PrometheusClient](https://github.com/prometheus-community/codemirror-promql/blob/master/src/lang-promql/client/prometheus.ts#L111-L117)
217.
218
219```typescript
220const promQL = new PromQLExtension().setComplete({ remote: { prometheusClient: MyPrometheusClient } })
221```
222
223#### Provide your own implementation of the autocompletion
224
225In case you would like to provide you own implementation of the autocompletion, you can simply do it like that:
226
227```typescript
228const promQL = new PromQLExtension().setComplete({ completeStrategy: myCustomImpl })
229```
230
231Note: In case this parameter is provided, then the rest of the configuration is ignored.
232
233### Example
234
235* The development [app](./src/app) can give you an example of how to use it with no TS Framework.
236* [ReactJS example](https://github.com/prometheus/prometheus/blob/431ea75a11ca165dad9dd5d629b3cf975f4c186b/web/ui/react-app/src/pages/graph/CMExpressionInput.tsx)
237* [Angular example](https://github.com/perses/perses/blob/28b3bdac88b0ed7a4602f9c91106442eafcb6c34/internal/api/front/perses/src/app/project/prometheusrule/promql-editor/promql-editor.component.ts)
238
239## Contributions
240
241Any contribution or suggestion would be really appreciated. Feel free
242to [file an issue](https://github.com/prometheus-community/codemirror-promql/issues)
243or [send a pull request](https://github.com/prometheus-community/codemirror-promql/pulls).
244
245## Development
246
247In case you want to contribute and change the code by yourself, run the following commands:
248
249To install all dependencies:
250
251```
252npm install
253```
254
255To start the web server:
256
257```
258npm start
259```
260
261This should create a tab in your browser with the development app that contains CodeMirror Next with the PromQL plugin.
262
263## License
264
265[MIT](./LICENSE)
266