These tutorials target Micronaut Framework 3. Read, Guides for Micronaut Framework 4.

OpenTelemetry Tracing with Google Cloud Trace and the Micronaut Framework

Use Google Cloud Trace to investigate the behavior of your Micronaut applications.

Authors: John Shingler

Micronaut Version: 3.9.2

1. Getting Started

In this guide, we will create a Micronaut application written in Java.

In this guide, you will discover how simple it is to add tracing to a Micronaut application.

Tracing allows you to track service requests in a single application or a distributed one. Trace data shows the path, time spent in each section (called a span), and other information collected along the way. Tracing gives you observability into what is causing bottlenecks and failures.

2. What you will need

To complete this guide, you will need the following:

3. Costs

This guide uses paid services; you may need to enable Billing in Google Cloud to complete some steps in this guide.

4. Google Cloud Platform

Signup for the Google Cloud Platform

4.1. Cloud SDK

Install the Cloud SDK CLI for your operating system.

Cloud SDK includes the gcloud command-line tool. Run the init command in your terminal:

gcloud init

Log in to your Google Cloud Platform:

gcloud auth login

4.2. Google Cloud Platform Project

Create a new project with a unique name (replace xxxxxx with alphanumeric characters of your choice):

gcloud projects create micronaut-guides-xxxxxx
In GCP, project ids are globally unique, so the id you used above is the one you should use in the rest of this guide.

Change your project:

gcloud config set project micronaut-guides-xxxxxx

If you forget the project id, you can list all projects:

gcloud projects list

5. Solution

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

6. Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide \
    --features=tracing-opentelemetry-gcp,tracing-opentelemetry-http \
    --build=gradle
    --lang=java
If you don’t specify the --build argument, Gradle is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

If you use Micronaut Launch, select Micronaut Application as application type and add tracing-opentelemetry-gcp, and tracing-opentelemetry-http features.

If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features and apply those changes to your application.

6.1. OpenTelemetry

The Micronaut framework uses OpenTelemetry to generate and export tracing data.

OpenTelemetry provides two annotations: one to create a span and another to include additional information to the span.

@WithSpan

Used on methods to create a new span; defaults to the method name, but a unique name may be assigned instead.

@SpanAttribute

Used on method parameters to assign a value to a span; defaults to the parameter name, but a unique name may be assigned instead.

@WithSpan and @SpanAttribute can be used only on non-private methods.

If these annotations are not enough, or if you want to add tracing to a private method, the Micronaut framework’s tracing integration registers a io.opentelemetry.api.trace.Tracer bean, which exposes the OpenTelemetry API and can be dependency-injected as needed.

The following `io.micronaut.tracing.annotation`s are available if you prefer to use them or if you are working on an existing Micronaut application that already uses them.

  • @NewSpan : Identical to @WithSpan, it is used on methods to create a new span; defaults to the method name, but a unique name may be assigned instead.

  • @ContinueSpan : Used on methods to continue an existing span; primarily used in conjunction with @SpanTag.

  • @SpanTag : Similar to @SpanAttribute, it is used on method parameters to assign a value to a span; defaults to the parameter name, but a unique name may be assigned instead. To use the @SpanTag on a method argument, the method must be annotated with either @NewSpan or @ContinueSpan.

6.2. Inventory Service

src/main/java/example/micronaut/InventoryService.java
package example.micronaut;

import io.opentelemetry.instrumentation.annotations.SpanAttribute;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import jakarta.inject.Singleton;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Singleton
public class InventoryService {

    private final Tracer tracer;
    private final WarehouseClient warehouse;
    private final Map<String, Integer> inventory = new ConcurrentHashMap<>();

    private static final String storeName = "my_store";

    public InventoryService(Tracer tracer, WarehouseClient warehouse) { (1)
        this.tracer    = tracer;
        this.warehouse = warehouse;

        inventory.put("laptop",  4);
        inventory.put("desktop", 2);
        inventory.put("monitor", 11);
    }

    public Collection<String> getProductNames() {
        return inventory.keySet();
    }

