<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nairoua Salaton on Medium]]></title>
        <description><![CDATA[Stories by Nairoua Salaton on Medium]]></description>
        <link>https://medium.com/@nairouasalaton?source=rss-868b66915371------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1MBzAu_KrZ8LBAUdmFnU1w@2x.jpeg</url>
            <title>Stories by Nairoua Salaton on Medium</title>
            <link>https://medium.com/@nairouasalaton?source=rss-868b66915371------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 17:23:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nairouasalaton/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction to Tracing in Go with Jaeger & OpenTelemetry]]></title>
            <link>https://medium.com/@nairouasalaton/introduction-to-tracing-in-go-with-jaeger-opentelemetry-71955c2afa39?source=rss-868b66915371------2</link>
            <guid isPermaLink="false">https://medium.com/p/71955c2afa39</guid>
            <category><![CDATA[opentelemetry]]></category>
            <category><![CDATA[tracing]]></category>
            <category><![CDATA[jaeger]]></category>
            <category><![CDATA[distributed-tracing]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Nairoua Salaton]]></dc:creator>
            <pubDate>Wed, 01 May 2024 20:57:41 GMT</pubDate>
            <atom:updated>2024-05-01T20:57:41.387Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Practical Approach to Adding Distributed Tracing to Your Go Projects</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rZtnwgyi6COPHKSk.png" /></figure><h3>Introduction</h3><p>Understanding how requests traverse through services is crucial for both performance tuning and debugging and this is where distributed tracing comes in, providing visibility into the lifecycle of requests across systems. Jaeger offers a way to monitor transactions in complex environments. This article will guide you through setting up jaeger in a local environment, integrating it with a Go application and using its UI to gain insights into your service’s behaviour</p><h3>Understanding Tracing</h3><p>A <strong>trace</strong> is a representation of a request as it moves through the various services of an application. Each trace is made up of several <strong>spans</strong>, where a span represents a single operation or a unit of work performed within a service. These spans include important metadata like start and end times, logs of relevant events, and tags for query and error information.</p><p>To help you understand spans and traces, I’ll give an example below:</p><p>Imagine a user making a request to a web application to check their order status. This request triggers a series of operations across different services in the application:</p><ul><li><strong>Trace</strong>: The entire journey of the request — from the moment it hits the web server, through the authentication service, then to the database service to retrieve order data — forms a trace. The trace provides a comprehensive view of the request’s path through the distributed system.</li><li><strong>Spans</strong>: Each individual operation within this journey is a span i.e</li><li><strong><em>Span 1</em></strong>: Web server processes the request.</li><li><strong><em>Span 2</em></strong>: Authentication service verifies user identity.</li><li><strong><em>Span 3</em></strong>: Database service retrieves the order details.</li></ul><p>Each span includes timing data, helping to identify how long each service took to complete its part of the request. This helps in <strong>pinpointing where delays occur</strong> if the request takes longer than expected.</p><blockquote>A simple analogy would be thinking of a <strong>trace</strong> as a detailed itinerary of a tourist on a multi-city tour (The entire trip from start to finish, showing all cities and stops along the way) and <strong>spans</strong> are the individual legs of the trip (Each visit within the trip, where each visit includes activities like tours, meals 🍔 etc)</blockquote><h3>Setting Up Jaeger for Local Testing</h3><p>When developing locally, you can run Jaeger using Docker. This simplifies the setup process and lets you focus on integrating and testing tracing within your applications without needing a full-scale deployment. Below are the steps to get Jaeger up and running on your machine.</p><h4>Downloading Jaeger</h4><p>First, pull the latest Jaeger Docker image that includes all necessary components (`all-in-one` setup) using the following command:</p><pre>docker pull jaegertracing/all-in-one:1.56</pre><h4>Running Jaeger</h4><p>Once the image is pulled, you can run Jaeger by executing the following command. This command starts the Jaeger backend along with its web UI and exposes all necessary ports for various protocols and operations:</p><pre>docker run - rm - name jaeger \<br> -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \<br> -p 16686:16686 \<br> -p 4318:4318 \<br> jaegertracing/all-in-one:1.56</pre><h4>Accessing the Jaeger UI</h4><p>With Jaeger running, you can access the web UI by navigating to:</p><pre>http://localhost:16686</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*15_7tShfg6BDFbdAet_tRg.png" /><figcaption>Jaeger UI</figcaption></figure><p>This UI is your gateway to viewing traces and understanding how data flows through your applications.</p><h3>Integrating Jaeger with a Go Application using OpenTelemetry</h3><h4>Why OpenTelemetry?</h4><p>OTel (OpenTelemetry) provides a unified way to collect and transport telemetry data. By using OTel libraries within your Go application, you enable comprehensive observability, which I find critical for maintaining system reliability and performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/903/1*YpxV5s_zBSre19yP6sKf5w.png" /><figcaption>OpenTelemetry with Jaeger</figcaption></figure><p>The architecture above shows the workflow of telemetry data as it is collected from the application and made available for visualisation on the Jaeger UI. Here’s a breakdown of what each component does:</p><ol><li>Application (OTel library): This is your application code that has been instrumented with the OpenTelemetry libraries.</li><li>OpenTelemetry Collector: The collected telemetry data from the application is sent to the OpenTelemetry Collector. This component can receive data from various application instances and forwards it to analytical tools like jaeger</li><li>jaeger-collector: The Jaeger Collector receives trace data from the OTel Collector. It’s responsible for processing the data, such as validating, transforming, and storing it.</li><li>Elasticsearch/Cassandra: The processed data is stored in a database or in-memory storage. This could be a datastore like Cassandra or Elasticsearch where Jaeger keeps its trace information.</li><li>jaeger-query: This component retrieves trace data from the storage when requested and serves it to the user interface.</li><li>jaeger-UI: This is the graphical user interface of Jaeger where users can visualize and query the trace data</li></ol><h3>Instrumenting a Go REST API</h3><p>This is where we get our hands dirty and dive into some code. This will enable you to implement tracing, track requests within your application, giving you a clear picture of its performance.</p><p>You’ll first need to install the necessary packages that enable OpenTelemetry. They include:</p><pre>go get go.opentelemetry.io/otel<br>go get go.opentelemetry.io/otel/exporters/otlp/otlptrace<br>go get go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp<br>go get go.opentelemetry.io/otel/sdk</pre><h4>Setting up Tracing</h4><p>In this section, we’ll walk through setting up tracing. Lets break down the components of the setup.</p><h4>Exporter Initialisation</h4><p>An exporter is responsible for sending telemetry data to one or more backends or a collector. A collector, e.g OTEL Collector acts like a middleware that can receive, process and export telemetry data. In such setups, the data flow typically looks like this:</p><ul><li>Application Instrumentation: The application uses OpenTelemetry SDKs to collect telemetry data.</li><li>Exporter: Configured within the application, the exporter sends this data to a collector. The communication might be over protocols such as HTTP or gRPC.</li><li>Collector: Receives the data and can perform various functions such as aggregation, transformation, or enrichment before exporting it to the final backend system (like Jaeger, Prometheus, etc.).</li></ul><p>The code below shows how to initialise an exporter</p><pre>headers := map[string]string{<br>  &quot;content-type&quot;: &quot;application/json&quot;,<br>}<br><br>exporter, err := otlptrace.New(<br>  context.Background(),<br>  otlptracehttp.NewClient(<br>   otlptracehttp.WithEndpoint(&quot;localhost:4318&quot;),<br>   otlptracehttp.WithHeaders(headers),<br>   otlptracehttp.WithInsecure(),<br>  ),<br>)</pre><p>We begin by defining the HTTP headers that the exporter will use when sending data. The content-type header is set to application/json, which tells the receiver to expect the data in JSON format.</p><p>The core of the function involves setting up an OTLP (OpenTelemetry Protocol) HTTP exporter.</p><ul><li>otlptracehttp.WithEndpoint(&quot;localhost:4318&quot;) — configures the exporter to send data to a local collector running on port 4318. This port is designated for HTTP connections receiving OTLP data</li><li>otlptracehttp.WithHeaders(headers) — applies the headers defined earlier, ensuring that the HTTP request includes them.</li><li>otlptracehttp.WithInsecure() tells the exporter to use an insecure connection, which means it does not use TLS. This is not recommended for production environments but is common in local testing environments</li></ul><h4>Tracer Setup</h4><p>Once the exporter is created, a tracer provider is instantiated. This provider is crucial as it manages the creation of tracers, which are used to generate the telemetry data i.e create the spans.</p><pre>tracerprovider := trace.NewTracerProvider(<br>  trace.WithBatcher(<br>    exporter,<br>    trace.WithMaxExportBatchSize(trace.DefaultMaxExportBatchSize),<br>    trace.WithBatchTimeout(trace.DefaultScheduleDelay*time.Millisecond),<br>    trace.WithMaxExportBatchSize(trace.DefaultMaxExportBatchSize),<br>  ),<br>  trace.WithResource(<br>   resource.NewWithAttributes(<br>    semconv.SchemaURL,<br>    semconv.ServiceNameKey.String(&quot;product-app&quot;),<br>   ),<br>  ),<br>)</pre><p>Additionally, the tracer provider includes resource attributes that append metadata and in this case we add the service name to each trace.</p><h4>Full Code</h4><p>Here is the complete function that sets up tracing, combining all the elements we’ve discussed</p><pre>package main<br><br>import (<br> &quot;context&quot;<br> &quot;fmt&quot;<br> &quot;log&quot;<br> &quot;os&quot;<br> &quot;os/signal&quot;<br> &quot;syscall&quot;<br> &quot;time&quot;<br><br> &quot;github.com/Salaton/tracing/pkg/presentation&quot;<br> &quot;go.opentelemetry.io/otel&quot;<br> &quot;go.opentelemetry.io/otel/exporters/otlp/otlptrace&quot;<br> &quot;go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp&quot;<br> &quot;go.opentelemetry.io/otel/sdk/resource&quot;<br> &quot;go.opentelemetry.io/otel/sdk/trace&quot;<br> semconv &quot;go.opentelemetry.io/otel/semconv/v1.4.0&quot;<br>)<br><br>func startTracing() (*trace.TracerProvider, error) {<br> headers := map[string]string{<br>  &quot;content-type&quot;: &quot;application/json&quot;,<br> }<br><br> exporter, err := otlptrace.New(<br>  context.Background(),<br>  otlptracehttp.NewClient(<br>   otlptracehttp.WithEndpoint(&quot;localhost:4318&quot;),<br>   otlptracehttp.WithHeaders(headers),<br>   otlptracehttp.WithInsecure(),<br>  ),<br> )<br> if err != nil {<br>  return nil, fmt.Errorf(&quot;creating new exporter: %w&quot;, err)<br> }<br><br> tracerprovider := trace.NewTracerProvider(<br>  trace.WithBatcher(<br>   exporter,<br>   trace.WithMaxExportBatchSize(trace.DefaultMaxExportBatchSize),<br>   trace.WithBatchTimeout(trace.DefaultScheduleDelay*time.Millisecond),<br>   trace.WithMaxExportBatchSize(trace.DefaultMaxExportBatchSize),<br>  ),<br>  trace.WithResource(<br>   resource.NewWithAttributes(<br>    semconv.SchemaURL,<br>    semconv.ServiceNameKey.String(&quot;product-app&quot;),<br>   ),<br>  ),<br> )<br><br> otel.SetTracerProvider(tracerprovider)<br><br> return tracerprovider, nil<br>}</pre><h4>Example in Code</h4><p>Spans are added within the codebase to trace the executions of certain methods. Here is a simple snippet</p><pre>var tracer = otel.Tracer(&quot;github.com/Salaton/tracing/pkg/usecases/product&quot;)<br><br><br>func (u UseCaseImplementation) CreateProduct(ctx context.Context, product Product) (Product, error) {<br> ctx, span := tracer.Start(ctx, &quot;CreateProduct&quot;)<br> defer span.End()<br> return u.store.CreateProduct(ctx, product)<br>}</pre><p>A tracer is initialised with a specific name to identify it uniquely. When the CreateProduct method is called, a new span is started. After the business logic is executed, the span is ended to mark the completion of tracing for that particular operation.</p><h3>Viewing Traces on the Jaeger UI</h3><p>After the application runs and we access the jaeger UI, we see the traces from our app. Each trace shows the path of an operation, in this example, there is the <strong><em>Create Product</em></strong> execution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gw-DYVpl2R1x3UHe8_r6bw.png" /><figcaption>Jaeger UI</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QvKd2fH55sq90q0tZpz1ng.png" /><figcaption>Individual spans</figcaption></figure><h3>Conclusion</h3><p>Wrapping up, we’ve walked through integrating OpenTelemetry in Go, which lights the path your data takes, giving you clear visibility into your application’s operations. With the examples and explanations provided, you’re ready to enhance your projects with powerful tracing capabilities. Next attempt will be instrumenting <strong>GraphQL APIs.</strong></p><p>For the complete code and more detailed exploration, visit the repository below. Happy coding!</p><p><a href="https://github.com/Salaton/tracing">GitHub - Salaton/tracing: A repo to demonstrate tracing in Golang using Jaeger</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71955c2afa39" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>