Compare commits

..

17 Commits

Author SHA1 Message Date
09b677ee53 Merge pull request #30 from unistack-org/spanfix
fix span tags
2023-01-18 10:08:13 +03:00
d0b475b987 fix span tags
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-01-18 10:05:02 +03:00
93cb40a39b Merge pull request #28 from unistack-org/fixup
add ability to pass opentracing.Tracer
2023-01-17 09:23:12 +03:00
38d841d5b6 add ability to pass opentracing.Tracer
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-01-17 09:21:48 +03:00
b7b6d50dc6 add ability to pass opentracing.Tracer
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-01-17 08:43:04 +03:00
1566ccf0d8 Merge pull request #27 from unistack-org/fixes
implement tracer.Tracer interface
2023-01-16 23:43:40 +03:00
2abea05b41 update
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-01-16 23:41:56 +03:00
a5c1f0a9a7 Merge pull request #26 from unistack-org/dependabot/go_modules/go.unistack.org/micro/v3-3.10.0
Bump go.unistack.org/micro/v3 from 3.9.18 to 3.10.0
2023-01-09 05:15:34 +03:00
dependabot[bot]
3d56f83bd2 Bump go.unistack.org/micro/v3 from 3.9.18 to 3.10.0
Bumps [go.unistack.org/micro/v3](https://github.com/unistack-org/micro) from 3.9.18 to 3.10.0.
- [Release notes](https://github.com/unistack-org/micro/releases)
- [Commits](https://github.com/unistack-org/micro/compare/v3.9.18...v3.10.0)

---
updated-dependencies:
- dependency-name: go.unistack.org/micro/v3
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-01-09 02:14:12 +00:00
9b7a48f410 Merge pull request #25 from unistack-org/dependabot/go_modules/go.unistack.org/micro/v3-3.9.18
Bump go.unistack.org/micro/v3 from 3.9.17 to 3.9.18
2022-12-28 05:06:53 +03:00
dependabot[bot]
418124c029 Bump go.unistack.org/micro/v3 from 3.9.17 to 3.9.18
Bumps [go.unistack.org/micro/v3](https://github.com/unistack-org/micro) from 3.9.17 to 3.9.18.
- [Release notes](https://github.com/unistack-org/micro/releases)
- [Commits](https://github.com/unistack-org/micro/compare/v3.9.17...v3.9.18)

---
updated-dependencies:
- dependency-name: go.unistack.org/micro/v3
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-12-28 02:05:24 +00:00
a0526b1790 Merge pull request #24 from unistack-org/dependabot/go_modules/go.unistack.org/micro/v3-3.9.17
Bump go.unistack.org/micro/v3 from 3.9.15 to 3.9.17
2022-12-26 05:14:42 +03:00
dependabot[bot]
ea7f1e66e7 Bump go.unistack.org/micro/v3 from 3.9.15 to 3.9.17
Bumps [go.unistack.org/micro/v3](https://github.com/unistack-org/micro) from 3.9.15 to 3.9.17.
- [Release notes](https://github.com/unistack-org/micro/releases)
- [Commits](https://github.com/unistack-org/micro/compare/v3.9.15...v3.9.17)

---
updated-dependencies:
- dependency-name: go.unistack.org/micro/v3
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-12-26 02:13:26 +00:00
690f9e1471 Merge pull request #23 from unistack-org/dependabot/go_modules/go.unistack.org/micro/v3-3.9.15
Bump go.unistack.org/micro/v3 from 3.9.14 to 3.9.15
2022-11-29 05:09:06 +03:00
dependabot[bot]
c4fcb7319c Bump go.unistack.org/micro/v3 from 3.9.14 to 3.9.15
Bumps [go.unistack.org/micro/v3](https://github.com/unistack-org/micro) from 3.9.14 to 3.9.15.
- [Release notes](https://github.com/unistack-org/micro/releases)
- [Commits](https://github.com/unistack-org/micro/compare/v3.9.14...v3.9.15)

---
updated-dependencies:
- dependency-name: go.unistack.org/micro/v3
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-11-29 02:07:50 +00:00
2f03982b7b Merge pull request #22 from unistack-org/dependabot/go_modules/go.unistack.org/micro/v3-3.9.14
Bump go.unistack.org/micro/v3 from 3.9.13 to 3.9.14
2022-11-28 05:13:17 +03:00
dependabot[bot]
051fcd556f Bump go.unistack.org/micro/v3 from 3.9.13 to 3.9.14
Bumps [go.unistack.org/micro/v3](https://github.com/unistack-org/micro) from 3.9.13 to 3.9.14.
- [Release notes](https://github.com/unistack-org/micro/releases)
- [Commits](https://github.com/unistack-org/micro/compare/v3.9.13...v3.9.14)

---
updated-dependencies:
- dependency-name: go.unistack.org/micro/v3
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-11-28 02:11:50 +00:00
5 changed files with 155 additions and 70 deletions

2
go.mod
View File

@@ -4,6 +4,6 @@ go 1.16
require (
github.com/opentracing/opentracing-go v1.2.0
go.unistack.org/micro/v3 v3.9.13
go.unistack.org/micro/v3 v3.10.4
gopkg.in/yaml.v2 v2.4.0 // indirect
)

4
go.sum
View File

@@ -73,8 +73,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.unistack.org/micro-proto/v3 v3.3.1/go.mod h1:cwRyv8uInM2I7EbU7O8Fx2Ls3N90Uw9UCCcq4olOdfE=
go.unistack.org/micro/v3 v3.9.13 h1:x1/H4rDgvz2JWXx6UvFiWDyRLxg2Kpp6V1kjzpLlyvo=
go.unistack.org/micro/v3 v3.9.13/go.mod h1:gI4RkJKHLPW7KV6h4+ZBOZD997MRvFRXMPQIHpozikI=
go.unistack.org/micro/v3 v3.10.4 h1:8HneC2t7oteTwwkFLmSg5bs62h/OqEzevx/IbXG1vRo=
go.unistack.org/micro/v3 v3.10.4/go.mod h1:gI4RkJKHLPW7KV6h4+ZBOZD997MRvFRXMPQIHpozikI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=

View File

@@ -1,54 +1,169 @@
// Package opentracing provides wrappers for OpenTracing
package opentracing
import (
"context"
"errors"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"go.unistack.org/micro/v3/metadata"
"go.unistack.org/micro/v3/tracer"
)
var _ tracer.Tracer = &opentracingTracer{}
var _ tracer.Tracer = &otTracer{}
type opentracingTracer struct {
opts tracer.Options
type otTracer struct {
opts tracer.Options
tracer opentracing.Tracer
}
func (ot *opentracingTracer) Name() string {
func (ot *otTracer) Name() string {
return ot.opts.Name
}
func (ot *opentracingTracer) Init(opts ...tracer.Option) error {
func (ot *otTracer) Init(opts ...tracer.Option) error {
for _, o := range opts {
o(&ot.opts)
}
if tr, ok := ot.opts.Context.Value(tracerKey{}).(opentracing.Tracer); ok {
ot.tracer = tr
} else {
return errors.New("Tracer option missing")
}
return nil
}
func (ot *opentracingTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOption) (context.Context, tracer.Span) {
return nil, nil
func (ot *otTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOption) (context.Context, tracer.Span) {
options := tracer.NewSpanOptions(opts...)
var span opentracing.Span
switch options.Kind {
case tracer.SpanKindInternal, tracer.SpanKindUnspecified:
ctx, span = ot.startSpanFromContext(ctx, name)
case tracer.SpanKindClient, tracer.SpanKindProducer:
ctx, span = ot.startSpanFromOutgoingContext(ctx, name)
case tracer.SpanKindServer, tracer.SpanKindConsumer:
ctx, span = ot.startSpanFromIncomingContext(ctx, ot.tracer, name)
}
return ctx, &otSpan{span: span, opts: options}
}
func NewTracer(opts ...tracer.Option) *opentracingTracer {
type otSpan struct {
span opentracing.Span
opts tracer.SpanOptions
status tracer.SpanStatus
statusMsg string
}
func (os *otSpan) SetStatus(st tracer.SpanStatus, msg string) {
switch st {
case tracer.SpanStatusError:
os.span.SetTag("error", true)
}
os.status = st
os.statusMsg = msg
}
func (os *otSpan) Status() (tracer.SpanStatus, string) {
return os.status, os.statusMsg
}
func (os *otSpan) Tracer() tracer.Tracer {
return &otTracer{tracer: os.span.Tracer()}
}
func (os *otSpan) Finish(opts ...tracer.SpanOption) {
if len(os.opts.Labels) > 0 {
os.span.LogKV(os.opts.Labels...)
}
os.span.Finish()
}
func (os *otSpan) AddEvent(name string, opts ...tracer.EventOption) {
os.span.LogFields(log.Event(name))
}
func (os *otSpan) Context() context.Context {
return opentracing.ContextWithSpan(context.Background(), os.span)
}
func (os *otSpan) SetName(name string) {
os.span = os.span.SetOperationName(name)
}
func (os *otSpan) SetLabels(labels ...interface{}) {
os.opts.Labels = labels
}
func (os *otSpan) Kind() tracer.SpanKind {
return os.opts.Kind
}
func (os *otSpan) AddLabels(labels ...interface{}) {
os.opts.Labels = append(os.opts.Labels, labels...)
}
func NewTracer(opts ...tracer.Option) *otTracer {
options := tracer.NewOptions(opts...)
return &opentracingTracer{opts: options}
return &otTracer{opts: options}
}
func spanFromContext(ctx context.Context) opentracing.Span {
return opentracing.SpanFromContext(ctx)
}
// StartSpanFromOutgoingContext returns a new span with the given operation name and options. If a span
// is found in the context, it will be used as the parent of the resulting span.
func StartSpanFromOutgoingContext(ctx context.Context, tracer opentracing.Tracer, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span, error) {
func (ot *otTracer) startSpanFromContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
opts = append(opts, opentracing.ChildOf(parentSpan.Context()))
}
md := metadata.New(1)
sp := ot.tracer.StartSpan(name, opts...)
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(md)); err != nil {
return nil, nil
}
ctx = opentracing.ContextWithSpan(ctx, sp)
return ctx, sp
}
func (ot *otTracer) startSpanFromOutgoingContext(ctx context.Context, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
var parentCtx opentracing.SpanContext
md, ok := metadata.FromOutgoingContext(ctx)
if ok && md != nil {
if spanCtx, err := ot.tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil && ok {
parentCtx = spanCtx
}
}
if parentCtx != nil {
opts = append(opts, opentracing.ChildOf(parentCtx))
}
nmd := metadata.Copy(md)
sp := ot.tracer.StartSpan(name, opts...)
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil {
return nil, nil
}
ctx = metadata.NewOutgoingContext(opentracing.ContextWithSpan(ctx, sp), nmd)
return ctx, sp
}
func (ot *otTracer) startSpanFromIncomingContext(ctx context.Context, tracer opentracing.Tracer, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
var parentCtx opentracing.SpanContext
md, ok := metadata.FromIncomingContext(ctx)
// Find parent span.
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
// First try to get span within current service boundary.
parentCtx = parentSpan.Context()
} else if spanCtx, err := tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil && ok {
// If there doesn't exist, try to get it from metadata(which is cross boundary)
parentCtx = spanCtx
if ok && md != nil {
if spanCtx, err := tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil {
parentCtx = spanCtx
}
}
if parentCtx != nil {
@@ -59,50 +174,10 @@ func StartSpanFromOutgoingContext(ctx context.Context, tracer opentracing.Tracer
sp := tracer.StartSpan(name, opts...)
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil {
return nil, nil, err
}
ctx = metadata.NewOutgoingContext(opentracing.ContextWithSpan(ctx, sp), nmd)
return ctx, sp, nil
}
// StartSpanFromIncomingContext returns a new span with the given operation name and options. If a span
// is found in the context, it will be used as the parent of the resulting span.
func StartSpanFromIncomingContext(ctx context.Context, tracer opentracing.Tracer, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span, error) {
var parentCtx opentracing.SpanContext
// Find parent span.
md, ok := metadata.FromIncomingContext(ctx)
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
// First try to get span within current service boundary.
parentCtx = parentSpan.Context()
} else if spanCtx, err := tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil && ok {
// If there doesn't exist, try to get it from metadata(which is cross boundary)
parentCtx = spanCtx
}
if parentCtx != nil {
opts = append(opts, opentracing.ChildOf(parentCtx))
}
var nmd metadata.Metadata
if ok {
nmd = metadata.New(len(md))
} else {
nmd = metadata.New(0)
}
sp := tracer.StartSpan(name, opts...)
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil {
return nil, nil, err
}
for k, v := range md {
nmd.Set(k, v)
return nil, nil
}
ctx = metadata.NewIncomingContext(opentracing.ContextWithSpan(ctx, sp), nmd)
return ctx, sp, nil
return ctx, sp
}

View File

@@ -20,16 +20,14 @@ func TestStartSpanFromIncomingContext(t *testing.T) {
ctx = metadata.NewIncomingContext(ctx, md)
tracer := opentracing.GlobalTracer()
ot := &otTracer{tracer: tracer}
g.Add(8000)
cherr := make(chan error)
for i := 0; i < 8000; i++ {
go func() {
defer g.Done()
_, sp, err := StartSpanFromIncomingContext(ctx, tracer, "test")
if err != nil {
cherr <- err
}
_, sp := ot.startSpanFromIncomingContext(ctx, tracer, "test")
sp.Finish()
}()
}

12
options.go Normal file
View File

@@ -0,0 +1,12 @@
package opentracing
import (
"github.com/opentracing/opentracing-go"
"go.unistack.org/micro/v3/tracer"
)
type tracerKey struct{}
func Tracer(ot opentracing.Tracer) tracer.Option {
return tracer.SetOption(tracerKey{}, ot)
}