Skip to content

Commit

Permalink
Merge pull request #170 from unistack-org/tracer
Browse files Browse the repository at this point in the history
tracer: add span kind
  • Loading branch information
vtolstov authored Jan 17, 2023
2 parents 6f17fd8 + db3fc42 commit f76b317
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 15 deletions.
5 changes: 5 additions & 0 deletions tracer/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func (t *noopTracer) Start(ctx context.Context, name string, opts ...SpanOption)
name: name,
ctx: ctx,
tracer: t,
opts: NewSpanOptions(opts...),
}
if span.ctx == nil {
span.ctx = context.Background()
Expand Down Expand Up @@ -64,6 +65,10 @@ func (s *noopSpan) AddLabels(labels ...interface{}) {
s.opts.Labels = append(s.opts.Labels, labels...)
}

func (s *noopSpan) Kind() SpanKind {
return s.opts.Kind
}

// NewTracer returns new memory tracer
func NewTracer(opts ...Option) Tracer {
return &noopTracer{
Expand Down
72 changes: 69 additions & 3 deletions tracer/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,58 @@ import (
"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
type SpanOptions struct {
Labels []interface{}
Kind SpanKind
}

// SpanOption func signature
Expand All @@ -20,20 +69,26 @@ type EventOptions struct{}
// EventOption func signature
type EventOption func(o *EventOptions)

func SpanLabels(labels ...interface{}) SpanOption {
func WithSpanLabels(labels ...interface{}) SpanOption {
return func(o *SpanOptions) {
o.Labels = labels
}
}

func WithSpanKind(k SpanKind) SpanOption {
return func(o *SpanOptions) {
o.Kind = k
}
}

// Options struct
type Options struct {
// Context used to store custome tracer options
Context context.Context
// Logger used for logging
Logger logger.Logger
// Name of the tracer
Name string
// Context used to store custome tracer options
Context context.Context
}

// Option func signature
Expand All @@ -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
func NewOptions(opts ...Option) Options {
options := Options{
Expand Down
2 changes: 2 additions & 0 deletions tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ type Span interface {
SetLabels(labels ...interface{})
// AddLabels append the span labels
AddLabels(labels ...interface{})
// Kind returns span kind
Kind() SpanKind
}
24 changes: 12 additions & 12 deletions tracer/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
if err != nil {
labels = append(labels, "error", true)
}
labels = append(labels, "type", "client")
labels = append(labels, "kind", sp.Kind())
sp.SetLabels(labels...)
}

Expand All @@ -40,7 +40,7 @@ var (
if err != nil {
labels = append(labels, "error", true)
}
labels = append(labels, "type", "client")
labels = append(labels, "kind", sp.Kind())
sp.SetLabels(labels...)
}

Expand All @@ -56,7 +56,7 @@ var (
if err != nil {
labels = append(labels, "error", true)
}
labels = append(labels, "type", "publisher")
labels = append(labels, "kind", sp.Kind())
sp.SetLabels(labels...)
}

Expand All @@ -72,7 +72,7 @@ var (
if err != nil {
labels = append(labels, "error", true)
}
labels = append(labels, "type", "server")
labels = append(labels, "kind", sp.Kind())
sp.SetLabels(labels...)
}

Expand All @@ -88,7 +88,7 @@ var (
if err != nil {
labels = append(labels, "error", true)
}
labels = append(labels, "type", "subscriber")
labels = append(labels, "kind", sp.Kind())
sp.SetLabels(labels...)
}

Expand All @@ -104,7 +104,7 @@ var (
if err != nil {
labels = append(labels, "error", true)
}
labels = append(labels, "type", "client")
labels = append(labels, "kind", sp.Kind())
sp.SetLabels(labels...)
}

Expand Down Expand Up @@ -237,7 +237,7 @@ func (ot *tWrapper) Call(ctx context.Context, req client.Request, rsp interface{

sp, ok := tracer.SpanFromContext(ctx)
if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, "")
ctx, sp = ot.opts.Tracer.Start(ctx, "", tracer.WithSpanKind(tracer.SpanKindClient))
}
defer sp.Finish()

Expand All @@ -260,7 +260,7 @@ func (ot *tWrapper) Stream(ctx context.Context, req client.Request, opts ...clie

sp, ok := tracer.SpanFromContext(ctx)
if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, "")
ctx, sp = ot.opts.Tracer.Start(ctx, "", tracer.WithSpanKind(tracer.SpanKindClient))
}
defer sp.Finish()

Expand All @@ -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 {
sp, ok := tracer.SpanFromContext(ctx)
if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, "")
ctx, sp = ot.opts.Tracer.Start(ctx, "", tracer.WithSpanKind(tracer.SpanKindProducer))
}
defer sp.Finish()

Expand All @@ -299,7 +299,7 @@ func (ot *tWrapper) ServerHandler(ctx context.Context, req server.Request, rsp i

sp, ok := tracer.SpanFromContext(ctx)
if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, "")
ctx, sp = ot.opts.Tracer.Start(ctx, "", tracer.WithSpanKind(tracer.SpanKindServer))
}
defer sp.Finish()

Expand All @@ -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 {
sp, ok := tracer.SpanFromContext(ctx)
if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, "")
ctx, sp = ot.opts.Tracer.Start(ctx, "", tracer.WithSpanKind(tracer.SpanKindConsumer))
}
defer sp.Finish()

Expand Down Expand Up @@ -362,7 +362,7 @@ func (ot *tWrapper) ClientCallFunc(ctx context.Context, addr string, req client.

sp, ok := tracer.SpanFromContext(ctx)
if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, "")
ctx, sp = ot.opts.Tracer.Start(ctx, "", tracer.WithSpanKind(tracer.SpanKindClient))
}
defer sp.Finish()

Expand Down

0 comments on commit f76b317

Please sign in to comment.