Image for post
Image for post

Generating Jaeger gRPC services and using Jaeger in JUnit with Testcontainers

Annanay Agarwal
Apr 8, 2020 · 4 min read

In this article you will learn how to generate Jaeger model classes and gRPC services from protobuf definitions. The generated code can be used to write integrations or it can be used in tests to query tracing data from Jaeger-query service. This is the use case we are going to look at in more detail.

Generating Java classes from proto definitions

Anyone working with protobuf files agrees that it is kind of a nightmare to get it right, with having to install the right dependency versions of all packages. To ease this process of generating gRPC services from proto definitions, we have created a lightweight docker image (47MB), with the protoc compiler and package dependencies baked in. The Dockerfile is hosted at jaegertracing/docker-protobuf, the image at JaegerTracing DockerHub, and is synced with the dependencies from the Jaeger core repo. It can be used to generate services in multiple languages including GoLang, Java, Python, PHP, etc.

To generate model and gRPC service classes in Java use the following commands:

The flag java_out is used to specify that we want the generated code for Java. For a complete list of options, run:

Sometimes, the generated code in the specific language might have dependencies on packages like GoGo or Swagger, as these are often used to make the internal representations more efficient. We expect that proto definition files for all dependencies are present in the image. The same image can be used to generate code from the proto definition file of the dependency. To find all proto files present in the image, run:

And once the dependency file is located, language specific code can be generated using the same command above.

Please note that detailed guidelines for code generation are available in the README.md in the Dockerfile repo.

Maven artifacts

Generated Java classes are also available as Maven artifacts, so they can be imported into the project directly as follows:

Using Jaeger in tests

There might be many use cases for using Jaeger in tests. The most common one is to verify whether a service is reporting tracing data or even a better use case is to use tracing in end-to-end tests to validate the system behavior by looking at reported trace data. Btw. you might want to check out Trace Driven Development: Unifying Testing and Observability talk by Ted Young where he is talking about using tracing/telemetry data in tests.

In this example we will have a look at using Jaeger in Java. The all-in-one distribution will be directly deployed in the test by Testcontainers library. This library makes it really simple to use docker services in JUnit tests.

Let’s have a look at how Jaeger all-in-one deployment can be implemented as GenericContainer:

There are a couple of interesting things to point out:

  • method createTracer returns a configured tracer instance that reports data to the deployed all-in-one instance.
  • method createBlockingQueryService returns gRPC query service.
  • the deployed all-in-one exposes Jaeger collector and query ports. Note that these ports are mapped to a random host port. The mapped port can be obtained via JaegerAllInOne.getMappedPort method.

Now let’s have a look at how to use this class in the test:

In this case a Jaeger all-in-one instance is created per each test run. This is because Jaeger does not provide an API to remove old data which might be necessary to do between test runs. The alternative approach without requiring to restart the server might be to add a unique tag to each span in a test and include it in the query.

Jaeger Java artifact with Testcontainers

The JaegerAllInOne class from the previous section is also available as a Maven artifact. The source code is hosted in the jaegertracing/jaeger-analytics-java repository.

References

This article has been written by Annanay Agarwal and Pavol Loffay.

JaegerTracing

Open source distributed tracing platform at Cloud Native…

Thanks to Pavol Loffay and Gary Brown

Annanay Agarwal

Written by

"All progress happens outside of the comfort zone". Software Developer, Grafana Labs.

JaegerTracing

Open source distributed tracing platform at Cloud Native Computing Foundation (graduated). https://jaegertracing.io

Annanay Agarwal

Written by

"All progress happens outside of the comfort zone". Software Developer, Grafana Labs.

JaegerTracing

Open source distributed tracing platform at Cloud Native Computing Foundation (graduated). https://jaegertracing.io

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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