tracer: improve
This commit is contained in:
@@ -9,19 +9,27 @@ import (
|
||||
var _ Tracer = (*noopTracer)(nil)
|
||||
|
||||
type noopTracer struct {
|
||||
opts Options
|
||||
opts Options
|
||||
spans []Span
|
||||
}
|
||||
|
||||
func (t *noopTracer) Spans() []Span {
|
||||
return t.spans
|
||||
}
|
||||
|
||||
func (t *noopTracer) Start(ctx context.Context, name string, opts ...options.Option) (context.Context, Span) {
|
||||
options := NewSpanOptions(opts...)
|
||||
span := &noopSpan{
|
||||
name: name,
|
||||
ctx: ctx,
|
||||
tracer: t,
|
||||
opts: NewSpanOptions(opts...),
|
||||
labels: options.Labels,
|
||||
kind: options.Kind,
|
||||
}
|
||||
if span.ctx == nil {
|
||||
span.ctx = context.Background()
|
||||
}
|
||||
t.spans = append(t.spans, span)
|
||||
return NewSpanContext(ctx, span), span
|
||||
}
|
||||
|
||||
@@ -40,13 +48,21 @@ func (t *noopTracer) Name() string {
|
||||
return t.opts.Name
|
||||
}
|
||||
|
||||
type noopEvent struct {
|
||||
name string
|
||||
labels []interface{}
|
||||
}
|
||||
|
||||
type noopSpan struct {
|
||||
ctx context.Context
|
||||
tracer Tracer
|
||||
name string
|
||||
opts SpanOptions
|
||||
status SpanStatus
|
||||
statusMsg string
|
||||
events []*noopEvent
|
||||
labels []interface{}
|
||||
logs []interface{}
|
||||
kind SpanKind
|
||||
status SpanStatus
|
||||
}
|
||||
|
||||
func (s *noopSpan) Finish(opts ...options.Option) {
|
||||
@@ -61,22 +77,24 @@ func (s *noopSpan) Tracer() Tracer {
|
||||
}
|
||||
|
||||
func (s *noopSpan) AddEvent(name string, opts ...options.Option) {
|
||||
options := NewEventOptions(opts...)
|
||||
s.events = append(s.events, &noopEvent{name: name, labels: options.Labels})
|
||||
}
|
||||
|
||||
func (s *noopSpan) SetName(name string) {
|
||||
s.name = name
|
||||
}
|
||||
|
||||
func (s *noopSpan) SetLabels(labels ...interface{}) {
|
||||
s.opts.Labels = labels
|
||||
func (s *noopSpan) AddLogs(kv ...interface{}) {
|
||||
s.logs = append(s.logs, kv...)
|
||||
}
|
||||
|
||||
func (s *noopSpan) AddLabels(labels ...interface{}) {
|
||||
s.opts.Labels = append(s.opts.Labels, labels...)
|
||||
func (s *noopSpan) AddLabels(kv ...interface{}) {
|
||||
s.labels = append(s.labels, kv...)
|
||||
}
|
||||
|
||||
func (s *noopSpan) Kind() SpanKind {
|
||||
return s.opts.Kind
|
||||
return s.kind
|
||||
}
|
||||
|
||||
func (s *noopSpan) Status() (SpanStatus, string) {
|
||||
|
@@ -93,14 +93,6 @@ type EventOptions struct {
|
||||
Labels []interface{}
|
||||
}
|
||||
|
||||
func WithEventLabels(labels ...interface{}) options.Option {
|
||||
return options.Labels(labels...)
|
||||
}
|
||||
|
||||
func WithSpanLabels(labels ...interface{}) options.Option {
|
||||
return options.Labels(labels...)
|
||||
}
|
||||
|
||||
func WithSpanKind(k SpanKind) options.Option {
|
||||
return func(src interface{}) error {
|
||||
return options.Set(src, k, ".Kind")
|
||||
@@ -128,6 +120,15 @@ func NewSpanOptions(opts ...options.Option) SpanOptions {
|
||||
return options
|
||||
}
|
||||
|
||||
// NewEventOptions returns default EventOptions
|
||||
func NewEventOptions(opts ...options.Option) EventOptions {
|
||||
options := EventOptions{}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
||||
// NewOptions returns default options
|
||||
func NewOptions(opts ...options.Option) Options {
|
||||
options := Options{
|
||||
|
@@ -29,8 +29,6 @@ type Span interface {
|
||||
Tracer() Tracer
|
||||
// Finish complete and send span
|
||||
Finish(opts ...options.Option)
|
||||
// AddEvent add event to span
|
||||
AddEvent(name string, opts ...options.Option)
|
||||
// Context return context with span
|
||||
Context() context.Context
|
||||
// SetName set the span name
|
||||
@@ -39,10 +37,12 @@ type Span interface {
|
||||
SetStatus(status SpanStatus, msg string)
|
||||
// Status returns span status and msg
|
||||
Status() (SpanStatus, string)
|
||||
// SetLabels set the span labels
|
||||
SetLabels(labels ...interface{})
|
||||
// AddLabels append the span labels
|
||||
AddLabels(labels ...interface{})
|
||||
// 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{})
|
||||
// Kind returns span kind
|
||||
Kind() SpanKind
|
||||
}
|
||||
|
Reference in New Issue
Block a user