github.com/go-kratos/kratos/v2/app.go
package kratos
import (
"context"
"errors"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/transport"
"github.com/google/uuid"
"golang.org/x/sync/errgroup"
)
// AppInfo is application context value.
type AppInfo interface {
ID() string
Name() string
Version() string
Metadata() map[string]string
Endpoint() []string
}
// App is an application components lifecycle manager.
type App struct {
opts options
ctx context.Context
cancel func()
mu sync.Mutex
instance *registry.ServiceInstance
}
// New create an application lifecycle manager.
func New(opts ...Option) *App {
o := options{
ctx: context.Background(),
sigs: []os.Signal{syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT},
registrarTimeout: 10 * time.Second,
stopTimeout: 10 * time.Second,
}
if id, err := uuid.NewUUID(); err == nil {
o.id = id.String()
}
for _, opt := range opts {
opt(&o)
}
if o.logger != nil {
log.SetLogger(o.logger)
}
ctx, cancel := context.WithCancel(o.ctx)
return &App{
ctx: ctx,
cancel: cancel,
opts: o,
}
}
// ID returns app instance id.
func (a *App) ID() string { return a.opts.id }
// Name returns service name.
func (a *App) Name() string { return a.opts.name }
// Version returns app version.
func (a *App) Version() string { return a.opts.version }
// Metadata returns service metadata.
func (a *App) Metadata() map[string]string { return a.opts.metadata }
// Endpoint returns endpoints.
func (a *App) Endpoint() []string {
if a.instance != nil {
return a.instance.Endpoints
}
return nil
}
// Run executes all OnStart hooks registered with the application's Lifecycle.
func (a *App) Run() error {
instance, err := a.buildInstance()
if err != nil {
return err
}
a.mu.Lock()
a.instance = instance
a.mu.Unlock()
eg, ctx := errgroup.WithContext(NewContext(a.ctx, a))
wg := sync.WaitGroup{}
for _, srv := range a.opts.servers {
srv := srv
eg.Go(func() error {
<-ctx.Done() // wait for stop signal
stopCtx, cancel := context.WithTimeout(NewContext(a.opts.ctx, a), a.opts.stopTimeout)
defer cancel()
return srv.Stop(stopCtx)
})
wg.Add(1)
eg.Go(func() error {
wg.Done()
return srv.Start(NewContext(a.opts.ctx, a))
})
}
wg.Wait()
if a.opts.registrar != nil {
rctx, rcancel := context.WithTimeout(ctx, a.opts.registrarTimeout)
defer rcancel()
if err := a.opts.registrar.Register(rctx, instance); err != nil {
return err
}
}
c := make(chan os.Signal, 1)
signal.Notify(c, a.opts.sigs...)
eg.Go(func() error {
select {
case <-ctx.Done():
return nil
case <-c:
return a.Stop()
}
})
if err := eg.Wait(); err != nil && !errors.Is(err, context.Canceled) {
return err
}
return nil
}
// Stop gracefully stops the application.
func (a *App) Stop() error {
a.mu.Lock()
instance := a.instance
a.mu.Unlock()
if a.opts.registrar != nil && instance != nil {
ctx, cancel := context.WithTimeout(NewContext(a.ctx, a), a.opts.registrarTimeout)
defer cancel()
if err := a.opts.registrar.Deregister(ctx, instance); err != nil {
return err
}
}
if a.cancel != nil {
a.cancel()
}
return nil
}
func (a App) buildInstance() (registry.ServiceInstance, error) {
endpoints := make([]string, 0, len(a.opts.endpoints))
for _, e := range a.opts.endpoints {
endpoints = append(endpoints, e.String())
}
if len(endpoints) == 0 {
for _, srv := range a.opts.servers {
if r, ok := srv.(transport.Endpointer); ok {
e, err := r.Endpoint()
if err != nil {
return nil, err
}
endpoints = append(endpoints, e.String())
}
}
}
return ®istry.ServiceInstance{
ID: a.opts.id,
Name: a.opts.name,
Version: a.opts.version,
Metadata: a.opts.metadata,
Endpoints: endpoints,
}, nil
}
type appKey struct{}
// NewContext returns a new Context that carries value.
func NewContext(ctx context.Context, s AppInfo) context.Context {
return context.WithValue(ctx, appKey{}, s)
}
// FromContext returns the Transport value stored in ctx, if any.
func FromContext(ctx context.Context) (s AppInfo, ok bool) {
s, ok = ctx.Value(appKey{}).(AppInfo)
return
}
Log
过滤日志、日志内容脱敏
github.com/go-kratos/kratos/v2/log/filter.go
// FilterOption is filter option.
type FilterOption func(*Filter)
//脱敏替换的符号
const fuzzyStr = "***"
//对参数进行赋值,其他参数类似,所以省略代码了
func FilterKey(key ...string) FilterOption {
return func(o *Filter) {
for _, v := range key {
o.key[v] = struct{}{}
}
}
}
// Filter is a logger filter.
type Filter struct {
logger Logger //日志实例
level Level //日志等级
key map[interface{}]struct{} //日志key
value map[interface{}]struct{} //日志value
filter func(level Level, keyvals ...interface{}) bool //自定义过滤函数
}
// Log Print log by level and keyvals.
func (f Filter) Log(level Level, keyvals ...interface{}) error {
//过滤大于参数错误值
if level < f.level {
return nil
}
// fkv is used to provide a slice to contains both logger.prefix and keyvals for filter
var fkv []interface{}
l, ok := f.logger.(logger)
if ok && len(l.prefix) > 0 {
fkv = make([]interface{}, 0, len(l.prefix)+len(keyvals))
fkv = append(fkv, l.prefix...)
fkv = append(fkv, keyvals...)
}
if !ok {
fkv = keyvals
}
//通过自定义的函数过滤日志
if f.filter != nil && f.filter(level, fkv...) {
return nil
}
//通过定义的key对值进行脱敏,通过value对值进行脱敏
if len(f.key) > 0 || len(f.value) > 0 {
for i := 0; i < len(keyvals); i += 2 {
v := i + 1
if v >= len(keyvals) {
continue
}
if _, ok := f.key[keyvals[i]]; ok {
keyvals[v] = fuzzyStr
}
if _, ok := f.value[keyvals[v]]; ok {
keyvals[v] = fuzzyStr
}
}
}
return f.logger.Log(level, keyvals...)
}
文件路径:github.com/go-kratos/kratos/v2/log/std.go
// 默认可以使用stdlog
var DefaultLogger = NewStdLogger(log.Writer())
// Logger is a logger interface.
type Logger interface {
Log(level Level, keyvals ...interface{}) error
}
type logger struct {
logger Logger
prefix []interface{}
hasValuer bool
ctx context.Context
}
func (c *logger) Log(level Level, keyvals ...interface{}) error {
kvs := make([]interface{}, 0, len(c.prefix)+len(keyvals))
kvs = append(kvs, c.prefix...)
if c.hasValuer {
bindValues(c.ctx, kvs)
}
kvs = append(kvs, keyvals...)
if err := c.logger.Log(level, kvs...); err != nil {
return err
}
return nil
}
// With with logger fields.
func With(l Logger, kv ...interface{}) Logger {
c, ok := l.(*logger)
if !ok {
return &logger{logger: l, prefix: kv, hasValuer: containsValuer(kv), ctx: context.Background()}
}
kvs := make([]interface{}, 0, len(c.prefix)+len(kv))
kvs = append(kvs, c.prefix...)
kvs = append(kvs, kv...)
return &logger{
logger: c.logger,
prefix: kvs,
hasValuer: containsValuer(kvs),
ctx: c.ctx,
}
}
// WithContext returns a shallow copy of l with its context changed
// to ctx. The provided ctx must be non-nil.
func WithContext(ctx context.Context, l Logger) Logger {
c, ok := l.(*logger)
if !ok {
return &logger{logger: l, ctx: ctx}
}
return &logger{
logger: c.logger,
prefix: c.prefix,
hasValuer: c.hasValuer,
ctx: ctx,
}
}
控制台输出日志(默认日志)
文件路径:github.com/go-kratos/kratos/v2/log/std.go
type stdLogger struct {
log *log.Logger
pool *sync.Pool
}
func (l stdLogger) Log(level Level, keyvals ...interface{}) error {
//keyvals为单数时增加一个默认值
if (len(keyvals) & 1) == 1 {
keyvals = append(keyvals, "KEYVALS UNPAIRED")
}
//使用sync.Pool来存储字符对象,减少创建对象的开销
buf := l.pool.Get().(bytes.Buffer)
//拼接字符串,最终为 INFO ts=2023-09-28T16:42:45+08:00
buf.WriteString(level.String())
for i := 0; i < len(keyvals); i += 2 {
_, _ = fmt.Fprintf(buf, " %s=%v", keyvals[i], keyvals[i+1])
}
_ = l.log.Output(4, buf.String()) //nolint:gomnd
buf.Reset()
l.pool.Put(buf)
return nil
}
Selector
标签:return,nil,ctx,keyvals,Kraots,logger,opts
From: https://www.cnblogs.com/sishuiliunian6x/p/17778323.html