forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (59 loc) · 1.53 KB
/
main.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
// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// TODO(cbro): find a better sample - panic, defer, recover should not be present.
// Sample fluent demonstrates integration of fluent and Cloud Error reporting.
package main
import (
"log"
"net/http"
"runtime"
"github.com/fluent/fluent-logger-golang/fluent"
)
var logger *fluent.Fluent
func main() {
var err error
logger, err = fluent.New(fluent.Config{
FluentHost: "localhost",
FluentPort: 24224,
})
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/demo", demoHandler)
http.ListenAndServe(":8080", nil)
}
func report(stackTrace string, r *http.Request) {
payload := map[string]interface{}{
"serviceContext": map[string]interface{}{
"service": "myapp",
},
"message": stackTrace,
"context": map[string]interface{}{
"httpRequest": map[string]interface{}{
"method": r.Method,
"url": r.URL.String(),
"userAgent": r.UserAgent(),
"referrer": r.Referer(),
"remoteIp": r.RemoteAddr,
},
},
}
if err := logger.Post("myapp.errors", payload); err != nil {
log.Print(err)
}
}
// Handler for the incoming requests.
func demoHandler(w http.ResponseWriter, r *http.Request) {
// How to handle a panic.
defer func() {
if e := recover(); e != nil {
stack := make([]byte, 1<<16)
stackSize := runtime.Stack(stack, true)
report(string(stack[:stackSize]), r)
}
}()
// Panic is triggered.
x := 0
log.Println(100500 / x)
}