forked from grafana/metrictank
/
tracing.go
38 lines (32 loc) · 1.17 KB
/
tracing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// package tracing contains some helpers to make working with opentracing
// a tad simpler
package tracing
import (
"context"
"fmt"
opentracing "github.com/opentracing/opentracing-go"
tags "github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
)
// NewSpan pulls the span out of the context, creates a new child span, and updates the context
// callers must call span.Finish() when done
// important: the context must contain a span, otherwise you'll see a nilpointer panic here.
func NewSpan(ctx context.Context, tracer opentracing.Tracer, name string) (context.Context, opentracing.Span) {
span := opentracing.SpanFromContext(ctx)
spanCtx := span.Context()
span = tracer.StartSpan(name, opentracing.ChildOf(spanCtx))
ctx = opentracing.ContextWithSpan(ctx, span)
return ctx, span
}
// Error logs error
func Error(span opentracing.Span, err error) {
span.LogFields(log.Error(err))
}
// Errorf logs error
func Errorf(span opentracing.Span, format string, a ...interface{}) {
span.LogFields(log.Error(fmt.Errorf(format, a...)))
}
// Failure marks the current request as a failure
func Failure(span opentracing.Span) {
tags.Error.Set(span, true)
}