tracer: add default context attr funcs option

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-07-06 00:09:27 +03:00
parent 00b3ceb468
commit 35e62fbeb0
4 changed files with 10 additions and 4 deletions

View File

@ -1,5 +1,5 @@
// Package logger provides a log interface // Package logger provides a log interface
package logger // import "go.unistack.org/micro/v3/logger" package logger
import ( import (
"context" "context"

View File

@ -74,7 +74,7 @@ func NewOptions(opts ...Option) Options {
return options return options
} }
// WithContextAttrFuncs appends default funcs for the context arrts filler // WithContextAttrFuncs appends default funcs for the context attrs filler
func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option { func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option {
return func(o *Options) { return func(o *Options) {
o.ContextAttrFuncs = append(o.ContextAttrFuncs, fncs...) o.ContextAttrFuncs = append(o.ContextAttrFuncs, fncs...)

View File

@ -140,6 +140,8 @@ type Options struct {
Logger logger.Logger Logger logger.Logger
// Name of the tracer // Name of the tracer
Name string Name string
// ContextAttrFuncs contains funcs that provides tracing
ContextAttrFuncs []ContextAttrFunc
} }
// Option func signature // Option func signature
@ -178,6 +180,7 @@ func NewOptions(opts ...Option) Options {
options := Options{ options := Options{
Logger: logger.DefaultLogger, Logger: logger.DefaultLogger,
Context: context.Background(), Context: context.Background(),
ContextAttrFuncs: DefaultContextAttrFuncs,
} }
for _, o := range opts { for _, o := range opts {
o(&options) o(&options)

View File

@ -21,8 +21,11 @@ var (
"HealthService.Ready", "HealthService.Ready",
"HealthService.Version", "HealthService.Version",
} }
DefaultContextAttrFuncs []ContextAttrFunc
) )
type ContextAttrFunc func(ctx context.Context) []interface{}
func init() { func init() {
logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs,
func(ctx context.Context) []interface{} { func(ctx context.Context) []interface{} {