Android Trace Collection
Datadog's Research Report: The State of Serverless Report: The State of Serverless

Android Trace Collection

The Android trace collection is in public alpha, and is currently not supported by Datadog.

Send traces to Datadog from your Android applications with Datadog’s dd-sdk-android client-side tracing library and leverage the following features:

  • Create custom spans for operations in your application.
  • Add context and extra custom attributes to each span sent.
  • Optimized network usage with automatic bulk posts.

Note: Traces are still experimental, and will be available in the dd-sdk-android library version 1.4.0 or higher. The dd-sdk-android library supports all Android versions from API level 19 (Kit-Kat).

Setup

  1. Add the Gradle dependency by declaring the library as a dependency in your build.gradle file:

    repositories {
        maven { url "https://dl.bintray.com/datadog/datadog-maven" }
    }
    
    dependencies {
        implementation "com.datadoghq:dd-sdk-android:x.x.x" {
            exclude group: "com.google.guava", module: "listenablefuture"
        }
    }
  2. Initialize the library with your application context and your Datadog client token. For security reasons, you must use a client token: you cannot use Datadog API keys to configure the dd-sdk-android library as they would be exposed client-side in the Android application APK byte code. For more information about setting up a client token, see the client token documentation:

    class SampleApplication : Application() {
        override fun onCreate() {
            super.onCreate()
    
            val config = DatadogConfig.Builder(BuildConfig.DD_CLIENT_TOKEN)
                            .build()
            Datadog.initialize(this, config)
        }
    }
    class SampleApplication : Application() {
        override fun onCreate() {
            super.onCreate()
    
            val config = DatadogConfig.Builder(BuildConfig.DD_CLIENT_TOKEN)
                            .useEUEndpoints()
                            .build()
            Datadog.initialize(this, config)
        }
    }
  3. Configure and register the Android Tracer. You only need to do it once, usually in your application’s onCreate() method:

    val tracer = Tracer.Builder()
        .setServiceName("<SERVICE_NAME>")
        .build()
    GlobalTracer.registerIfAbsent(tracer)
  4. Start a custom span using the following method:

    val tracer = GlobalTracer.get()
    val span = tracer.buildSpan("<SPAN_NAME>").start()
    // Do something ...
    // ...
    // Then when the span should be closed
    span.finish()
  5. (Optional) - Provide additional tags alongside your span.

    span.setTag("http.url", url)

Integrations

In addition to manual tracing, the dd-sdk-android library provides the following integration.

OkHttp

If you want to trace your OkHttp requests, you can add the provided Interceptor as follow:

OkHttpClient client = new OkHttpClient.Builder()
    .addInterceptor(new TracingInterceptor())
    .build();

This creates a span around each request processed by the OkHttpClient, with all the relevant information automatically filled (url, method, status code, error), and propagates the tracing information to your backend to get a unified trace within Datadog

Note: If you use multiple Interceptors, this one must be called first.

Further Reading