2020-03-24 13:15:01 +00:00
|
|
|
package tracing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2020-11-20 06:57:39 +00:00
|
|
|
sdk_trace "go.opentelemetry.io/otel/sdk/trace"
|
2021-08-10 05:27:27 +00:00
|
|
|
api_trace "go.opentelemetry.io/otel/trace"
|
2020-03-24 13:15:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Tracer interface {
|
|
|
|
NewSpan(ctx context.Context, caller string) (context.Context, *Span)
|
|
|
|
NewClientSpan(ctx context.Context, caller string) (context.Context, *Span)
|
|
|
|
NewServerSpan(ctx context.Context, caller string) (context.Context, *Span)
|
|
|
|
NewClientInterceptorSpan(ctx context.Context, name string) (context.Context, *Span)
|
|
|
|
NewServerInterceptorSpan(ctx context.Context, name string) (context.Context, *Span)
|
|
|
|
NewSpanHTTP(r *http.Request, caller string) (*http.Request, *Span)
|
2020-11-20 06:57:39 +00:00
|
|
|
Sampler() sdk_trace.Sampler
|
2020-03-24 13:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var T Tracer
|
|
|
|
|
2020-11-20 06:57:39 +00:00
|
|
|
func Sampler() sdk_trace.Sampler {
|
2020-03-24 13:15:01 +00:00
|
|
|
if T == nil {
|
2020-11-20 06:57:39 +00:00
|
|
|
return sdk_trace.NeverSample()
|
2020-03-24 13:15:01 +00:00
|
|
|
}
|
|
|
|
return T.Sampler()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSpan(ctx context.Context) (context.Context, *Span) {
|
|
|
|
if T == nil {
|
|
|
|
return ctx, CreateSpan(nil)
|
|
|
|
}
|
|
|
|
return T.NewSpan(ctx, GetCaller())
|
|
|
|
}
|
|
|
|
|
2020-10-21 08:18:34 +00:00
|
|
|
func NewNamedSpan(ctx context.Context, name string) (context.Context, *Span) {
|
|
|
|
if T == nil {
|
|
|
|
return ctx, CreateSpan(nil)
|
|
|
|
}
|
|
|
|
return T.NewSpan(ctx, name)
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:15:01 +00:00
|
|
|
func NewClientSpan(ctx context.Context) (context.Context, *Span) {
|
|
|
|
if T == nil {
|
|
|
|
return ctx, CreateSpan(nil)
|
|
|
|
}
|
|
|
|
return T.NewClientSpan(ctx, GetCaller())
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServerSpan(ctx context.Context) (context.Context, *Span) {
|
|
|
|
if T == nil {
|
|
|
|
return ctx, CreateSpan(nil)
|
|
|
|
}
|
|
|
|
return T.NewServerSpan(ctx, GetCaller())
|
|
|
|
}
|
|
|
|
|
2020-10-21 08:18:34 +00:00
|
|
|
func NewClientInterceptorSpan(ctx context.Context) (context.Context, *Span) {
|
2020-03-24 13:15:01 +00:00
|
|
|
if T == nil {
|
|
|
|
return ctx, CreateSpan(nil)
|
|
|
|
}
|
2020-10-21 08:18:34 +00:00
|
|
|
return T.NewClientInterceptorSpan(ctx, GetCaller())
|
2020-03-24 13:15:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 08:18:34 +00:00
|
|
|
func NewServerInterceptorSpan(ctx context.Context) (context.Context, *Span) {
|
2020-03-24 13:15:01 +00:00
|
|
|
if T == nil {
|
|
|
|
return ctx, CreateSpan(nil)
|
|
|
|
}
|
2020-10-21 08:18:34 +00:00
|
|
|
return T.NewServerInterceptorSpan(ctx, GetCaller())
|
2020-03-24 13:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSpanHTTP(r *http.Request) (*http.Request, *Span) {
|
|
|
|
if T == nil {
|
|
|
|
return r, CreateSpan(nil)
|
|
|
|
}
|
|
|
|
return T.NewSpanHTTP(r, GetCaller())
|
|
|
|
}
|
2020-11-20 06:57:39 +00:00
|
|
|
|
|
|
|
func TraceIDFromCtx(ctx context.Context) string {
|
2021-08-10 05:27:27 +00:00
|
|
|
return api_trace.SpanFromContext(ctx).SpanContext().TraceID().String()
|
2020-11-20 06:57:39 +00:00
|
|
|
}
|