tracer: dont return noop from context

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-10-07 22:46:47 +03:00
parent 0854a7ea72
commit 7d68f2396e
3 changed files with 16 additions and 14 deletions

View File

@ -8,14 +8,14 @@ import (
type tracerKey struct{}
// FromContext returns a tracer from context
func FromContext(ctx context.Context) Tracer {
func FromContext(ctx context.Context) (Tracer, bool) {
if ctx == nil {
return DefaultTracer
return nil, false
}
if tracer, ok := ctx.Value(tracerKey{}).(Tracer); ok {
return tracer
return tracer, true
}
return DefaultTracer
return nil, false
}
// NewContext saves the tracer in the context
@ -29,14 +29,14 @@ func NewContext(ctx context.Context, tracer Tracer) context.Context {
type spanKey struct{}
// SpanFromContext returns a span from context
func SpanFromContext(ctx context.Context) Span {
func SpanFromContext(ctx context.Context) (Span, bool) {
if ctx == nil {
return &noopSpan{}
return nil, false
}
if span, ok := ctx.Value(spanKey{}).(Span); ok {
return span
return span, true
}
return &noopSpan{}
return nil, false
}
// NewSpanContext saves the span in the context

View File

@ -35,6 +35,7 @@ type noopSpan struct {
ctx context.Context
tracer Tracer
name string
labels []Label
}
func (s *noopSpan) Finish(opts ...SpanOption) {
@ -56,6 +57,7 @@ func (s *noopSpan) SetName(name string) {
}
func (s *noopSpan) SetLabels(labels ...Label) {
s.labels = labels
}
// NewTracer returns new memory tracer

View File

@ -38,26 +38,26 @@ type Label struct {
key string
}
func Any(k string, v interface{}) Label {
func LabelAny(k string, v interface{}) Label {
return Label{key: k, val: v}
}
func String(k string, v string) Label {
func LabelString(k string, v string) Label {
return Label{key: k, val: v}
}
func Int(k string, v int) Label {
func LabelInt(k string, v int) Label {
return Label{key: k, val: v}
}
func Int64(k string, v int64) Label {
func LabelInt64(k string, v int64) Label {
return Label{key: k, val: v}
}
func Float64(k string, v float64) Label {
func LabelFloat64(k string, v float64) Label {
return Label{key: k, val: v}
}
func Bool(k string, v bool) Label {
func LabelBool(k string, v bool) Label {
return Label{key: k, val: v}
}