Documentation ¶
Overview ¶
Package tracer contains Datadog's core tracing client. It is used to trace requests as they flow across web servers, databases and microservices, giving developers visibility into bottlenecks and troublesome requests. To start the tracer, simply call the start method along with an optional set of options. By default, the trace agent is considered to be found at "localhost:8126". In a setup where this would be different (let's say 127.0.0.1:1234), we could do:
tracer.Start(tracer.WithAgentAddr("127.0.0.1:1234")) defer tracer.Stop()
The tracing client can perform trace sampling. While the trace agent already samples traces to reduce bandwidth usage, client sampling reduces performance overhead. To make use of it, the package comes with a ready-to-use rate sampler that can be passed to the tracer. To use it and keep only 30% of the requests, one would do:
s := tracer.NewRateSampler(0.3) tracer.Start(tracer.WithSampler(s))
All spans created by the tracer contain a context hereby referred to as the span context. Note that this is different from Go's context. The span context is used to package essential information from a span, which is needed when creating child spans that inherit from it. Thus, a child span is created from a span's span context. The span context can originate from within the same process, but also a different process or even a different machine in the case of distributed tracing.
To make use of distributed tracing, a span's context may be injected via a carrier into a transport (HTTP, RPC, etc.) to be extracted on the other end and used to create spans that are direct descendants of it. A couple of carrier interfaces which should cover most of the use-case scenarios are readily provided, such as HTTPCarrier and TextMapCarrier. Users are free to create their own, which will work with our propagation algorithm as long as they implement the TextMapReader and TextMapWriter interfaces. An example alternate implementation is the MDCarrier in our gRPC integration.
As an example, injecting a span's context into an HTTP request would look like this:
req, err := http.NewRequest("GET", "http://example.com", nil) // ... err := tracer.Inject(span.Context(), tracer.HTTPHeadersCarrier(req.Header)) // ... http.DefaultClient.Do(req)
Then, on the server side, to continue the trace one would do:
sctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(req.Header)) // ... span := tracer.StartSpan("child.span", tracer.ChildOf(sctx))
In the same manner, any means can be used as a carrier to inject a context into a transport. Go's context can also be used as a means to transport spans within the same process. The methods StartSpanFromContext, ContextWithSpan and SpanFromContext exist for this reason.
Some libraries and frameworks are supported out-of-the-box by using one of our integrations. You can see a list of supported integrations here: https://godoc.org/gopkg.in/DataDog/dd-trace-go.v1/contrib
Example ¶
A basic example demonstrating how to start the tracer, as well as how to create a root span and a child span that is a descendant of it.
// Start the tracer and defer the Stop method. Start(WithAgentAddr("host:port")) defer Stop() // Start a root span. span := StartSpan("get.data") defer span.Finish() // Create a child of it, computing the time needed to read a file. child := StartSpan("read.file", ChildOf(span.Context())) child.SetTag(ext.ResourceName, "test.json") // Perform an operation. _, err := ioutil.ReadFile("~/test.json") // We may finish the child span using the returned error. If it's // nil, it will be disregarded. child.Finish(WithError(err)) if err != nil { log.Fatal(err) }
Output:
Index ¶
- Constants
- Variables
- func ContextWithSpan(ctx context.Context, s Span) context.Context
- func Extract(carrier interface{}) (ddtrace.SpanContext, error)
- func Inject(ctx ddtrace.SpanContext, carrier interface{}) error
- func Start(opts ...StartOption)
- func Stop()
- type FinishOption
- type HTTPHeadersCarrier
- type Propagator
- type PropagatorConfig
- type RateSampler
- type Sampler
- type Span
- type StartOption
- func WithAgentAddr(addr string) StartOption
- func WithAnalytics(on bool) StartOption
- func WithAnalyticsRate(rate float64) StartOption
- func WithDebugMode(enabled bool) StartOption
- func WithGlobalTag(k string, v interface{}) StartOption
- func WithHTTPRoundTripper(r http.RoundTripper) StartOption
- func WithPrioritySampling() StartOption
- func WithPropagator(p Propagator) StartOption
- func WithSampler(s Sampler) StartOption
- func WithServiceName(name string) StartOption
- type StartSpanOption
- func ChildOf(ctx ddtrace.SpanContext) StartSpanOption
- func ResourceName(name string) StartSpanOption
- func ServiceName(name string) StartSpanOption
- func SpanType(name string) StartSpanOption
- func StartTime(t time.Time) StartSpanOption
- func Tag(k string, v interface{}) StartSpanOption
- func WithSpanID(id uint64) StartSpanOption
- type TextMapCarrier
- type TextMapReader
- type TextMapWriter
Examples ¶
Constants ¶
const ( // DefaultBaggageHeaderPrefix specifies the prefix that will be used in // HTTP headers or text maps to prefix baggage keys. DefaultBaggageHeaderPrefix = "ot-baggage-" // DefaultTraceIDHeader specifies the key that will be used in HTTP headers // or text maps to store the trace ID. DefaultTraceIDHeader = "x-datadog-trace-id" // DefaultParentIDHeader specifies the key that will be used in HTTP headers // or text maps to store the parent ID. DefaultParentIDHeader = "x-datadog-parent-id" // DefaultPriorityHeader specifies the key that will be used in HTTP headers // or text maps to store the sampling priority value. DefaultPriorityHeader = "x-datadog-sampling-priority" )
Variables ¶
var ( // ErrInvalidCarrier is returned when the carrier provided to the propagator // does not implemented the correct interfaces. ErrInvalidCarrier = errors.New("invalid carrier") // ErrInvalidSpanContext is returned when the span context found in the // carrier is not of the expected type. ErrInvalidSpanContext = errors.New("invalid span context") // ErrSpanContextCorrupted is returned when there was a problem parsing // the information found in the carrier. ErrSpanContextCorrupted = errors.New("span context corrupted") // ErrSpanContextNotFound represents missing information in the given carrier. ErrSpanContextNotFound = errors.New("span context not found") )
Functions ¶
func ContextWithSpan ¶
ContextWithSpan returns a copy of the given context which includes the span s.
func Extract ¶
func Extract(carrier interface{}) (ddtrace.SpanContext, error)
Extract extracts a SpanContext from the carrier. The carrier is expected to implement TextMapReader, otherwise an error is returned. If the tracer is not started, calling this function is a no-op.
func Inject ¶
func Inject(ctx ddtrace.SpanContext, carrier interface{}) error
Inject injects the given SpanContext into the carrier. The carrier is expected to implement TextMapWriter, otherwise an error is returned. If the tracer is not started, calling this function is a no-op.
func Start ¶
func Start(opts ...StartOption)
Start starts the tracer with the given set of options. It will stop and replace any running tracer, meaning that calling it several times will result in a restart of the tracer by replacing the current instance with a new one.
Types ¶
type FinishOption ¶
type FinishOption = ddtrace.FinishOption
FinishOption is a configuration option for FinishSpan. It is aliased in order to help godoc group all the functions returning it together. It is considered more correct to refer to it as the type as the origin, ddtrace.FinishOption.
func FinishTime ¶
func FinishTime(t time.Time) FinishOption
FinishTime sets the given time as the finishing time for the span. By default, the current time is used.
func NoDebugStack ¶ added in v1.5.0
func NoDebugStack() FinishOption
NoDebugStack prevents any error presented using the WithError finishing option from generating a stack trace. This is useful in situations where errors are frequent and performance is critical.
func StackFrames ¶ added in v1.14.0
func StackFrames(n, skip uint) FinishOption
StackFrames limits the number of stack frames included into erroneous spans to n, starting from skip.
func WithError ¶
func WithError(err error) FinishOption
WithError marks the span as having had an error. It uses the information from err to set tags such as the error message, error type and stack trace. It has no effect if the error is nil.
type HTTPHeadersCarrier ¶
HTTPHeadersCarrier wraps an http.Header as a TextMapWriter and TextMapReader, allowing it to be used using the provided Propagator implementation.
func (HTTPHeadersCarrier) ForeachKey ¶
func (c HTTPHeadersCarrier) ForeachKey(handler func(key, val string) error) error
ForeachKey implements TextMapReader.
func (HTTPHeadersCarrier) Set ¶
func (c HTTPHeadersCarrier) Set(key, val string)
Set implements TextMapWriter.
type Propagator ¶
type Propagator interface { // Inject takes the SpanContext and injects it into the carrier. Inject(context ddtrace.SpanContext, carrier interface{}) error // Extract returns the SpanContext from the given carrier. Extract(carrier interface{}) (ddtrace.SpanContext, error) }
Propagator implementations should be able to inject and extract SpanContexts into an implementation specific carrier.
func NewPropagator ¶
func NewPropagator(cfg *PropagatorConfig) Propagator
NewPropagator returns a new propagator which uses TextMap to inject and extract values. It propagates trace and span IDs and baggage. To use the defaults, nil may be provided in place of the config.
type PropagatorConfig ¶
type PropagatorConfig struct { // BaggagePrefix specifies the prefix that will be used to store baggage // items in a map. It defaults to DefaultBaggageHeaderPrefix. BaggagePrefix string // TraceHeader specifies the map key that will be used to store the trace ID. // It defaults to DefaultTraceIDHeader. TraceHeader string // ParentHeader specifies the map key that will be used to store the parent ID. // It defaults to DefaultParentIDHeader. ParentHeader string // PriorityHeader specifies the map key that will be used to store the sampling priority. // It deafults to DefaultPriorityHeader. PriorityHeader string }
PropagatorConfig defines the configuration for initializing a propagator.
type RateSampler ¶
type RateSampler interface { Sampler // Rate returns the current sample rate. Rate() float64 // SetRate sets a new sample rate. SetRate(rate float64) }
RateSampler is a sampler implementation which randomly selects spans using a provided rate. For example, a rate of 0.75 will permit 75% of the spans. RateSampler implementations should be safe for concurrent use.
func NewAllSampler ¶
func NewAllSampler() RateSampler
NewAllSampler is a short-hand for NewRateSampler(1). It is all-permissive.
func NewRateSampler ¶
func NewRateSampler(rate float64) RateSampler
NewRateSampler returns an initialized RateSampler with a given sample rate.
type Sampler ¶
type Sampler interface { // Sample returns true if the given span should be sampled. Sample(span Span) bool }
Sampler is the generic interface of any sampler. It must be safe for concurrent use.
type Span ¶
Span is an alias for ddtrace.Span. It is here to allow godoc to group methods returning ddtrace.Span. It is recommended and is considered more correct to refer to this type as ddtrace.Span instead.
func SpanFromContext ¶
SpanFromContext returns the span contained in the given context. A second return value indicates if a span was found in the context. If no span is found, a no-op span is returned.
func StartSpan ¶
func StartSpan(operationName string, opts ...StartSpanOption) Span
StartSpan starts a new span with the given operation name and set of options. If the tracer is not started, calling this function is a no-op.
func StartSpanFromContext ¶
func StartSpanFromContext(ctx context.Context, operationName string, opts ...StartSpanOption) (Span, context.Context)
StartSpanFromContext returns a new span with the given operation name and options. If a span is found in the context, it will be used as the parent of the resulting span. If the ChildOf option is passed, the span from context will take precedence over it as the parent span.
type StartOption ¶
type StartOption func(*config)
StartOption represents a function that can be provided as a parameter to Start.
func WithAgentAddr ¶
func WithAgentAddr(addr string) StartOption
WithAgentAddr sets the address where the agent is located. The default is localhost:8126. It should contain both host and port.
func WithAnalytics ¶ added in v1.11.0
func WithAnalytics(on bool) StartOption
WithAnalytics allows specifying whether Trace Search & Analytics should be enabled for integrations.
func WithAnalyticsRate ¶ added in v1.11.0
func WithAnalyticsRate(rate float64) StartOption
WithAnalyticsRate sets the global sampling rate for sampling APM events.
func WithDebugMode ¶
func WithDebugMode(enabled bool) StartOption
WithDebugMode enables debug mode on the tracer, resulting in more verbose logging.
func WithGlobalTag ¶
func WithGlobalTag(k string, v interface{}) StartOption
WithGlobalTag sets a key/value pair which will be set as a tag on all spans created by tracer. This option may be used multiple times.
func WithHTTPRoundTripper ¶ added in v1.7.0
func WithHTTPRoundTripper(r http.RoundTripper) StartOption
WithHTTPRoundTripper allows customizing the underlying HTTP transport for emitting spans. This is useful for advanced customization such as emitting spans to a unix domain socket. The default should be used in most cases.
func WithPrioritySampling ¶ added in v1.7.0
func WithPrioritySampling() StartOption
WithPrioritySampling is deprecated, and priority sampling is enabled by default. When using distributed tracing, the priority sampling value is propagated in order to get all the parts of a distributed trace sampled. To learn more about priority sampling, please visit: https://docs.datadoghq.com/tracing/getting_further/trace_sampling_and_storage/#priority-sampling-for-distributed-tracing
func WithPropagator ¶
func WithPropagator(p Propagator) StartOption
WithPropagator sets an alternative propagator to be used by the tracer.
func WithSampler ¶
func WithSampler(s Sampler) StartOption
WithSampler sets the given sampler to be used with the tracer. By default an all-permissive sampler is used.
func WithServiceName ¶
func WithServiceName(name string) StartOption
WithServiceName sets the default service name to be used with the tracer.
type StartSpanOption ¶
type StartSpanOption = ddtrace.StartSpanOption
StartSpanOption is a configuration option for StartSpan. It is aliased in order to help godoc group all the functions returning it together. It is considered more correct to refer to it as the type as the origin, ddtrace.StartSpanOption.
func ChildOf ¶
func ChildOf(ctx ddtrace.SpanContext) StartSpanOption
ChildOf tells StartSpan to use the given span context as a parent for the created span.
func ResourceName ¶
func ResourceName(name string) StartSpanOption
ResourceName sets the given resource name on the started span. A resource could be an SQL query, a URL, an RPC method or something else.
func ServiceName ¶
func ServiceName(name string) StartSpanOption
ServiceName sets the given service name on the started span. For example "http.server".
func SpanType ¶
func SpanType(name string) StartSpanOption
SpanType sets the given span type on the started span. Some examples in the case of the Datadog APM product could be "web", "db" or "cache".
func StartTime ¶
func StartTime(t time.Time) StartSpanOption
StartTime sets a custom time as the start time for the created span. By default a span is started using the creation time.
func Tag ¶
func Tag(k string, v interface{}) StartSpanOption
Tag sets the given key/value pair as a tag on the started Span.
func WithSpanID ¶ added in v1.10.0
func WithSpanID(id uint64) StartSpanOption
WithSpanID sets the SpanID on the started span, instead of using a random number. If there is no parent Span (eg from ChildOf), then the TraceID will also be set to the value given here.
type TextMapCarrier ¶
TextMapCarrier allows the use of a regular map[string]string as both TextMapWriter and TextMapReader, making it compatible with the provided Propagator.
func (TextMapCarrier) ForeachKey ¶
func (c TextMapCarrier) ForeachKey(handler func(key, val string) error) error
ForeachKey conforms to the TextMapReader interface.
func (TextMapCarrier) Set ¶
func (c TextMapCarrier) Set(key, val string)
Set implements TextMapWriter.
type TextMapReader ¶
type TextMapReader interface { // ForeachKey iterates over all keys that exist in the underlying // carrier. It takes a callback function which will be called // using all key/value pairs as arguments. ForeachKey will return // the first error returned by the handler. ForeachKey(handler func(key, val string) error) error }
TextMapReader allows iterating over sets of key/value pairs. Carriers implementing TextMapReader are compatible to be used with Datadog's TextMapPropagator.
type TextMapWriter ¶
type TextMapWriter interface { // Set sets the given key/value pair. Set(key, val string) }
TextMapWriter allows setting key/value pairs of strings on the underlying data structure. Carriers implementing TextMapWriter are compatible to be used with Datadog's TextMapPropagator.