-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
97 additions
and
65 deletions.
There are no files selected for viewing
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
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
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
This file was deleted.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| 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" | ||
| ) | ||
|
|
||
| type SentryConfig struct { | ||
| DSN string `json:"dsn" yaml:"dsn" env:"SENTRY_DSN"` | ||
| EnableTracing bool `json:"enable_tracing" yaml:"enable_tracing" env:"SENTRY_ENABLE_TRACING"` | ||
| // 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 NewSentryConfig(v *viper.Viper, 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") | ||
|
|
||
| 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, client *sentry.Client) *zap.Logger { | ||
| cfg := zapsentry.Configuration{ | ||
| Level: zapcore.ErrorLevel, //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()) | ||
| } |