Merge pull request #170 from unistack-org/tracer

tracer: add span kind
This commit is contained in:
Василий Толстов 2023-01-18 00:28:02 +03:00 committed by GitHub
commit f76b3171d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 15 deletions

View File

@ -13,6 +13,7 @@ func (t *noopTracer) Start(ctx context.Context, name string, opts ...SpanOption)
name: name, name: name,
ctx: ctx, ctx: ctx,
tracer: t, tracer: t,
opts: NewSpanOptions(opts...),
} }
if span.ctx == nil { if span.ctx == nil {
span.ctx = context.Background() span.ctx = context.Background()
@ -64,6 +65,10 @@ func (s *noopSpan) AddLabels(labels ...interface{}) {
s.opts.Labels = append(s.opts.Labels, labels...) s.opts.Labels = append(s.opts.Labels, labels...)
} }
func (s *noopSpan) Kind() SpanKind {
return s.opts.Kind
}
// NewTracer returns new memory tracer // NewTracer returns new memory tracer
func NewTracer(opts ...Option) Tracer { func NewTracer(opts ...Option) Tracer {
return &noopTracer{ return &noopTracer{

View File

@ -6,9 +6,58 @@ import (
"go.unistack.org/micro/v3/logger" "go.unistack.org/micro/v3/logger"
) )
type SpanKind int
const (
// SpanKindUnspecified is an unspecified SpanKind and is not a valid
// SpanKind. SpanKindUnspecified should be replaced with SpanKindInternal
// if it is received.
SpanKindUnspecified SpanKind = 0
// SpanKindInternal is a SpanKind for a Span that represents an internal
// operation within an application.
SpanKindInternal SpanKind = 1
// SpanKindServer is a SpanKind for a Span that represents the operation
// of handling a request from a client.
SpanKindServer SpanKind = 2
// SpanKindClient is a SpanKind for a Span that represents the operation
// of client making a request to a server.
SpanKindClient SpanKind = 3
// SpanKindProducer is a SpanKind for a Span that represents the operation
// of a producer sending a message to a message broker. Unlike
// SpanKindClient and SpanKindServer, there is often no direct
// relationship between this kind of Span and a SpanKindConsumer kind. A
// SpanKindProducer Span will end once the message is accepted by the
// message broker which might not overlap with the processing of that
// message.
SpanKindProducer SpanKind = 4
// SpanKindConsumer is a SpanKind for a Span that represents the operation
// of a consumer receiving a message from a message broker. Like
// SpanKindProducer Spans, there is often no direct relationship between
// this Span and the Span that produced the message.
SpanKindConsumer SpanKind = 5
)
func (sk SpanKind) String() string {
switch sk {
case SpanKindInternal:
return "internal"
case SpanKindServer:
return "server"
case SpanKindClient:
return "client"
case SpanKindProducer:
return "producer"
case SpanKindConsumer:
return "consumer"
default:
return "unspecified"
}
}
// SpanOptions contains span option // SpanOptions contains span option
type SpanOptions struct { type SpanOptions struct {
Labels []interface{} Labels []interface{}
Kind SpanKind
} }
// SpanOption func signature // SpanOption func signature
@ -20,20 +69,26 @@ type EventOptions struct{}
// EventOption func signature // EventOption func signature
type EventOption func(o *EventOptions) type EventOption func(o *EventOptions)
func SpanLabels(labels ...interface{}) SpanOption { func WithSpanLabels(labels ...interface{}) SpanOption {
return func(o *SpanOptions) { return func(o *SpanOptions) {
o.Labels = labels o.Labels = labels
} }
} }
func WithSpanKind(k SpanKind) SpanOption {
return func(o *SpanOptions) {
o.Kind = k
}
}
// Options struct // Options struct
type Options struct { type Options struct {
// Context used to store custome tracer options
Context context.Context
// Logger used for logging // Logger used for logging
Logger logger.Logger Logger logger.Logger
// Name of the tracer // Name of the tracer
Name string Name string
// Context used to store custome tracer options
Context context.Context
} }
// Option func signature // Option func signature
@ -46,6 +101,17 @@ func Logger(l logger.Logger) Option {
} }
} }
// NewSpanOptions returns default SpanOptions
func NewSpanOptions(opts ...SpanOption) SpanOptions {
options := SpanOptions{
Kind: SpanKindInternal,
}
for _, o := range opts {
o(&options)
}
return options
}
// NewOptions returns default options // NewOptions returns default options
func NewOptions(opts ...Option) Options { func NewOptions(opts ...Option) Options {
options := Options{ options := Options{

View File

@ -33,4 +33,6 @@ type Span interface {
SetLabels(labels ...interface{}) SetLabels(labels ...interface{})
// AddLabels append the span labels // AddLabels append the span labels
AddLabels(labels ...interface{}) AddLabels(labels ...interface{})
// Kind returns span kind
Kind() SpanKind
} }

View File

@ -24,7 +24,7 @@ var (
if err != nil { if err != nil {
labels = append(labels, "error", true) labels = append(labels, "error", true)
} }
labels = append(labels, "type", "client") labels = append(labels, "kind", sp.Kind())
sp.SetLabels(labels...) sp.SetLabels(labels...)
} }
@ -40,7 +40,7 @@ var (
if err != nil { if err != nil {
labels = append(labels, "error", true) labels = append(labels, "error", true)
} }
labels = append(labels, "type", "client") labels = append(labels, "kind", sp.Kind())
sp.SetLabels(labels...) sp.SetLabels(labels...)
} }
@ -56,7 +56,7 @@ var (
if err != nil { if err != nil {
labels = append(labels, "error", true) labels = append(labels, "error", true)
} }
labels = append(labels, "type", "publisher") labels = append(labels, "kind", sp.Kind())
sp.SetLabels(labels...) sp.SetLabels(labels...)
} }
@ -72,7 +72,7 @@ var (
if err != nil { if err != nil {
labels = append(labels, "error", true) labels = append(labels, "error", true)
} }
labels = append(labels, "type", "server") labels = append(labels, "kind", sp.Kind())
sp.SetLabels(labels...) sp.SetLabels(labels...)
} }
@ -88,7 +88,7 @@ var (
if err != nil { if err != nil {
labels = append(labels, "error", true) labels = append(labels, "error", true)
} }
labels = append(labels, "type", "subscriber") labels = append(labels, "kind", sp.Kind())
sp.SetLabels(labels...) sp.SetLabels(labels...)
} }
@ -104,7 +104,7 @@ var (
if err != nil { if err != nil {
labels = append(labels, "error", true) labels = append(labels, "error", true)
} }
labels = append(labels, "type", "client") labels = append(labels, "kind", sp.Kind())
sp.SetLabels(labels...) sp.SetLabels(labels...)
} }
@ -237,7 +237,7 @@ func (ot *tWrapper) Call(ctx context.Context, req client.Request, rsp interface{
sp, ok := tracer.SpanFromContext(ctx) sp, ok := tracer.SpanFromContext(ctx)
if !ok { if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, "") ctx, sp = ot.opts.Tracer.Start(ctx, "", tracer.WithSpanKind(tracer.SpanKindClient))
} }
defer sp.Finish() defer sp.Finish()
@ -260,7 +260,7 @@ func (ot *tWrapper) Stream(ctx context.Context, req client.Request, opts ...clie
sp, ok := tracer.SpanFromContext(ctx) sp, ok := tracer.SpanFromContext(ctx)
if !ok { if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, "") ctx, sp = ot.opts.Tracer.Start(ctx, "", tracer.WithSpanKind(tracer.SpanKindClient))
} }
defer sp.Finish() defer sp.Finish()
@ -276,7 +276,7 @@ func (ot *tWrapper) Stream(ctx context.Context, req client.Request, opts ...clie
func (ot *tWrapper) Publish(ctx context.Context, msg client.Message, opts ...client.PublishOption) error { func (ot *tWrapper) Publish(ctx context.Context, msg client.Message, opts ...client.PublishOption) error {
sp, ok := tracer.SpanFromContext(ctx) sp, ok := tracer.SpanFromContext(ctx)
if !ok { if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, "") ctx, sp = ot.opts.Tracer.Start(ctx, "", tracer.WithSpanKind(tracer.SpanKindProducer))
} }
defer sp.Finish() defer sp.Finish()
@ -299,7 +299,7 @@ func (ot *tWrapper) ServerHandler(ctx context.Context, req server.Request, rsp i
sp, ok := tracer.SpanFromContext(ctx) sp, ok := tracer.SpanFromContext(ctx)
if !ok { if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, "") ctx, sp = ot.opts.Tracer.Start(ctx, "", tracer.WithSpanKind(tracer.SpanKindServer))
} }
defer sp.Finish() defer sp.Finish()
@ -315,7 +315,7 @@ func (ot *tWrapper) ServerHandler(ctx context.Context, req server.Request, rsp i
func (ot *tWrapper) ServerSubscriber(ctx context.Context, msg server.Message) error { func (ot *tWrapper) ServerSubscriber(ctx context.Context, msg server.Message) error {
sp, ok := tracer.SpanFromContext(ctx) sp, ok := tracer.SpanFromContext(ctx)
if !ok { if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, "") ctx, sp = ot.opts.Tracer.Start(ctx, "", tracer.WithSpanKind(tracer.SpanKindConsumer))
} }
defer sp.Finish() defer sp.Finish()
@ -362,7 +362,7 @@ func (ot *tWrapper) ClientCallFunc(ctx context.Context, addr string, req client.
sp, ok := tracer.SpanFromContext(ctx) sp, ok := tracer.SpanFromContext(ctx)
if !ok { if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, "") ctx, sp = ot.opts.Tracer.Start(ctx, "", tracer.WithSpanKind(tracer.SpanKindClient))
} }
defer sp.Finish() defer sp.Finish()