This repository has been archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
csv.go
200 lines (168 loc) · 5.89 KB
/
csv.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
// Copyright The OpenTelemetry Authors
//
// 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 csv
import (
"context"
csvparser "encoding/csv"
"errors"
"fmt"
"io"
"strings"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-log-collection/entry"
"github.com/open-telemetry/opentelemetry-log-collection/operator"
"github.com/open-telemetry/opentelemetry-log-collection/operator/helper"
)
func init() {
operator.Register("csv_parser", func() operator.Builder { return NewCSVParserConfig("") })
}
// NewCSVParserConfig creates a new csv parser config with default values
func NewCSVParserConfig(operatorID string) *CSVParserConfig {
return &CSVParserConfig{
ParserConfig: helper.NewParserConfig(operatorID, "csv_parser"),
}
}
// CSVParserConfig is the configuration of a csv parser operator.
type CSVParserConfig struct {
helper.ParserConfig `yaml:",inline"`
Header string `json:"header" yaml:"header"`
HeaderAttribute string `json:"header_attribute" yaml:"header_attribute"`
FieldDelimiter string `json:"delimiter,omitempty" yaml:"delimiter,omitempty"`
LazyQuotes bool `json:"lazy_quotes,omitempty" yaml:"lazy_quotes,omitempty"`
}
// Build will build a csv parser operator.
func (c CSVParserConfig) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
parserOperator, err := c.ParserConfig.Build(logger)
if err != nil {
return nil, err
}
if c.FieldDelimiter == "" {
c.FieldDelimiter = ","
}
fieldDelimiter := []rune(c.FieldDelimiter)[0]
if len([]rune(c.FieldDelimiter)) != 1 {
return nil, fmt.Errorf("invalid 'delimiter': '%s'", c.FieldDelimiter)
}
headers := make([]string, 0)
switch {
case c.Header == "" && c.HeaderAttribute == "":
return nil, errors.New("missing required field 'header' or 'header_attribute'")
case c.Header != "" && c.HeaderAttribute != "":
return nil, errors.New("only one header parameter can be set: 'header' or 'header_attribute'")
case c.Header != "" && !strings.Contains(c.Header, c.FieldDelimiter):
return nil, errors.New("missing field delimiter in header")
case c.Header != "":
headers = strings.Split(c.Header, c.FieldDelimiter)
}
return &CSVParser{
ParserOperator: parserOperator,
header: headers,
headerAttribute: c.HeaderAttribute,
fieldDelimiter: fieldDelimiter,
lazyQuotes: c.LazyQuotes,
parse: generateParseFunc(headers, fieldDelimiter, c.LazyQuotes),
}, nil
}
// CSVParser is an operator that parses csv in an entry.
type CSVParser struct {
helper.ParserOperator
fieldDelimiter rune
header []string
headerAttribute string
lazyQuotes bool
parse parseFunc
}
type parseFunc func(interface{}) (interface{}, error)
// Process will parse an entry for csv.
func (r *CSVParser) Process(ctx context.Context, e *entry.Entry) error {
parse := r.parse
// If we have a headerAttribute set we need to dynamically generate our parser function
if r.headerAttribute != "" {
h, ok := e.Attributes[r.headerAttribute]
if !ok {
err := fmt.Errorf("failed to read dynamic header attribute %s", r.headerAttribute)
r.Error(err)
return err
}
headerString, ok := h.(string)
if !ok {
err := fmt.Errorf("header is expected to be a string but is %T", h)
r.Error(err)
return err
}
headers := strings.Split(headerString, string([]rune{r.fieldDelimiter}))
parse = generateParseFunc(headers, r.fieldDelimiter, r.lazyQuotes)
}
return r.ParserOperator.ProcessWith(ctx, e, parse)
}
// generateParseFunc returns a parse function for a given header, allowing
// each entry to have a potentially unique set of fields when using dynamic
// field names retrieved from an entry's attribute
func generateParseFunc(headers []string, fieldDelimiter rune, lazyQuotes bool) parseFunc {
return func(value interface{}) (interface{}, error) {
var csvLine string
switch t := value.(type) {
case string:
csvLine += t
case []byte:
csvLine += string(t)
default:
return nil, fmt.Errorf("type '%T' cannot be parsed as csv", value)
}
reader := csvparser.NewReader(strings.NewReader(csvLine))
reader.Comma = fieldDelimiter
reader.FieldsPerRecord = len(headers)
reader.LazyQuotes = lazyQuotes
// Typically only need one
lines := make([][]string, 0, 1)
for {
line, err := reader.Read()
if err == io.EOF {
break
}
if err != nil && len(line) == 0 {
return nil, errors.New("failed to parse entry")
}
lines = append(lines, line)
}
/*
This parser is parsing a single value, which came from a single log entry.
Therefore, if there are multiple lines here, it should be assumed that each
subsequent line contains a continuation of the last field in the previous line.
Given a file w/ headers "A,B,C,D,E" and contents "aa,b\nb,cc,d\nd,ee",
expect reader.Read() to return bodies:
- ["aa","b"]
- ["b","cc","d"]
- ["d","ee"]
*/
joinedLine := lines[0]
for i := 1; i < len(lines); i++ {
nextLine := lines[i]
// The first element of the next line is a continuation of the previous line's last element
joinedLine[len(joinedLine)-1] += "\n" + nextLine[0]
// The remainder are separate elements
for n := 1; n < len(nextLine); n++ {
joinedLine = append(joinedLine, nextLine[n])
}
}
parsedValues := make(map[string]interface{})
if len(joinedLine) != len(headers) {
return nil, errors.New("wrong number of fields")
}
for i, val := range joinedLine {
parsedValues[headers[i]] = val
}
return parsedValues, nil
}
}