Skip to content

Commit

Permalink
feat: implement HoneycombOptions (#2)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?

Implements `HoneycombOptions`, including `HoneycombOptions.Builder`, as
per the design doc.

## Short description of the changes

* `Builder` is pre-populated by any XML resource values with the
expected keys.
* There are setters for all values.
* `HoneycombOptions` only contains specific values like
`tracesEndpoint`, whereas the `Builder` has fallback settings like
`setEndpoint`. The specific values are resolved when `build()` is
called.
* Automatic headers and resources are injected upon calling `build()`.
* The only required field is `apiKey`.
* Throws HoneycombException on any fatal configuration error. These
should only be unexpected developer bugs, not expected runtime errors.

## How to verify that this has the expected result

There are comprehensive unit tests that I believe test all XML values,
setters, and fallbacks, as well as any error conditions.

In review please look for the following:
* Are there any missing unit test cases?
* Is the fallback logic correct?
* Are the resources and headers auto-populated correctly?
* Should the error cases actually be error cases?
  • Loading branch information
beekhc authored Aug 7, 2024
1 parent 23f3abc commit f8e922d
Show file tree
Hide file tree
Showing 16 changed files with 1,407 additions and 68 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
/.idea/androidTestResultsUserPreferences.xml
/.idea/deploymentTargetSelector.xml
.DS_Store
/build
/captures
Expand Down
10 changes: 0 additions & 10 deletions .idea/deploymentTargetSelector.xml

This file was deleted.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.jetbrains.kotlin.android) apply false
alias(libs.plugins.android.library) apply false
}
}
19 changes: 15 additions & 4 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ android {
defaultConfig {
minSdk = 21

buildConfigField("String","HONEYCOMB_DISTRO_VERSION","\"0.0.1-alpha\"")

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

buildFeatures {
buildConfig = true
}
buildTypes {
release {
isMinifyEnabled = false
Expand All @@ -24,6 +28,7 @@ android {
}
}
compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
Expand All @@ -33,11 +38,17 @@ android {
}

dependencies {
// This is required by opentelemetry-android.
coreLibraryDesugaring(libs.desugar.jdk.libs)

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.opentelemetry.android.agent)
implementation(libs.opentelemetry.api)
implementation(libs.opentelemetry.sdk)
implementation(libs.opentelemetry.exporter.otlp)

testImplementation(libs.junit)

androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.honeycomb.opentelemetry.android

import android.os.Build
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*
import kotlin.time.Duration.Companion.seconds

/**
* Instrumented test, which will execute on an Android device.
*/
@RunWith(AndroidJUnit4::class)
class HoneycombOptionsInstrumentedTest {
@Test
fun builder_usesContext() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("io.honeycomb.opentelemetry.android.test", context.packageName)

val options = HoneycombOptions.builder(context).build()
assertEquals("key", options.tracesApiKey)
}

@Test
fun runtimeVersion_isAutomaticallySet() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val options = HoneycombOptions.builder(context).build()
assertEquals(
mapOf(
"service.name" to "unknown_service",
"honeycomb.distro.version" to "0.0.1-alpha",
"honeycomb.distro.runtime_version" to Build.VERSION.RELEASE,
), options.resourceAttributes
)
}

@Test
fun source_getString() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val source = HoneycombOptionsResourceSource(context)
assertEquals("some string", source.getString("STRING"))
}

@Test
fun source_getNullForEmptyString() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val source = HoneycombOptionsResourceSource(context)
assertNull(source.getString("EMPTY_STRING"))
}

@Test
fun source_getNullForBlankString() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val source = HoneycombOptionsResourceSource(context)
assertNull(source.getString("BLANK_STRING"))
}

@Test
fun source_getInt() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val source = HoneycombOptionsResourceSource(context)
assertEquals(5000, source.getInt("NUMBER"))
}

@Test
fun source_getDuration() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val source = HoneycombOptionsResourceSource(context)
assertEquals(5.seconds, source.getDuration("NUMBER"))
}

@Test
fun source_getBoolean() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val source = HoneycombOptionsResourceSource(context)
assertEquals(true, source.getBoolean("BOOL"))
}
}
9 changes: 9 additions & 0 deletions core/src/androidTest/res/values/test_values.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="HONEYCOMB_API_KEY">key</string>
<string name="STRING">some string</string>
<string name="EMPTY_STRING"></string>
<string name="BLANK_STRING"> </string>
<integer name="NUMBER">5000</integer>
<bool name="BOOL">true</bool>
</resources>
Loading

0 comments on commit f8e922d

Please sign in to comment.