forked from tsaikd/gogstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterdate.go
164 lines (145 loc) · 3.76 KB
/
filterdate.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
package filterdate
import (
"context"
"math"
"strconv"
"strings"
"time"
"github.com/tengattack/jodatime"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/goglog"
"github.com/tsaikd/gogstash/config/logevent"
)
// ModuleName is the name used in config file
const ModuleName = "date"
// ErrorTag tag added to event when process module failed
const ErrorTag = "gogstash_filter_date_error"
// DefaultTarget default event field to store date as
const DefaultTarget = "@timestamp"
// FilterConfig holds the configuration json fields and internal objects
type FilterConfig struct {
config.FilterConfig
Format []string `json:"format"` // date parse format
Source string `json:"source"` // source message field name
Joda bool `json:"joda"` // whether using joda time format
Target string `json:"target"` // target field where date should be stored
timeParser func(layout, value string) (time.Time, error)
}
// DefaultFilterConfig returns an FilterConfig struct with default values
func DefaultFilterConfig() FilterConfig {
return FilterConfig{
FilterConfig: config.FilterConfig{
CommonConfig: config.CommonConfig{
Type: ModuleName,
},
},
Format: []string{time.RFC3339Nano},
Source: "message",
Target: DefaultTarget,
}
}
// InitHandler initialize the filter plugin
func InitHandler(ctx context.Context, raw *config.ConfigRaw) (config.TypeFilterConfig, error) {
conf := DefaultFilterConfig()
if err := config.ReflectConfig(raw, &conf); err != nil {
return nil, err
}
if conf.Joda {
conf.timeParser = jodatime.Parse
} else {
conf.timeParser = time.Parse
}
return &conf, nil
}
// Event the main filter event
func (f *FilterConfig) Event(ctx context.Context, event logevent.LogEvent) ([]logevent.LogEvent, bool) {
eventsOut := make([]logevent.LogEvent, 0)
var (
timestamp time.Time
err error
)
for _, thisFormat := range f.Format {
if thisFormat == "UNIX" {
var sec, nsec int64
value := event.Get(f.Source)
switch value := value.(type) {
case float64:
sec, nsec = convertFloat(value)
default:
sec, nsec, err = convert(event.GetString(f.Source))
}
if err != nil {
continue
}
timestamp = time.Unix(sec, nsec)
} else {
timestamp, err = f.timeParser(thisFormat, event.GetString(f.Source))
}
if err == nil {
break
}
}
if err != nil {
event.AddTag(ErrorTag)
goglog.Logger.Error(err)
eventsOut = append(eventsOut, event)
return eventsOut, false
}
if f.Target == DefaultTarget {
event.Timestamp = timestamp.UTC()
} else {
event.SetValue(f.Target, timestamp.UTC())
}
eventsOut = append(eventsOut, event)
return eventsOut, true
}
func convertFloat(value float64) (int64, int64) {
sec := int64(value)
rounded := value - float64(sec)
nsec := int64(rounded * 1000000000)
return sec, nsec
}
func convert(s string) (int64, int64, error) {
var sec, nsec int64
var err error
dot := strings.Index(s, ".")
if indexOfe := strings.Index(s, "e"); dot == 1 && indexOfe != -1 {
// looks like exponential notation
result, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0, 0, err
}
sec = int64(result)
rounded := math.Round((result - float64(sec)) * 1000)
nsec = int64(rounded)
return sec, nsec, nil
}
if dot >= 0 {
sec, err = strconv.ParseInt(s[:dot], 10, 64)
if err != nil {
return 0, 0, err
}
// fraction to nano seconds, avoid precision loss
nsec, err = strconv.ParseInt(s[dot+1:], 10, 64)
if err != nil {
return 0, 0, err
}
nsec *= exponent(10, 9-(len(s)-dot-1))
} else {
sec, err = strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, 0, err
}
}
return sec, nsec, nil
}
func exponent(a int64, n int) int64 {
result := int64(1)
for i := n; i > 0; i >>= 1 {
if i&1 != 0 {
result *= a
}
a *= a
}
return result
}