-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
515 lines (470 loc) · 15.6 KB
/
query.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright 2022 Google LLC
//
// 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.
package logging
import (
"fmt"
"io"
"reflect"
"strconv"
"strings"
"time"
"github.com/TiagoMalhadas/xcweaver/runtime/protos"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
"github.com/google/cel-go/common/operators"
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
// Query is a filter for log entries.
//
// # Syntax
//
// Queries are written using a subset of the CEL language [1]. Thus, every
// syntactically valid query is also a syntactically valid CEL program.
// Specifically, a query is a CEL program over the following fields:
//
// - app: string
// - version: string
// - full_version: string
// - component: string
// - full_component: string
// - node: string
// - full_node: string
// - time: timestamp
// - level: string
// - source: string
// - msg: string
// - attrs: map[string]string
//
// A query is restricted to:
//
// - boolean algebra (!, &&, ||),
// - equalities and inequalities (==, !=, <, <=, >, >=),
// - the string operations "contains" and "matches",
// - map indexing (attrs["foo"]) and membership ("foo" in attrs), and
// - constant strings, timestamps, and ints.
//
// All equalities and inequalities must look like `app == "todo"` or
// `attrs["foo"] == "bar"`; i.e. a field or attribute on the left and a constant
// on the right.
//
// # Semantics
//
// Queries have the same semantics as CEL programs except for one small
// exception. An attribute expression like `attrs["foo"]` has an implicit
// membership test `"foo" in attrs`. For example, the query
//
// attrs["foo"] == "bar"
//
// is evaluated like the CEL program
//
// "foo" in attrs && attrs["foo"] == "bar"
//
// TODO(mwhittaker): Expand the set of valid queries. For example, we can allow
// more constant expressions on the right hand side of a comparison. We can
// also allow fields on the right and constants on the left.
//
// [1]: https://opensource.google/projects/cel
type Query = string
// There are three phases in the lifecycle of query: First, the parse function
// parses and type checks a string-vaued query into a *cel.Ast. Second, the
// compile function compiles a *cel.Ast into an executable *cel.Program. Third,
// the matches function matches a compiled program against a log entry.
//
// Note that we use *cel.Ast as both an AST and as a compilation target. That
// is, we parse a query into a *cel.Ast, but executing this AST as a CEL
// program would not have the correct semantics. We instead have to rewrite
// this AST into a different AST with the correct semantics. This is much
// clearer for the GKE deployer where we parse a query into a *cel.Ast and then
// transpile the AST into a Google Cloud Logging query. The confusing
// difference here is that we transpile from CEL to CEL.
// env returns the cel.Env needed to compile a query.
//
// TODO(mwhittaker): Only make this environment once.
func env() (*cel.Env, error) {
return cel.NewEnv(cel.Declarations(
decls.NewVar("app", decls.String),
decls.NewVar("version", decls.String),
decls.NewVar("full_version", decls.String),
decls.NewVar("component", decls.String),
decls.NewVar("full_component", decls.String),
decls.NewVar("node", decls.String),
decls.NewVar("full_node", decls.String),
decls.NewVar("time", decls.Timestamp),
decls.NewVar("level", decls.String),
decls.NewVar("source", decls.String),
decls.NewVar("msg", decls.String),
decls.NewVar("attrs", decls.NewMapType(decls.String, decls.String)),
))
}
// Parse parses and type-checks a query.
func Parse(query Query) (*cel.Ast, error) {
_, ast, err := parse(query)
return ast, err
}
// parse parses and type-checks a query.
func parse(query Query) (*cel.Env, *cel.Ast, error) {
// Build the environment.
env, err := env()
if err != nil {
return nil, nil, fmt.Errorf("Parse(%s) environment error: %w", query, err)
}
// Parse and type-check the query.
ast, issues := env.Compile(query)
if issues != nil && issues.Err() != nil {
return nil, nil, fmt.Errorf("Parse(%s) compilation error: %w", query, issues.Err())
}
if ast.OutputType() != cel.BoolType {
return nil, nil, fmt.Errorf("Parse(%s) type error: got %v, want %v", query, ast.OutputType(), "bool")
}
// Restrict the query.
if err := restrict(ast.Expr()); err != nil {
return nil, nil, fmt.Errorf("Parse(%s) restriction error: %w", query, err)
}
return env, ast, nil
}
// restrict recursively walks an expression, checking to see if it conforms to
// the restricted subset of CEL used to write queries. If the expression does
// conform the subset, then nil is returned. Otherwise, an error is returned
// that explains why the expression doesn't conform.
//
// This function and related functions borrow heavily from [1].
//
// [1]: https://github.com/google/cel-go/blob/8e5d9877f0ab106269dee64e5bf10c5315281830/parser/unparser.go
func restrict(e *exprpb.Expr) error {
// TODO(mwhittaker): Handle macros [1].
//
// [1]: https://github.com/google/cel-go/blob/8e5d9877f0ab106269dee64e5bf10c5315281830/parser/unparser.go#L58-L61
switch e.ExprKind.(type) {
case *exprpb.Expr_CallExpr:
// Note that CEL represents operators like || and ! as calls.
return restrictCall(e.GetCallExpr())
default:
return fmt.Errorf("unsupported expression: %v", e)
}
}
func restrictCall(e *exprpb.Expr_Call) error {
switch e.GetFunction() {
// !
case operators.LogicalNot:
return restrict(e.Args[0])
// &&, ||
case operators.LogicalAnd, operators.LogicalOr:
for i := 0; i < 2; i++ {
if err := restrict(e.Args[i]); err != nil {
return err
}
}
return nil
// ==, !=, <, <=, >, >=
case operators.Equals, operators.NotEquals,
operators.Less, operators.LessEquals,
operators.Greater, operators.GreaterEquals:
if err := restrictField(e.Args[0]); err != nil {
return err
}
return restrictLiteral(e.Args[1])
// contains, matches
case "contains", "matches":
if err := restrictField(e.Target); err != nil {
return err
}
return restrictLiteral(e.Args[0])
// in
case operators.In:
if err := restrictLiteral(e.Args[0]); err != nil {
return err
}
return restrictField(e.Args[1])
default:
return fmt.Errorf("unsupported call: %v", e)
}
}
// restrictField checks whether the provided expression is a log entry field,
// either an identifier like `line` or an attribute expression like `attrs["foo"]`.
func restrictField(e *exprpb.Expr) error {
switch t := e.ExprKind.(type) {
case *exprpb.Expr_IdentExpr:
return nil
case *exprpb.Expr_CallExpr:
fn := t.CallExpr.Function
if fn == operators.Index { // Map [] operator.
if tg := t.CallExpr.Args[0].GetIdentExpr(); tg == nil || tg.GetName() != "attrs" {
return fmt.Errorf(`unsupported map target, want "attrs", got %v`, t.CallExpr.Args[0])
}
if i := t.CallExpr.Args[1].GetConstExpr(); i == nil || i.GetStringValue() == "" {
return fmt.Errorf("unsupported map index, want a non-empty string constant, got %v", t.CallExpr.Args[1])
}
return nil
}
return fmt.Errorf("unsupported function %s: %v", fn, t)
default:
return fmt.Errorf("unsupported field: %v", e)
}
}
// restrictLiteral checks whether the provided expression is a literal (e.g.,
// 42, "foo").
func restrictLiteral(e *exprpb.Expr) error {
switch e.ExprKind.(type) {
case *exprpb.Expr_ConstExpr:
return nil
case *exprpb.Expr_CallExpr:
call := e.GetCallExpr()
if f := call.Function; f != "timestamp" {
return fmt.Errorf("unsupported literal: %v", e)
}
return restrictLiteral(call.Args[0])
default:
return fmt.Errorf("unsupported literal: %v", e)
}
}
// rewrite rewrites an expression parsed from a query into a CEL expression
// with the same semantics as the query. Specifically, binary expressions over
// attributes, like `attrs["foo"] == "bar"`, are translated to include an implicit
// membership test like `"foo" in attrs && attrs["foo"] == "bar"`.
func rewrite(e *exprpb.Expr) (*exprpb.Expr, error) {
e = proto.Clone(e).(*exprpb.Expr)
return rewriteExpr(e)
}
func rewriteExpr(e *exprpb.Expr) (*exprpb.Expr, error) {
switch e.ExprKind.(type) {
case *exprpb.Expr_CallExpr:
// Note that CEL represents operators like || and ! as calls.
call, err := rewriteCall(e.GetCallExpr())
if err != nil {
return nil, err
}
return callexpr(call), nil
default:
return nil, fmt.Errorf("unsupported expression: %v", e)
}
}
func rewriteCall(e *exprpb.Expr_Call) (*exprpb.Expr_Call, error) {
switch e.GetFunction() {
// !
case operators.LogicalNot:
sub, err := rewriteExpr(e.Args[0])
e.Args[0] = sub
return e, err
// &&, ||
case operators.LogicalAnd, operators.LogicalOr:
for i := 0; i < 2; i++ {
sub, err := rewriteExpr(e.Args[i])
if err != nil {
return nil, err
}
e.Args[i] = sub
}
return e, nil
// ==, !=, <, <=, >, >=
case operators.Equals, operators.NotEquals,
operators.Less, operators.LessEquals,
operators.Greater, operators.GreaterEquals:
attrs, attr, ok := explodeIndex(e.Args[0])
if !ok {
// There is no attrs["foo"] expression, so we don't have to
// rewrite the expression.
return e, nil
}
// Inject a `"foo" in attrs` check.
contains := callexpr(binop(attr, operators.In, attrs))
return binop(contains, operators.LogicalAnd, callexpr(e)), nil
// contains, matches
case "contains", "matches":
attrs, attr, ok := explodeIndex(e.Target)
if !ok {
return e, nil
}
contains := callexpr(binop(attr, operators.In, attrs))
return binop(contains, operators.LogicalAnd, callexpr(e)), nil
// in
case operators.In:
return e, nil
default:
return nil, fmt.Errorf("unsupported call: %v", e)
}
}
// callexpr wraps an Expr_Call into an Expr.
func callexpr(call *exprpb.Expr_Call) *exprpb.Expr {
return &exprpb.Expr{ExprKind: &exprpb.Expr_CallExpr{CallExpr: call}}
}
// binop returns the ExprCall with the provided operator and operands.
func binop(lhs *exprpb.Expr, op string, rhs *exprpb.Expr) *exprpb.Expr_Call {
return &exprpb.Expr_Call{Function: op, Args: []*exprpb.Expr{lhs, rhs}}
}
// explodeIndex deconstructs an index expression. If the provided expression e
// has the form m[k], then it returns m, k, true. Otherwise, it returns nil,
// nil, false. For example, `attrs["foo"]` returns `attrs, "foo", true`.
func explodeIndex(e *exprpb.Expr) (*exprpb.Expr, *exprpb.Expr, bool) {
if _, ok := e.ExprKind.(*exprpb.Expr_CallExpr); ok {
call := e.GetCallExpr()
if call.GetFunction() == operators.Index {
return call.Args[0], call.Args[1], true
}
}
return nil, nil, false
}
// format returns a string representation of the provided expression. The
// returned string is not intended to be read by humans. It's overly
// parenthesized and very ugly.
func format(e *exprpb.Expr) (string, error) {
var b strings.Builder
err := formatExpr(&b, e)
return b.String(), err
}
func formatExpr(w io.Writer, e *exprpb.Expr) error {
fmt.Fprint(w, "(")
defer fmt.Fprint(w, ")")
switch e.ExprKind.(type) {
case *exprpb.Expr_IdentExpr:
fmt.Fprint(w, e.GetIdentExpr().Name)
return nil
case *exprpb.Expr_ConstExpr:
return formatConst(w, e.GetConstExpr())
case *exprpb.Expr_CallExpr:
// Note that CEL represents operators like || and ! as calls.
return formatCall(w, e.GetCallExpr())
default:
return fmt.Errorf("unsupported expression: %v", e)
}
}
func formatCall(w io.Writer, e *exprpb.Expr_Call) error {
fmt.Fprint(w, "(")
defer fmt.Fprint(w, ")")
ops := map[string]string{
operators.LogicalNot: "!",
"timestamp": "timestamp",
operators.LogicalAnd: "&&",
operators.LogicalOr: "||",
operators.Equals: "==",
operators.NotEquals: "!=",
operators.Less: "<",
operators.LessEquals: "<=",
operators.Greater: ">",
operators.GreaterEquals: ">=",
operators.In: "in",
}
switch f := e.GetFunction(); f {
// !, timestamp
case operators.LogicalNot, "timestamp":
fmt.Fprint(w, ops[f])
return formatExpr(w, e.Args[0])
// &&, ||, ==, !=, <, <=, >, >=, in
case operators.LogicalAnd, operators.LogicalOr,
operators.Equals, operators.NotEquals,
operators.Less, operators.LessEquals,
operators.Greater, operators.GreaterEquals,
operators.In:
if err := formatExpr(w, e.Args[0]); err != nil {
return err
}
fmt.Fprintf(w, " %s ", ops[f])
return formatExpr(w, e.Args[1])
// []
case operators.Index:
if err := formatExpr(w, e.Args[0]); err != nil {
return err
}
fmt.Fprintf(w, "[")
err := formatExpr(w, e.Args[1])
fmt.Fprintf(w, "]")
return err
// contains, matches
case "contains", "matches":
if err := formatExpr(w, e.Target); err != nil {
return err
}
fmt.Fprintf(w, ".%s", f)
return formatExpr(w, e.Args[0])
default:
return fmt.Errorf("unsupported call: %v", e)
}
}
// formatConst formats the provided const into w.
func formatConst(w io.Writer, c *exprpb.Constant) error {
// This implementation was borrowed from [1].
//
// [1]: https://github.com/google/cel-go/blob/v0.12.5/parser/unparser.go#L250
switch c.GetConstantKind().(type) {
case *exprpb.Constant_Int64Value:
fmt.Fprint(w, strconv.FormatInt(c.GetInt64Value(), 10))
return nil
case *exprpb.Constant_StringValue:
fmt.Fprint(w, strconv.Quote(c.GetStringValue()))
return nil
default:
return fmt.Errorf("unsupported constant: %v", c)
}
}
// compile compiles a query into a cel.Program.
func compile(env *cel.Env, ast *cel.Ast) (cel.Program, error) {
// CEL does not provide any helper functions to rewrite ASTs. Instead, we
// have to massage the AST, format it, and then parse it again.
e, err := rewrite(ast.Expr())
if err != nil {
return nil, fmt.Errorf("compile rewrite: %w", err)
}
q, err := format(e)
if err != nil {
return nil, fmt.Errorf("compile format: %w", err)
}
ast, issues := env.Compile(q)
if issues != nil && issues.Err() != nil {
return nil, fmt.Errorf("compile: %w", issues.Err())
}
prog, err := env.Program(ast, cel.EvalOptions(cel.OptTrackState))
if err != nil {
return nil, fmt.Errorf("compile: %w", err)
}
return prog, nil
}
// matches returns whether the provided compiled query matches the provided log
// entry.
func matches(prog cel.Program, entry *protos.LogEntry) (bool, error) {
if entry == nil {
return false, nil
}
attrs := make(map[string]string, len(entry.Attrs))
for i := 0; i+1 < len(entry.Attrs); i += 2 {
attrs[entry.Attrs[i]] = entry.Attrs[i+1]
}
out, _, err := prog.Eval(map[string]interface{}{
"app": entry.App,
"version": Shorten(entry.Version),
"full_version": entry.Version,
"component": ShortenComponent(entry.Component),
"full_component": entry.Component,
"node": Shorten(entry.Node),
"full_node": entry.Node,
"time": timestamppb.New(time.UnixMicro(entry.TimeMicros)),
"level": entry.Level,
"source": fmt.Sprintf("%s:%d", entry.File, entry.Line),
"msg": entry.Msg,
"attrs": attrs,
})
if err != nil {
if out != nil {
// Successful eval to an error result: we interpret this as a non-match.
return false, nil
}
// Unsuccessful eval: that's an error.
return false, err
}
b, err := out.ConvertToNative(reflect.TypeOf(true))
if err != nil {
return false, err
}
return b.(bool), nil
}