forked from grafana/loki
/
util.go
195 lines (184 loc) · 5.41 KB
/
util.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
package stages
import (
"fmt"
"math"
"strconv"
"strings"
"time"
)
var (
// Debug is used to wrap debug log statements, the go-kit logger won't let us introspect the current log level
// so this global is used for that purpose. This allows us to skip allocations of log messages at the
// debug level when debug level logging is not enabled. Log level allocations can become very expensive
// as we log numerous log entries per log line at debug level.
Debug = false
// Inspect is used to debug promtail pipelines by showing diffs between pipeline stages
Inspect = false
)
const (
ErrTimestampContainsYear = "timestamp '%s' is expected to not contain the year date component"
)
// convertDateLayout converts pre-defined date format layout into date format
func convertDateLayout(predef string, location *time.Location) parser {
switch predef {
case "ANSIC":
return func(t string) (time.Time, error) {
return time.Parse(time.ANSIC, t)
}
case "UnixDate":
return func(t string) (time.Time, error) {
return time.Parse(time.UnixDate, t)
}
case "RubyDate":
return func(t string) (time.Time, error) {
return time.Parse(time.RubyDate, t)
}
case "RFC822":
return func(t string) (time.Time, error) {
return time.Parse(time.RFC822, t)
}
case "RFC822Z":
return func(t string) (time.Time, error) {
return time.Parse(time.RFC822Z, t)
}
case "RFC850":
return func(t string) (time.Time, error) {
return time.Parse(time.RFC850, t)
}
case "RFC1123":
return func(t string) (time.Time, error) {
return time.Parse(time.RFC1123, t)
}
case "RFC1123Z":
return func(t string) (time.Time, error) {
return time.Parse(time.RFC1123Z, t)
}
case "RFC3339":
return func(t string) (time.Time, error) {
return time.Parse(time.RFC3339, t)
}
case "RFC3339Nano":
return func(t string) (time.Time, error) {
return time.Parse(time.RFC3339Nano, t)
}
case "Unix":
return func(t string) (time.Time, error) {
if strings.Count(t, ".") == 1 {
split := strings.Split(t, ".")
if len(split) != 2 {
return time.Time{}, fmt.Errorf("can't split %v into two parts", t)
}
sec, err := strconv.ParseInt(split[0], 10, 64)
if err != nil {
return time.Time{}, err
}
frac, err := strconv.ParseInt(split[1], 10, 64)
if err != nil {
return time.Time{}, err
}
nsec := int64(float64(frac) * math.Pow(10, float64(9-len(split[1]))))
return time.Unix(sec, nsec), nil
}
i, err := strconv.ParseInt(t, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(i, 0), nil
}
case "UnixMs":
return func(t string) (time.Time, error) {
i, err := strconv.ParseInt(t, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(0, i*int64(time.Millisecond)), nil
}
case "UnixUs":
return func(t string) (time.Time, error) {
i, err := strconv.ParseInt(t, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(0, i*int64(time.Microsecond)), nil
}
case "UnixNs":
return func(t string) (time.Time, error) {
i, err := strconv.ParseInt(t, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(0, i), nil
}
default:
if !strings.Contains(predef, "06") && !strings.Contains(predef, "2006") {
return func(t string) (time.Time, error) {
return parseTimestampWithoutYear(predef, location, t, time.Now())
}
}
return func(t string) (time.Time, error) {
if location != nil {
return time.ParseInLocation(predef, t, location)
}
return time.Parse(predef, t)
}
}
}
// parseTimestampWithoutYear parses the input timestamp without the year component,
// assuming the timestamp is related to a point in time close to "now", and correctly
// handling the edge cases around new year's eve
func parseTimestampWithoutYear(layout string, location *time.Location, timestamp string, now time.Time) (time.Time, error) {
var parsedTime time.Time
var err error
if location != nil {
parsedTime, err = time.ParseInLocation(layout, timestamp, location)
} else {
parsedTime, err = time.Parse(layout, timestamp)
}
if err != nil {
return parsedTime, err
}
// Ensure the year component of the input date string has not been
// parsed for real
if parsedTime.Year() != 0 {
return parsedTime, fmt.Errorf(ErrTimestampContainsYear, timestamp)
}
// Handle the case we're crossing the new year's eve midnight
if parsedTime.Month() == 12 && now.Month() == 1 {
parsedTime = parsedTime.AddDate(now.Year()-1, 0, 0)
} else if parsedTime.Month() == 1 && now.Month() == 12 {
parsedTime = parsedTime.AddDate(now.Year()+1, 0, 0)
} else {
parsedTime = parsedTime.AddDate(now.Year(), 0, 0)
}
return parsedTime, nil
}
// getString will convert the input variable to a string if possible
func getString(unk interface{}) (string, error) {
switch i := unk.(type) {
case float64:
return strconv.FormatFloat(i, 'f', -1, 64), nil
case float32:
return strconv.FormatFloat(float64(i), 'f', -1, 32), nil
case int64:
return strconv.FormatInt(i, 10), nil
case int32:
return strconv.FormatInt(int64(i), 10), nil
case int:
return strconv.Itoa(i), nil
case uint64:
return strconv.FormatUint(i, 10), nil
case uint32:
return strconv.FormatUint(uint64(i), 10), nil
case uint:
return strconv.FormatUint(uint64(i), 10), nil
case string:
return unk.(string), nil
case bool:
if i {
return "true", nil
}
return "false", nil
default:
return "", fmt.Errorf("Can't convert %v to string", unk)
}
}