lightstep-tracer-android
November 3, 2021 ยท View on GitHub
The LightStep distributed tracing library for Android.
Getting started: Android
The Android library is hosted on Bintray, jcenter, and Maven Central. The Bintray lightstep-tracer-android project contains additional installation and setup information for using the library with various build systems such as Ivy and Maven.
Gradle
In most cases, modifying your build.gradle with the below is all that is required:
repositories {
jcenter() // OR mavenCentral()
}
dependencies {
compile 'com.lightstep.tracer:lightstep-tracer-android:VERSION'
}
- If using the
grpctransport, use the 0.3x.x version family. - If using the
okhttptransport, use the 0.2x.x version family. This is the recommended deployment.
See more in the Maven section and Gradle sections.
Update your AndroidManifest.xml
Ensure the app's AndroidManifest.xml has the following (under the <manifest> tag):
<!-- Permissions required to make http calls -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Initializing the LightStep Tracer
// Import the OpenTracing interfaces
import io.opentracing.Span;
import io.opentracing.Tracer;
// ...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize LightStep tracer implementation in the main activity
// (or anywhere with a valid android.content.Context).
this.tracer = new com.lightstep.tracer.android.Tracer(
this,
new com.lightstep.tracer.shared.Options.OptionsBuilder()
.withAccessToken("{your_access_token}")
.build()
);
// Start and finish a Span
Span span = this.tracer.buildSpan("my_span").start();
this.doSomeWorkHere();
span.finish();
API Documentation
Tracing instrumentation should use the OpenTracing APIs to stay portable and in sync with the standard:
For reference, the generated LightStep documentation is also available:
Options
Setting a custom component name
To set the name used in the LightStep UI for this instance of the Tracer, call withComponentName() on the OptionsBuilder object:
options = new com.lightstep.tracer.shared.Options.OptionsBuilder()
.withAccessToken("{your_access_token}")
.withComponentName("your_custom_name")
.build();
Disabling the reporting loop
By default, the Java library does a report of any buffered data on a fairly regular interval. To disable this behavior and rely only on explicit calls to flush() to report data, initialize with:
options = new com.lightstep.tracer.shared.Options.OptionsBuilder()
.withAccessToken("{your_access_token}")
.withDisableReportingLoop(true)
.build();
To then manually flush by using the LightStep tracer object directly:
// Flush any buffered tracing data
((com.lightstep.tracer.android.Tracer)tracer).flush();
Flushing the report at exit
In order to send a final flush of the data prior to exit, clients should manually flush by using the LightStep tracer object as described above.
Disabling default clock correction
By default, the Java library performs clock correction based on timestamp information provided in the spans. To disable this behavior, initialize with:
options = new com.lightstep.tracer.shared.Options.OptionsBuilder()
.withAccessToken("{your_access_token}")
.withClockSkewCorrection(false)
.build();
Advanced Option: Transport and Serialization Protocols
By following the above configuration, the tracer will send information to LightStep using HTTP and Protocol Buffers which is the recommended configuration. If there are no specific transport protocol needs you have, there is no need to change this default.
There are two options for transport protocols:
- Protocol Buffers over HTTP using OkHttp 3 - The recommended and default solution. Supported in the 0.25.x version family, as OkHttp 3 runs under the supported Android versions.
- Protocol Buffers over GRPC - This is a more advanced solution that might be desirable if you already have gRPC networking configured. Use the 0.30.x version family.
You can configure the tracer to support gRPC by replacing com.lightstep.tracer:tracer-okhttp with com.lightstep.tracer:tracer-grpc when including the tracer dependency and including a grpc dependency. i.e.
Maven
<dependency>
<groupId>com.lightstep.tracer</groupId>
<artifactId>lightstep-tracer-android</artifactId>
<version> VERSION </version>
</dependency>
<!-- Additional dependency if using the grpc/0.3x.x family -->
<dependency>
<groupId>com.lightstep.tracer</groupId>
<artifactId>tracer-grpc</artifactId>
<version> VERSION </version>
</dependency>
Gradle
repositories {
mavenCentral() // OR jcenter()
}
dependencies {
compile 'com.lightstep.tracer:lightstep-tracer-android:VERSION'
// Additional dependency if using the grpc/0.3x.x family
compile 'com.lightstep.tracer:tracer-grpc:VERSION'
}
Development info
See DEV.md for information on contributing to this instrumentation library.