2020-11-03 02:02:32 +03:00
|
|
|
// Package tracer provides an interface for distributed tracing
|
2021-10-02 19:55:07 +03:00
|
|
|
package tracer // import "go.unistack.org/micro/v3/tracer"
|
2020-01-18 13:20:46 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-03-06 00:53:20 +03:00
|
|
|
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
2020-01-18 13:20:46 +03:00
|
|
|
)
|
|
|
|
|
2024-03-06 00:53:20 +03:00
|
|
|
var (
|
2024-05-04 19:05:07 +03:00
|
|
|
// DefaultTracer is the global default tracer
|
|
|
|
DefaultTracer Tracer = NewTracer() //nolint:revive
|
2024-03-06 00:53:20 +03:00
|
|
|
// TraceIDKey is the key used for the trace id in the log call
|
|
|
|
TraceIDKey = "trace-id"
|
|
|
|
// SpanIDKey is the key used for the span id in the log call
|
|
|
|
SpanIDKey = "span-id"
|
2024-05-04 19:05:07 +03:00
|
|
|
// DefaultSkipEndpoints is the slice of endpoint that must not be traced
|
|
|
|
DefaultSkipEndpoints = []string{
|
|
|
|
"MeterService.Metrics",
|
|
|
|
"HealthService.Live",
|
|
|
|
"HealthService.Ready",
|
|
|
|
"HealthService.Version",
|
|
|
|
}
|
2024-07-06 00:09:27 +03:00
|
|
|
DefaultContextAttrFuncs []ContextAttrFunc
|
2024-03-06 00:53:20 +03:00
|
|
|
)
|
|
|
|
|
2024-07-06 00:09:27 +03:00
|
|
|
type ContextAttrFunc func(ctx context.Context) []interface{}
|
|
|
|
|
2024-03-06 00:53:20 +03:00
|
|
|
func init() {
|
|
|
|
logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs,
|
|
|
|
func(ctx context.Context) []interface{} {
|
|
|
|
if span, ok := SpanFromContext(ctx); ok {
|
|
|
|
return []interface{}{
|
|
|
|
TraceIDKey, span.TraceID(),
|
|
|
|
SpanIDKey, span.SpanID(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-29 18:45:11 +03:00
|
|
|
// Tracer is an interface for distributed tracing
|
|
|
|
type Tracer interface {
|
2021-03-04 01:12:16 +03:00
|
|
|
// Name return tracer name
|
2021-01-29 13:17:32 +03:00
|
|
|
Name() string
|
2021-03-04 01:12:16 +03:00
|
|
|
// Init tracer with options
|
2021-01-29 13:17:32 +03:00
|
|
|
Init(...Option) error
|
2020-01-18 13:20:46 +03:00
|
|
|
// Start a trace
|
2021-03-04 01:12:16 +03:00
|
|
|
Start(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span)
|
2024-05-09 19:16:12 +03:00
|
|
|
// Extract get span metadata from context
|
|
|
|
// Extract(ctx context.Context)
|
2023-07-04 00:23:50 +03:00
|
|
|
// Flush flushes spans
|
|
|
|
Flush(ctx context.Context) error
|
2021-03-04 01:12:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Span interface {
|
|
|
|
// Tracer return underlining tracer
|
|
|
|
Tracer() Tracer
|
|
|
|
// Finish complete and send span
|
|
|
|
Finish(opts ...SpanOption)
|
|
|
|
// Context return context with span
|
|
|
|
Context() context.Context
|
|
|
|
// SetName set the span name
|
|
|
|
SetName(name string)
|
2023-01-18 09:48:58 +03:00
|
|
|
// SetStatus set the span status code and msg
|
|
|
|
SetStatus(status SpanStatus, msg string)
|
|
|
|
// Status returns span status and msg
|
|
|
|
Status() (SpanStatus, string)
|
2023-09-08 13:40:01 +03:00
|
|
|
// AddLabels append labels to span
|
|
|
|
AddLabels(kv ...interface{})
|
|
|
|
// AddEvent append event to span
|
|
|
|
AddEvent(name string, opts ...EventOption)
|
|
|
|
// AddEvent append event to span
|
|
|
|
AddLogs(kv ...interface{})
|
2023-01-18 00:21:18 +03:00
|
|
|
// Kind returns span kind
|
|
|
|
Kind() SpanKind
|
2024-03-06 00:53:20 +03:00
|
|
|
// TraceID returns trace id
|
|
|
|
TraceID() string
|
|
|
|
// SpanID returns span id
|
|
|
|
SpanID() string
|
2020-01-18 13:20:46 +03:00
|
|
|
}
|