2020-01-18 13:20:46 +03:00
|
|
|
// Package trace provides an interface for distributed tracing
|
|
|
|
package trace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Trace is an interface for distributed tracing
|
|
|
|
type Trace interface {
|
|
|
|
// Start a trace
|
2020-01-25 00:44:48 +03:00
|
|
|
Start(ctx context.Context, name string) (context.Context, *Span)
|
2020-01-18 13:20:46 +03:00
|
|
|
// Finish the trace
|
|
|
|
Finish(*Span) error
|
|
|
|
// Read the traces
|
|
|
|
Read(...ReadOption) ([]*Span, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Span is used to record an entry
|
|
|
|
type Span struct {
|
|
|
|
// Id of the trace
|
|
|
|
Trace string
|
|
|
|
// name of the span
|
|
|
|
Name string
|
|
|
|
// id of the span
|
|
|
|
Id string
|
|
|
|
// parent span id
|
|
|
|
Parent string
|
|
|
|
// Start time
|
|
|
|
Started time.Time
|
2020-01-25 00:24:51 +03:00
|
|
|
// Duration in nano seconds
|
|
|
|
Duration time.Duration
|
2020-01-18 13:20:46 +03:00
|
|
|
// associated data
|
|
|
|
Metadata map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type spanKey struct{}
|
|
|
|
|
2020-01-25 00:31:57 +03:00
|
|
|
var (
|
|
|
|
// Default tracer
|
|
|
|
DefaultTrace = NewTrace()
|
|
|
|
)
|
|
|
|
|
2020-01-18 13:20:46 +03:00
|
|
|
// FromContext returns a span from context
|
|
|
|
func FromContext(ctx context.Context) (*Span, bool) {
|
|
|
|
s, ok := ctx.Value(spanKey{}).(*Span)
|
|
|
|
return s, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext creates a new context with the span
|
|
|
|
func NewContext(ctx context.Context, s *Span) context.Context {
|
|
|
|
return context.WithValue(ctx, spanKey{}, s)
|
|
|
|
}
|