Exclude Endpoints with Sample Rate in OpenTelemetry in the Golang Language

Open telemetry is one of the best systems for tracing system requests in Golang. Our DevOps team (Mohsen Mottaghi) gathers all requests into Graphana and we track all bugs and causes in our system. This system is extremely cute and important for each enterprise and commercial system.

In this article, I explain a way to exclude endpoints in routing with a sample rate interface in the Golang language.

In the open telemetry official account, they mention samples in open telemetry.And said, by using these method we can handle requests AlwaysSample, NeverSample, TraceIDRatioBased, ParentBased.
It ‘s mean that we can send these four function as parameter and determined types of sample rate in your system.

For example, in below code set 0.2 for sample rate with using TraceIDRatioBased function.

go
secureOption := otlptracegrpc.WithInsecure() exporter, err := otlptrace.New(  context.Background(),
otlptracegrpc.NewClient(   secureOption,   otlptracegrpc.WithEndpoint(config.OtelEndpoint()),  ), )
if err != nil {  log.Fatal(err) } resources, err := resource.New(  context.Background(),
resource.WithHost(), // This option configures a set of Detectors that discover host information
resource.WithAttributes(   attribute.String("service.name", "Service Name"),  ), ) if err != nil {
log.Fatal("Could not set resources: ", err) } tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.TraceIDRatioBased(0.2)),  sdktrace.WithBatcher(exporter),
sdktrace.WithResource(resources), ) otel.SetTracerProvider(tp) //propagator
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{},
propagation.Baggage{}))

Exclude Endpoint with sample rate

If using open telemetry in the gateway and your services have a health check or metrics, then you don’t want to capture these endpoints, you need to implement a sampler interface.

Sample of implementing interface for excluding health check endpoints.

go
package tracer
import (
"strings" sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
type ExcludeSampler struct {
    delegate sdktrace.Sampler
}
func New(delegate sdktrace.Sampler) sdktrace.Sampler {
    return &ExcludeSampler{
        delegate: delegate
    }
}
func (e ExcludeSampler) ShouldSample(parameters sdktrace.SamplingParameters) sdktrace.SamplingResult
{
    if parameters.Name == "/health" || strings.HasPrefix(parameters.Name, "/static"){
        return sdktrace.SamplingResult{
            Decision: sdktrace.Drop
        }
    }
    return sdktrace.TraceIDRatioBased("0.5").ShouldSample(parameters)
}
func (e ExcludeSampler) Description() string {
    return "excludeEndpoints"
}

Stackademic

Thank you for reading until the end. Before you go:

  • Please consider clapping and following the writer! 👏
  • Follow us on Twitter(X), LinkedIn, and YouTube.
  • Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.