2019-09-23 06:26:05 -05:00
import { Grammar } from 'prismjs' ;
2022-04-22 08:33:13 -05:00
2019-11-15 09:38:25 -06:00
import { CompletionItem } from '@grafana/ui' ;
2019-09-23 06:26:05 -05:00
2022-10-06 09:35:30 -05:00
export const AGGREGATION_OPERATORS : CompletionItem [ ] = [
2019-11-15 09:38:25 -06:00
{
2022-10-06 09:35:30 -05:00
label : 'avg' ,
insertText : 'avg' ,
documentation : 'Calculate the average over dimensions' ,
2019-11-15 09:38:25 -06:00
} ,
{
2022-10-06 09:35:30 -05:00
label : 'bottomk' ,
insertText : 'bottomk' ,
documentation : 'Smallest k elements by sample value' ,
} ,
{
label : 'count' ,
insertText : 'count' ,
documentation : 'Count number of elements in the vector' ,
2019-11-15 09:38:25 -06:00
} ,
{
label : 'max' ,
insertText : 'max' ,
documentation : 'Select maximum over dimensions' ,
} ,
{
2022-10-06 09:35:30 -05:00
label : 'min' ,
insertText : 'min' ,
documentation : 'Select minimum over dimensions' ,
2019-11-15 09:38:25 -06:00
} ,
{
label : 'stddev' ,
insertText : 'stddev' ,
documentation : 'Calculate population standard deviation over dimensions' ,
} ,
{
label : 'stdvar' ,
insertText : 'stdvar' ,
documentation : 'Calculate population standard variance over dimensions' ,
} ,
{
2022-10-06 09:35:30 -05:00
label : 'sum' ,
insertText : 'sum' ,
documentation : 'Calculate sum over dimensions' ,
2019-11-15 09:38:25 -06:00
} ,
{
label : 'topk' ,
insertText : 'topk' ,
documentation : 'Largest k elements by sample value' ,
} ,
] ;
2020-10-14 03:22:39 -05:00
export const PIPE_PARSERS : CompletionItem [ ] = [
{
label : 'json' ,
insertText : 'json' ,
2022-10-06 09:35:30 -05:00
documentation : 'Extracting labels from the log line using json parser.' ,
2020-10-14 03:22:39 -05:00
} ,
{
label : 'regexp' ,
insertText : 'regexp ""' ,
2022-10-06 09:35:30 -05:00
documentation : 'Extracting labels from the log line using regexp parser.' ,
2020-10-14 03:22:39 -05:00
move : - 1 ,
} ,
{
label : 'logfmt' ,
insertText : 'logfmt' ,
2022-10-06 09:35:30 -05:00
documentation : 'Extracting labels from the log line using logfmt parser.' ,
2020-10-14 03:22:39 -05:00
} ,
2021-08-15 14:20:37 -05:00
{
label : 'pattern' ,
insertText : 'pattern' ,
documentation : 'Extracting labels from the log line using pattern parser. Only available in Loki 2.3+.' ,
} ,
2022-05-26 04:26:20 -05:00
{
label : 'unpack' ,
insertText : 'unpack' ,
detail : 'unpack identifier' ,
documentation :
'Parses a JSON log line, unpacking all embedded labels in the pack stage. A special property "_entry" will also be used to replace the original log line. Only available in Loki 2.2+.' ,
} ,
2020-10-14 03:22:39 -05:00
] ;
export const PIPE_OPERATORS : CompletionItem [ ] = [
{
label : 'unwrap' ,
insertText : 'unwrap' ,
detail : 'unwrap identifier' ,
2022-10-06 09:35:30 -05:00
documentation : 'Take labels and use the values as sample data for metric aggregations.' ,
2020-10-14 03:22:39 -05:00
} ,
{
label : 'label_format' ,
insertText : 'label_format' ,
2022-10-06 09:35:30 -05:00
documentation : 'Use to rename, modify or add labels. For example, | label_format foo=bar .' ,
2020-10-14 03:22:39 -05:00
} ,
{
label : 'line_format' ,
insertText : 'line_format' ,
2022-10-06 09:35:30 -05:00
documentation : 'Rewrites log line content. For example, | line_format "{{.query}} {{.duration}}" .' ,
2020-10-14 03:22:39 -05:00
} ,
] ;
2019-11-15 09:38:25 -06:00
export const RANGE_VEC_FUNCTIONS = [
2020-10-14 03:22:39 -05:00
{
insertText : 'avg_over_time' ,
label : 'avg_over_time' ,
detail : 'avg_over_time(range-vector)' ,
2022-10-06 09:35:30 -05:00
documentation : 'The average of all values in the specified interval.' ,
2020-10-14 03:22:39 -05:00
} ,
{
2022-10-06 09:35:30 -05:00
insertText : 'bytes_over_time' ,
label : 'bytes_over_time' ,
detail : 'bytes_over_time(range-vector)' ,
documentation : 'Counts the amount of bytes used by each log stream for a given range' ,
2020-10-14 03:22:39 -05:00
} ,
{
2022-10-06 09:35:30 -05:00
insertText : 'bytes_rate' ,
label : 'bytes_rate' ,
detail : 'bytes_rate(range-vector)' ,
documentation : 'Calculates the number of bytes per second for each stream.' ,
2020-10-14 03:22:39 -05:00
} ,
2022-05-26 03:23:28 -05:00
{
insertText : 'first_over_time' ,
label : 'first_over_time' ,
detail : 'first_over_time(range-vector)' ,
documentation : 'The first of all values in the specified interval. Only available in Loki 2.3+.' ,
} ,
{
insertText : 'last_over_time' ,
label : 'last_over_time' ,
detail : 'last_over_time(range-vector)' ,
documentation : 'The last of all values in the specified interval. Only available in Loki 2.3+.' ,
} ,
2020-10-14 03:22:39 -05:00
{
insertText : 'sum_over_time' ,
label : 'sum_over_time' ,
detail : 'sum_over_time(range-vector)' ,
2022-10-06 09:35:30 -05:00
documentation : 'The sum of all values in the specified interval.' ,
2020-10-14 03:22:39 -05:00
} ,
2019-11-15 09:38:25 -06:00
{
insertText : 'count_over_time' ,
label : 'count_over_time' ,
detail : 'count_over_time(range-vector)' ,
documentation : 'The count of all values in the specified interval.' ,
} ,
2020-10-14 03:22:39 -05:00
{
2022-10-06 09:35:30 -05:00
insertText : 'max_over_time' ,
label : 'max_over_time' ,
detail : 'max_over_time(range-vector)' ,
documentation : 'The maximum of all values in the specified interval.' ,
2020-10-14 03:22:39 -05:00
} ,
{
2022-10-06 09:35:30 -05:00
insertText : 'min_over_time' ,
label : 'min_over_time' ,
detail : 'min_over_time(range-vector)' ,
documentation : 'The minimum of all values in the specified interval.' ,
2020-10-14 03:22:39 -05:00
} ,
{
insertText : 'quantile_over_time' ,
label : 'quantile_over_time' ,
detail : 'quantile_over_time(scalar, range-vector)' ,
2022-10-06 09:35:30 -05:00
documentation : 'The φ-quantile (0 ≤ φ ≤ 1) of the values in the specified interval.' ,
2020-10-14 03:22:39 -05:00
} ,
2019-11-15 09:38:25 -06:00
{
insertText : 'rate' ,
label : 'rate' ,
detail : 'rate(v range-vector)' ,
2022-01-31 00:57:14 -06:00
documentation : 'Calculates the number of entries per second.' ,
2019-11-15 09:38:25 -06:00
} ,
2022-10-06 09:35:30 -05:00
{
insertText : 'stddev_over_time' ,
label : 'stddev_over_time' ,
detail : 'stddev_over_time(range-vector)' ,
documentation : 'The population standard deviation of the values in the specified interval.' ,
} ,
{
insertText : 'stdvar_over_time' ,
label : 'stdvar_over_time' ,
detail : 'stdvar_over_time(range-vector)' ,
documentation : 'The population standard variance of the values in the specified interval.' ,
} ,
2019-11-15 09:38:25 -06:00
] ;
2023-03-03 03:09:34 -06:00
export const BUILT_IN_FUNCTIONS = [
{
insertText : 'vector' ,
label : 'vector' ,
detail : 'vector(scalar)' ,
documentation : 'Returns the scalar as a vector with no labels.' ,
} ,
] ;
export const FUNCTIONS = [ . . . AGGREGATION_OPERATORS , . . . RANGE_VEC_FUNCTIONS , . . . BUILT_IN_FUNCTIONS ] ;
2021-12-03 07:11:43 -06:00
export const LOKI_KEYWORDS = [ . . . FUNCTIONS , . . . PIPE_OPERATORS , . . . PIPE_PARSERS ] . map ( ( keyword ) = > keyword . label ) ;
2018-11-30 08:13:53 -06:00
2022-01-31 00:57:14 -06:00
export const lokiGrammar : Grammar = {
2018-11-30 08:13:53 -06:00
comment : {
2020-02-12 08:45:36 -06:00
pattern : /#.*/ ,
2018-11-30 08:13:53 -06:00
} ,
2019-11-15 09:38:25 -06:00
'context-aggregation' : {
pattern : /((without|by)\s*)\([^)]*\)/ , // by ()
lookbehind : true ,
inside : {
'label-key' : {
pattern : /[^(),\s][^,)]*[^),\s]*/ ,
alias : 'attr-name' ,
} ,
punctuation : /[()]/ ,
} ,
} ,
2018-11-30 08:13:53 -06:00
'context-labels' : {
2020-10-14 03:22:39 -05:00
pattern : /\{[^}]*(?=}?)/ ,
2020-02-12 08:45:36 -06:00
greedy : true ,
2018-11-30 08:13:53 -06:00
inside : {
2020-02-12 08:45:36 -06:00
comment : {
pattern : /#.*/ ,
} ,
2018-11-30 08:13:53 -06:00
'label-key' : {
2022-02-15 05:02:51 -06:00
pattern : /[a-zA-Z_]\w*(?=\s*(=|!=|=~|!~))/ ,
2018-11-30 08:13:53 -06:00
alias : 'attr-name' ,
2020-02-12 08:45:36 -06:00
greedy : true ,
2018-11-30 08:13:53 -06:00
} ,
'label-value' : {
pattern : /"(?:\\.|[^\\"])*"/ ,
greedy : true ,
alias : 'attr-value' ,
} ,
2018-12-04 07:57:01 -06:00
punctuation : /[{]/ ,
2018-11-30 08:13:53 -06:00
} ,
} ,
2020-10-14 03:22:39 -05:00
'context-pipe' : {
pattern : /\s\|[^=~]\s?\w*/i ,
inside : {
'pipe-operator' : {
pattern : /\|/i ,
alias : 'operator' ,
} ,
'pipe-operations' : {
2021-01-20 00:59:48 -06:00
pattern : new RegExp ( ` ${ [ . . . PIPE_PARSERS , . . . PIPE_OPERATORS ] . map ( ( f ) = > f . label ) . join ( '|' ) } ` , 'i' ) ,
2020-10-14 03:22:39 -05:00
alias : 'keyword' ,
} ,
} ,
} ,
2021-01-20 00:59:48 -06:00
function : new RegExp ( ` \\ b(?: ${ FUNCTIONS . map ( ( f ) = > f . label ) . join ( '|' ) } )(?= \\ s* \\ () ` , 'i' ) ,
2019-11-15 09:38:25 -06:00
'context-range' : [
{
pattern : /\[[^\]]*(?=\])/ , // [1m]
inside : {
'range-duration' : {
pattern : /\b\d+[smhdwy]\b/i ,
alias : 'number' ,
} ,
} ,
} ,
{
pattern : /(offset\s+)\w+/ , // offset 1m
lookbehind : true ,
inside : {
'range-duration' : {
pattern : /\b\d+[smhdwy]\b/i ,
alias : 'number' ,
} ,
} ,
} ,
] ,
2022-03-02 06:43:56 -06:00
quote : {
pattern : /"(?:\\.|[^\\"])*"/ ,
alias : 'string' ,
greedy : true ,
} ,
backticks : {
pattern : /`(?:\\.|[^\\`])*`/ ,
alias : 'string' ,
greedy : true ,
} ,
2019-11-15 09:38:25 -06:00
number : /\b-?\d+((\.\d*)?([eE][+-]?\d+)?)?\b/ ,
2020-10-14 03:22:39 -05:00
operator : /\s?(\|[=~]?|!=?|<(?:=>?|<|>)?|>[>=]?)\s?/i ,
2022-03-02 06:43:56 -06:00
punctuation : /[{}(),.]/ ,
2018-11-30 08:13:53 -06:00
} ;
2022-01-31 00:57:14 -06:00
export default lokiGrammar ;