2022-03-05 19:10:19 +03:00
|
|
|
package opentracing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-05-09 19:30:32 +03:00
|
|
|
"fmt"
|
2022-03-05 19:10:19 +03:00
|
|
|
"testing"
|
|
|
|
|
2024-03-06 12:36:29 +03:00
|
|
|
"github.com/opentracing/opentracing-go/mocktracer"
|
2024-07-17 17:37:06 +03:00
|
|
|
// jconfig "github.com/uber/jaeger-client-go/config"
|
|
|
|
"go.unistack.org/micro/v3/logger/slog"
|
2022-03-05 19:10:19 +03:00
|
|
|
"go.unistack.org/micro/v3/metadata"
|
2024-03-06 12:36:29 +03:00
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2022-03-05 19:10:19 +03:00
|
|
|
)
|
|
|
|
|
2024-07-17 17:37:06 +03:00
|
|
|
func TestNoopTraceID(t *testing.T) {
|
2024-03-06 12:36:29 +03:00
|
|
|
md := metadata.New(1)
|
2022-03-05 19:10:19 +03:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
ctx = metadata.NewIncomingContext(ctx, md)
|
|
|
|
|
2024-03-06 12:36:29 +03:00
|
|
|
tr := NewTracer(Tracer(mocktracer.New()))
|
|
|
|
if err := tr.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
2024-03-06 12:36:29 +03:00
|
|
|
var sp tracer.Span
|
|
|
|
|
|
|
|
ctx, sp = tr.Start(ctx, "test")
|
|
|
|
if v := sp.TraceID(); v != "43" {
|
|
|
|
t.Fatalf("invalid span trace id %#+v", v)
|
|
|
|
}
|
|
|
|
if v := sp.SpanID(); v != "44" {
|
|
|
|
t.Fatalf("invalid span span id %#+v", v)
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
2024-07-17 17:37:06 +03:00
|
|
|
|
|
|
|
l := slog.NewLogger()
|
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
l.Info(ctx, "msg")
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
func TestRealTraceID(t *testing.T) {
|
|
|
|
md := metadata.New(1)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
ctx = metadata.NewIncomingContext(ctx, md)
|
|
|
|
|
|
|
|
jcfg := &jconfig.Configuration{
|
|
|
|
ServiceName: "test",
|
|
|
|
Sampler: &jconfig.SamplerConfig{
|
|
|
|
Type: "const",
|
|
|
|
Param: 1,
|
|
|
|
},
|
|
|
|
Reporter: &jconfig.ReporterConfig{
|
|
|
|
LogSpans: true,
|
|
|
|
QueueSize: 100,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
jtr, closer, err := jcfg.NewTracer()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer closer.Close()
|
|
|
|
|
|
|
|
tr := NewTracer(Tracer(jtr))
|
|
|
|
if err := tr.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var sp tracer.Span
|
|
|
|
|
|
|
|
ctx, sp = tr.Start(ctx, "test")
|
|
|
|
if v := sp.TraceID(); v != "43" {
|
|
|
|
t.Fatalf("invalid span trace id %#+v", v)
|
|
|
|
}
|
|
|
|
if v := sp.SpanID(); v != "44" {
|
|
|
|
t.Fatalf("invalid span span id %#+v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
l := slog.NewLogger()
|
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
l.Info(ctx, "msg")
|
2022-03-05 19:10:19 +03:00
|
|
|
}
|
2024-07-17 17:51:51 +03:00
|
|
|
*/
|
2024-03-17 00:51:29 +03:00
|
|
|
|
|
|
|
func TestTraceTags(t *testing.T) {
|
|
|
|
md := metadata.New(1)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
ctx = metadata.NewIncomingContext(ctx, md)
|
|
|
|
|
|
|
|
mtr := mocktracer.New()
|
|
|
|
tr := NewTracer(Tracer(mtr))
|
|
|
|
if err := tr.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var sp tracer.Span
|
|
|
|
|
|
|
|
ctx, sp = tr.Start(ctx, "test", tracer.WithSpanLabels("key", "val", "odd"))
|
2024-05-09 19:30:32 +03:00
|
|
|
sp.Finish(tracer.WithSpanLabels("xkey", "xval"))
|
|
|
|
_ = ctx
|
2024-03-17 00:51:29 +03:00
|
|
|
msp := mtr.FinishedSpans()[0]
|
2024-05-09 19:30:32 +03:00
|
|
|
|
|
|
|
if "val" != fmt.Sprintf("%v", msp.Tags()["key"]) {
|
|
|
|
t.Fatal("mock span invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
if "xval" != fmt.Sprintf("%v", msp.Tags()["xkey"]) {
|
|
|
|
t.Fatalf("mock span invalid %#+v", msp)
|
|
|
|
}
|
2024-03-17 00:51:29 +03:00
|
|
|
}
|