-
Notifications
You must be signed in to change notification settings - Fork 3
/
doc_test.go
110 lines (84 loc) · 3.12 KB
/
doc_test.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
package logger
import (
"context"
"net/http"
"github.com/getsentry/sentry-go"
"github.com/pkg/errors"
"go.uber.org/zap"
)
func Example_simple() {
// This will not work without SENTRY_DSN environment variable
_ = sentry.Init(sentry.ClientOptions{
Transport: sentry.NewHTTPSyncTransport(),
})
// Create logger for logging directly to sentry (without local output)
log := zap.New(NewSentryCore(sentry.CurrentHub()))
log.Debug("this message will be logged as breadcrumb", zap.Int("key", 1337))
log.Error("and this will create event in sentry")
log.Error("and this message will attach stacktrace", zap.Error(errors.New("error from pkg/errors")))
}
func Example_breadcrumbTransport() {
// This will not work without SENTRY_DSN environment variable
_ = sentry.Init(sentry.ClientOptions{
Transport: sentry.NewHTTPSyncTransport(),
})
// Create non-default http-client with
client := http.Client{
Transport: NewBreadcrumbTransport(sentry.LevelDebug, nil),
}
// Create context with sentry.Hub
// This is not required: if hub is not available from context sentry.CurrentHub() will be used instead
ctx := WithHub(context.Background(), sentry.CurrentHub())
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "https://go.pr0ger.dev/", nil)
resp, err := client.Do(req)
if err != nil {
// Either send error to sentry
sentry.CaptureException(err)
return
}
defer resp.Body.Close()
// Or just log response
sentry.CaptureMessage("Response status: " + resp.Status)
// Either way it will contain full info about request in breadcrumb
}
func Example_webServer() {
// This will not work without SENTRY_DSN environment variable
_ = sentry.Init(sentry.ClientOptions{
Transport: sentry.NewHTTPSyncTransport(),
TracesSampleRate: 1,
})
// Create core for logging to stdout/stderr
localCore := NewCore(true)
// Create core splitter to logging both to local and sentry
// zapcore.NewTee also can be used, but is not recommended if you want to use RequestLogger middleware
core := NewSentryCoreWrapper(localCore, sentry.CurrentHub())
// And create logger
logger := zap.New(core)
logger.Debug("this event will be logged to stdout but will not appear in request breadcrumbs")
// Create handler for network requests
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log := Ctx(r.Context())
log.Debug("some debug logs from request")
// Create an HTTP client with our transport
client := http.Client{
Transport: NewBreadcrumbTransport(sentry.LevelInfo, http.DefaultTransport),
}
// We need to pass current context to HTTP request so transport will know where to log
req, _ := http.NewRequestWithContext(r.Context(), http.MethodGet, "https://go.pr0ger.dev/logger", nil)
resp, err := client.Do(req)
if err != nil {
log.Warn("request failed", zap.Error(err))
} else {
log.Info("Response status: " + resp.Status)
resp.Body.Close()
}
_, _ = w.Write([]byte("ok"))
log.Error("let's assume we have an error here")
})
// And use it with our middleware
server := &http.Server{
Addr: ":8080",
Handler: RequestLogger(logger)(handler),
}
_ = server.ListenAndServe()
}