Skip to content

[feature] #38 add sentry release #39

Merged
merged 1 commit into from Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/config.template.yml
Expand Up @@ -26,6 +26,11 @@ db:
cache:
defaultExpiration: 0 # 默认缓存过期时间 单位 秒 0 永不过期
cleanupInterval: 600 # 清理过期缓存间隔 单位 秒 0 不自动清理
sentry:
level: "error" # debug info warn error panic fatal, default: error
dsn: ""
enable_tracing: true
traces_sample_rate: 1.0
log:
filename: /tmp/open.log
maxSize: 500
Expand Down
3 changes: 2 additions & 1 deletion internal/app/api/provide.go
Expand Up @@ -7,6 +7,7 @@ import (
"vlink.dev/eoefans/internal/app/api/service"
"vlink.dev/eoefans/internal/app/api/util/password"
"vlink.dev/eoefans/internal/pkg/cache"
"vlink.dev/eoefans/internal/pkg/iversion"
"vlink.dev/eoefans/internal/pkg/log"
"vlink.dev/eoefans/internal/pkg/smsclient"
)
Expand All @@ -21,7 +22,7 @@ func Provide() fx.Option {
fx.Provide(cache.NewGoCache),
fx.Provide(smsclient.NewRandomNumberCodeGenerator),
fx.Provide(password.NewDefaultPasswordHandler),
fx.Provide(log.NewSentryConfig, log.NewSentry),
fx.Provide(iversion.NewFileVersion, log.NewSentryConfig, log.NewSentry),
)
}

Expand Down
29 changes: 29 additions & 0 deletions internal/pkg/iversion/file.go
@@ -0,0 +1,29 @@
package iversion

import (
"os"
"strings"
)

type Version interface {
Version() string
}

type FileVersion struct {
version string
}

func NewFileVersion() (Version, error) {
b, err := os.ReadFile("config/version")
if err != nil {
return &FileVersion{version: "Not Set Version"}, nil
}

v := strings.Replace(string(b), "\r\n", "", -1)
v = strings.Replace(v, "\n", "", -1)
return &FileVersion{version: v}, nil
}

func (v FileVersion) Version() string {
return v.version
}
45 changes: 40 additions & 5 deletions internal/pkg/log/sentry.go
Expand Up @@ -4,21 +4,44 @@ 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 NewSentryConfig(v *viper.Viper, logger *zap.Logger) (*SentryConfig, error) {
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 {
Expand All @@ -27,6 +50,9 @@ func NewSentryConfig(v *viper.Viper, logger *zap.Logger) (*SentryConfig, error)

logger.Info("load sentry config success")

if semver.IsValid(iVersion.Version()) {
o.Release = iVersion.Version()
}
return o, err
}

Expand All @@ -43,11 +69,20 @@ func NewSentry(sentryCfg *SentryConfig) (*sentry.Client, error) {
return client, nil
}

func ModifyToSentryLogger(logger *zap.Logger, client *sentry.Client) *zap.Logger {
func ModifyToSentryLogger(
logger *zap.Logger,
sentryCfg *SentryConfig,
client *sentry.Client,
) *zap.Logger {
// skip
if sentryCfg.DSN == "" {
return 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
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",
},
Expand Down