-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathlogs.go
More file actions
191 lines (179 loc) · 5.05 KB
/
Copy pathlogs.go
File metadata and controls
191 lines (179 loc) · 5.05 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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package routingconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/routingconnector"
import (
"context"
"errors"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/plog"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/connector/routingconnector/internal/plogutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottllog"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlotelcol"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlresource"
)
type logsConnector struct {
component.StartFunc
component.ShutdownFunc
logger *zap.Logger
config *Config
router *router[consumer.Logs]
}
func newLogsConnector(
set connector.Settings,
config component.Config,
logs consumer.Logs,
) (*logsConnector, error) {
cfg := config.(*Config)
lr, ok := logs.(connector.LogsRouterAndConsumer)
if !ok {
return nil, errUnexpectedConsumer
}
r, err := newRouter(
cfg.Table,
cfg.DefaultPipelines,
lr.Consumer,
set.TelemetrySettings)
if err != nil {
return nil, err
}
return &logsConnector{
logger: set.Logger,
config: cfg,
router: r,
}, nil
}
func (*logsConnector) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: true}
}
func (c *logsConnector) ConsumeLogs(ctx context.Context, ld plog.Logs) error {
groups := make(map[consumer.Logs]plog.Logs)
matched := plog.NewLogs()
for i := 0; i < len(c.router.routeSlice) && ld.ResourceLogs().Len() > 0; i++ {
var errs error
route := c.router.routeSlice[i]
switch route.statementContext {
case "request":
if route.requestCondition.matchRequest(ctx) {
switch route.action {
case Copy:
ld.CopyTo(matched)
default:
// all logs are routed
ld.MoveTo(matched)
}
}
case "otelcol":
otx := ottlotelcol.NewTransformContextPtr()
_, isMatch, err := route.otelcolStatement.Execute(ctx, otx)
otx.Close()
if err != nil {
errs = errors.Join(errs, err)
} else if isMatch {
switch route.action {
case Copy:
ld.CopyTo(matched)
default:
ld.MoveTo(matched)
}
}
case "", "resource":
switch route.action {
case Copy:
plogutil.CopyResourcesIf(ld, matched,
func(rl plog.ResourceLogs) bool {
rtx := ottlresource.NewTransformContextPtr(rl.Resource(), rl)
defer rtx.Close()
_, isMatch, err := route.resourceStatement.Execute(ctx, rtx)
// If error during statement evaluation consider it as not a match.
if err != nil {
errs = errors.Join(errs, err)
return false
}
return isMatch
},
)
default:
plogutil.MoveResourcesIf(ld, matched,
func(rl plog.ResourceLogs) bool {
rtx := ottlresource.NewTransformContextPtr(rl.Resource(), rl)
defer rtx.Close()
_, isMatch, err := route.resourceStatement.Execute(ctx, rtx)
// If error during statement evaluation consider it as not a match.
if err != nil {
errs = errors.Join(errs, err)
return false
}
return isMatch
},
)
}
case "log":
switch route.action {
case Copy:
plogutil.CopyRecordsWithContextIf(ld, matched,
func(rl plog.ResourceLogs, sl plog.ScopeLogs, lr plog.LogRecord) bool {
ltx := ottllog.NewTransformContextPtr(rl, sl, lr)
defer ltx.Close()
_, isMatch, err := route.logStatement.Execute(ctx, ltx)
// If error during statement evaluation consider it as not a match.
if err != nil {
errs = errors.Join(errs, err)
return false
}
return isMatch
},
)
default:
plogutil.MoveRecordsWithContextIf(ld, matched,
func(rl plog.ResourceLogs, sl plog.ScopeLogs, lr plog.LogRecord) bool {
ltx := ottllog.NewTransformContextPtr(rl, sl, lr)
defer ltx.Close()
_, isMatch, err := route.logStatement.Execute(ctx, ltx)
// If error during statement evaluation consider it as not a match.
if err != nil {
errs = errors.Join(errs, err)
return false
}
return isMatch
},
)
}
}
if errs != nil && c.config.ErrorMode == ottl.PropagateError {
return errs
}
groupAllLogs(groups, route.consumer, matched)
}
// anything left wasn't matched by any route. Send to default consumer
groupAllLogs(groups, c.router.defaultConsumer, ld)
var errs error
for consumer, group := range groups {
err := consumer.ConsumeLogs(ctx, group)
if err != nil {
errs = errors.Join(errs, err)
}
}
return errs
}
func groupAllLogs(
groups map[consumer.Logs]plog.Logs,
cons consumer.Logs,
logs plog.Logs,
) {
if cons == nil {
return
}
if logs.ResourceLogs().Len() == 0 {
return
}
group, ok := groups[cons]
if !ok {
group = plog.NewLogs()
groups[cons] = group
}
logs.ResourceLogs().MoveAndAppendTo(group.ResourceLogs())
}