-
Notifications
You must be signed in to change notification settings - Fork 693
Expand file tree
/
Copy pathconfig.go
More file actions
88 lines (71 loc) · 2.23 KB
/
config.go
File metadata and controls
88 lines (71 loc) · 2.23 KB
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
package config
import (
"fmt"
"github.com/grafana/tempo/pkg/traceql"
)
type FilterPolicy struct {
Include *PolicyMatch `yaml:"include" json:"include,omitempty"`
IncludeAny *PolicyMatch `yaml:"include_any" json:"include_any,omitempty"`
Exclude *PolicyMatch `yaml:"exclude" json:"exclude,omitempty"`
}
type MatchType string
const (
Strict MatchType = "strict"
Regex MatchType = "regex"
)
var supportedIntrinsics = []traceql.Intrinsic{
traceql.IntrinsicKind,
traceql.IntrinsicName,
traceql.IntrinsicStatus,
}
type PolicyMatch struct {
MatchType MatchType `yaml:"match_type" json:"match_type"`
Attributes []MatchPolicyAttribute `yaml:"attributes" json:"attributes"`
}
type MatchPolicyAttribute struct {
Key string `yaml:"key" json:"key"`
Value interface{} `yaml:"value" json:"value"`
}
func ValidateFilterPolicy(policy FilterPolicy) error {
if policy.Include == nil && policy.IncludeAny == nil && policy.Exclude == nil {
return fmt.Errorf("invalid filter policy; policies must have at least an `include`, `includeAny` or `exclude`: %v", policy)
}
if policy.Include != nil {
if err := ValidatePolicyMatch(policy.Include); err != nil {
return fmt.Errorf("invalid include policy: %w", err)
}
}
if policy.IncludeAny != nil {
if err := ValidatePolicyMatch(policy.IncludeAny); err != nil {
return fmt.Errorf("invalid includeAny policy: %w", err)
}
}
if policy.Exclude != nil {
if err := ValidatePolicyMatch(policy.Exclude); err != nil {
return fmt.Errorf("invalid exclude policy: %w", err)
}
}
return nil
}
func ValidatePolicyMatch(match *PolicyMatch) error {
if match.MatchType != Strict && match.MatchType != Regex {
return fmt.Errorf("invalid match type: %v", match.MatchType)
}
for _, attr := range match.Attributes {
if attr.Key == "" {
return fmt.Errorf("invalid attribute: %v", attr)
}
a, err := traceql.ParseIdentifier(attr.Key)
if err != nil {
return err
}
if a.Scope == traceql.AttributeScopeNone {
switch a.Intrinsic {
case traceql.IntrinsicKind, traceql.IntrinsicName, traceql.IntrinsicStatus: // currently supported
default:
return fmt.Errorf("unsupported intrinsic: %s; supported intrinsics: %q", a.Intrinsic, supportedIntrinsics)
}
}
}
return nil
}