Blog My Happy PlaceGemma Lara Savill

Getting started with OpenTelemetry on Android

By Gemma Lara Savill
Published at May 15, 2025

Getting

Once your Android application is live, knowing what's happening under the hood isn't just a good idea, it's essential. I've worked with different observability solutions, from Google Analytics and Adobe Analytics for market insights to Sentry, Crashlytics, and Datadog for more technical monitoring. While each has its strengths, I've recently discovered Opentelemetry, and it feels like a game-changer. It's an open-source, versatile platform that's surprisingly simple to adopt yet profoundly powerful in the insights it delivers.

What is Opentelemetry?

Opentelemetry is an open source observability platform. It provides a standardized set of APIs, SDKs, and tools to help you instrument, generate, collect, and export telemetry data (logs, metrics, and traces) from your applications.

Essentially, it's a vendor-neutral framework designed to make it easier for developers to get deep insights into the behavior and performance of their software, regardless of where it's deployed or what programming language it uses.

As it is open source it is also cheaper to run, and evolving fast.

What can you use Opentelemetry for?

Opentelemetry empowers you to gain comprehensive visibility into your application's inner workings. You can use it to monitor the health and performance of your services, troubleshoot issues quickly, understand user behavior, and optimize resource utilization.

The real power lies in its flexibility: you can send logs, metrics, and traces of your choice, and in combination, to build a truly compelling narrative of almost anything that is happening inside your app. For example, you could trace a user's journey through your application, capturing metrics on load times and recording logs for any errors encountered. Or, you could track the performance of a specific microservice, logging requests and responses while monitoring its resource consumption. This ability to mix and match telemetry data allows for incredibly creative and powerful custom monitoring solutions tailored precisely to your application's unique needs and your specific questions about its operation.

How to add Opentelemetry to your Android app

When it comes to integrating Opentelemetry into your Android application, you primarily have two paths:

  1. Utilize the dedicated Opentelemetry Android SDK.
  2. Directly employ the Java implementation of the OpenTelemetry API.

My strong recommendation is to go with Option 1: the Opentelemetry Android SDK. While it leverages the underlying Java implementation of the OpenTelemetry API, it provides crucial Android-specific features and conveniences, making your observability efforts much more effective. These include:

  • Built-in Crash reporting
  • Automatic ANR (Application Not Responding) detection
  • Monitoring of network changes
  • Automatic instrumentation of Android Activity lifecycles
  • Effortless monitoring of Android Fragment lifecycles
  • View click instrumentation for user interaction insights
  • Direct access to the core OpenTelemetry APIs for custom, manual instrumentation
  • Helpers for redacting sensitive information from spans or modifying attributes before export
  • Detection of slow or frozen renders to identify UI performance issues
  • Offline buffering of telemetry data via local storage, ensuring data isn't lost

The Android SDK is clearly the superior choice for most Android applications, providing a robust and feature-rich foundation. However, it's valuable to know that the direct Java API offers an even deeper level of customization for highly specific scenarios.

Setup and send data to a local collector with Docker

To quickly get started with a local OpenTelemetry environment, you can leverage Docker. The OpenTelemetry Android SDK's GitHub project includes a convenient compose.yaml file within its demo-app folder. This Docker Compose file sets up the necessary components for local testing.

Simply navigate to the demo-app directory in your terminal and run the following command:

docker compose build
docker compose up

Running this command will provision a local OpenTelemetry Collector, typically accessible at localhost on port 4318. Additionally, it will launch the Jaeger UI (a powerful tracing visualization tool) on port 16686 (e.g., http://localhost:16686), where you can explore the traces and spans sent from your emulator or device.

Configuring your Android App for local collector communication

If you're sending data from an Android emulator to your local collector, remember that localhost on your machine translates to 10.0.2.2 within the emulator's network. Therefore, you should set your OpenTelemetry exporter's URL to http://10.0.2.2:4318.

Furthermore, for local development and testing, Android's default network security configurations prevent cleartext HTTP traffic. To allow your app to communicate with your local collector, you'll need to set up a Network Security Configuration exception. Create a new XML file (e.g., network_security_config.xml) in your app's res/xml folder with the following content:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">10.0.2.2</domain>
    </domain-config>
</network-security-config>

Then, reference this configuration in your AndroidManifest.xml file by adding the android:networkSecurityConfig attribute to your tag:

<application 
    ...
    android:networkSecurityConfig="@xml/network_security_config"
>
...
</application>

Monitoring Collector Logs

Beyond the Jaeger UI, you can also inspect all the telemetry data being sent to your collector directly in its Docker container logs. If you're using Docker Desktop, simply click on the collector's container name to go straight to its logs, providing a real-time view of incoming data.

Sending data to a live collector

Once you're ready to send your telemetry data beyond a local Docker setup to a live, production-grade collector over the internet, security becomes paramount. You'll definitely want to implement an authentication method to secure the data exporters that transmit information to your collector. Common approaches include using an API key or an OAuth token within the request headers.

Both methods are relatively straightforward to implement. However, if you opt for an OAuth token, remember that these typically expire and will require a refresh mechanism. You'll need to handle token refreshes and ensure your exporters are updated with the new token to maintain continuous data flow without interruption.

Important Security Note for Production

Remember, the network-security-config rule allowing cleartext traffic to 10.0.2.2 is solely for local development and testing with an emulator. It is critical to remove this configuration or ensure it's only active for debug builds before releasing your app to production. Allowing cleartext traffic in a production application is a significant security vulnerability.

ProGuard / R8 Considerations

Finally, when preparing your app for a release build, especially if you're using ProGuard or R8 for code shrinking and obfuscation, it's crucial to ensure that the OpenTelemetry SDK's components are not inadvertently stripped away. Running a debug build with ProGuard/R8 enabled (often by setting minifyEnabled true in your build.gradle for debug variants) will typically provide recommendations in your build output about which ProGuard rules to add. The SDK also provides a handy initializeOtelWithGrpc() that can be used to get R8 missing rules warnings. Always review and include these rules to prevent unexpected runtime issues due to missing classes or methods that OpenTelemetry relies on.

Final Thoughts

Implementing OpenTelemetry in your Android application offers a valuable way to understand its internal workings. By adopting this open-source standard, you gain essential observability capabilities that can help you monitor performance and user interactions more effectively. The flexibility to combine logs, metrics, and traces means you can gather diverse data to troubleshoot issues, optimize features, and gain insights into your app's behavior.

Ultimately, OpenTelemetry provides the tools needed to build more stable and efficient Android applications. It's a practical approach to gaining the visibility required to improve your app continuously.

Useful Resources: