forked from bugsnag/bugsnag-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appengine.go
81 lines (66 loc) · 1.66 KB
/
appengine.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
// +build appengine
package bugsnag
import (
"appengine"
"appengine/urlfetch"
"appengine/user"
"fmt"
"log"
"net/http"
)
func defaultPanicHandler() {}
func init() {
OnBeforeNotify(appengineMiddleware)
}
func appengineMiddleware(event *Event, config *Configuration) (err error) {
var c appengine.Context
for _, datum := range event.RawData {
if r, ok := datum.(*http.Request); ok {
c = appengine.NewContext(r)
break
} else if context, ok := datum.(appengine.Context); ok {
c = context
break
}
}
if c == nil {
return fmt.Errorf("No appengine context given")
}
// You can only use the builtin http library if you pay for appengine,
// so we use the appengine urlfetch service instead.
config.Transport = &urlfetch.Transport{
Context: c,
}
// Anything written to stderr/stdout is discarded, so lets log to the request.
if configuredLogger, ok := config.Logger.(*log.Logger); ok {
config.Logger = log.New(appengineWriter{c}, configuredLogger.Prefix(), configuredLogger.Flags())
} else {
config.Logger = log.New(appengineWriter{c}, log.Prefix(), log.Flags())
}
// Set the releaseStage appropriately
if config.ReleaseStage == "" {
if appengine.IsDevAppServer() {
config.ReleaseStage = "development"
} else {
config.ReleaseStage = "production"
}
}
if event.User == nil {
u := user.Current(c)
if u != nil {
event.User = &User{
Id: u.ID,
Email: u.Email,
}
}
}
return nil
}
// Convert an appengine.Context into an io.Writer so we can create a log.Logger.
type appengineWriter struct {
appengine.Context
}
func (c appengineWriter) Write(b []byte) (int, error) {
c.Warningf(string(b))
return len(b), nil
}