    @WithSpan("stock-counts") (2)
    public Map<String, Integer> getStockCounts(@SpanAttribute("inventory.item") String item) { (3)
        HashMap<String, Integer> counts = new HashMap<>();
        if(inventory.containsKey(item)) {
            int count = inventory.get(item);
            counts.put("store", count);

            if(count < 10) {
                counts.put("warehouse", inWarehouse(storeName, item));
            }
        }

        return counts;
    }

    private int inWarehouse(String store, String item) {
        Span.current().setAttribute("inventory.store-name", store); (4)

        return warehouse.getItemCount(store, getUPC(item));
    }

    public void order(String item, int count) {
        orderFromWarehouse(item, count);
        if(inventory.containsKey(item)) {
            count += inventory.get(item);
        }
        inventory.put(item, count);
    }

    private void orderFromWarehouse(String item, int count) {
        Span span = tracer.spanBuilder("warehouse-order") (5)
                          .setAttribute("item", item)
                          .setAttribute("count", count)
                          .startSpan();

        Map<String, Object> json = new HashMap<>(4);
        json.put("store", storeName);
        json.put("product", item);
        json.put("amount", count);
        json.put("upc", getUPC(item));

        warehouse.order(json);

        span.end();
    }

    private int getUPC(String item) {
        return Math.abs(item.hashCode());
    }
}
1 Inject Tracing bean into class
2 Creates a new span called "stock-counts"
3 Adds a label, or tag, called "inventory.item" that will contain the value contained in item
4 Same as @SpanAttribute("inventory.store-name")
5 Same as @WithSpan("warehouse-order") with @SpanAttributes for item and count

6.3. Store Controller

This class demonstrates use of the io.micronaut.tracing.annotation instead of the OpenTelemetry annotations.

If you have the following dependency declared, all HTTP Server methods (those annotated with @Get, @Post, etc.) will create spans automatically.

build.gradle
implementation("io.micronaut.tracing:micronaut-tracing-opentelemetry-http")
src/main/java/example/micronaut/StoreController.java
package example.micronaut;

import io.micronaut.http.HttpStatus;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.annotation.Status;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.scheduling.annotation.ExecuteOn;
import io.micronaut.tracing.annotation.ContinueSpan;
import io.micronaut.tracing.annotation.NewSpan;
import io.micronaut.tracing.annotation.SpanTag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ExecuteOn(TaskExecutors.IO)
@Controller("/store")
public class StoreController {
    private final InventoryService inventory;

    public StoreController(InventoryService inventory) {
        this.inventory = inventory;
    }

    @Post("/order")
    @Status(HttpStatus.CREATED)
    @NewSpan("store.order") (1)
    public void order(@SpanTag("order.item") String item, @SpanTag int count) { (2)
        inventory.order(item, count);
    }

    @Get("/inventory") (3)
    public List<Map<String, Object>> getInventory() {
        List<Map<String, Object>> currentInventory = new ArrayList<>();
        for (String product: inventory.getProductNames()) {
            currentInventory.add(getInventory(product));
        }
        return currentInventory;
    }

    @Get("/inventory/{item}")
    @ContinueSpan (4)
    public Map<String, Object> getInventory(@SpanTag("item") String item) { (5)
        Map<String, Object> counts = new HashMap<>(inventory.getStockCounts(item));
        if(counts.isEmpty()) {
            counts.put("note", "Not available at store");
        }

        counts.put("item", item);

        return counts;
    }
}
1 Equivalent to @WithSpan("store.order")
2 Same as @SpanAttribute("order.item")`and `@SpanAttribute
3 Span created automatically if micronaut-tracing-opentelemetry-http is declared
4 Required for @SpanTag
5 Tag is only added to the span if micronaut-tracing-opentelemetry-http is declared; otherwise ignored

6.4. Warehouse Client

You can also mix OpenTelemetry and Micronaut Tracing annotations in the same class.

If you have the following dependency declared, all HTTP Client methods (those annotated with @Get, @Post, etc.) will create spans automatically.

build.gradle
implementation("io.micronaut.tracing:micronaut-tracing-opentelemetry-http")
src/main/java/example/micronaut/WarehouseClient.java
package example.micronaut;

import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.tracing.annotation.ContinueSpan;
import io.micronaut.tracing.annotation.SpanTag;
import io.opentelemetry.instrumentation.annotations.SpanAttribute;
import io.opentelemetry.instrumentation.annotations.WithSpan;

