Skip to content
Permalink
70c7b6775c
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
103 lines (90 sloc) 2.91 KB
package log
import (
"github.com/TheZeroSlave/zapsentry"
"github.com/getsentry/sentry-go"
"github.com/pkg/errors"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/mod/semver"
"vlink.dev/eoefans/internal/pkg/iversion"
)
type SentryConfig struct {
Level string `json:"level" yaml:"level" env:"SENTRY_LEVEL"`
DSN string `json:"dsn" yaml:"dsn" env:"SENTRY_DSN"`
EnableTracing bool `json:"enable_tracing" yaml:"enable_tracing" env:"SENTRY_ENABLE_TRACING"`
Release string `json:"release" yaml:"release" env:"SENTRY_RELEASE"`
// Set TracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production,
TracesSampleRate float64 `json:"traces_sample_rate" yaml:"traces_sample_rate" env:"SENTRY_TRACES_SAMPLE_RATE"`
}
func (cfg SentryConfig) GetLevel() zapcore.Level {
switch cfg.Level {
case "debug":
return zapcore.DebugLevel
case "info":
return zapcore.InfoLevel
case "warn":
return zapcore.WarnLevel
case "error":
return zapcore.ErrorLevel
case "panic":
return zapcore.PanicLevel
case "fatal":
return zapcore.FatalLevel
default:
return zapcore.ErrorLevel
}
}
func NewSentryConfig(v *viper.Viper, iVersion iversion.Version, logger *zap.Logger) (*SentryConfig, error) {
var err error
o := &SentryConfig{}
if err = v.UnmarshalKey("sentry", o); err != nil {
return nil, errors.Wrap(err, "unmarshal sentry config error")
}
logger.Info("load sentry config success")
if semver.IsValid(iVersion.Version()) {
o.Release = iVersion.Version()
}
return o, err
}
func NewSentry(sentryCfg *SentryConfig) (*sentry.Client, error) {
client, err := sentry.NewClient(sentry.ClientOptions{
Dsn: sentryCfg.DSN,
EnableTracing: sentryCfg.EnableTracing,
TracesSampleRate: sentryCfg.TracesSampleRate,
Release: sentryCfg.Release,
})
if err != nil {
return nil, errors.Wrapf(err, "failed to initialize sentry")
}
return client, nil
}
func ModifyToSentryLogger(
logger *zap.Logger,
sentryCfg *SentryConfig,
client *sentry.Client,
) *zap.Logger {
// skip
if sentryCfg.DSN == "" {
return logger
}
cfg := zapsentry.Configuration{
Level: sentryCfg.GetLevel(), //when to send message to sentry
EnableBreadcrumbs: true, // enable sending breadcrumbs to Sentry
BreadcrumbLevel: zapcore.InfoLevel, // at what level should we sent breadcrumbs to sentry
Tags: map[string]string{
"component": "system",
},
}
core, err := zapsentry.NewCore(cfg, zapsentry.NewSentryClientFromClient(client))
//in case of err it will return noop core. so we can safely attach it
if err != nil {
logger.Warn("failed to init zap", zap.Error(err))
}
logger = zapsentry.AttachCoreToLogger(core, logger)
// to use breadcrumbs feature - create new scope explicitly
// and attach after attaching the core
return logger.With(zapsentry.NewScope())
}