-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser_variadic_kv.go
More file actions
130 lines (115 loc) · 2.99 KB
/
Copy pathparser_variadic_kv.go
File metadata and controls
130 lines (115 loc) · 2.99 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
/*
* Copyright (c) 2016 Yanko Bolanos
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
package main
import (
"encoding/json"
"errors"
"regexp"
"strconv"
"time"
log "github.com/Sirupsen/logrus"
"github.com/iancoleman/strcase"
)
type VariadicKVParser struct {
App string
AppVer string
Filename string
Hostname string
Table []Attribute
datePrefixLength int
cleanKeys bool
keyValueRegex *regexp.Regexp
}
func NewVariadicKVParser(app, appVer, filename, hostname string, re *regexp.Regexp, defaultTable []Attribute, options []string) *VariadicKVParser {
datePrefixLength := 24
cleanKeys := false
parsedOptions := ParseOptions(options)
for k, v := range parsedOptions {
if k == "date_prefix_length" {
var valInt int
var err error
if valInt, err = strconv.Atoi(v); err != nil {
log.Fatalf("invalid date_prefix_length option: %v", v)
}
datePrefixLength = valInt
}
if k == "clean_keys" && v == "true" {
cleanKeys = true
}
}
initRE := regexp.MustCompile(`([^=]*)=\"([^\"]*)\"\s?`)
if re != nil {
log.Warnf("Using custom regex: %s", re.String())
initRE = re
}
return &VariadicKVParser{
App: app,
AppVer: appVer,
Filename: filename,
Hostname: hostname,
datePrefixLength: datePrefixLength,
cleanKeys: cleanKeys,
keyValueRegex: initRE,
}
}
func (p *VariadicKVParser) Init(defaults, fieldMappings map[string]string, FieldsOrder []string, defaultTable []Attribute) {
}
func (p *VariadicKVParser) GetTable() []Attribute {
return p.Table
}
func (p *VariadicKVParser) Defaults() map[string]string {
d := make(map[string]string)
for _, k := range p.Table {
d[k.Key] = "\\N"
}
d["app"] = p.App
d["app_ver"] = p.AppVer
d["filename"] = p.Filename
d["hostname"] = p.Hostname
d["event"] = "" // init to avoid forcing runtime to increase map capacity in Parse
d["meta"] = ""
d["ingest_datetime"] = time.Now().UTC().Format(ISO_8601)
return d
}
func (p *VariadicKVParser) Parse(line string) (map[string]string, error) {
if len(line) < 24 {
return nil, errors.New("failed to parse")
}
matches := make(map[string]string)
result := p.Defaults()
vals := p.keyValueRegex.FindAllStringSubmatch(line[p.datePrefixLength:], -1)
if p.cleanKeys {
for _, item := range vals {
item[1] = strcase.ToLowerCamel(item[1])
}
}
for _, item := range vals {
switch len(item) {
case 3:
matches[item[1]] = item[2]
case 0:
continue
default:
for i := 2; i < len(item); i++ {
if item[i] != "" && item[i] != " " {
matches[item[1]] = item[i]
}
}
if _, ok := matches[item[1]]; !ok {
matches[item[1]] = ""
}
}
}
parsedJson, err := json.Marshal(matches)
if err == nil {
result["event"] = string(parsedJson)
}
result["log_line"] = truncateString(line, 16777216)
return result, nil
}