import java.util.Map;

@Client("/warehouse") (1)
public interface WarehouseClient {

    @Post("/order")
    @WithSpan
    void order(@SpanTag("warehouse.order") Map<String, ?> json);

    @Get("/count")
    @ContinueSpan
    int getItemCount(@QueryValue String store, @SpanAttribute @QueryValue int upc);

}
1 Some external service without tracing

6.5. Warehouse Controller

The WarehouseController class represents external service that will be called by WarehouseClient.

src/main/java/example/micronaut/WarehouseController.java
package example.micronaut;

import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.scheduling.annotation.ExecuteOn;

import java.util.Random;

@ExecuteOn(TaskExecutors.IO) (1)
@Controller("/warehouse") (2)
public class WarehouseController {

    @Get("/count") (3)
    public HttpResponse getItemCount() {
        return HttpResponse.ok(new Random().nextInt(11));
    }

    @Post("/order") (4)
    public HttpResponse order() {
        try {
            //To simulate an external process taking time
            Thread.sleep(500);
        } catch (InterruptedException e) {
        }

        return HttpResponse.accepted();
    }
}
1 It is critical that any blocking I/O operations (such as fetching the data from the database) are offloaded to a separate thread pool that does not block the Event loop.
2 The class is defined as a controller with the @Controller annotation mapped to the path /warehouse.
3 The @Get annotation maps the getItemCount method to an HTTP GET request on /warehouse/count.
4 The @Get annotation maps the order method to an HTTP GET request on /warehouse/order.

6.6. Tracer Configuration

If you used Micronaut CLI or Launch to create your application, the OpenTelemetry exporter will automatically be added to your application.yml configurations.

src/main/resources/application.yml
otel:
  traces:
    exporter: google_cloud_trace

7. Run the Application

The application can be deployed to Google Cloud Run, Cloud Function, Cloud Compute, or App Engine.

Traces can be sent to Google Cloud Trace outside the Google Cloud.

This allows us to run the application locally and see the traces in Google Cloud Trace.

To run the application, use the ./gradlew run command, which starts the application on port 8080.

You might get this error message when running your application:
"Message: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information."

If you are developing locally you can do:

gcloud auth application-default login

However, it is strongly recommended that you set up a service account. Follow the instructions in the link above and Micronaut GCP setup instructions for creating and configuring the service account.

8. Traces

trace list graph


If the data is not displayed, give it a minute. It takes a few seconds for the traces to show up in the console.

8.1. Get Item Counts

curl http://localhost:8080/store/inventory/laptop


inventory item


Each span is represented by a blue bar.

8.2. Order Item

curl -X "POST" "http://localhost:8080/store/order" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{"item":"laptop", "count":5}'


order item


Selecting a different span will show you the labels (a.k.a. attributes/tags) and other details of the span.

8.3. Get Inventory

curl http://localhost:8080/store/inventory


inventory all


Looking at the trace, we can conclude that retrieving the items sequentially might not be the best design choice.

9. Cleaning Up

After you’ve finished this guide, you can clean up the resources you created on Google Cloud Platform so you won’t be billed for them in the future. The following sections describe how to delete or turn off these resources.

9.1. Deleting the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

Deleting a project has the following consequences:

  • If you used an existing project, you’ll also delete any other work you’ve done in the project.

  • You can’t reuse the project ID of a deleted project. If you created a custom project ID that you plan to use in the future, you should delete the resources inside the project instead. This ensures that URLs that use the project ID, such as an appspot.com URL, remain available.

  • If you are exploring multiple tutorials and quickstarts, reusing projects instead of deleting them prevents you from exceeding project quota limits.

9.1.1. Via the CLI

To delete the project using the Cloud SDK, run the following command, replacing YOUR_PROJECT_ID with the project ID:

gcloud projects delete YOUR_PROJECT_ID

9.1.2. Via the Cloud Platform Console

In the Cloud Platform Console, go to the Projects page.

In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click Delete project

In the dialog, type the project ID, and then click Shut down to delete the project.

Deleting or turning off specific resources

You can individually delete or turn off some of the resources that you created during the tutorial.

10. Next Steps

Read more about Micronaut Tracing.

Read more about Micronaut GCP integration.