Instrumenting your Go distributed application for tracing with Jaeger

Recently I've been reading about application observation and got fascinated with Jaeger Tracing and the benefits tracing brings to visualize your data flow.

For deeper understanding, I recommend the book Mastering Distributed Tracing by Yuri Shkuro, an engineer at Uber, creator of Jaeger. He wrote a great tutorial on tracing and optimizing an application so here I’ll mostly focus how to basically instrument your Go application for tracing.

Since I was already playing with Echo, a nice and minimalist web framework for Go I decided to use it together with other tools to create some sample applications. I also contributed a tracing middleware and submitted a Prometheus Metrics middleware as well, waiting to be merged.

The example is composed of 3 “microservices”. The first is a Formatter, taking a name and returning a “Hello [Name]” string. The second, Publisher, just receives a string, calls the Echo microservice and prints the string to the console. The base of these two came from Yuri’s own tutorial repo and uses Go standard lib and a tracing library that provides some methods to make one’s life easier that I will detail later.

The third is a server created with Echo Framework, and the middleware to provide tracing that can be found it on the echo-contrib repo. It receives the “Hello” string, prints it out and calls a simulated function that takes some random time to execute.

The complete code is in my Go-playground repository under microservices and can be used as base for your own applications or tests.

To run this code, you can use Jaeger all-in-one on your own machine, just run it in Docker and open http://localhost:16686 on your browser.

This is a good post that discusses Jaeger infrastructure and it's components. Take a look if planning to deploy on your cluster:

Let's go deeper on code.

Client app

The client app generates the initial call for the data flow with a name as argument. The main method starts by creating a tracer instance, where you set the type of tracing required like sampling or constant, log the traces and etc. On our example it’s wrapped by an Init function on the lib and reused for other services as well.

In the main function, we create the tracer named “hello-client” and a span which is the definition of a unit of work within a service then we call two internal functions with the created context. This is important so the tracing IDs are persisted during the whole execution of the flow not only in the application but also between different applications or services as we will see.

In this function we initially create a span to trace it’s execution, then do the logic required in the function like encoding the data into the URL. Finally we call the NewTracedRequest function from our lib. This function is similar to the http.NewRequest from std library but it takes the span and embeds the trace headers into the call.

Let’s take a look into this function:

Here we can see that we take the request parameters, create a standard http.NewRequest and inject into this request the span information to be sent to the external service. If the called service uses tracing too it can add “child” spans that will be linked to this execution.

Going back to our formatString function, we send the HTTP request and then with the call results we add this info to the trace itself as log fields. This can be seen in the trace details:

Formatter service

This microservice is a simple HTTP server listening for a URL and formatting the output.

The service creates it’s own tracer named “formatter” so it’s correctly identified by Jaeger, then on the handler we extract the tracing information from the request, create a span to report this execution and do the function business logic. See that we also add logging information to this span so it can be seen on the trace:

Notice that each function (tracer) is identified by a color

We will see that by using the Echo framework and it’s tracing middleware, all the header extraction, injection is transparent. We have to do it here because this service uses pure Go Std library.

Publisher App

The publisher app is very similar in structure to the Formatter where we create a tracer and in the handler we extract the trace information from HTTP headers, create the function span and call the Echo-app microservice. See that in case this call fails, we add the info to the trace with an error tag so it get indicated on Jaeger (small exclamation mark on the span):

Echo app

The Echo app is a little more complete and provides easy to use functions so you can focus on the business logic.

First we define new Echo app, add the tracing middleware and start it:

Whenever we create an Echo app and attach the tracing middleware to it, the tracer “echo-tracer” is created and a root span also is created to trace the whole execution of the call inside the Echo application. We can then add “child” spans to the handlers:

Here in the testHandler function, we create a child span called “test handler” by using a convenience function from the tracing middleware lib by just passing the http context and the name. Then we add a log event, a tag and baggage to the span as optional items to show tracing capabilities. Baggages can be added to the trace and be persisted thru other services.

Then we call slowFunc (which just sleeps a random amount of time) by using another convenience function from our middleware in Echo (the tracing lib in the playground project also has this for Std lib) called TraceFunction. This is a wrapper that receives the http context, the called function and it’s arguments. Then it wraps the execution with a span adding some information to it.

The advantage of using the middleware or the lib is that the amount of tracing code added to your business logic is minimal taking little time and effort to add this instrumentation. The idea of this function is to work like a Python Decorator, avoiding changes on the calling or called code. Of course more info can be added to the traces as tags, logs or baggages as seen in the example.

Complete trace

Here Jaeger shows the complete timeline for our event and the functions that were called on multiple services shown in different colors.

Jaeger also generates the trace graph for all services, times and amount of calls.


As demonstrated, instrumenting an application with tracing brings fantastic visibility of your data flow and does not put a burden on the developers to add boilerplate to the code. The libs created for this example shows that can be simple to add proper tooling to your arsenal making everyone’s life easier.

If you have suggestions, want to contribute or support me, message me on Twitter or open an issue on the tracker repository.

Writing everything cloud and all the tech behind it. If you like my projects and would like to support me, check my Patreon on

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store