Skip to content

Commit 24580ec

Browse files
authored
chore: adds skeleton Android observability plugin with barebones metric, log, error support (#138)
This code is untested and is just to get a skeleton in place to further work off. [Guts of logic are here](https://github.com/launchdarkly/observability-sdk/pull/138/files#diff-8acec4ff8212d652bfa437476cd1151fec3673eb4f34ba3d30a5fc6bf566f10d). Most of the code in the e2e directory is boilerplate new app code except for [BaseApplication](https://github.com/launchdarkly/observability-sdk/compare/ta/O11Y-339/basic-android-obs-client-squash?expand=1#diff-a87a1951f681c62d61f9b6f2c36e420aa88647bc927b4715c57624ba32435df2) and the [view model](https://github.com/launchdarkly/observability-sdk/pull/138/files#diff-f0e07a6791ac0bcdc8f6c842920965f7e3b88d991ef678bceb74230edb979d4c)
1 parent 2954803 commit 24580ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1872
-0
lines changed

e2e/android/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

e2e/android/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

e2e/android/app/build.gradle.kts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
plugins {
2+
alias(libs.plugins.android.application)
3+
alias(libs.plugins.kotlin.android)
4+
alias(libs.plugins.kotlin.compose)
5+
}
6+
7+
android {
8+
namespace = "com.example.androidobservability"
9+
compileSdk = 35
10+
11+
defaultConfig {
12+
applicationId = "com.example.androidobservability"
13+
minSdk = 24
14+
targetSdk = 35
15+
versionCode = 1
16+
versionName = "1.0"
17+
18+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
19+
}
20+
21+
buildTypes {
22+
release {
23+
isMinifyEnabled = false
24+
proguardFiles(
25+
getDefaultProguardFile("proguard-android-optimize.txt"),
26+
"proguard-rules.pro"
27+
)
28+
}
29+
}
30+
compileOptions {
31+
sourceCompatibility = JavaVersion.VERSION_11
32+
targetCompatibility = JavaVersion.VERSION_11
33+
}
34+
kotlinOptions {
35+
jvmTarget = "11"
36+
}
37+
buildFeatures {
38+
compose = true
39+
}
40+
}
41+
42+
dependencies {
43+
implementation("com.launchdarkly:launchdarkly-android-client-sdk:5.9.0")
44+
45+
implementation("io.opentelemetry:opentelemetry-api:1.51.0")
46+
implementation("io.opentelemetry:opentelemetry-sdk:1.51.0")
47+
implementation("io.opentelemetry:opentelemetry-exporter-otlp:1.51.0")
48+
implementation("io.opentelemetry:opentelemetry-sdk-metrics:1.51.0")
49+
50+
implementation(project(":observability-android"))
51+
implementation(libs.androidx.core.ktx)
52+
implementation(libs.androidx.lifecycle.runtime.ktx)
53+
implementation(libs.androidx.activity.compose)
54+
implementation(platform(libs.androidx.compose.bom))
55+
implementation(libs.androidx.ui)
56+
implementation(libs.androidx.ui.graphics)
57+
implementation(libs.androidx.ui.tooling.preview)
58+
implementation(libs.androidx.material3)
59+
testImplementation(libs.junit)
60+
androidTestImplementation(libs.androidx.junit)
61+
androidTestImplementation(libs.androidx.espresso.core)
62+
androidTestImplementation(platform(libs.androidx.compose.bom))
63+
androidTestImplementation(libs.androidx.ui.test.junit4)
64+
debugImplementation(libs.androidx.ui.tooling)
65+
debugImplementation(libs.androidx.ui.test.manifest)
66+
}

e2e/android/app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.androidobservability
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.example.androidobservability", appContext.packageName)
23+
}
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:name=".BaseApplication"
7+
android:usesCleartextTraffic="true"
8+
android:allowBackup="true"
9+
android:dataExtractionRules="@xml/data_extraction_rules"
10+
android:fullBackupContent="@xml/backup_rules"
11+
android:icon="@mipmap/ic_launcher"
12+
android:label="@string/app_name"
13+
android:roundIcon="@mipmap/ic_launcher_round"
14+
android:supportsRtl="true"
15+
android:theme="@style/Theme.AndroidObservability"
16+
tools:targetApi="31">
17+
<activity
18+
android:name=".MainActivity"
19+
android:exported="true"
20+
android:label="@string/app_name"
21+
android:theme="@style/Theme.AndroidObservability">
22+
<intent-filter>
23+
<action android:name="android.intent.action.MAIN" />
24+
25+
<category android:name="android.intent.category.LAUNCHER" />
26+
</intent-filter>
27+
</activity>
28+
</application>
29+
30+
</manifest>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.example.androidobservability
2+
3+
import android.app.Application
4+
import com.launchdarkly.sdk.ContextKind
5+
import com.launchdarkly.sdk.LDContext
6+
import com.launchdarkly.sdk.android.Components
7+
import com.launchdarkly.sdk.android.LDClient
8+
import com.launchdarkly.sdk.android.LDConfig
9+
import com.launchdarkly.observability.plugin.Observability
10+
import com.launchdarkly.sdk.android.integrations.Plugin
11+
import java.util.Collections
12+
13+
class BaseApplication : Application() {
14+
15+
companion object {
16+
// TODO O11Y-376: Update this credential to be driven by env variable or gradle property
17+
// Set LAUNCHDARKLY_MOBILE_KEY to your LaunchDarkly SDK mobile key.
18+
const val LAUNCHDARKLY_MOBILE_KEY = "MOBILE_KEY_GOES_HERE"
19+
}
20+
21+
override fun onCreate() {
22+
super.onCreate()
23+
24+
// Set LAUNCHDARKLY_MOBILE_KEY to your LaunchDarkly mobile key found on the LaunchDarkly
25+
// dashboard in the start guide.
26+
// If you want to disable the Auto EnvironmentAttributes functionality.
27+
// Use AutoEnvAttributes.Disabled as the argument to the Builder
28+
val ldConfig = LDConfig.Builder(LDConfig.Builder.AutoEnvAttributes.Enabled)
29+
.mobileKey(LAUNCHDARKLY_MOBILE_KEY)
30+
.plugins(Components.plugins().setPlugins(
31+
Collections.singletonList<Plugin>(Observability())
32+
))
33+
.build()
34+
35+
// Set up the context properties. This context should appear on your LaunchDarkly context
36+
// dashboard soon after you run the demo.
37+
val context = LDContext.builder(ContextKind.DEFAULT, "example-user-key")
38+
.anonymous(true)
39+
.build()
40+
41+
LDClient.init(this@BaseApplication, ldConfig, context)
42+
}
43+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.example.androidobservability
2+
3+
import android.os.Bundle
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.activity.enableEdgeToEdge
7+
import androidx.activity.viewModels
8+
import androidx.compose.foundation.layout.Column
9+
import androidx.compose.foundation.layout.fillMaxSize
10+
import androidx.compose.foundation.layout.padding
11+
import androidx.compose.material3.Button
12+
import androidx.compose.material3.Scaffold
13+
import androidx.compose.material3.Text
14+
import androidx.compose.runtime.Composable
15+
import androidx.compose.ui.Modifier
16+
import androidx.compose.ui.tooling.preview.Preview
17+
import com.example.androidobservability.ui.theme.AndroidObservabilityTheme
18+
19+
class MainActivity : ComponentActivity() {
20+
override fun onCreate(savedInstanceState: Bundle?) {
21+
super.onCreate(savedInstanceState)
22+
val viewModel: ViewModel by viewModels()
23+
enableEdgeToEdge()
24+
setContent {
25+
AndroidObservabilityTheme {
26+
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
27+
Column {
28+
Text(
29+
text = "Hello Telemetry",
30+
modifier = Modifier.padding(innerPadding)
31+
)
32+
Button(
33+
onClick = {
34+
viewModel.triggerMetric()
35+
}
36+
) {
37+
Text("Trigger Metric")
38+
}
39+
Button(
40+
onClick = {
41+
viewModel.triggerError()
42+
}
43+
) {
44+
Text("Trigger Error")
45+
}
46+
Button(
47+
onClick = {
48+
viewModel.triggerLog()
49+
}
50+
) {
51+
Text("Trigger Log")
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}
59+
60+
@Composable
61+
fun Greeting(name: String, modifier: Modifier = Modifier) {
62+
Text(
63+
text = "Hello $name!",
64+
modifier = modifier
65+
)
66+
}
67+
68+
@Preview(showBackground = true)
69+
@Composable
70+
fun GreetingPreview() {
71+
AndroidObservabilityTheme {
72+
Greeting("Android")
73+
}
74+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.androidobservability
2+
3+
import LDObserve
4+
import androidx.lifecycle.ViewModel
5+
import com.launchdarkly.observability.interfaces.Metric
6+
import io.opentelemetry.api.common.AttributeKey
7+
import io.opentelemetry.api.common.Attributes
8+
9+
class ViewModel : ViewModel() {
10+
fun triggerMetric() {
11+
LDObserve.recordMetric(Metric("test", 50.0))
12+
}
13+
14+
fun triggerError() {
15+
LDObserve.recordError(
16+
Error("Manual error womp womp"),
17+
Attributes.of(AttributeKey.stringKey("FakeAttribute"), "FakeVal")
18+
)
19+
}
20+
21+
fun triggerLog() {
22+
LDObserve.recordLog(
23+
"Test Log",
24+
"debug",
25+
Attributes.of(AttributeKey.stringKey("FakeAttribute"), "FakeVal")
26+
)
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.androidobservability.ui.theme
2+
3+
import androidx.compose.ui.graphics.Color
4+
5+
val Purple80 = Color(0xFFD0BCFF)
6+
val PurpleGrey80 = Color(0xFFCCC2DC)
7+
val Pink80 = Color(0xFFEFB8C8)
8+
9+
val Purple40 = Color(0xFF6650a4)
10+
val PurpleGrey40 = Color(0xFF625b71)
11+
val Pink40 = Color(0xFF7D5260)

0 commit comments

Comments
 (0)