-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtime.go
296 lines (258 loc) · 8.26 KB
/
time.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// 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 (
"fmt"
"strconv"
"strings"
"time"
strptime "github.com/observiq/ctimefmt"
"go.opentelemetry.io/collector/confmap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/errors"
)
// StrptimeKey is literally "strptime", and is the default layout type
const StrptimeKey = "strptime"
// GotimeKey is literally "gotime" and uses Golang's native time.Parse
const GotimeKey = "gotime"
// EpochKey is literally "epoch" and can parse seconds and/or subseconds
const EpochKey = "epoch"
// NativeKey is literally "native" and refers to Golang's native time.Time
const NativeKey = "native" // provided for operator development
// NewTimeParser creates a new time parser with default values
func NewTimeParser() TimeParser {
return TimeParser{
LayoutType: "strptime",
}
}
// TimeParser is a helper that parses time onto an entry.
type TimeParser struct {
ParseFrom *entry.Field `mapstructure:"parse_from"`
Layout string `mapstructure:"layout"`
LayoutType string `mapstructure:"layout_type"`
Location string `mapstructure:"location"`
location *time.Location
}
// Unmarshal starting from default settings
func (t *TimeParser) Unmarshal(component *confmap.Conf) error {
cfg := NewTimeParser()
err := component.Unmarshal(&cfg, confmap.WithErrorUnused())
if err != nil {
return err
}
*t = cfg
return nil
}
// IsZero returns true if the TimeParser is not a valid config
func (t *TimeParser) IsZero() bool {
return t.Layout == ""
}
// Validate validates a TimeParser, and reconfigures it if necessary
func (t *TimeParser) Validate() error {
if t.ParseFrom == nil {
return fmt.Errorf("missing required parameter 'parse_from'")
}
if t.Layout == "" && t.LayoutType != "native" {
return errors.NewError("missing required configuration parameter `layout`", "")
}
if t.LayoutType == "" {
t.LayoutType = StrptimeKey
}
switch t.LayoutType {
case NativeKey, GotimeKey: // ok
case StrptimeKey:
var err error
t.Layout, err = strptime.ToNative(t.Layout)
if err != nil {
return errors.Wrap(err, "parse strptime layout")
}
t.LayoutType = GotimeKey
case EpochKey:
switch t.Layout {
case "s", "ms", "us", "ns", "s.ms", "s.us", "s.ns": // ok
default:
return errors.NewError(
"invalid `layout` for `epoch` type",
"specify 's', 'ms', 'us', 'ns', 's.ms', 's.us', or 's.ns'",
)
}
default:
return errors.NewError(
fmt.Sprintf("unsupported layout_type %s", t.LayoutType),
"valid values are 'strptime', 'gotime', and 'epoch'",
)
}
if t.LayoutType == GotimeKey { // also covers StrptimeKey because it was remapped above
if err := t.setLocation(); err != nil {
return errors.Wrap(err, "invalid 'location'")
}
}
return nil
}
func (t *TimeParser) setLocation() error {
if t.Location != "" {
// If "location" is specified, it must be in the local timezone database
loc, err := time.LoadLocation(t.Location)
if err != nil {
return fmt.Errorf("failed to load location %s: %w", t.Location, err)
}
t.location = loc
return nil
}
if strings.HasSuffix(t.Layout, "Z") {
// If a timestamp ends with 'Z', it should be interpretted at Zulu (UTC) time
t.location = time.UTC
return nil
}
t.location = time.Local
return nil
}
// Parse will parse time from a field and attach it to the entry
func (t *TimeParser) Parse(entry *entry.Entry) error {
value, ok := entry.Get(t.ParseFrom)
if !ok {
return errors.NewError(
"log entry does not have the expected parse_from field",
"ensure that all entries forwarded to this parser contain the parse_from field",
"parse_from", t.ParseFrom.String(),
)
}
switch t.LayoutType {
case NativeKey:
timeValue, ok := value.(time.Time)
if !ok {
return fmt.Errorf("native time.Time field required, but found %v of type %T", value, value)
}
entry.Timestamp = setTimestampYear(timeValue)
case GotimeKey:
timeValue, err := t.parseGotime(value)
if err != nil {
return err
}
entry.Timestamp = setTimestampYear(timeValue)
case EpochKey:
timeValue, err := t.parseEpochTime(value)
if err != nil {
return err
}
entry.Timestamp = setTimestampYear(timeValue)
default:
return fmt.Errorf("unsupported layout type: %s", t.LayoutType)
}
return nil
}
func (t *TimeParser) parseGotime(value interface{}) (time.Time, error) {
var str string
switch v := value.(type) {
case string:
str = v
case []byte:
str = string(v)
default:
return time.Time{}, fmt.Errorf("type %T cannot be parsed as a time", value)
}
result, err := time.ParseInLocation(t.Layout, str, t.location)
// Depending on the timezone database, we may get a pseudo-matching timezone
// This is apparent when the zone is not "UTC", but the offset is still 0
zone, offset := result.Zone()
if offset != 0 || zone == "UTC" {
return result, err
}
// Manually look up the location based on the zone
loc, locErr := time.LoadLocation(zone)
if locErr != nil {
// can't correct offset, just return what we have
return result, fmt.Errorf("failed to load location %s: %w", zone, locErr)
}
// Reparse the timestamp, with the location
resultLoc, locErr := time.ParseInLocation(t.Layout, str, loc)
if locErr != nil {
// can't correct offset, just return original result
return result, err
}
return resultLoc, locErr
}
func (t *TimeParser) parseEpochTime(value interface{}) (time.Time, error) {
stamp, err := getEpochStamp(t.Layout, value)
if err != nil {
return time.Time{}, err
}
switch t.Layout {
case "s", "ms", "us", "ns":
i, err := strconv.ParseInt(stamp, 10, 64)
if err != nil {
return time.Time{}, fmt.Errorf("invalid value '%v' for layout '%s'", stamp, t.Layout)
}
return toTime[t.Layout](i), nil
case "s.ms", "s.us", "s.ns":
secSubsec := strings.Split(stamp, ".")
if len(secSubsec) != 2 {
return time.Time{}, fmt.Errorf("invalid value '%v' for layout '%s'", stamp, t.Layout)
}
sec, secErr := strconv.ParseInt(secSubsec[0], 10, 64)
subsec, subsecErr := strconv.ParseInt(secSubsec[1], 10, 64)
if secErr != nil || subsecErr != nil {
return time.Time{}, fmt.Errorf("invalid value '%v' for layout '%s'", stamp, t.Layout)
}
return time.Unix(sec, subsec*subsecToNs[t.Layout]), nil
default:
return time.Time{}, fmt.Errorf("invalid layout '%s'", t.Layout)
}
}
func getEpochStamp(layout string, value interface{}) (string, error) {
switch v := value.(type) {
case string:
return v, nil
case []byte:
return string(v), nil
case int, int32, int64, uint32, uint64:
switch layout {
case "s", "ms", "us", "ns":
return fmt.Sprintf("%d", v), nil
case "s.ms", "s.us", "s.ns":
return fmt.Sprintf("%d.0", v), nil
default:
return "", fmt.Errorf("invalid layout '%s'", layout)
}
case float64:
switch layout {
case "s", "ms", "us", "ns":
return fmt.Sprintf("%d", int64(v)), nil
case "s.ms":
return fmt.Sprintf("%10.3f", v), nil
case "s.us":
return fmt.Sprintf("%10.6f", v), nil
case "s.ns":
return fmt.Sprintf("%10.9f", v), nil
default:
return "", fmt.Errorf("invalid layout '%s'", layout)
}
default:
return "", fmt.Errorf("type %T cannot be parsed as a time", v)
}
}
type toTimeFunc = func(int64) time.Time
var toTime = map[string]toTimeFunc{
"s": func(s int64) time.Time { return time.Unix(s, 0) },
"ms": func(ms int64) time.Time { return time.Unix(ms/1e3, (ms%1e3)*1e6) },
"us": func(us int64) time.Time { return time.Unix(us/1e6, (us%1e6)*1e3) },
"ns": func(ns int64) time.Time { return time.Unix(0, ns) },
}
var subsecToNs = map[string]int64{"s.ms": 1e6, "s.us": 1e3, "s.ns": 1}
// setTimestampYear sets the year of a timestamp to the current year.
// This is needed because year is missing from some time formats, such as rfc3164.
func setTimestampYear(t time.Time) time.Time {
if t.Year() > 0 {
return t
}
n := now()
d := time.Date(n.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
// If the timestamp would be more than 7 days in the future using this year,
// assume it's from last year.
if d.After(n.AddDate(0, 0, 7)) {
d = d.AddDate(-1, 0, 0)
}
return d
}
// Allows tests to override with deterministic value
var now = time.Now