Permalink
Cannot retrieve contributors at this time
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?
eoefans-api/internal/pkg/log/sentry.go
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
102 lines (89 sloc)
2.88 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package log | |
import ( | |
"github.com/TheZeroSlave/zapsentry" | |
"github.com/getsentry/sentry-go" | |
"github.com/pkg/errors" | |
"github.com/rogpeppe/go-internal/semver" | |
"github.com/spf13/viper" | |
"go.uber.org/zap" | |
"go.uber.org/zap/zapcore" | |
"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, | |
}) | |
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()) | |
} |