logger: add ability to fill fileds from context
Some checks failed
lint / lint (pull_request) Successful in 1m1s
pr / test (pull_request) Failing after 1m7s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2023-10-16 23:43:39 +03:00
parent 5c3fa31ad2
commit 7568779db4
6 changed files with 133 additions and 64 deletions

View File

@@ -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
}

View File

@@ -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