Quarkus - Using OpenTracing

This guide explains how your Quarkus application can utilize opentracing to provide distributed tracing forinteractive web applications.

Prerequisites

To complete this guide, you need:

  • less than 15 minutes

  • an IDE

  • JDK 1.8+ installed with JAVA_HOME configured appropriately

  • Apache Maven 3.5.3+

  • Docker

Architecture

In this guide, we create a straightforward REST application to demonstrate distributed tracing.

Solution

We recommend that you follow the instructions in the next sections and create the application step by step.However, you can skip right to the completed example.

Clone the Git repository: git clone https://github.com/quarkusio/quarkus-quickstarts.git, or download an archive.

The solution is located in the opentracing-quickstart directory.

Creating the Maven project

First, we need a new project. Create a new project with the following command:

  1. mvn io.quarkus:quarkus-maven-plugin:1.0.0.CR1:create \
  2. -DprojectGroupId=org.acme \
  3. -DprojectArtifactId=opentracing-quickstart \
  4. -DclassName="org.acme.opentracing.TracedResource" \
  5. -Dpath="/hello" \
  6. -Dextensions="opentracing"
  7. cd opentracing-quickstart

This command generates the Maven project with a REST endpoint and imports the smallrye-opentracing extension, whichincludes the OpenTracing support and the default Jaeger tracer.

Examine the JAX-RS resource

Open the src/main/java/org/acme/opentracing/TracedResource.java file and see the following content:

  1. package org.acme.opentracing;
  2. import javax.ws.rs.GET;
  3. import javax.ws.rs.Path;
  4. import javax.ws.rs.Produces;
  5. import javax.ws.rs.core.MediaType;
  6. @Path("/hello")
  7. public class TracedResource {
  8. @GET
  9. @Produces(MediaType.TEXT_PLAIN)
  10. public String hello() {
  11. return "hello";
  12. }
  13. }

Notice that there is no tracing specific code included in the application. By default, requests sent to thisendpoint will be traced without any code changes being required. It is also possible to enhance the tracing information. For more information on this, please see the MicroProfile OpenTracing specification.

Create the configuration

There are two ways to configure the Jaeger tracer within the application.

The first approach is by providing the properties within the src/main/resources/application.properties file:

  1. quarkus.jaeger.service-name=myservice (1)
  2. quarkus.jaeger.sampler-type=const (2)
  3. quarkus.jaeger.sampler-param=1 (3)
1If the quarkus.jaeger.service-name property (or JAEGER_SERVICE_NAME environment variable) is not provided then a "no-op" tracer will be configured, resulting in no tracing data being reported to the backend.
2Setup a sampler, that uses a constant sampling strategy.
3Sample all requests. Set sampler-param to somewhere between 0 and 1, e.g. 0.50, if you do not wish to sample all requests.

The second approach is to supply the properties as environment variables. These can be specified as jvm.args as shown in the following section.

Run the application

The first step is to start the tracing system to collect and display the captured traces:

  1. docker run -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 -p 5775:5775/udp -p 6831:6831/udp -p 6832:6832/udp -p 5778:5778 -p 16686:16686 -p 14268:14268 -p 9411:9411 jaegertracing/all-in-one:latest

Now we are ready to run our application. If using application.properties to configure the tracer:

  1. ./mvnw compile quarkus:dev

or if configuring the tracer via environment variables:

  1. ./mvnw compile quarkus:dev -Djvm.args="-DJAEGER_SERVICE_NAME=myservice -DJAEGER_SAMPLER_TYPE=const -DJAEGER_SAMPLER_PARAM=1"

Once both the application and tracing system are started, you can make a request to the provided endpoint:

  1. $ curl http://localhost:8080/hello
  2. hello

When the first request has been submitted, the Jaeger tracer within the app will be initialized:

  1. 2019-10-16 09:35:23,464 INFO [io.jae.Configuration] (executor-thread-1) Initialized tracer=JaegerTracer(version=Java-0.34.0, serviceName=myservice, reporter=RemoteReporter(sender=UdpSender(), closeEnqueueTimeout=1000), sampler=ConstSampler(decision=true, tags={sampler.type=const, sampler.param=true}), tags={hostname=localhost.localdomain, jaeger.version=Java-0.34.0, ip=127.0.0.1}, zipkinSharedRpcSpan=false, expandExceptionLogs=false, useTraceId128Bit=false)

Then visit the Jaeger UI to see the tracing information.

Hit CTRL+C to stop the application.

Additional instrumentation

The OpenTracing API Contributions project offers additional instrumentation that can be used to add tracing to a large variety of technologies/components.

The instrumentation documented in this section has been tested with Quarkus and works in both standard and native mode.

JDBC

The JDBC instrumentation will add a span for each JDBC queries done by your application, to enable it, add the following dependency to your pom.xml:

  1. <dependency>
  2. <groupId>io.opentracing.contrib</groupId>
  3. <artifactId>opentracing-jdbc</artifactId>
  4. </dependency>

As it uses a dedicated JDBC driver, you must configure your datasource and Hibernate to use it.

  1. # add ':tracing' to your database URL
  2. quarkus.datasource.url=jdbc:tracing:postgresql://localhost:5432/mydatabase
  3. # use the 'TracingDriver' instead of the one for your database
  4. quarkus.datasource.driver=io.opentracing.contrib.jdbc.TracingDriver
  5. # configure Hibernate dialect
  6. quarkus.hibernate-orm.dialect=org.hibernate.dialect.PostgreSQLDialect

Jaeger Configuration Reference

Configuration property fixed at build time - ️ Configuration property overridable at runtime

Configuration propertyTypeDefault
quarkus.jaeger.enabledDefines if the Jaeger extension is enabled.booleantrue
quarkus.jaeger.endpointThe traces endpoint, in case the client should connect directly to the Collector, like http://jaeger-collector:14268/api/tracesURI
quarkus.jaeger.auth-tokenAuthentication Token to send as "Bearer" to the endpointstring
quarkus.jaeger.userUsername to send as part of "Basic" authentication to the endpointstring
quarkus.jaeger.passwordPassword to send as part of "Basic" authentication to the endpointstring
quarkus.jaeger.agent-host-portThe hostname and port for communicating with agent via UDPhost:port
quarkus.jaeger.reporter-log-spansWhether the reporter should also log the spansboolean
quarkus.jaeger.reporter-max-queue-sizeThe reporter’s maximum queue sizeint
quarkus.jaeger.reporter-flush-intervalThe reporter’s flush intervalDuration
quarkus.jaeger.sampler-typeThe sampler type (const, probabilistic, ratelimiting or remote)string
quarkus.jaeger.sampler-paramThe sampler parameter (number)BigDecimal
quarkus.jaeger.sampler-manager-host-portThe host name and port when using the remote controlled samplerhost:port
quarkus.jaeger.service-nameThe service namestring
quarkus.jaeger.tagsA comma separated list of name = value tracer level tags, which get added to all reported spans. The value can also refer to an environment variable using the format ${envVarName:default}, where the :default is optional, and identifies a value to be used if the environment variable cannot be foundstring
quarkus.jaeger.propagationComma separated list of formats to use for propagating the trace context. Defaults to the standard Jaeger format. Valid values are jaeger and b3string
quarkus.jaeger.sender-factoryThe sender factory class namestring
About the Duration formatThe format for durations uses the standard java.time.Duration format.You can learn more about it in the Duration#parse() javadoc.You can also provide duration values starting with a number.In this case, if the value consists only of a number, the converter treats the value as seconds.Otherwise, PT is implicitly prepended to the value to obtain a standard java.time.Duration format.