forked from tchap/zapext
/
main.go
59 lines (47 loc) · 1.12 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
package main
import (
"encoding/hex"
"fmt"
"os"
"github.com/tchap/zapext/zapsentry"
"github.com/getsentry/raven-go"
"github.com/google/uuid"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %+v\n", err)
os.Exit(1)
}
}
func run() error {
// Read DSN from the environment.
dsn := os.Getenv("SENTRY_DSN")
if dsn == "" {
return errors.New("SENTRY_DSN is not set")
}
// Instantiate a client.
client, err := raven.New(dsn)
if err != nil {
return errors.Wrap(err, "failed to get a Sentry client")
}
// Instantiate Zap.
logger := zap.New(zapsentry.NewCore(zapcore.ErrorLevel, client))
// Generate event ID.
uu, err := uuid.NewRandom()
if err != nil {
return errors.Wrap(err, "failed to generate UUID")
}
eventID := hex.EncodeToString(uu[:])
// Log.
logger.Error("nuked",
zap.String("event_id", eventID),
zap.String("#subsystem", "example"),
)
// Print the event ID to check with Sentry manually.
fmt.Println(eventID)
// Sync.
return errors.Wrap(logger.Sync(), "failed to sync packets to Sentry")
}