-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
parser.go
188 lines (159 loc) · 5.51 KB
/
parser.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package helper // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
import (
"context"
"fmt"
"go.opentelemetry.io/collector/component"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/errors"
)
// NewParserConfig creates a new parser config with default values
func NewParserConfig(operatorID, operatorType string) ParserConfig {
return ParserConfig{
TransformerConfig: NewTransformerConfig(operatorID, operatorType),
ParseFrom: entry.NewBodyField(),
ParseTo: entry.RootableField{Field: entry.NewAttributeField()},
}
}
// ParserConfig provides the basic implementation of a parser config.
type ParserConfig struct {
TransformerConfig `mapstructure:",squash"`
ParseFrom entry.Field `mapstructure:"parse_from"`
ParseTo entry.RootableField `mapstructure:"parse_to"`
BodyField *entry.Field `mapstructure:"body"`
TimeParser *TimeParser `mapstructure:"timestamp,omitempty"`
SeverityConfig *SeverityConfig `mapstructure:"severity,omitempty"`
TraceParser *TraceParser `mapstructure:"trace,omitempty"`
ScopeNameParser *ScopeNameParser `mapstructure:"scope_name,omitempty"`
}
// Build will build a parser operator.
func (c ParserConfig) Build(set component.TelemetrySettings) (ParserOperator, error) {
transformerOperator, err := c.TransformerConfig.Build(set)
if err != nil {
return ParserOperator{}, err
}
if c.BodyField != nil && c.ParseTo.String() == entry.NewBodyField().String() {
return ParserOperator{}, fmt.Errorf("`parse_to: body` not allowed when `body` is configured")
}
parserOperator := ParserOperator{
TransformerOperator: transformerOperator,
ParseFrom: c.ParseFrom,
ParseTo: c.ParseTo.Field,
BodyField: c.BodyField,
}
if c.TimeParser != nil {
if err := c.TimeParser.Validate(); err != nil {
return ParserOperator{}, err
}
parserOperator.TimeParser = c.TimeParser
}
if c.SeverityConfig != nil {
severityParser, err := c.SeverityConfig.Build(set)
if err != nil {
return ParserOperator{}, err
}
parserOperator.SeverityParser = &severityParser
}
if c.TraceParser != nil {
if err := c.TraceParser.Validate(); err != nil {
return ParserOperator{}, err
}
parserOperator.TraceParser = c.TraceParser
}
if c.ScopeNameParser != nil {
parserOperator.ScopeNameParser = c.ScopeNameParser
}
return parserOperator, nil
}
// ParserOperator provides a basic implementation of a parser operator.
type ParserOperator struct {
TransformerOperator
ParseFrom entry.Field
ParseTo entry.Field
BodyField *entry.Field
TimeParser *TimeParser
SeverityParser *SeverityParser
TraceParser *TraceParser
ScopeNameParser *ScopeNameParser
}
// ProcessWith will run ParseWith on the entry, then forward the entry on to the next operators.
func (p *ParserOperator) ProcessWith(ctx context.Context, entry *entry.Entry, parse ParseFunction) error {
return p.ProcessWithCallback(ctx, entry, parse, nil)
}
func (p *ParserOperator) ProcessWithCallback(ctx context.Context, entry *entry.Entry, parse ParseFunction, cb func(*entry.Entry) error) error {
// Short circuit if the "if" condition does not match
skip, err := p.Skip(ctx, entry)
if err != nil {
return p.HandleEntryError(ctx, entry, err)
}
if skip {
return p.Write(ctx, entry)
}
if err = p.ParseWith(ctx, entry, parse); err != nil {
return err
}
if cb != nil {
err = cb(entry)
if err != nil {
return err
}
}
return p.Write(ctx, entry)
}
// ParseWith will process an entry's field with a parser function.
func (p *ParserOperator) ParseWith(ctx context.Context, entry *entry.Entry, parse ParseFunction) error {
value, ok := entry.Get(p.ParseFrom)
if !ok {
err := errors.NewError(
"Entry is missing the expected parse_from field.",
"Ensure that all incoming entries contain the parse_from field.",
"parse_from", p.ParseFrom.String(),
)
return p.HandleEntryError(ctx, entry, err)
}
newValue, err := parse(value)
if err != nil {
return p.HandleEntryError(ctx, entry, err)
}
if err := entry.Set(p.ParseTo, newValue); err != nil {
return p.HandleEntryError(ctx, entry, errors.Wrap(err, "set parse_to"))
}
if p.BodyField != nil {
if body, ok := p.BodyField.Get(entry); ok {
entry.Body = body
}
}
var timeParseErr error
if p.TimeParser != nil {
timeParseErr = p.TimeParser.Parse(entry)
}
var severityParseErr error
if p.SeverityParser != nil {
severityParseErr = p.SeverityParser.Parse(entry)
}
var traceParseErr error
if p.TraceParser != nil {
traceParseErr = p.TraceParser.Parse(entry)
}
var scopeNameParserErr error
if p.ScopeNameParser != nil {
scopeNameParserErr = p.ScopeNameParser.Parse(entry)
}
// Handle parsing errors after attempting to parse all
if timeParseErr != nil {
return p.HandleEntryError(ctx, entry, errors.Wrap(timeParseErr, "time parser"))
}
if severityParseErr != nil {
return p.HandleEntryError(ctx, entry, errors.Wrap(severityParseErr, "severity parser"))
}
if traceParseErr != nil {
return p.HandleEntryError(ctx, entry, errors.Wrap(traceParseErr, "trace parser"))
}
if scopeNameParserErr != nil {
return p.HandleEntryError(ctx, entry, errors.Wrap(scopeNameParserErr, "scope_name parser"))
}
return nil
}
// ParseFunction is function that parses a raw value.
type ParseFunction = func(any) (any, error)