Tracing: Add span's sampling status check to labkit
We didn't have a way to extract the sampling status of a distributed tracing span in Gitaly. This information is important for Explore use of Git's trace2 mechanism (#2769 - closed), because trace2 is expensive. We can only enable it for some particular commands, or when the current active trace is enabled.
The tracing feature is now powered by opentracing and labkit. We allow feeding multiple drivers (jaeger, stackdriver, datadog, etc.) as an environment variable GITLAB_TRACING
. That makes extracting such information is not feasible in Gitaly. The following code will work for most tracing libraries, but not stackdriver. Labkit doesn't expose the internal span context (source code).
func IsSampled(span opentracing.Span) bool {
ctx := span.Context()
if jaegerContext, ok := ctx.(jaeger.SpanContext); ok {
return jaegerContext.IsSampled()
}
if lightstepContext, ok := ctx.(lightstep.SpanContext); ok {
return lightstepContext.Sampled == "true"
}
if stackdriverContext, ok := span.(stackdriver.spanContext); ok {
// Oops, span context is not exposed
}
}
To fix this, we add a helper method func IsSampled(*opentracing.span) bool
in labkit. It has enough information to extract this information.