This package is a Go platform API for OpenTracing.
In order to understand the Go platform API, one must first be familiar with the OpenTracing project and terminology more generally.
Everyday consumers of this opentracing package really only need to worry
about a couple of key abstractions: the StartSpan function, the Span
interface, and binding a Tracer at main()-time. Here are code snippets
demonstrating some important use cases.
The simplest starting point is ./default_tracer.go. As early as possible, call
import "github.com/opentracing/opentracing-go"
import ".../some_tracing_impl"
func main() {
tracerImpl := some_tracing_impl.New(...) // tracing impl specific
opentracing.InitGlobalTracer(tracerImpl)
...
}If you prefer direct control to singletons, manage ownership of the
opentracing.Tracer implementation explicitly.
If you use context.Context in your application, OpenTracing's Go library will
happily use it for Span propagation. To start a new (child) Span, you can use
StartSpanFromContext.
func xyz(ctx context.Context, ...) {
...
span, ctx := opentracing.StartSpanFromContext(ctx, "operation_name")
defer span.Finish()
span.LogEvent("xyz_called")
...
}It's always possible to create a "root" (parentless) Span.
func xyz() {
...
sp := opentracing.StartSpan("operation_name")
defer sp.Finish()
sp.LogEvent("xyz_called")
...
} func xyz(parentSpan opentracing.Span, ...) {
...
sp := opentracing.StartChildSpan(parentSpan, "operation_name")
defer sp.Finish()
sp.LogEvent("xyz_called")
...
} func makeSomeRequest(ctx context.Context) ... {
if span := opentracing.SpanFromContext(ctx); span != nil {
httpClient := &http.Client{}
httpReq, _ := http.NewRequest("GET", "http://myservice/", nil)
// Transmit the span's TraceContext as HTTP headers on our
// outbound request.
tracer.Inject(
span,
opentracing.TextMap,
opentracing.HTTPHeaderTextMapCarrier(httpReq.Header))
resp, err := httpClient.Do(httpReq)
...
}
...
} http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
serverSpan, err := opentracing.GlobalTracer().Join(
"serverSpan",
opentracing.TextMap,
opentracing.HTTPHeaderTextMapCarrier(req.Header))
if err != nil {
// Create a root span if necessary
serverSpan = opentracing.StartTrace("serverSpan")
}
var goCtx context.Context = ...
goCtx, _ = opentracing.ContextWithSpan(goCtx, serverSpan)
defer serverSpan.Finish()
...
}The entire public API is goroutine-safe and does not require external synchronization.
Tracing system implementors may be able to reuse or copy-paste-modify the basictracer package, found here. In particular, see basictracer.New(...).
For the time being, "mild" backwards-incompatible changes may be made without changing the major version number. As OpenTracing and opentracing-go mature, backwards compatibility will become more of a priority.