This package is a Go platform API for OpenTracing.
Required Reading
In order to understand the Go platform API, one must first be familiar with the
OpenTracing project and
terminology more specifically.
API overview for those adding instrumentation
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.
Singleton initialization
The simplest starting point is ./default_tracer.go. As early as possible, call
If you prefer direct control to singletons, manage ownership of the
opentracing.Tracer implementation explicitly.
Creating a Span given an existing Go context.Context
If you use context.Context in your application, OpenTracing's Go library will
happily rely on it for Span propagation. To start a new (blocking child)
Span, you can use StartSpanFromContext.
http.HandleFunc("/", func(w http.ResponseWriter, req*http.Request) {
varserverSpan opentracing.SpanappSpecificOperationName:=...wireContext, err:=opentracing.GlobalTracer().Extract(
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(req.Header))
iferr!=nil {
// Optionally record something about err here
}
// Create the span referring to the RPC client if available.// If wireContext == nil, a root span will be created.serverSpan=opentracing.StartSpan(
appSpecificOperationName,
ext.RPCServerOption(wireContext))
deferserverSpan.Finish()
ctx:=opentracing.ContextWithSpan(context.Background(), serverSpan)
...
}
Conditionally capture a field using log.Noop
In some situations, you may want to dynamically decide whether or not
to log a field. For example, you may want to capture additional data,
such as a customer ID, in non-production environments:
The entire public API is goroutine-safe and does not require external
synchronization.
API pointers for those implementing a tracing system
Tracing system implementors may be able to reuse or copy-paste-modify the basictracer package, found here. In particular, see basictracer.New(...).
API compatibility
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.
Tracer test suite
A test suite is available in the harness package that can assist Tracer implementors to assert that their Tracer is working correctly.
请发表评论