-
Notifications
You must be signed in to change notification settings - Fork 112
test: add minified emulator smoke test #1972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aranhave
wants to merge
5
commits into
open-telemetry:main
Choose a base branch
from
aranhave:vishwan/minified-emulator-smoke-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a1c7b91
test: add minified emulator smoke test
aranhave 044c4f9
test: strengthen minified emulator smoke coverage
aranhave d576fd8
ci: exclude instrumented smoke harness from coverage
aranhave 23e71ac
test: remove unnecessary Error Prone suppression
aranhave a253492
test: remove unnecessary packaging exclusion
aranhave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| plugins { | ||
| id("otel.android-app-conventions") | ||
| } | ||
|
|
||
| android { | ||
| namespace = "io.opentelemetry.android.smoketestapp" | ||
|
|
||
| defaultConfig { | ||
| applicationId = "io.opentelemetry.android.smoketestapp" | ||
| } | ||
|
|
||
| buildTypes { | ||
| release { | ||
| isMinifyEnabled = true | ||
| isShrinkResources = true | ||
| proguardFiles( | ||
| getDefaultProguardFile("proguard-android-optimize.txt"), | ||
| "proguard-rules.pro", | ||
| ) | ||
| signingConfig = signingConfigs.getByName("debug") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(project(":android-agent")) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # AndroidJUnitRunner lives in the test APK and references this class from the target APK. | ||
| -keep class androidx.tracing.Trace { *; } | ||
|
|
||
| # AGP treats dependencies shared by a minified target and its test APK as target-provided. Keep | ||
| # Kotlin's runtime available to AndroidJUnitRunner, which executes inside the target process. | ||
| -keep class kotlin.** { *; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:tools="http://schemas.android.com/tools"> | ||
|
|
||
| <uses-permission android:name="android.permission.INTERNET" /> | ||
|
|
||
| <application | ||
| android:label="minified-smoke-test" | ||
| android:theme="@android:style/Theme.Material.Light.NoActionBar" | ||
| android:usesCleartextTraffic="true" | ||
| tools:ignore="MissingApplicationIcon"> | ||
| <activity | ||
| android:name=".SmokeTestActivity" | ||
| android:exported="false" /> | ||
| </application> | ||
|
|
||
| </manifest> |
48 changes: 48 additions & 0 deletions
48
smoke-test-app/src/main/kotlin/io/opentelemetry/android/smoketestapp/SmokeTestActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.android.smoketestapp | ||
|
|
||
| import android.app.Activity | ||
| import android.os.Bundle | ||
| import io.opentelemetry.android.agent.OpenTelemetryRumInitializer | ||
|
|
||
| const val OTLP_ENDPOINT_EXTRA = "io.opentelemetry.android.smoketest.OTLP_ENDPOINT" | ||
| const val SMOKE_TEST_SCOPE_NAME = "smoke-test" | ||
| const val SMOKE_TEST_SPAN_NAME = "minified-app-smoke-test" | ||
|
|
||
| class SmokeTestActivity : Activity() { | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| val endpoint = | ||
| requireNotNull(intent.getStringExtra(OTLP_ENDPOINT_EXTRA)) { | ||
| "Missing OTLP endpoint" | ||
| } | ||
| val openTelemetryRum = | ||
| OpenTelemetryRumInitializer.initialize(application) { | ||
| httpExport { | ||
| baseUrl = endpoint | ||
| } | ||
| diskBuffering { | ||
| enabled(false) | ||
| } | ||
| disableLogging() | ||
| disableMetrics() | ||
| } | ||
|
|
||
| try { | ||
| openTelemetryRum.openTelemetry | ||
| .tracerProvider | ||
| .get(SMOKE_TEST_SCOPE_NAME) | ||
| .spanBuilder(SMOKE_TEST_SPAN_NAME) | ||
| .startSpan() | ||
| .end() | ||
| } finally { | ||
| // Shutdown starts the exporter flush; the instrumentation test waits for its request. | ||
| openTelemetryRum.shutdown() | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
| import org.jetbrains.kotlin.gradle.dsl.KotlinVersion | ||
|
|
||
| plugins { | ||
| id("com.android.test") | ||
| id("otel.spotless-conventions") | ||
| } | ||
|
|
||
| android { | ||
| namespace = "io.opentelemetry.android.smoketest" | ||
| compileSdk = (property("android.compileSdk") as String).toInt() | ||
|
|
||
| defaultConfig { | ||
| minSdk = (property("android.minSdk") as String).toInt() | ||
| testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
| } | ||
|
|
||
| targetProjectPath = ":smoke-test-app" | ||
|
|
||
| buildTypes { | ||
| create("release") { | ||
| isMinifyEnabled = true | ||
| signingConfig = signingConfigs.getByName("debug") | ||
| testProguardFiles( | ||
| getDefaultProguardFile("proguard-android-optimize.txt"), | ||
| "proguard-rules.pro", | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| compileOptions { | ||
| sourceCompatibility(rootProject.extra["java_version"] as JavaVersion) | ||
| targetCompatibility(rootProject.extra["java_version"] as JavaVersion) | ||
| } | ||
|
|
||
| kotlin { | ||
| compilerOptions { | ||
| jvmTarget.set(rootProject.extra["jvm_target"] as JvmTarget) | ||
| apiVersion.set(rootProject.extra["kotlin_min_supported_version"] as KotlinVersion) | ||
| languageVersion.set(rootProject.extra["kotlin_min_supported_version"] as KotlinVersion) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| androidComponents { | ||
| // The test must run against the target's minified release variant. | ||
| beforeVariants(selector().withBuildType("debug")) { | ||
| it.enable = false | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(libs.androidx.junit) | ||
| implementation(libs.androidx.test.core) | ||
| implementation(libs.androidx.test.rules) | ||
| implementation(libs.androidx.test.runner) | ||
| implementation(libs.opentelemetry.proto) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Kotlin runs from the optimized target APK because AndroidJUnitRunner executes in its process. | ||
| # Keep the runtime methods used only by the test APK. | ||
| -keep class kotlin.** { *; } | ||
|
|
||
| # The OTLP protobuf artifact includes optional gRPC stubs; this test only decodes HTTP payloads. | ||
| -dontwarn io.grpc.** |
159 changes: 159 additions & 0 deletions
159
smoke-test/src/main/kotlin/io/opentelemetry/android/smoketest/MinifiedAppSmokeTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.android.smoketest | ||
|
|
||
| import android.app.Activity | ||
| import android.content.Intent | ||
| import androidx.test.core.app.ActivityScenario | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import io.opentelemetry.android.smoketestapp.OTLP_ENDPOINT_EXTRA | ||
| import io.opentelemetry.android.smoketestapp.SMOKE_TEST_SCOPE_NAME | ||
| import io.opentelemetry.android.smoketestapp.SMOKE_TEST_SPAN_NAME | ||
| import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest | ||
| import io.opentelemetry.proto.common.v1.KeyValue | ||
| import io.opentelemetry.proto.trace.v1.ResourceSpans | ||
| import io.opentelemetry.proto.trace.v1.ScopeSpans | ||
| import io.opentelemetry.proto.trace.v1.Span | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import java.io.InputStream | ||
| import java.util.concurrent.TimeUnit | ||
| import java.util.zip.GZIPInputStream | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class MinifiedAppSmokeTest { | ||
| @Test | ||
| fun appLaunchesAndExportsTrace() { | ||
| OtlpHttpServer().use { server -> | ||
| val intent = | ||
| Intent() | ||
| .setClassName(TARGET_PACKAGE, TARGET_ACTIVITY) | ||
| .putExtra(OTLP_ENDPOINT_EXTRA, server.url) | ||
|
|
||
| ActivityScenario.launch<Activity>(intent).use { | ||
| awaitExpectedTrace(server) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun awaitExpectedTrace(server: OtlpHttpServer) { | ||
| val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(EXPORT_TIMEOUT_SECONDS) | ||
| val receivedTargets = mutableListOf<String>() | ||
| val receivedSpanNames = mutableListOf<String>() | ||
|
|
||
| while (System.nanoTime() < deadline) { | ||
| val request = | ||
| server.takeRequest(deadline - System.nanoTime()) ?: break | ||
| receivedTargets.add("${request.method} ${request.target}") | ||
| if (request.target != TRACE_PATH) { | ||
| continue | ||
| } | ||
|
|
||
| val exportRequest = parseTraceRequest(request) | ||
| exportRequest.resourceSpansList.forEach { resourceSpans -> | ||
| resourceSpans.scopeSpansList.forEach { scopeSpans -> | ||
| scopeSpans.spansList.forEach { span -> | ||
| receivedSpanNames.add(span.name) | ||
| if (span.name == SMOKE_TEST_SPAN_NAME) { | ||
| assertExpectedTrace(request, resourceSpans, scopeSpans, span) | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| throw AssertionError( | ||
| "Timed out waiting for span '$SMOKE_TEST_SPAN_NAME'. " + | ||
| "Requests: $receivedTargets; spans: $receivedSpanNames", | ||
| ) | ||
| } | ||
|
|
||
| private fun assertExpectedTrace( | ||
| request: CapturedHttpRequest, | ||
| resourceSpans: ResourceSpans, | ||
| scopeSpans: ScopeSpans, | ||
| span: Span, | ||
| ) { | ||
| assertSmoke(request.method == "POST") { "Expected POST but received ${request.method}" } | ||
| assertSmoke(request.headers["content-type"]?.substringBefore(';') == PROTOBUF_CONTENT_TYPE) { | ||
| "Expected $PROTOBUF_CONTENT_TYPE but received ${request.headers["content-type"]}" | ||
| } | ||
| assertSmoke(request.headers["content-encoding"] == "gzip") { | ||
| "Expected gzip content encoding but received ${request.headers["content-encoding"]}" | ||
| } | ||
| assertSmoke(scopeSpans.scope.name == SMOKE_TEST_SCOPE_NAME) { | ||
| "Expected scope '$SMOKE_TEST_SCOPE_NAME' but received '${scopeSpans.scope.name}'" | ||
| } | ||
|
|
||
| val resourceAttributes = resourceSpans.resource.attributesList.stringValues() | ||
| EXPECTED_RESOURCE_ATTRIBUTES.forEach { (key, value) -> | ||
| assertSmoke(resourceAttributes[key] == value) { | ||
| "Expected $key=$value resource attribute: $resourceAttributes" | ||
| } | ||
| } | ||
| listOf("telemetry.sdk.version", "device.model.name", "device.model.identifier").forEach { key -> | ||
| assertSmoke(!resourceAttributes[key].isNullOrBlank()) { | ||
| "Missing $key resource attribute: $resourceAttributes" | ||
| } | ||
| } | ||
| val spanAttributes = span.attributesList.stringValues() | ||
| assertSmoke(!spanAttributes["session.id"].isNullOrBlank()) { | ||
| "Expected a non-empty session.id span attribute: $spanAttributes" | ||
| } | ||
| assertValidId("trace", span.traceId.toByteArray(), TRACE_ID_BYTES) | ||
| assertValidId("span", span.spanId.toByteArray(), SPAN_ID_BYTES) | ||
| } | ||
|
|
||
| private fun parseTraceRequest(request: CapturedHttpRequest): ExportTraceServiceRequest { | ||
| val inputStream: InputStream = | ||
| when (val encoding = request.headers["content-encoding"]) { | ||
| null, "identity" -> request.body.inputStream() | ||
| "gzip" -> GZIPInputStream(request.body.inputStream()) | ||
| else -> throw AssertionError("Unsupported OTLP content encoding: $encoding") | ||
| } | ||
| return inputStream.use(ExportTraceServiceRequest::parseFrom) | ||
| } | ||
|
|
||
| private fun List<KeyValue>.stringValues(): Map<String, String> = associate { it.key to it.value.stringValue } | ||
|
|
||
| private fun assertValidId( | ||
| name: String, | ||
| value: ByteArray, | ||
| expectedSize: Int, | ||
| ) { | ||
| assertSmoke(value.size == expectedSize && value.any { it != 0.toByte() }) { | ||
| "Expected a valid $name ID but received ${value.joinToString("") { "%02x".format(it) }}" | ||
| } | ||
| } | ||
|
|
||
| private fun assertSmoke( | ||
| condition: Boolean, | ||
| message: () -> String, | ||
| ) { | ||
| if (!condition) { | ||
| throw AssertionError(message()) | ||
| } | ||
| } | ||
|
|
||
| private companion object { | ||
| const val TARGET_PACKAGE = "io.opentelemetry.android.smoketestapp" | ||
| const val TARGET_ACTIVITY = "$TARGET_PACKAGE.SmokeTestActivity" | ||
| const val TRACE_PATH = "/v1/traces" | ||
| const val PROTOBUF_CONTENT_TYPE = "application/x-protobuf" | ||
| const val SERVICE_NAME = "minified-smoke-test" | ||
| const val EXPORT_TIMEOUT_SECONDS = 15L | ||
| const val TRACE_ID_BYTES = 16 | ||
| const val SPAN_ID_BYTES = 8 | ||
| val EXPECTED_RESOURCE_ATTRIBUTES = | ||
| mapOf( | ||
| "service.name" to SERVICE_NAME, | ||
| "telemetry.sdk.name" to "opentelemetry", | ||
| "telemetry.sdk.language" to "java", | ||
| "os.type" to "linux", | ||
| ) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.