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"
|
|
|
|
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
)
|
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 {
|
2024-05-09 19:16:12 +03:00
|
|
|
StatusMsg string
|
|
|
|
Labels []interface{}
|
|
|
|
Status SpanStatus
|
|
|
|
Kind SpanKind
|
|
|
|
Record bool
|
2022-12-24 18:09:48 +03:00
|
|
|
}
|
2021-03-04 01:12:16 +03:00
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// SpanOption func signature
|
2021-03-04 01:12:16 +03:00
|
|
|
type SpanOption func(o *SpanOptions)
|
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// EventOptions contains event options
|
2023-09-01 08:41:23 +03:00
|
|
|
type EventOptions struct {
|
|
|
|
Labels []interface{}
|
|
|
|
}
|
2021-03-04 01:12:16 +03:00
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// EventOption func signature
|
2021-03-04 01:12:16 +03:00
|
|
|
type EventOption func(o *EventOptions)
|
2020-11-03 02:02:32 +03:00
|
|
|
|
2023-09-08 13:40:01 +03:00
|
|
|
func WithEventLabels(kv ...interface{}) EventOption {
|
2023-09-01 08:41:23 +03:00
|
|
|
return func(o *EventOptions) {
|
2024-03-17 00:17:10 +03:00
|
|
|
o.Labels = append(o.Labels, kv...)
|
2023-09-01 08:41:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 13:40:01 +03:00
|
|
|
func WithSpanLabels(kv ...interface{}) SpanOption {
|
2022-12-24 18:09:48 +03:00
|
|
|
return func(o *SpanOptions) {
|
2024-03-17 00:17:10 +03:00
|
|
|
o.Labels = append(o.Labels, kv...)
|
2022-12-24 18:09:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 19:16:12 +03:00
|
|
|
func WithSpanStatus(st SpanStatus, msg string) SpanOption {
|
|
|
|
return func(o *SpanOptions) {
|
|
|
|
o.Status = st
|
|
|
|
o.StatusMsg = msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:21:18 +03:00
|
|
|
func WithSpanKind(k SpanKind) SpanOption {
|
|
|
|
return func(o *SpanOptions) {
|
|
|
|
o.Kind = k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-04 19:18:12 +03:00
|
|
|
func WithSpanRecord(b bool) SpanOption {
|
|
|
|
return func(o *SpanOptions) {
|
|
|
|
o.Record = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2024-07-06 00:09:27 +03:00
|
|
|
// ContextAttrFuncs contains funcs that provides tracing
|
|
|
|
ContextAttrFuncs []ContextAttrFunc
|
2020-01-29 15:45:11 +00:00
|
|
|
}
|
2020-01-18 10:20:46 +00:00
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// Option func signature
|
2020-01-18 10:20:46 +00:00
|
|
|
type Option func(o *Options)
|
|
|
|
|
2020-12-09 12:10:25 +03:00
|
|
|
// Logger sets the logger
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 13:40:01 +03:00
|
|
|
// NewEventOptions returns default EventOptions
|
|
|
|
func NewEventOptions(opts ...EventOption) EventOptions {
|
|
|
|
options := EventOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:21:18 +03:00
|
|
|
// NewSpanOptions returns default SpanOptions
|
|
|
|
func NewSpanOptions(opts ...SpanOption) SpanOptions {
|
|
|
|
options := SpanOptions{
|
2024-05-04 19:31:35 +03:00
|
|
|
Kind: SpanKindInternal,
|
|
|
|
Record: true,
|
2023-01-18 00:21:18 +03:00
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
// NewOptions returns default options
|
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
2024-07-06 00:09:27 +03:00
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Context: context.Background(),
|
|
|
|
ContextAttrFuncs: DefaultContextAttrFuncs,
|
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
|
|
|
}
|
2021-01-29 14:07:35 +03:00
|
|
|
|
|
|
|
// Name sets the name
|
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|