-
Notifications
You must be signed in to change notification settings - Fork 108
/
routes.go
172 lines (146 loc) · 4.81 KB
/
routes.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
// Package transform provides some intermediate nodes that might filter/process/transform the events
package transform
import (
"log/slog"
"github.com/mariomac/pipes/pipe"
"github.com/grafana/beyla/pkg/internal/request"
"github.com/grafana/beyla/pkg/internal/transform/route"
)
// UnmatchType defines which actions to do when a route pattern is not recognized
type UnmatchType string
const (
// UnmatchUnset leaves the Route field as empty
UnmatchUnset = UnmatchType("unset")
// UnmatchPath sets the Route field to the same values as the Path
UnmatchPath = UnmatchType("path")
// UnmatchWildcard sets the route field to a generic asterisk symbol
UnmatchWildcard = UnmatchType("wildcard")
// UnmatchHeuristic detects the route field using a heuristic
UnmatchHeuristic = UnmatchType("heuristic")
UnmatchDefault = UnmatchWildcard
)
type IgnoreMode string
const (
// IgnoreMetrics prevents sending metric events for ignored patterns
IgnoreMetrics = IgnoreMode("metrics")
// IgnoreTraces prevents sending trace events for ignored patterns
IgnoreTraces = IgnoreMode("traces")
// IgnoreAll prevents sending both metrics and traces for ignored patterns
IgnoreAll = IgnoreMode("all")
IgnoreDefault = IgnoreAll
)
const wildCard = "/**"
// RoutesConfig allows grouping URLs sharing a given pattern.
type RoutesConfig struct {
// Unmatch specifies what to do when a route pattern is not matched
Unmatch UnmatchType `yaml:"unmatched"`
// Patterns of the paths that will match to a route
Patterns []string `yaml:"patterns"`
IgnorePatterns []string `yaml:"ignored_patterns"`
IgnoredEvents IgnoreMode `yaml:"ignore_mode"`
}
func RoutesProvider(rc *RoutesConfig) pipe.MiddleProvider[[]request.Span, []request.Span] {
return (&routerNode{config: rc}).provideRoutes
}
type routerNode struct {
config *RoutesConfig
}
func (rn *routerNode) provideRoutes() (pipe.MiddleFunc[[]request.Span, []request.Span], error) {
rc := rn.config
if rc == nil {
// if no configuration is provided, we just bypass the node
return pipe.Bypass[[]request.Span](), nil
}
// set default value for Unmatch action
unmatchAction, err := chooseUnmatchPolicy(rc)
if err != nil {
return nil, err
}
matcher := route.NewMatcher(rc.Patterns)
discarder := route.NewMatcher(rc.IgnorePatterns)
routesEnabled := len(rc.Patterns) > 0
ignoreEnabled := len(rc.IgnorePatterns) > 0
ignoreMode := rc.IgnoredEvents
if ignoreMode == "" {
ignoreMode = IgnoreDefault
}
return func(in <-chan []request.Span, out chan<- []request.Span) {
for spans := range in {
for i := range spans {
s := &spans[i]
if ignoreEnabled {
if discarder.Find(s.Path) != "" {
if ignoreMode == IgnoreAll {
s.SetIgnoreMetrics()
s.SetIgnoreTraces()
}
// we can't discard it here, ignoring is selective (metrics | traces)
setSpanIgnoreMode(ignoreMode, s)
}
}
if routesEnabled {
s.Route = matcher.Find(s.Path)
}
unmatchAction(s)
}
out <- spans
}
}, nil
}
func chooseUnmatchPolicy(rc *RoutesConfig) (func(span *request.Span), error) {
var unmatchAction func(span *request.Span)
switch rc.Unmatch {
case UnmatchWildcard, "":
unmatchAction = setUnmatchToWildcard
if len(rc.Patterns) == 0 {
slog.With("component", "RoutesProvider").
Warn("No route match patterns configured. " +
"Without route definitions Beyla will not be able to generate a low cardinality " +
"route for trace span names. For optimal experience, please define your application " +
"HTTP route patterns or enable the route 'heuristic' mode. " +
"For more information please see the documentation at: " +
"https://grafana.com/docs/beyla/latest/configure/options/#routes-decorator. " +
"If your application is only using gRPC you can ignore this warning.")
}
case UnmatchUnset:
unmatchAction = leaveUnmatchEmpty
case UnmatchPath:
unmatchAction = setUnmatchToPath
case UnmatchHeuristic:
err := route.InitAutoClassifier()
if err != nil {
return nil, err
}
unmatchAction = classifyFromPath
default:
slog.With("component", "RoutesProvider").
Warn("invalid 'unmatch' value in configuration, defaulting to '"+string(UnmatchDefault)+"'",
"value", rc.Unmatch)
unmatchAction = setUnmatchToWildcard
}
return unmatchAction, nil
}
func leaveUnmatchEmpty(_ *request.Span) {}
func setUnmatchToWildcard(str *request.Span) {
if str.Route == "" {
str.Route = wildCard
}
}
func setUnmatchToPath(str *request.Span) {
if str.Route == "" {
str.Route = str.Path
}
}
func classifyFromPath(s *request.Span) {
if s.Route == "" && (s.Type == request.EventTypeHTTP || s.Type == request.EventTypeHTTPClient) {
s.Route = route.ClusterPath(s.Path)
}
}
func setSpanIgnoreMode(mode IgnoreMode, s *request.Span) {
switch mode {
case IgnoreMetrics:
s.SetIgnoreMetrics()
case IgnoreTraces:
s.SetIgnoreTraces()
}
}