Compare commits

...

3 Commits
v3.0.0 ... v3

Author SHA1 Message Date
b8aea859fe Merge pull request '#347 update micro' (#1) from kgorbunov/micro-tracer-instana:#347-v3 into v3
Reviewed-on: #1
2024-09-16 15:00:49 +03:00
Gorbunov Kirill Andreevich
dda452fb89 #347 update micro 2024-09-16 14:59:56 +03:00
cdf097d959 updates
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-11 14:03:53 +03:00
5 changed files with 226 additions and 29 deletions

11
go.mod
View File

@ -3,12 +3,19 @@ module go.unistack.org/micro-tracer-instana/v3
go 1.20 go 1.20
require ( require (
github.com/instana/go-sensor v1.55.2 github.com/instana/go-sensor v1.60.0
github.com/opentracing/opentracing-go v1.2.0 github.com/opentracing/opentracing-go v1.2.0
go.unistack.org/micro/v3 v3.10.25 go.unistack.org/micro/v3 v3.10.85
) )
require ( require (
dario.cat/mergo v1.0.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/looplab/fsm v1.0.1 // indirect github.com/looplab/fsm v1.0.1 // indirect
github.com/silas/dag v0.0.0-20220518035006-a7e85ada93c5 // indirect
github.com/stretchr/testify v1.8.3 // indirect github.com/stretchr/testify v1.8.3 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
) )

29
go.sum
View File

@ -1,14 +1,24 @@
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/instana/go-sensor v1.55.2 h1:XdLN38mSFsHpaL+jIDkE/ZrW7pxgPeUC/bV9bSwVHyM= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/instana/go-sensor v1.55.2/go.mod h1:Ks06EG9Da5O3hbdJiHIePG/vNmToovkaJjMlUBd70Yc= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/instana/go-sensor v1.60.0 h1:lsAiwmK5AtNQ9gLHPLvZt0aLddZxM4R0oakkO7rXO2U=
github.com/instana/go-sensor v1.60.0/go.mod h1:Ks06EG9Da5O3hbdJiHIePG/vNmToovkaJjMlUBd70Yc=
github.com/looplab/fsm v1.0.1 h1:OEW0ORrIx095N/6lgoGkFkotqH6s7vaFPsgjLAaF5QU= github.com/looplab/fsm v1.0.1 h1:OEW0ORrIx095N/6lgoGkFkotqH6s7vaFPsgjLAaF5QU=
github.com/looplab/fsm v1.0.1/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4= github.com/looplab/fsm v1.0.1/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/silas/dag v0.0.0-20220518035006-a7e85ada93c5 h1:G/FZtUu7a6NTWl3KUHMV9jkLAh/Rvtf03NWMHaEDl+E=
github.com/silas/dag v0.0.0-20220518035006-a7e85ada93c5/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@ -18,8 +28,19 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.unistack.org/micro/v3 v3.10.25 h1:A0epdZHOqjnXx103wwFhPKgmvVVbScvfbmn3HmHz1wE= go.unistack.org/micro/v3 v3.10.50 h1:U0aYtKHrpGHFDYdTRgsIYxIejU4HKDKbLHcLZngvDIs=
go.unistack.org/micro/v3 v3.10.25/go.mod h1:ALkeXpqChYDjx8KPi7tz9mmIyOnob6nlNswsg8BnZjQ= go.unistack.org/micro/v3 v3.10.50/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
go.unistack.org/micro/v3 v3.10.85 h1:yKM5eqenL6x2eRbdsx+IEwtpx/RE7+quxjHQdyLE2PI=
go.unistack.org/micro/v3 v3.10.85/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e h1:NumxXLPfHSndr3wBBdeKiVHjGVFzi9RX2HwwQke94iY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@ -2,60 +2,107 @@ package instana
import ( import (
"context" "context"
"fmt"
instana "github.com/instana/go-sensor"
sensor "github.com/instana/go-sensor" sensor "github.com/instana/go-sensor"
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log" "github.com/opentracing/opentracing-go/log"
"go.unistack.org/micro/v3/metadata" "go.unistack.org/micro/v3/metadata"
"go.unistack.org/micro/v3/tracer" "go.unistack.org/micro/v3/tracer"
rutil "go.unistack.org/micro/v3/util/reflect"
"go.unistack.org/micro/v3/util/sort"
) )
var _ tracer.Tracer = &Tracer{} var _ tracer.Tracer = (*Tracer)(nil)
type Tracer struct { type Tracer struct {
opts tracer.Options opts tracer.Options
sensor sensor.Tracer sensor sensor.Tracer
} }
func (ot *Tracer) Name() string { func (t *Tracer) Name() string {
return ot.opts.Name return t.opts.Name
} }
func (ot *Tracer) Flush(ctx context.Context) error { func (t *Tracer) Flush(ctx context.Context) error {
sensor.ShutdownSensor()
return nil return nil
} }
func (ot *Tracer) Init(opts ...tracer.Option) error { func (t *Tracer) Init(opts ...tracer.Option) error {
for _, o := range opts { for _, o := range opts {
o(&ot.opts) o(&t.opts)
} }
sensorOptions := sensor.DefaultOptions() sensorOptions := sensor.DefaultOptions()
if v, ok := ot.opts.Context.Value(tracerOptionsKey{}).(*sensor.Options); ok && v != nil { if v, ok := t.opts.Context.Value(tracerOptionsKey{}).(*sensor.Options); ok && v != nil {
sensorOptions = v sensorOptions = v
} }
ot.sensor = sensor.NewSensorWithTracer(sensor.NewTracerWithOptions(sensorOptions)) if sensorOptions.Service == "" {
sensorOptions.Service = "micro"
}
var recorder instana.SpanRecorder
recorder = &sensor.Recorder{}
if v, ok := t.opts.Context.Value(recorderKey{}).(sensor.SpanRecorder); ok && v != nil {
recorder = v
}
t.sensor = sensor.NewSensorWithTracer(sensor.NewTracerWithEverything(sensorOptions, recorder))
sensor.StartMetrics(sensorOptions)
return nil return nil
} }
func (ot *Tracer) Start(ctx context.Context, name string, opts ...tracer.SpanOption) (context.Context, tracer.Span) { type idStringer struct {
s string
}
func (s idStringer) String() string {
return s.s
}
type spanContext interface {
TraceID() idStringer
SpanID() idStringer
}
func (t *Tracer) Start(ctx context.Context, name string, opts ...tracer.SpanOption) (context.Context, tracer.Span) {
options := tracer.NewSpanOptions(opts...) options := tracer.NewSpanOptions(opts...)
var span opentracing.Span var span opentracing.Span
switch options.Kind { switch options.Kind {
case tracer.SpanKindInternal, tracer.SpanKindUnspecified: case tracer.SpanKindInternal, tracer.SpanKindUnspecified:
ctx, span = ot.startSpanFromContext(ctx, name) ctx, span = t.startSpanFromContext(ctx, name)
case tracer.SpanKindClient, tracer.SpanKindProducer: case tracer.SpanKindClient, tracer.SpanKindProducer:
ctx, span = ot.startSpanFromOutgoingContext(ctx, name) ctx, span = t.startSpanFromOutgoingContext(ctx, name)
case tracer.SpanKindServer, tracer.SpanKindConsumer: case tracer.SpanKindServer, tracer.SpanKindConsumer:
ctx, span = ot.startSpanFromIncomingContext(ctx, name) ctx, span = t.startSpanFromIncomingContext(ctx, name)
} }
return ctx, &otSpan{span: span, opts: options}
sp := &otSpan{topts: t.opts, span: span, opts: options, sensor: t.sensor, status: tracer.SpanStatusOK}
spctx := span.Context()
if v, ok := spctx.(spanContext); ok {
sp.traceID = v.TraceID().String()
sp.spanID = v.SpanID().String()
} else {
if val, err := rutil.StructFieldByName(spctx, "TraceID"); err == nil {
sp.traceID = fmt.Sprintf("%v", val)
}
if val, err := rutil.StructFieldByName(spctx, "SpanID"); err == nil {
sp.spanID = fmt.Sprintf("%v", val)
}
}
return ctx, sp
} }
type otSpan struct { type otSpan struct {
span opentracing.Span span opentracing.Span
spanID string
traceID string
sensor sensor.Tracer sensor sensor.Tracer
topts tracer.Options topts tracer.Options
opts tracer.SpanOptions opts tracer.SpanOptions
@ -63,6 +110,14 @@ type otSpan struct {
statusMsg string statusMsg string
} }
func (os *otSpan) TraceID() string {
return os.traceID
}
func (os *otSpan) SpanID() string {
return os.spanID
}
func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) { func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) {
switch st { switch st {
case tracer.SpanStatusError: case tracer.SpanStatusError:
@ -80,10 +135,37 @@ func (os *otSpan) Tracer() tracer.Tracer {
return &Tracer{sensor: os.sensor, opts: os.topts} return &Tracer{sensor: os.sensor, opts: os.topts}
} }
func (os *otSpan) AddLogs(kv ...interface{}) {
os.span.LogKV(kv...)
}
func (os *otSpan) Finish(opts ...tracer.SpanOption) { func (os *otSpan) Finish(opts ...tracer.SpanOption) {
if len(os.opts.Labels) > 0 { if len(os.opts.Labels)%2 != 0 {
os.span.LogKV(os.opts.Labels...) os.opts.Labels = os.opts.Labels[:len(os.opts.Labels)-1]
} }
os.opts.Labels = sort.Uniq(os.opts.Labels)
for idx := 0; idx < len(os.opts.Labels); idx += 2 {
switch os.opts.Labels[idx] {
case "err":
os.status = tracer.SpanStatusError
os.statusMsg = fmt.Sprintf("%v", os.opts.Labels[idx+1])
case "error":
continue
case "X-Request-Id", "x-request-id":
os.span.SetTag("x-request-id", os.opts.Labels[idx+1])
case "rpc.call", "rpc.call_type", "rpc.flavor", "rpc.service", "rpc.method",
"sdk.database", "db.statement", "db.args", "db.query", "db.method",
"messaging.destination.name", "messaging.source.name", "messaging.operation":
os.span.SetTag(fmt.Sprintf("%v", os.opts.Labels[idx]), os.opts.Labels[idx+1])
default:
os.span.LogKV(os.opts.Labels[idx], os.opts.Labels[idx+1])
}
}
if os.status == tracer.SpanStatusError {
os.span.SetTag("error", true)
os.span.LogKV("error", os.statusMsg)
}
os.span.SetTag("span.kind", os.opts.Kind)
os.span.Finish() os.span.Finish()
} }
@ -120,14 +202,34 @@ func spanFromContext(ctx context.Context) (opentracing.Span, bool) {
return sensor.SpanFromContext(ctx) return sensor.SpanFromContext(ctx)
} }
func (ot *Tracer) startSpanFromContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) { func (t *Tracer) startSpanFromAny(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
if tracerSpan, ok := tracer.SpanFromContext(ctx); ok && tracerSpan != nil {
return t.startSpanFromContext(ctx, name, opts...)
}
if otSpan := opentracing.SpanFromContext(ctx); otSpan != nil {
return t.startSpanFromContext(ctx, name, opts...)
}
if md, ok := metadata.FromIncomingContext(ctx); ok && md != nil {
return t.startSpanFromIncomingContext(ctx, name, opts...)
}
if md, ok := metadata.FromOutgoingContext(ctx); ok && md != nil {
return t.startSpanFromOutgoingContext(ctx, name, opts...)
}
return t.startSpanFromContext(ctx, name, opts...)
}
func (t *Tracer) startSpanFromContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil { if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
opts = append(opts, opentracing.ChildOf(parentSpan.Context())) opts = append(opts, opentracing.ChildOf(parentSpan.Context()))
} }
md := metadata.New(1) md := metadata.New(1)
sp := ot.sensor.StartSpan(name, opts...) sp := t.sensor.StartSpan(name, opts...)
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(md)); err != nil { if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(md)); err != nil {
return nil, nil return nil, nil
} }
@ -137,12 +239,12 @@ func (ot *Tracer) startSpanFromContext(ctx context.Context, name string, opts ..
return ctx, sp return ctx, sp
} }
func (ot *Tracer) startSpanFromOutgoingContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) { func (t *Tracer) startSpanFromOutgoingContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
var parentCtx opentracing.SpanContext var parentCtx opentracing.SpanContext
md, ok := metadata.FromOutgoingContext(ctx) md, ok := metadata.FromOutgoingContext(ctx)
if ok && md != nil { if ok && md != nil {
if spanCtx, err := ot.sensor.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil && ok { if spanCtx, err := t.sensor.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil && ok {
parentCtx = spanCtx parentCtx = spanCtx
} }
} }
@ -153,7 +255,7 @@ func (ot *Tracer) startSpanFromOutgoingContext(ctx context.Context, name string,
nmd := metadata.Copy(md) nmd := metadata.Copy(md)
sp := ot.sensor.StartSpan(name, opts...) sp := t.sensor.StartSpan(name, opts...)
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil { if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil {
return nil, nil return nil, nil
} }
@ -163,12 +265,12 @@ func (ot *Tracer) startSpanFromOutgoingContext(ctx context.Context, name string,
return ctx, sp return ctx, sp
} }
func (ot *Tracer) startSpanFromIncomingContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) { func (t *Tracer) startSpanFromIncomingContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
var parentCtx opentracing.SpanContext var parentCtx opentracing.SpanContext
md, ok := metadata.FromIncomingContext(ctx) md, ok := metadata.FromIncomingContext(ctx)
if ok && md != nil { if ok && md != nil {
if spanCtx, err := ot.sensor.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil { if spanCtx, err := t.sensor.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil {
parentCtx = spanCtx parentCtx = spanCtx
} }
} }
@ -179,7 +281,8 @@ func (ot *Tracer) startSpanFromIncomingContext(ctx context.Context, name string,
nmd := metadata.Copy(md) nmd := metadata.Copy(md)
sp := ot.sensor.StartSpan(name, opts...) sp := t.sensor.StartSpan(name, opts...)
// fmt.Printf("StartSpan %#+v\n", sp)
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil { if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil {
return nil, nil return nil, nil
} }

59
instana_test.go Normal file
View File

@ -0,0 +1,59 @@
package instana
import (
"context"
"testing"
sensor "github.com/instana/go-sensor"
"go.unistack.org/micro/v3/metadata"
"go.unistack.org/micro/v3/tracer"
)
func TestTraceID(t *testing.T) {
md := metadata.New(1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx = metadata.NewIncomingContext(ctx, md)
tr := NewTracer()
if err := tr.Init(); err != nil {
t.Fatal(err)
}
var sp tracer.Span
ctx, sp = tr.Start(ctx, "test")
if v := sp.TraceID(); v == "" {
t.Fatalf("invalid span trace id %#+v", v)
}
if v := sp.SpanID(); v == "" {
t.Fatalf("invalid span span id %#+v", v)
}
}
func TestSpanPropogation(t *testing.T) {
md := metadata.New(0)
md.Set(
"X-Instana-T", "0000000000002435",
"X-Instana-S", "0000000000003546",
"X-Instana-L", "1",
"Traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000003546-01",
"Tracestate", "in=0000000000002435;0000000000003546,rojo=00f067aa0ba902b7",
)
ctx := metadata.NewIncomingContext(context.TODO(), md)
var span tracer.Span
r := sensor.NewTestRecorder()
tr := NewTracer(Recorder(r))
if err := tr.Init(); err != nil {
t.Fatal(err)
}
ctx, span = tr.Start(ctx, "test", tracer.WithSpanKind(tracer.SpanKindServer))
span.Finish()
spans := r.GetQueuedSpans()
for _, s := range spans {
t.Logf("spans %#+v\n", s)
}
tr.Flush(ctx)
}

View File

@ -1,6 +1,7 @@
package instana package instana
import ( import (
instana "github.com/instana/go-sensor"
sensor "github.com/instana/go-sensor" sensor "github.com/instana/go-sensor"
"go.unistack.org/micro/v3/tracer" "go.unistack.org/micro/v3/tracer"
) )
@ -10,3 +11,9 @@ type tracerOptionsKey struct{}
func Options(opts *sensor.Options) tracer.Option { func Options(opts *sensor.Options) tracer.Option {
return tracer.SetOption(tracerOptionsKey{}, opts) return tracer.SetOption(tracerOptionsKey{}, opts)
} }
type recorderKey struct{}
func Recorder(r instana.SpanRecorder) tracer.Option {
return tracer.SetOption(recorderKey{}, r)
}