logger: add ability to fill fileds from context
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"go.unistack.org/micro/v4/options"
|
||||
"go.unistack.org/micro/v4/util/id"
|
||||
)
|
||||
|
||||
var _ Tracer = (*noopTracer)(nil)
|
||||
@@ -26,6 +27,8 @@ func (t *noopTracer) Start(ctx context.Context, name string, opts ...options.Opt
|
||||
labels: options.Labels,
|
||||
kind: options.Kind,
|
||||
}
|
||||
span.spanID, _ = id.New()
|
||||
span.traceID, _ = id.New()
|
||||
if span.ctx == nil {
|
||||
span.ctx = context.Background()
|
||||
}
|
||||
@@ -63,6 +66,8 @@ type noopSpan struct {
|
||||
logs []interface{}
|
||||
kind SpanKind
|
||||
status SpanStatus
|
||||
traceID string
|
||||
spanID string
|
||||
}
|
||||
|
||||
func (s *noopSpan) Finish(opts ...options.Option) {
|
||||
@@ -97,6 +102,14 @@ func (s *noopSpan) Kind() SpanKind {
|
||||
return s.kind
|
||||
}
|
||||
|
||||
func (s *noopSpan) TraceID() string {
|
||||
return s.traceID
|
||||
}
|
||||
|
||||
func (s *noopSpan) SpanID() string {
|
||||
return s.spanID
|
||||
}
|
||||
|
||||
func (s *noopSpan) Status() (SpanStatus, string) {
|
||||
return s.status, s.statusMsg
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"go.unistack.org/micro/v4/logger"
|
||||
"go.unistack.org/micro/v4/options"
|
||||
)
|
||||
|
||||
@@ -45,6 +46,20 @@ type Span interface {
|
||||
AddLogs(kv ...interface{})
|
||||
// Kind returns span kind
|
||||
Kind() SpanKind
|
||||
// TraceID returns trace id
|
||||
TraceID() string
|
||||
// SpanID returns span id
|
||||
SpanID() string
|
||||
}
|
||||
|
||||
func init() {
|
||||
logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, func(ctx context.Context) []interface{} {
|
||||
span, ok := SpanFromContext(ctx)
|
||||
if !ok || span == nil {
|
||||
return nil
|
||||
}
|
||||
return []interface{}{"trace", span.TraceID(), "span", span.SpanID()}
|
||||
})
|
||||
}
|
||||
|
||||
// sort labels alphabeticaly by label name
|
||||
|
||||
Reference in New Issue
Block a user