Documentation
¶
Overview ¶
Package sentry initializes the Sentry SDK (github.com/getsentry/sentry-go) for dim: Resource initializes it, returns the current Hub and flushes the buffered events on shutdown.
Adding Sentry to a service ¶
A service made by `skit new --full` has no Sentry and does not build sentry-go. Copy the parts below to add it; Sentry is on iff SENTRY_DSN is set, and the events are tagged with APP_ENV and the version of the build.
internal/app/config/opts.go — the group in ServerOpts and its type:
Sentry Sentry `group:"sentry" namespace:"sentry" env-namespace:"SENTRY"`
// Sentry holds the Sentry settings. Sentry is on iff DSN is non-empty, so
// an empty SENTRY_DSN turns it off without a separate flag.
type Sentry struct {
DSN string `long:"dsn" env:"DSN" description:"Sentry project DSN (empty disables Sentry)" json:"-"`
}
internal/app/deps/sentry.go, with initSentry in Initializers right after initTelemetry. It initializes at once, so a malformed DSN fails the start:
var initSentry = func(c *Deps) (dim.CleanupFunc, error) {
if c.Opts.Sentry.DSN == "" {
return nil, nil
}
_, cleanup, err := sentry.Resource(sentry.Config{
DSN: c.Opts.Sentry.DSN,
Environment: c.Opts.Env,
Release: buildinfo.Read().Version,
})(context.Background())
return cleanup, err
}
.env.example:
SENTRY_DSN=
The SDK is global: sentry-go captures through its current Hub, and the initializer only sets it up. To send the ERROR records, capture them in the Events hook of logger.Config (buildLogger of the service). A file that imports both this package and sentry-go names one of them, e.g. sentrygo.
Config ¶
- DSN: the project DSN; empty disables the transport.
- Environment: tags the events.
- Release: the build of the service, e.g. buildinfo.Read().Version.
- Debug: the debug logging of the SDK.
- IgnoreErrors: appended to the default ignore list ("context canceled").
- FlushTimeout: bounds the flush on cleanup (default 2s).
Example ¶
The initializer of a service calls the factory at once, so a malformed DSN fails the start. An empty DSN keeps the SDK on but sends nothing, as in development; the cleanup flushes the buffered events.
package main
import (
"context"
"fmt"
"github.com/assanoff/skit/provider/sentry"
)
func main() {
hub, cleanup, err := sentry.Resource(sentry.Config{
Environment: "staging",
Release: "orders@1.4.0",
})(context.Background())
if err != nil {
fmt.Println(err)
return
}
defer func() { _ = cleanup() }()
opts := hub.Client().Options()
fmt.Println(opts.Environment, opts.Release, opts.Dsn == "")
_, _, err = sentry.Resource(sentry.Config{DSN: "not a dsn"})(context.Background())
fmt.Println(err != nil)
}
Output: staging orders@1.4.0 true true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// DSN is the Sentry project DSN. An empty DSN disables the transport: the
// SDK initializes and drops the events.
DSN string
// Environment tags the events, e.g. "production".
Environment string
// Release names the build of the service in the events, e.g.
// buildinfo.Read().Version; empty leaves it to sentry-go (SENTRY_RELEASE).
Release string
// Debug enables the debug logging of the Sentry SDK.
Debug bool
// IgnoreErrors is appended to the default ignore list ("context canceled").
IgnoreErrors []string
// FlushTimeout bounds the flush on cleanup (default 2s).
FlushTimeout time.Duration
}
Config configures the Sentry SDK.