Android App in Java Instrumentation

This document contains instructions on how to set up OpenTelemetry instrumentation in your Android Mobile applications built using Java and view your application traces in SigNoz.

Requirements

Java 8 or higher

Send traces directly to SigNoz cloud

Info

You can test sample application for Java

Step 1 : Instrument your application with OpenTelemetry

To configure your Java application to send traces to OpenTelemetry you need to add the following dependencies to the build.gradle file of a module or project

For that, paste the following inside build.gradle of your project.

implementation platform('io.opentelemetry:opentelemetry-bom:1.25.0')
implementation "io.opentelemetry:opentelemetry-api"
implementation "io.opentelemetry:opentelemetry-context"
implementation 'io.opentelemetry:opentelemetry-exporter-otlp'
implementation 'io.opentelemetry:opentelemetry-exporter-logging'
implementation 'io.opentelemetry:opentelemetry-extension-kotlin'
implementation 'io.opentelemetry:opentelemetry-sdk'
implementation 'io.opentelemetry:opentelemetry-semconv'

Step 2: Configure network settings

Create a file named network_security_config.xml in the app/res/xml directory and add the following content to the file

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">ingest.<region>.signoz.cloud</domain>
    </domain-config>
</network-security-config>

Add the following content to the app/src/main/AndroidManifest.xml file to allow the application to access the network

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
  <!-- Add the following line to grant network access permissions. -->
  <uses-permission android:name="android.permission.INTERNET" />

  <application
    ...
    <!-- Add the following line to configure the network for the domain name to which you want to report data. -->
    android:networkSecurityConfig="@xml/network_security_config"
    ...>

    ...
  </application>

</manifest>

Create a file named OpentelemetryUtl.java with the following content

package com.example.androidjavademo;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;

public class OpenTelemetryUtil {
    public static void init() {
        Resource otelResource = Resource.getDefault().merge(
            Resource.create(
                Attributes.of(
                    ResourceAttributes.SERVICE_NAME, "<service_name>",
                    ResourceAttributes.HOST_NAME, "<service_name>"
                )
            )
        );

        SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
                .addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create()))
                .addSpanProcessor(BatchSpanProcessor.builder(
                    OtlpGrpcSpanExporter.builder()
                        .setEndpoint("ingest.<region>.signoz.cloud:443/v1/traces")
                        .addHeader("signoz-ingestion-key", "<your-ingestion-key>")
                        .build()).build()
                )
                .setResource(otelResource)
                .build();

        OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
                .setTracerProvider(sdkTracerProvider)
                .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
                .buildAndRegisterGlobal();

        tracer = openTelemetry.getTracer("android-tracer", "1.0.0");
    }

    private static Tracer tracer;

    public static Tracer getTracer() {
        return tracer;
    }
}
  • Set the <region> to match your SigNoz Cloud region
  • Replace <your-ingestion-key> with your SigNoz ingestion key.
  • <service_name> is name of your service

Inside MainActivity.java file, initialize the util

public class MainActivity extends AppCompatActivity {
	...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Add the following line to initialize OpenTelemetry. 
        OpenTelemetryUtil.init();
        ...
    }
    ...
}

Step 3: Send Telemetry data to SigNoz

Import these at top

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;

Use this to do the Manual Instrumentation

public void parentSpan() {
        Tracer tracer = OpenTelemetryUtil.getTracer();
        Span span = tracer.spanBuilder("Parent Span").startSpan();
        try (Scope scope = span.makeCurrent()) {
            System.out.println(span.getSpanContext().getTraceId());
            System.out.println(span.getSpanContext().getSpanId());
            childSpan();
        } finally {
            span.end();
        }
    }

    public void childSpan() {
        Tracer tracer = OpenTelemetryUtil.getTracer();
        Span span = tracer.spanBuilder("Child Span").startSpan();
        try (Scope scope = span.makeCurrent()) {
            System.out.println(span.getSpanContext().getTraceId());
            System.out.println(span.getSpanContext().getSpanId());
        } finally {
            span.end();
        }
    }

Call the above functions to add spans.

Step 4: Run app

Run your application from Android Studio to see the output & you can verify the sent span in SigNoz .

Was this page helpful?