summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/cmetrics/src/cmt_decode_prometheus.y
blob: d4396b9e208e381f1ebe0844aee3a4effa24e5db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
%define api.pure true
%name-prefix "cmt_decode_prometheus_"
%define parse.error verbose

%param {void *yyscanner}
%param {struct cmt_decode_prometheus_context *context}

%{
// we inline cmt_decode_prometheus.c which contains all the actions to avoid
// having to export a bunch of symbols that are only used by the generated
// parser code
#include "cmt_decode_prometheus.c"
%}

%union {
    cfl_sds_t str;
    char numstr[64];
    int integer;
}

%token '=' '{' '}' ','
%token <str> IDENTIFIER QUOTED HELP TYPE METRIC_DOC
%token COUNTER GAUGE SUMMARY UNTYPED HISTOGRAM
%token START_HEADER START_LABELS START_SAMPLES
%token <numstr> NUMSTR INFNAN

%type <integer> metric_type
%type <numstr> value

%destructor {
    cfl_sds_destroy($$);
} <str>

%start start;

%%

start:
    START_HEADER header
  | START_LABELS labels
  | START_SAMPLES samples
  | metrics {
    if (finish_metric(context, true, NULL)) {
        YYABORT;
    }
  }
;

metrics:
    metrics metric
  | metric
;

metric:
    header samples
  | samples
  | header
;

header:
    help type
  | help
  | type help
  | type
;

help:
    HELP METRIC_DOC {
        if (parse_metric_name(context, $1)) {
            YYABORT;
        }
        context->metric.docstring = $2;
    }
;

type:
    TYPE metric_type {
        if (parse_metric_name(context, $1)) {
            YYABORT;
        }
        context->metric.type = $2;
    }
;

metric_type:
    COUNTER { $$ = COUNTER; }
  | GAUGE { $$ = GAUGE; }
  | SUMMARY { $$ = SUMMARY; }
  | UNTYPED { $$ = UNTYPED; }
  | HISTOGRAM { $$ = HISTOGRAM; }
;

samples:
    samples sample
  | sample
;

sample:
    IDENTIFIER { 
        if (parse_metric_name(context, $1)) {
            YYABORT;
        }
        $1 = NULL;
        if (sample_start(context)) {
            YYABORT;
        }
    } sample_data
;

sample_data:
    '{' '}' values
  | '{' labels '}' values
  | values
;

labels:
    labellist ','
  | labellist
;

labellist:
    labellist ',' label
  | label
;

label:
    IDENTIFIER '=' QUOTED {
        if (parse_label(context, $1, $3)) {
            YYABORT;
        }
    }
;

values:
    value value {
        if (parse_sample(context, $1, $2)) {
            YYABORT;
        }
    }
  | value {
        if (parse_sample(context, $1, "")) {
            YYABORT;
        }
    }
;

value:
    NUMSTR | INFNAN
;

%%