logger: improvements
Some checks failed
lint / lint (pull_request) Successful in 1m8s
pr / test (pull_request) Failing after 1m5s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2023-10-17 01:00:00 +03:00
parent 69c6d78920
commit eb8851ab58
5 changed files with 150 additions and 23 deletions

View File

@@ -27,8 +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()
span.spanID.s, _ = id.New()
span.traceID.s, _ = id.New()
if span.ctx == nil {
span.ctx = context.Background()
}
@@ -56,18 +56,26 @@ type noopEvent struct {
labels []interface{}
}
type noopStringer struct {
s string
}
func (s noopStringer) String() string {
return s.s
}
type noopSpan struct {
ctx context.Context
tracer Tracer
name string
statusMsg string
traceID noopStringer
spanID noopStringer
events []*noopEvent
labels []interface{}
logs []interface{}
kind SpanKind
status SpanStatus
traceID string
spanID string
}
func (s *noopSpan) Finish(opts ...options.Option) {
@@ -103,11 +111,11 @@ func (s *noopSpan) Kind() SpanKind {
}
func (s *noopSpan) TraceID() string {
return s.traceID
return s.traceID.String()
}
func (s *noopSpan) SpanID() string {
return s.spanID
return s.spanID.String()
}
func (s *noopSpan) Status() (SpanStatus, string) {

24
tracer/tracer_test.go Normal file
View File

@@ -0,0 +1,24 @@
package tracer
import (
"bytes"
"context"
"strings"
"testing"
"go.unistack.org/micro/v4/logger"
)
func TestLoggerWithTracer(t *testing.T) {
ctx := context.TODO()
buf := bytes.NewBuffer(nil)
if err := logger.Init(logger.WithOutput(buf)); err != nil {
t.Fatal(err)
}
var span Span
ctx, span = DefaultTracer.Start(ctx, "test")
logger.Info(ctx, "msg")
if !strings.Contains(buf.String(), span.TraceID()) {
t.Fatalf("log does not contains tracer id")
}
}