{
  "slug": "Exclude-Endpoints-with-Sample-Rate-in-OpenTelemetry-in-the-Golang-Language-969da599af82",
  "title": "Exclude Endpoints with Sample Rate in OpenTelemetry in the Golang Language",
  "subtitle": "Open telemetry is one of the best systems for tracing system requests in Golang. Our DevOps team (Mohsen Mottaghi) gathers all requests…",
  "excerpt": "Open telemetry is one of the best systems for tracing system requests in Golang. Our DevOps team (Mohsen Mottaghi) gathers all requests…",
  "date": "2023-11-28",
  "tags": [
    "Go",
    "DevOps"
  ],
  "readingTime": "2 min",
  "url": "https://medium.com/@mobinshaterian/exclude-endpoints-with-sample-rate-in-open-telemetry-in-the-golang-language-969da599af82",
  "hero": "https://cdn-images-1.medium.com/max/800/1*NC2QXAjoyj8z80gA3nBCeg.png",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Exclude Endpoints with Sample Rate in OpenTelemetry in the Golang Language"
    },
    {
      "type": "paragraph",
      "html": "Open telemetry is one of the best systems for tracing system requests in Golang. Our DevOps team (<a href=\"https://medium.com/u/44a7f70b3770\" target=\"_blank\" rel=\"noreferrer noopener\">Mohsen Mottaghi</a>) 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."
    },
    {
      "type": "paragraph",
      "html": "In this article, I explain a way to exclude endpoints in routing with a sample rate interface in the Golang language."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*NC2QXAjoyj8z80gA3nBCeg.png",
      "alt": "https://techcommunity.microsoft.com/t5/azure-data-explorer-blog/getting-started-with-open-telemetry-and-azure-data-explorer/ba-p/3675708",
      "caption": "https://techcommunity.microsoft.com/t5/azure-data-explorer-blog/getting-started-with-open-telemetry-and-azure-data-explorer/ba-p/3675708",
      "width": 885,
      "height": 570
    },
    {
      "type": "paragraph",
      "html": "In the open telemetry official account, they mention samples in open telemetry.And said, by using these method we can handle requests <code><a href=\"https://pkg.go.dev/go.opentelemetry.io/otel/sdk/trace#AlwaysSample\" target=\"_blank\" rel=\"noreferrer noopener\">AlwaysSample</a>, <a href=\"https://pkg.go.dev/go.opentelemetry.io/otel/sdk/trace#NeverSample\" target=\"_blank\" rel=\"noreferrer noopener\">NeverSample</a>, <a href=\"https://pkg.go.dev/go.opentelemetry.io/otel/sdk/trace#TraceIDRatioBased\" target=\"_blank\" rel=\"noreferrer noopener\">TraceIDRatioBased</a>, ParentBased.<br></code>It ‘s mean that we can send these four function as parameter and determined types of sample rate in your system."
    },
    {
      "type": "paragraph",
      "html": "For example, in below code set 0.2 for sample rate with using TraceIDRatioBased function."
    },
    {
      "type": "code",
      "lang": "go",
      "code": "secureOption := otlptracegrpc.WithInsecure() exporter, err := otlptrace.New(  context.Background(),\notlptracegrpc.NewClient(   secureOption,   otlptracegrpc.WithEndpoint(config.OtelEndpoint()),  ), )\nif err != nil {  log.Fatal(err) } resources, err := resource.New(  context.Background(),\nresource.WithHost(), // This option configures a set of Detectors that discover host information\nresource.WithAttributes(   attribute.String(\"service.name\", \"Service Name\"),  ), ) if err != nil {\nlog.Fatal(\"Could not set resources: \", err) } tp := sdktrace.NewTracerProvider(\nsdktrace.WithSampler(sdktrace.TraceIDRatioBased(0.2)),  sdktrace.WithBatcher(exporter),\nsdktrace.WithResource(resources), ) otel.SetTracerProvider(tp) //propagator\notel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{},\npropagation.Baggage{}))"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Exclude Endpoint with sample rate"
    },
    {
      "type": "paragraph",
      "html": "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."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Sample of implementing interface for excluding health check endpoints."
    },
    {
      "type": "code",
      "lang": "go",
      "code": "package tracer\nimport (\n\"strings\" sdktrace \"go.opentelemetry.io/otel/sdk/trace\"\n)\ntype ExcludeSampler struct {\n    delegate sdktrace.Sampler\n}\nfunc New(delegate sdktrace.Sampler) sdktrace.Sampler {\n    return &ExcludeSampler{\n        delegate: delegate\n    }\n}\nfunc (e ExcludeSampler) ShouldSample(parameters sdktrace.SamplingParameters) sdktrace.SamplingResult\n{\n    if parameters.Name == \"/health\" || strings.HasPrefix(parameters.Name, \"/static\"){\n        return sdktrace.SamplingResult{\n            Decision: sdktrace.Drop\n        }\n    }\n    return sdktrace.TraceIDRatioBased(\"0.5\").ShouldSample(parameters)\n}\nfunc (e ExcludeSampler) Description() string {\n    return \"excludeEndpoints\"\n}"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Stackademic"
    },
    {
      "type": "paragraph",
      "html": "<em>Thank you for reading until the end. Before you go:</em>"
    },
    {
      "type": "list",
      "ordered": false,
      "items": [
        "<em>Please consider </em><strong><em>clapping</em></strong><em> and </em><strong><em>following</em></strong><em> the writer! 👏</em>",
        "<em>Follow us on </em><a href=\"https://twitter.com/stackademichq\" target=\"_blank\" rel=\"noreferrer noopener\"><strong><em>Twitter(X)</em></strong></a><em>, </em><a href=\"https://www.linkedin.com/company/stackademic\" target=\"_blank\" rel=\"noreferrer noopener\"><strong><em>LinkedIn</em></strong></a><em>, and </em><a href=\"https://www.youtube.com/c/stackademic\" target=\"_blank\" rel=\"noreferrer noopener\"><strong><em>YouTube</em></strong></a><strong><em>.</em></strong>",
        "<em>Visit </em><a href=\"http://stackademic.com/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong><em>Stackademic.com</em></strong></a><em> to find out more about how we are democratizing free programming education around the world.</em>"
      ]
    }
  ]
}
