Exploring the New Frontiers of OpenTelemetry Android

DeveloperSteve
Lumigo
4 min readFeb 12, 2024

--

Exploring the New Frontiers of OpenTelemetry Android

Android mobile app development can be challenging and complex, with the need to have a fairly solid grasp of the Android SDK, Java and or Kotlin and the variety of tools, frameworks, and APIs, each with its own set of rules and guidelines. The ultimate goal is to create an app that performs well on various devices, screen sizes, and operating system versions while providing a smooth and responsive user experience.

However, the challenges do not end there, and depending on the application functionality, there's the need to integrate APIs and server-side interactions and introduce a new dimension of complexity, where the incorporation of data synchronization, cloud storage, and user authentication becomes key. These backend interactions are core to the app’s functionality and introduce additional hurdles related to network reliability, data security, and the efficient processing of server responses.

This is where OpenTelemetry steps in as an industry-embraced evolving tool suite that already encompasses many commonly utilized programming languages, platforms and deployment services to enhance observability within deployments that can span the myriad services to produce comprehensive distributed traces.

The easiest method to weave OpenTelemetry into your Android app is using a RUM (Real User Monitoring) initializer, an example one can be found on this gist. This resource simplifies the integration process, allowing more advanced monitoring capabilities to be incorporated easily. By utilizing this RUM initializer, you’re streamlining the integration of OpenTelemetry and unlocking valuable insights into your app’s end-to-end performance.

From there, you need to reference that from within your android code, which looks something like this:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Initialize RUM at the start of your MainActivity
RumInitializer.init(application)

val callApiButton: Button = findViewById(R.id.callApiButton)
val apiResponseText: TextView = findViewById(R.id.apiResponseText)

callApiButton.setOnClickListener {
CoroutineScope(Dispatchers.IO).launch {
val result = apiCall()
withContext(Dispatchers.Main) {
apiResponseText.text = result
}
}
}
}

This snippet sets the stage for tracing within your app by initializing the OpenTelemetry SDK and creating a new span. Spans are the building blocks of a trace, representing individual units of work within your application. By wrapping your code — such as an API call — within a span, you enable OpenTelemetry to monitor and record the execution of that segment of your application.

Invoking OpenTelemetry in Android using Lumigo

Further integration can be demonstrated by setting up OpenTelemetry to monitor network requests. Here’s an example of how you might trace an API call within your Android app:

// Execute an API call within a coroutine
CoroutineScope(Dispatchers.IO).launch {
val span = tracer.spanBuilder("APIRequestSpan").startSpan()
try (Scope scope = span.makeCurrent()) {
// Make your network request here
val response = makeNetworkRequest("https://api.example.com/data")
withContext(Dispatchers.Main) {
// Process the response on the main thread
processResponse(response)
}
} finally {
span.end() // Ensure the span is ended
}
}

// Function to make a network request
suspend fun makeNetworkRequest(url: String): String {
// Implementation of network request
}

In this example, a coroutine performs a network request asynchronously, with the request itself wrapped in a span. This approach allows OpenTelemetry to trace the execution of the network request, providing valuable insights into the request’s duration, success, and any errors that may occur.

Android Otel SDK trace

As the mobile development landscape evolves, integrating OpenTelemetry into Android applications represents a significant leap forward in pursuing enhanced observability and performance optimization. By building in tools like this from the start of the project, you can further trace the execution of applications from end to end and identify bottlenecks, inefficiencies, and errors with unprecedented precision.

For those eager to further explore the capabilities of OpenTelemetry within Android, the full exploration awaits in the blog post Full Stack Clarity Troubleshooting Android. There, we cover more through setup, integration, and utilization of OpenTelemetry in Android app development, complete with detailed code snippets and practical examples.

If you have found this post informative and are interested in further exploring the topic of mobile observability, consider contributing to the Android OpenTelemetry project or any other OpenTelemetry projects within the wider community. Your contributions will always be appreciated, and together, we can help create a more diverse approach to OpenTelemetry observability.

--

--

DeveloperSteve
Lumigo

Lilypad Network Chief Innovation Officer | Director The Coochin Company | 30+ years Developer | 10+ years Data Analyst | 10+ years Devrel