Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: improve xlog with less and robust api #321

Merged
merged 10 commits into from
Jul 11, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
name: Check PR title
name: "Lint PR"

on:
pull_request:
types:
- opened
- reopened
- edited
- synchronize

jobs:
lint:
main:
name: Validate PR title
runs-on: ubuntu-latest
steps:
- uses: aslafy-z/conventional-pr-title-action@v2.2.5
with:
success-state: Title follows the specification.
failure-state: Title does not follow the specification.
context-name: conventional-pr-title
preset: conventional-changelog-angular@latest
- uses: amannn/action-semantic-pull-request@v4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
102 changes: 4 additions & 98 deletions pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (app *Application) initialize() {
app.servers = make([]server.Server, 0)
app.workers = make([]worker.Worker, 0)
app.jobs = make(map[string]job.Runner)
app.logger = xlog.JupiterLogger
app.logger = xlog.Jupiter()
app.configParser = toml.Unmarshal
app.disableMap = make(map[Disable]bool)
app.stopped = make(chan struct{})
Expand Down Expand Up @@ -251,8 +251,8 @@ func (app *Application) Run(servers ...server.Server) error {

//clean after app quit
func (app *Application) clean() {
_ = xlog.DefaultLogger.Flush()
_ = xlog.JupiterLogger.Flush()
_ = xlog.Default().Sync()
_ = xlog.Jupiter().Sync()
}

// Stop application immediately after necessary cleanup
Expand Down Expand Up @@ -412,104 +412,10 @@ func (app *Application) parseFlags() error {
app.logger.Info("parseFlags disable", xlog.FieldMod(ecode.ModApp))
return nil
}
// flag.Register(&flag.StringFlag{
// Name: "config",
// Usage: "--config",
// EnvVar: "JUPITER_CONFIG",
// Default: "",
// Action: func(name string, fs *flag.FlagSet) {},
// })

// flag.Register(&flag.BoolFlag{
// Name: "version",
// Usage: "--version, print version",
// Default: false,
// Action: func(string, *flag.FlagSet) {
// pkg.PrintVersion()
// os.Exit(0)
// },
// })

// flag.Register(&flag.StringFlag{
// Name: "host",
// Usage: "--host, print host",
// Default: "127.0.0.1",
// Action: func(string, *flag.FlagSet) {},
// })

return flag.Parse()
}

//loadConfig init
// func (app *Application) loadConfig() error {
// if app.isDisable(DisableLoadConfig) {
// app.logger.Info("load config disable", xlog.FieldMod(ecode.ModConfig))
// return nil
// }

// var configAddr = flag.String("config")
// provider, err := manager.NewDataSource(configAddr)
// if err != manager.ErrConfigAddr {
// if err != nil {
// app.logger.Panic("data source: provider error", xlog.FieldMod(ecode.ModConfig), xlog.FieldErr(err))
// }

// if err := conf.LoadFromDataSource(provider, app.configParser); err != nil {
// app.logger.Panic("data source: load config", xlog.FieldMod(ecode.ModConfig), xlog.FieldErrKind(ecode.ErrKindUnmarshalConfigErr), xlog.FieldErr(err))
// }
// } else {
// app.logger.Info("no config... ", xlog.FieldMod(ecode.ModConfig))
// }
// return nil
// }

//initLogger init
// func (app *Application) initLogger() error {
// if conf.Get(xlog.ConfigEntry("default")) != nil {
// xlog.DefaultLogger = xlog.RawConfig(constant.ConfigPrefix + ".logger.default").Build()
// }
// xlog.DefaultLogger.AutoLevel(constant.ConfigPrefix + ".logger.default")

// if conf.Get(constant.ConfigPrefix+".logger.jupiter") != nil {
// xlog.JupiterLogger = xlog.RawConfig(constant.ConfigPrefix + ".logger.jupiter").Build()
// }
// xlog.JupiterLogger.AutoLevel(constant.ConfigPrefix + ".logger.jupiter")

// return nil
// }

//initTracer init
// func (app *Application) initTracer() error {
// // init tracing component jaeger
// if conf.Get("jupiter.trace.jaeger") != nil {
// var config = jaeger.RawConfig("jupiter.trace.jaeger")
// trace.SetGlobalTracer(config.Build())
// }
// return nil
// }

//initSentinel init
// func (app *Application) initSentinel() error {
// // init reliability component sentinel
// if conf.Get("jupiter.reliability.sentinel") != nil {
// app.logger.Info("init sentinel")
// return sentinel.RawConfig("jupiter.reliability.sentinel").Build()
// }
// return nil
// }

//initMaxProcs init
// func (app *Application) initMaxProcs() error {
// if maxProcs := conf.GetInt("maxProc"); maxProcs != 0 {
// runtime.GOMAXPROCS(maxProcs)
// } else {
// if _, err := maxprocs.Set(); err != nil {
// app.logger.Panic("auto max procs", xlog.FieldMod(ecode.ModProc), xlog.FieldErrKind(ecode.ErrKindAny), xlog.FieldErr(err))
// }
// }
// app.logger.Info("auto max procs", xlog.FieldMod(ecode.ModProc), xlog.Int64("procs", int64(runtime.GOMAXPROCS(-1))))
// return nil
// }

func (app *Application) isDisable(d Disable) bool {
b, ok := app.disableMap[d]
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions pkg/autoproc/autoproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import (
func init() {
// 初始化注册中心
if _, err := maxprocs.Set(); err != nil {
xlog.Panic("auto max procs", xlog.FieldMod(ecode.ModProc), xlog.FieldErrKind(ecode.ErrKindAny), xlog.FieldErr(err))
xlog.Jupiter().Panic("auto max procs", xlog.FieldMod(ecode.ModProc), xlog.FieldErrKind(ecode.ErrKindAny), xlog.FieldErr(err))
}
conf.OnLoaded(func(c *conf.Configuration) {
if maxProcs := conf.GetInt("maxProc"); maxProcs != 0 {
runtime.GOMAXPROCS(maxProcs)
}
xlog.Info("auto max procs", xlog.FieldMod(ecode.ModProc), xlog.Int64("procs", int64(runtime.GOMAXPROCS(-1))))
xlog.Jupiter().Info("auto max procs", xlog.FieldMod(ecode.ModProc), xlog.Int64("procs", int64(runtime.GOMAXPROCS(-1))))
})
}
5 changes: 3 additions & 2 deletions pkg/client/etcdv3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/douyu/jupiter/pkg/flag"
"github.com/douyu/jupiter/pkg/util/xtime"
"github.com/douyu/jupiter/pkg/xlog"
"go.uber.org/zap"
)

var ConfigPrefix = constant.ConfigPrefix + ".etcdv3"
Expand Down Expand Up @@ -57,7 +58,7 @@ func DefaultConfig() *Config {
BasicAuth: false,
ConnectTimeout: xtime.Duration("5s"),
Secure: false,
logger: xlog.JupiterLogger.With(xlog.FieldMod("client.etcd")),
logger: xlog.Jupiter().With(xlog.FieldMod("client.etcd")),
}
}

Expand Down Expand Up @@ -89,7 +90,7 @@ func (config *Config) Build() (*Client, error) {
func (config *Config) MustBuild() *Client {
client, err := config.Build()
if err != nil {
xlog.Panicf("build etcd client failed: %v", err)
xlog.Jupiter().Panic("build etcd client failed", zap.Error(err))
}
return client
}
4 changes: 2 additions & 2 deletions pkg/client/etcdv3/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ func (client *Client) WatchPrefix(ctx context.Context, prefix string) (*Watch, e
w.revision = n.Header.GetRevision()
}
if err := n.Err(); err != nil {
xlog.Error(ecode.MsgWatchRequestErr, xlog.FieldErrKind(ecode.ErrKindRegisterErr), xlog.FieldErr(err), xlog.FieldAddr(prefix))
xlog.Jupiter().Error(ecode.MsgWatchRequestErr, xlog.FieldErrKind(ecode.ErrKindRegisterErr), xlog.FieldErr(err), xlog.FieldAddr(prefix))
continue
}
for _, ev := range n.Events {
select {
case w.eventChan <- ev:
default:
xlog.Error("watch etcd with prefix", xlog.Any("err", "block event chan, drop event message"))
xlog.Jupiter().Error("watch etcd with prefix", xlog.Any("err", "block event chan, drop event message"))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/grpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
package grpc

import (
"github.com/douyu/jupiter/pkg/util/xtime"
"time"

"github.com/douyu/jupiter/pkg/conf"
"github.com/douyu/jupiter/pkg/ecode"
"github.com/douyu/jupiter/pkg/util/xtime"
"github.com/douyu/jupiter/pkg/xlog"
"google.golang.org/grpc"
"google.golang.org/grpc/balancer/roundrobin"
Expand Down Expand Up @@ -57,7 +57,7 @@ func DefaultConfig() *Config {
dialOptions: []grpc.DialOption{
grpc.WithInsecure(),
},
logger: xlog.JupiterLogger.With(xlog.FieldMod(ecode.ModClientGrpc)),
logger: xlog.Jupiter().With(xlog.FieldMod(ecode.ModClientGrpc)),
BalancerName: roundrobin.Name, // round robin by default
DialTimeout: time.Second * 3,
ReadTimeout: xtime.Duration("1s"),
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/redis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func DefaultRedisConfig() Config {
EnableTrace: false,
SlowThreshold: xtime.Duration("250ms"),
OnDialError: "panic",
logger: xlog.JupiterLogger,
logger: xlog.Jupiter(),
}
}

Expand All @@ -100,7 +100,7 @@ func RawRedisConfig(key string) Config {
var config = DefaultRedisConfig()

if err := conf.UnmarshalKey(key, &config); err != nil {
xlog.Panic("unmarshal redisConfig",
xlog.Jupiter().Panic("unmarshal redisConfig",
xlog.String("key", key),
xlog.Any("redisConfig", config),
xlog.String("error", err.Error()))
Expand Down
21 changes: 12 additions & 9 deletions pkg/client/resty/resty.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ package resty

import (
"errors"
"github.com/douyu/jupiter/pkg/xtrace"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
"net/http"
"time"

Expand All @@ -28,13 +24,16 @@ import (
"github.com/douyu/jupiter/pkg/util/xdebug"
"github.com/douyu/jupiter/pkg/util/xtime"
"github.com/douyu/jupiter/pkg/xlog"
"github.com/douyu/jupiter/pkg/xtrace"
"github.com/go-resty/resty/v2"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)

var _logger = xlog.DefaultLogger.With(xlog.FieldMod("resty"))
var errSlowCommand = errors.New("http resty slow command")

// Config ...
Expand Down Expand Up @@ -64,8 +63,11 @@ type (
// 访问日志开关
EnableAccessLog bool `json:"enableAccessLog" toml:"enableAccessLog"`
// 熔断降级
EnableSentinel bool `json:"enableSentinel" toml:"enableSentinel"`
EnableSentinel bool `json:"enableSentinel" toml:"enableSentinel"`
// 重试
RetryCondition resty.RetryConditionFunc `json:"-" toml:"-"`
// 日志
logger *zap.Logger
}
)

Expand All @@ -78,7 +80,7 @@ func StdConfig(name string) Config {
func RawConfig(key string) Config {
var config = DefaultConfig()
if err := conf.UnmarshalKey(key, &config, conf.TagName("toml")); err != nil {
xlog.Panic("unmarshal config", xlog.FieldName(key), xlog.FieldExtMessage(config))
xlog.Jupiter().Panic("unmarshal config", xlog.FieldName(key), xlog.FieldExtMessage(config))
}

if xdebug.IsDevelopmentMode() {
Expand All @@ -101,6 +103,7 @@ func DefaultConfig() Config {
Timeout: xtime.Duration("3000ms"),
EnableAccessLog: false,
EnableSentinel: true,
logger: xlog.Jupiter().With(xlog.FieldMod("resty")),
}
}

Expand Down Expand Up @@ -184,7 +187,7 @@ func (config *Config) Build() (*resty.Client, error) {
if config.SlowThreshold > time.Duration(0) {
// 慢日志
if cost > config.SlowThreshold {
_logger.Error("slow",
config.logger.Error("slow",
xlog.FieldErr(errSlowCommand),
xlog.FieldMethod(r.Request.Method),
xlog.FieldCost(cost),
Expand All @@ -203,7 +206,7 @@ func (config *Config) Build() (*resty.Client, error) {
func (c *Config) MustBuild() *resty.Client {
cc, err := c.Build()
if err != nil {
xlog.Panic("resty build failed", zap.Error(err), zap.Any("config", c))
xlog.Jupiter().Panic("resty build failed", zap.Error(err), zap.Any("config", c))
}

return cc
Expand Down
Loading