2020-11-03 02:02:32 +03:00
|
|
|
// Package tracer provides an interface for distributed tracing
|
2023-04-11 22:20:37 +03:00
|
|
|
package tracer // import "go.unistack.org/micro/v4/tracer"
|
2020-01-18 10:20:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-01 15:48:27 +03:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
2023-07-29 00:40:58 +03:00
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
"go.unistack.org/micro/v4/logger"
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2020-01-18 10:20:46 +00:00
|
|
|
)
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
// DefaultTracer is the global default tracer
|
2022-05-03 14:38:44 +03:00
|
|
|
var DefaultTracer = NewTracer()
|
2020-09-10 00:06:29 +03:00
|
|
|
|
2024-03-06 01:17:19 +03:00
|
|
|
var (
|
|
|
|
// 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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 15:45:11 +00: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
|
2023-07-29 00:40:58 +03:00
|
|
|
Init(...options.Option) error
|
2020-01-18 10:20:46 +00:00
|
|
|
// Start a trace
|
2023-07-29 00:40:58 +03:00
|
|
|
Start(ctx context.Context, name string, opts ...options.Option) (context.Context, Span)
|
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
|
2023-07-29 00:40:58 +03:00
|
|
|
Finish(opts ...options.Option)
|
2021-03-04 01:12:16 +03:00
|
|
|
// 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:58:11 +03:00
|
|
|
// AddLabels append labels to span
|
|
|
|
AddLabels(kv ...interface{})
|
|
|
|
// AddEvent append event to span
|
|
|
|
AddEvent(name string, opts ...options.Option)
|
|
|
|
// AddLogs append logs to span
|
|
|
|
AddLogs(kv ...interface{})
|
2023-01-18 00:21:18 +03:00
|
|
|
// Kind returns span kind
|
|
|
|
Kind() SpanKind
|
2023-10-16 23:43:39 +03:00
|
|
|
// TraceID returns trace id
|
|
|
|
TraceID() string
|
|
|
|
// SpanID returns span id
|
|
|
|
SpanID() string
|
|
|
|
}
|
|
|
|
|
2023-09-01 15:48:27 +03:00
|
|
|
// sort labels alphabeticaly by label name
|
|
|
|
type byKey []interface{}
|
|
|
|
|
|
|
|
func (k byKey) Len() int { return len(k) / 2 }
|
|
|
|
func (k byKey) Less(i, j int) bool { return fmt.Sprintf("%s", k[i*2]) < fmt.Sprintf("%s", k[j*2]) }
|
|
|
|
func (k byKey) Swap(i, j int) {
|
|
|
|
k[i*2], k[j*2] = k[j*2], k[i*2]
|
|
|
|
k[i*2+1], k[j*2+1] = k[j*2+1], k[i*2+1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func UniqLabels(labels []interface{}) []interface{} {
|
|
|
|
if len(labels)%2 == 1 {
|
|
|
|
labels = labels[:len(labels)-1]
|
|
|
|
}
|
2024-03-06 01:17:19 +03:00
|
|
|
|
2023-09-01 15:48:27 +03:00
|
|
|
if len(labels) > 2 {
|
|
|
|
sort.Sort(byKey(labels))
|
|
|
|
|
|
|
|
idx := 0
|
|
|
|
for {
|
|
|
|
if labels[idx] == labels[idx+2] {
|
|
|
|
copy(labels[idx:], labels[idx+2:])
|
|
|
|
labels = labels[:len(labels)-2]
|
|
|
|
} else {
|
|
|
|
idx += 2
|
|
|
|
}
|
|
|
|
if idx+2 >= len(labels) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|