Skip to content

Commit

Permalink
feat: fix core-api sentry (#121)
Browse files Browse the repository at this point in the history
* fix: core-api sentry
  • Loading branch information
Han-Ya-Jun authored Jul 21, 2023
1 parent f159824 commit 9c138ac
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 15 deletions.
15 changes: 11 additions & 4 deletions src/core-api/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package cmd
import (
"fmt"

"github.com/getsentry/raven-go"
sentry "github.com/getsentry/sentry-go"
"github.com/spf13/viper"

Expand Down Expand Up @@ -52,7 +53,7 @@ func initConfig() {
}

func initLogger() {
logging.InitLogger(&globalConfig.Logger)
logging.InitLogger(globalConfig)
}

func initDatabase() {
Expand All @@ -67,7 +68,7 @@ func initDatabase() {
}

func initSentry() {
if globalConfig.Sentry.Enable {
if len(globalConfig.Sentry.DSN) != 0 {
err := sentry.Init(sentry.ClientOptions{
Dsn: globalConfig.Sentry.DSN,
})
Expand All @@ -76,11 +77,17 @@ func initSentry() {
return
}
logging.GetLogger().Info("init Sentry success")

// init gin sentry
err = raven.SetDSN(globalConfig.Sentry.DSN)
if err != nil {
logging.GetLogger().Errorf("init gin Sentry fail: %s", err)
return
}
logging.GetLogger().Info("init gin Sentry success")
} else {
logging.GetLogger().Info("Sentry is not enabled, will not init it")
}

// util.InitErrorReport(globalConfig.Sentry.Enable)
}

func initMetrics() {
Expand Down
2 changes: 1 addition & 1 deletion src/core-api/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func Start() {

// 1. init components
initTracing()
initLogger()
initSentry()
initLogger()
initMetrics()
initDatabase()

Expand Down
3 changes: 2 additions & 1 deletion src/core-api/config.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ server:
idleTimeout: 180

sentry:
enable: false
dsn: ""
## zapcore.Level
reportLogLevel: 2


databases:
Expand Down
5 changes: 5 additions & 0 deletions src/core-api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/agiledragon/gomonkey/v2 v2.10.1
github.com/dlmiddlecote/sqlstats v1.0.2
github.com/getsentry/sentry-go v0.20.0
github.com/gin-gonic/contrib v0.0.0-20221130124618-7e01895a63f2
github.com/gin-gonic/gin v1.8.2
github.com/go-playground/validator/v10 v10.11.1
github.com/go-sql-driver/mysql v1.7.0
Expand All @@ -22,6 +23,7 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.3
github.com/tchap/zapext/v2 v2.1.1
github.com/uptrace/opentelemetry-go-extra/otelsql v0.2.2
github.com/uptrace/opentelemetry-go-extra/otelsqlx v0.2.2
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.42.0
Expand All @@ -38,9 +40,11 @@ require (
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/getsentry/raven-go v0.2.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -63,6 +67,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
Expand Down
157 changes: 157 additions & 0 deletions src/core-api/go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/core-api/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ type Database struct {

// Sentry is the config for sentry
type Sentry struct {
Enable bool
DSN string
DSN string
ReportLogLevel int
}

// Tracing is the config for trace
Expand Down
45 changes: 39 additions & 6 deletions src/core-api/pkg/logging/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ package logging

import (
"fmt"
"log"
"strings"
"sync"
"time"

"github.com/getsentry/sentry-go"
"go.uber.org/zap"

"github.com/tchap/zapext/v2/zapsentry"
"go.uber.org/zap/zapcore"

"core/pkg/config"
Expand All @@ -49,15 +52,41 @@ func newZapSugarLogger() *zap.SugaredLogger {
}

// InitLogger ...
func InitLogger(logger *config.Logger) {
func InitLogger(config *config.Config) {
loggerInitOnce.Do(func() {
defaultLogger = newZapJSONLogger(&logger.Default).Sugar()
options := make([]zap.Option, 0, 3)
if len(config.Sentry.DSN) != 0 {
// init sentryCore
sentryCore, err := newSentryLogCore(config)
if err != nil {
log.Printf("new sentry log core fail: %s\n", err.Error())
} else {
options = append(options, zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewTee(sentryCore, core)
}))
}
}
options = append(options, zap.AddCaller())

defaultLogger = newZapJSONLogger(&config.Logger.Default, options).Sugar()

// json logger
apiLogger = newZapJSONLogger(&logger.API)
apiLogger = newZapJSONLogger(&config.Logger.API, options)
})
}

// newSentryLogCore
func newSentryLogCore(cfg *config.Config) (zapcore.Core, error) {
rawCore := zapsentry.NewCore(zapcore.Level(cfg.Sentry.ReportLogLevel), sentry.CurrentHub().Client())
sentryCore := zapcore.RegisterHooks(rawCore, func(entry zapcore.Entry) error {
if entry.Level == zapcore.FatalLevel {
sentry.CurrentHub().Client().Flush(2 * time.Second)
}
return nil
})
return sentryCore, nil
}

// parseZapLogLevel takes a string level and returns the zap log level constant.
func parseZapLogLevel(lvl string) (zapcore.Level, error) {
switch strings.ToLower(lvl) {
Expand All @@ -79,7 +108,7 @@ func parseZapLogLevel(lvl string) (zapcore.Level, error) {
return l, fmt.Errorf("not a valid logrus Level: %q", lvl)
}

func newZapJSONLogger(cfg *config.LogConfig) *zap.Logger {
func newZapJSONLogger(cfg *config.LogConfig, options []zap.Option) *zap.Logger {
writer, err := getWriter(cfg.Writer, cfg.Settings)
if err != nil {
panic(err)
Expand All @@ -96,6 +125,10 @@ func newZapJSONLogger(cfg *config.LogConfig) *zap.Logger {
}
}

encoderConfig := zap.NewProductionEncoderConfig()
// 设置时间格式
encoderConfig.EncodeTime = zapcore.RFC3339NanoTimeEncoder

// 设置日志级别
l, err := parseZapLogLevel(cfg.Level)
if err != nil {
Expand All @@ -104,11 +137,11 @@ func newZapJSONLogger(cfg *config.LogConfig) *zap.Logger {
}

core := zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zapcore.NewJSONEncoder(encoderConfig),
w,
l,
)
return zap.New(core)
return zap.New(core, options...)
}

// GetLogger will return the default logger
Expand Down
2 changes: 1 addition & 1 deletion src/core-api/pkg/middleware/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

func TestAPILogger(t *testing.T) {
logging.InitLogger(&config.Logger{})
logging.InitLogger(&config.Config{})

r := gin.Default()
r.Use(APILogger())
Expand Down
7 changes: 7 additions & 0 deletions src/core-api/pkg/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"fmt"
"net/http"

"github.com/getsentry/raven-go"
"github.com/gin-gonic/contrib/sentry"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
Expand Down Expand Up @@ -82,7 +84,12 @@ func NewRouter(cfg *config.Config) *gin.Engine {
router.Use(otelgin.Middleware(cfg.Tracing.ServiceName))
}
microGatewayRouter := router.Group("/api/v1/micro-gateway")

// metrics
microGatewayRouter.Use(middleware.Metrics())
// recovery sentry
microGatewayRouter.Use(sentry.Recovery(raven.DefaultClient, false))

microGatewayRouter.Use(middleware.APILogger())
microGatewayRouter.Use(middleware.MicroGatewayInstanceMiddleware())
microGatewayRouter.GET("/:micro_gateway_instance_id/permissions/", microgateway.QueryPermission)
Expand Down

0 comments on commit 9c138ac

Please sign in to comment.