2020-09-10 00:06:29 +03:00
|
|
|
package tracer
|
2020-01-18 10:20:46 +00:00
|
|
|
|
2023-01-17 08:30:57 +03:00
|
|
|
import (
|
|
|
|
"context"
|
2023-07-29 00:40:58 +03:00
|
|
|
"reflect"
|
2023-01-17 08:30:57 +03:00
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/logger"
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
|
|
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
2023-01-17 08:30:57 +03:00
|
|
|
)
|
2020-12-09 12:10:25 +03:00
|
|
|
|
2023-01-18 09:48:58 +03:00
|
|
|
type SpanStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// SpanStatusUnset is the default status code.
|
|
|
|
SpanStatusUnset SpanStatus = 0
|
|
|
|
|
|
|
|
// SpanStatusError indicates the operation contains an error.
|
|
|
|
SpanStatusError SpanStatus = 1
|
|
|
|
|
|
|
|
// SpanStatusOK indicates operation has been validated by an Application developers
|
|
|
|
// or Operator to have completed successfully, or contain no error.
|
|
|
|
SpanStatusOK SpanStatus = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s SpanStatus) String() string {
|
|
|
|
switch s {
|
|
|
|
case SpanStatusUnset:
|
|
|
|
return "Unset"
|
|
|
|
case SpanStatusError:
|
|
|
|
return "Error"
|
|
|
|
case SpanStatusOK:
|
|
|
|
return "OK"
|
|
|
|
default:
|
|
|
|
return "Unset"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:21:18 +03:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// SpanOptions contains span option
|
2022-12-24 18:09:48 +03:00
|
|
|
type SpanOptions struct {
|
|
|
|
Labels []interface{}
|
2023-01-18 00:21:18 +03:00
|
|
|
Kind SpanKind
|
2022-12-24 18:09:48 +03:00
|
|
|
}
|
2021-03-04 01:12:16 +03:00
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// EventOptions contains event options
|
2023-10-14 16:58:40 +03:00
|
|
|
type EventOptions struct {
|
|
|
|
Labels []interface{}
|
|
|
|
}
|
2021-03-04 01:12:16 +03:00
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func WithSpanLabels(ls ...interface{}) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
v, err := options.Get(src, ".Labels")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-10-14 16:58:40 +03:00
|
|
|
} else if rutil.IsZero(v) {
|
|
|
|
v = reflect.MakeSlice(reflect.TypeOf(v), 0, len(ls)).Interface()
|
|
|
|
}
|
|
|
|
cv := reflect.ValueOf(v)
|
|
|
|
for _, l := range ls {
|
|
|
|
reflect.Append(cv, reflect.ValueOf(l))
|
|
|
|
}
|
|
|
|
err = options.Set(src, cv, ".Labels")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventOption func signature
|
|
|
|
type EventOption func(o *EventOptions)
|
|
|
|
|
|
|
|
func WithEventLabels(ls ...interface{}) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
v, err := options.Get(src, ".Labels")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-07-29 00:40:58 +03:00
|
|
|
} else if rutil.IsZero(v) {
|
|
|
|
v = reflect.MakeSlice(reflect.TypeOf(v), 0, len(ls)).Interface()
|
|
|
|
}
|
|
|
|
cv := reflect.ValueOf(v)
|
|
|
|
for _, l := range ls {
|
|
|
|
reflect.Append(cv, reflect.ValueOf(l))
|
|
|
|
}
|
|
|
|
err = options.Set(src, cv, ".Labels")
|
|
|
|
return err
|
2022-12-24 18:09:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func WithSpanKind(k SpanKind) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, k, ".Kind")
|
2023-01-18 00:21:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// Options struct
|
2020-01-29 15:45:11 +00:00
|
|
|
type Options struct {
|
2023-01-18 00:21:18 +03:00
|
|
|
// Context used to store custome tracer options
|
|
|
|
Context context.Context
|
2021-03-06 19:45:13 +03:00
|
|
|
// Logger used for logging
|
|
|
|
Logger logger.Logger
|
2021-03-04 01:12:16 +03:00
|
|
|
// Name of the tracer
|
2021-01-29 13:17:32 +03:00
|
|
|
Name string
|
2020-01-29 15:45:11 +00:00
|
|
|
}
|
2020-01-18 10:20:46 +00:00
|
|
|
|
2023-01-18 00:21:18 +03:00
|
|
|
// NewSpanOptions returns default SpanOptions
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewSpanOptions(opts ...options.Option) SpanOptions {
|
2023-01-18 00:21:18 +03:00
|
|
|
options := SpanOptions{
|
|
|
|
Kind: SpanKindInternal,
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2023-09-08 13:58:11 +03:00
|
|
|
// NewEventOptions returns default EventOptions
|
|
|
|
func NewEventOptions(opts ...options.Option) EventOptions {
|
|
|
|
options := EventOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
// NewOptions returns default options
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewOptions(opts ...options.Option) Options {
|
2020-11-03 02:02:32 +03:00
|
|
|
options := Options{
|
2024-03-11 01:14:30 +03:00
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Context: context.Background(),
|
2020-01-29 15:45:11 +00:00
|
|
|
}
|
2020-11-03 02:02:32 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
2020-01-29 15:45:11 +00:00
|
|
|
}
|