|
| 1 | +# LaunchDarkly Observability SDK for Android |
| 2 | + |
| 3 | +## Early Access Preview️ |
| 4 | + |
| 5 | +**NB: APIs are subject to change until a 1.x version is released.** |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +### Automatic Instrumentation |
| 10 | + |
| 11 | +The Android observability plugin automatically instruments: |
| 12 | +- **Activity Lifecycle**: App lifecycle events and transitions |
| 13 | +- **HTTP Requests**: OkHttp and HttpURLConnection requests (requires setup of ByteBuddy compile time plugin and additional dependencies) |
| 14 | +- **Crash Reporting**: Automatic crash reporting and stack traces |
| 15 | +- **Feature Flag Evaluations**: Evaluation events added to your spans. |
| 16 | +- **Session Management**: User session tracking and background timeout handling |
| 17 | + |
| 18 | +## Example Application |
| 19 | + |
| 20 | +A complete example application is available in the [e2e/android](../e2e/android) directory. |
| 21 | + |
| 22 | +## Install |
| 23 | + |
| 24 | +Add the dependency to your app's Gradle file: |
| 25 | + |
| 26 | +```kotlin |
| 27 | +dependencies { |
| 28 | + implementation("com.launchdarkly:launchdarkly-android-client-sdk:5.+") |
| 29 | + implementation("com.launchdarkly:launchdarkly-observability-android:0.5.0") |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +### Basic Setup |
| 36 | + |
| 37 | +Add the observability plugin to your LaunchDarkly Android Client SDK configuration: |
| 38 | + |
| 39 | +```kotlin |
| 40 | +import com.launchdarkly.observability.plugin.Observability |
| 41 | +import com.launchdarkly.sdk.android.LDConfig |
| 42 | +import com.launchdarkly.sdk.android.Components |
| 43 | +import com.launchdarkly.sdk.android.LDClient |
| 44 | + |
| 45 | +class MyApplication : Application() { |
| 46 | + override fun onCreate() { |
| 47 | + super.onCreate() |
| 48 | + |
| 49 | + val ldConfig = LDConfig.Builder(LDConfig.Builder.AutoEnvAttributes.Enabled) |
| 50 | + .mobileKey("your-mobile-key") |
| 51 | + .plugins( |
| 52 | + Components.plugins().setPlugins( |
| 53 | + listOf( |
| 54 | + Observability(this@MyApplication) |
| 55 | + ) |
| 56 | + ) |
| 57 | + ) |
| 58 | + .build() |
| 59 | + |
| 60 | + val context = LDContext.builder(ContextKind.DEFAULT, "user-key") |
| 61 | + .build() |
| 62 | + |
| 63 | + LDClient.init(this@MyApplication, ldConfig, context) |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Configure additional instrumentations |
| 69 | + |
| 70 | +To enable HTTP request instrumentation and user interaction instrumentation, add the following plugin and dependencies to your top level application's Gradle file. |
| 71 | + |
| 72 | +<CodeBlocks> |
| 73 | +<CodeBlock title='Gradle Groovy'> |
| 74 | + |
| 75 | +```java |
| 76 | +plugins { |
| 77 | + id 'net.bytebuddy.byte-buddy-gradle-plugin' version '1.+' |
| 78 | +} |
| 79 | + |
| 80 | +dependencies { |
| 81 | + // Android HTTP Url instrumentation |
| 82 | + implementation 'io.opentelemetry.android.instrumentation:httpurlconnection-library:0.11.0-alpha' |
| 83 | + byteBuddy 'io.opentelemetry.android.instrumentation:httpurlconnection-agent:0.11.0-alpha' |
| 84 | + |
| 85 | + // OkHTTP instrumentation |
| 86 | + implementation 'io.opentelemetry.android.instrumentation:okhttp3-library:0.11.0-alpha' |
| 87 | + byteBuddy 'io.opentelemetry.android.instrumentation:okhttp3-agent:0.11.0-alpha' |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Advanced Configuration |
| 92 | + |
| 93 | +You can customize the observability plugin with various options: |
| 94 | + |
| 95 | +```kotlin |
| 96 | +import com.launchdarkly.observability.api.Options |
| 97 | +import com.launchdarkly.sdk.android.LDAndroidLogging |
| 98 | +import io.opentelemetry.api.common.AttributeKey |
| 99 | +import io.opentelemetry.api.common.Attributes |
| 100 | + |
| 101 | +val observabilityPlugin = Observability( |
| 102 | + application = this@MyApplication, |
| 103 | + options = Options( |
| 104 | + serviceName = "my-android-app", |
| 105 | + serviceVersion = "1.0.0", |
| 106 | + debug = true, |
| 107 | + logAdapter = LDAndroidLogging.adapter(), |
| 108 | + resourceAttributes = Attributes.of( |
| 109 | + AttributeKey.stringKey("environment"), "production", |
| 110 | + AttributeKey.stringKey("team"), "mobile" |
| 111 | + ), |
| 112 | + customHeaders = mapOf( |
| 113 | + "X-Custom-Header" to "custom-value" |
| 114 | + ) |
| 115 | + ) |
| 116 | +) |
| 117 | +``` |
| 118 | + |
| 119 | +### Recording Observability Data |
| 120 | + |
| 121 | +After initialization of the LaunchDarkly Android Client SDK, use `LDObserve` to record metrics, logs, errors, and traces: |
| 122 | + |
| 123 | +```kotlin |
| 124 | +import com.launchdarkly.observability.sdk.LDObserve |
| 125 | +import com.launchdarkly.observability.interfaces.Metric |
| 126 | +import io.opentelemetry.api.common.AttributeKey |
| 127 | +import io.opentelemetry.api.common.Attributes |
| 128 | +import io.opentelemetry.api.logs.Severity |
| 129 | + |
| 130 | +// Record metrics |
| 131 | +LDObserve.recordMetric(Metric("user_actions", 1.0)) |
| 132 | +LDObserve.recordCount(Metric("api_calls", 1.0)) |
| 133 | +LDObserve.recordIncr(Metric("page_views", 1.0)) |
| 134 | +LDObserve.recordHistogram(Metric("response_time", 150.0)) |
| 135 | +LDObserve.recordUpDownCounter(Metric("active_connections", 1.0)) |
| 136 | + |
| 137 | +// Record logs |
| 138 | +LDObserve.recordLog( |
| 139 | + "User performed action", |
| 140 | + Severity.INFO, |
| 141 | + Attributes.of( |
| 142 | + AttributeKey.stringKey("user_id"), "12345", |
| 143 | + AttributeKey.stringKey("action"), "button_click" |
| 144 | + ) |
| 145 | +) |
| 146 | + |
| 147 | +// Record errors |
| 148 | +LDObserve.recordError( |
| 149 | + Exception("Something went wrong"), |
| 150 | + Attributes.of( |
| 151 | + AttributeKey.stringKey("component"), "payment", |
| 152 | + AttributeKey.stringKey("error_code"), "PAYMENT_FAILED" |
| 153 | + ) |
| 154 | +) |
| 155 | + |
| 156 | +// Create spans for tracing |
| 157 | +val span = LDObserve.startSpan( |
| 158 | + "api_request", |
| 159 | + Attributes.of( |
| 160 | + AttributeKey.stringKey("endpoint"), "/api/users", |
| 161 | + AttributeKey.stringKey("method"), "GET" |
| 162 | + ) |
| 163 | +) |
| 164 | +span.makeCurrent().use { |
| 165 | + // Your code here |
| 166 | +} |
| 167 | +span.end() |
| 168 | +``` |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +## Contributing |
| 173 | + |
| 174 | +We encourage pull requests and other contributions from the community. Check out our [contributing guidelines](../../CONTRIBUTING.md) for instructions on how to contribute to this SDK. |
| 175 | + |
| 176 | +## About LaunchDarkly |
| 177 | + |
| 178 | +* LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can: |
| 179 | + * Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases. |
| 180 | + * Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?). |
| 181 | + * Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file. |
| 182 | + * Grant access to certain features based on user attributes, like payment plan (eg: users on the ‘gold’ plan get access to more features than users in the ‘silver’ plan). Disable parts of your application to facilitate maintenance, without taking everything offline. |
| 183 | +* LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Read [our documentation](https://docs.launchdarkly.com/sdk) for a complete list. |
| 184 | +* Explore LaunchDarkly |
| 185 | + * [launchdarkly.com](https://www.launchdarkly.com/ "LaunchDarkly Main Website") for more information |
| 186 | + * [docs.launchdarkly.com](https://docs.launchdarkly.com/ "LaunchDarkly Documentation") for our documentation and SDK reference guides |
| 187 | + * [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ "LaunchDarkly API Documentation") for our API documentation |
| 188 | + * [launchdarkly.com/blog](https://launchdarkly.com/blog/ "LaunchDarkly Blog Documentation") for the latest product updates |
0 commit comments