Skip to content

Commit 44fbaf0

Browse files
committed
feat: Autodetect timestamp
1 parent 188dad7 commit 44fbaf0

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.12
55
require (
66
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
77
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
8+
github.com/araddon/dateparse v0.0.0-20190622164848-0fb0a474d195
89
github.com/fsnotify/fsnotify v1.4.7
910
github.com/getsentry/sentry-go v0.1.1-0.20190624124141-69c26e4dfca8
1011
github.com/hpcloud/tail v1.0.1-0.20180514194441-a1dbeea552b7

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5Vpd
22
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
33
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
44
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
5+
github.com/araddon/dateparse v0.0.0-20190622164848-0fb0a474d195 h1:c4mLfegoDw6OhSJXTd2jUEQgZUQuJWtocudb97Qn9EM=
6+
github.com/araddon/dateparse v0.0.0-20190622164848-0fb0a474d195/go.mod h1:SLqhdZcd+dF3TEVL2RMoob5bBP5R1P1qkox+HtCBgGI=
57
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
68
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
79
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=

grok-patterns.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ package main
33
import "github.com/vjeantet/grok"
44

55
func AddDefaultPatterns(g *grok.Grok) {
6-
g.AddPattern("NGINX_ERROR_LOG", `%{DATESTAMP:timestamp} \[%{DATA:err_severity}\] (%{NUMBER:pid:int}#%{NUMBER}: \*%{NUMBER}|\*%{NUMBER}) %{DATA:err_message}(?:, client: "?%{IPORHOST:client}"?)(?:, server: %{IPORHOST:server})(?:, request: "%{WORD:verb} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}")?(?:, upstream: "%{DATA:upstream}")?(?:, host: "%{URIHOST:host}")?(?:, referrer: "%{URI:referrer}")?`)
6+
// Nginx
7+
g.AddPattern("NGINX_ERROR_DATESTAMP", `\d{4}/\d{2}/\d{2}[- ]%{TIME}`)
8+
g.AddPattern("NGINX_ERROR_LOG", `%{NGINX_ERROR_DATESTAMP:timestamp} \[%{DATA:err_severity}\] (%{NUMBER:pid:int}#%{NUMBER}: \*%{NUMBER}|\*%{NUMBER}) %{DATA:message}(?:, client: "?%{IPORHOST:client}"?)(?:, server: %{IPORHOST:server})(?:, request: "%{WORD:verb} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}")?(?:, upstream: "%{DATA:upstream}")?(?:, host: "%{URIHOST:host}")?(?:, referrer: "%{URI:referrer}")?`)
79
}

main.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ import (
99
"sort"
1010
"time"
1111

12+
"github.com/araddon/dateparse"
1213
"github.com/getsentry/sentry-go"
1314
"github.com/hpcloud/tail"
1415
"github.com/vjeantet/grok"
1516
"gopkg.in/alecthomas/kingpin.v2"
1617
)
1718

18-
func printMap(m map[string]string) {
19+
const MessageField = "message"
20+
const TimeStampField = "timestamp"
21+
22+
func PrintMap(m map[string]string) {
1923
keys := make([]string, 0, len(m))
2024
for k := range m {
2125
keys = append(keys, k)
@@ -24,8 +28,8 @@ func printMap(m map[string]string) {
2428

2529
for _, k := range keys {
2630
fmt.Printf("%+15s: %s\n", k, m[k])
27-
2831
}
32+
fmt.Println()
2933
}
3034

3135
func IsDryRun() bool {
@@ -45,12 +49,12 @@ func InitSentry() {
4549
err := sentry.Init(sentry.ClientOptions{})
4650

4751
if err != nil {
48-
log.Fatal("Sentry initialization failed: %v\n", err)
52+
log.Fatalf("Sentry initialization failed: %v\n", err)
4953
}
5054
}
5155

5256
func CaptureEvent(line string, values map[string]string) {
53-
message := values["err_message"]
57+
message := values[MessageField]
5458
if message == "" {
5559
message = line
5660
}
@@ -75,6 +79,20 @@ func CaptureEvent(line string, values map[string]string) {
7579
})
7680
}
7781

82+
func ParseTimestamp(str string) int64 {
83+
fallback := int64(0)
84+
if str == "" {
85+
return fallback
86+
}
87+
88+
time, err := dateparse.ParseLocal(str)
89+
if err != nil {
90+
return fallback
91+
}
92+
93+
return time.Unix()
94+
}
95+
7896
func ProcessLine(line string, pattern string, g *grok.Grok) {
7997
values, err := g.Parse(pattern, line)
8098
if err != nil {
@@ -83,10 +101,14 @@ func ProcessLine(line string, pattern string, g *grok.Grok) {
83101
}
84102

85103
if !IsDryRun() {
104+
// Attempt to parse the timestamp
105+
timestamp := ParseTimestamp(values[TimeStampField])
106+
86107
// Original log line
87108
sentry.AddBreadcrumb(&sentry.Breadcrumb{
88-
Message: line,
89-
Level: sentry.LevelInfo,
109+
Message: line,
110+
Level: sentry.LevelInfo,
111+
Timestamp: timestamp,
90112
})
91113
}
92114

@@ -96,8 +118,8 @@ func ProcessLine(line string, pattern string, g *grok.Grok) {
96118

97119
CaptureEvent(line, values)
98120

99-
log.Println(">>> Entry:")
100-
printMap(values)
121+
log.Println("Entry found:")
122+
PrintMap(values)
101123
}
102124

103125
func InitGrokProcessor() *grok.Grok {
@@ -157,14 +179,16 @@ func ProcessFile(filename string, pattern string) {
157179
}
158180
}
159181

160-
t, err := tail.TailFile(
182+
follow := !*noFollow
183+
tailFile, err := tail.TailFile(
161184
filename,
162185
tail.Config{
163-
Follow: !*noFollow,
186+
Follow: follow,
164187
Location: &seekInfo,
188+
ReOpen: follow,
165189
})
166190

167-
for line := range t.Lines {
191+
for line := range tailFile.Lines {
168192
ProcessLine(line.Text, pattern, g)
169193

170194
}

0 commit comments

Comments
 (0)