tracer: improve
Some checks failed
pr / test (pull_request) Failing after 2m43s
lint / lint (pull_request) Failing after 1m28s

This commit is contained in:
2023-09-08 13:58:11 +03:00
parent f890f1d8b4
commit 247c7f6fa2
7 changed files with 562 additions and 31 deletions

View File

@@ -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) {

View File

@@ -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{

View File

@@ -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
}