Skip to content
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

Allow null for traceparent generator #1137

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import android.os.Build.VERSION_CODES.TIRAMISU
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.embrace.android.embracesdk.Embrace.AppFramework
import io.embrace.android.embracesdk.IntegrationTestRule
import io.embrace.android.embracesdk.fakes.fakeNetworkSpanForwardingBehavior
import io.embrace.android.embracesdk.internal.IdGeneratorTest.Companion.validPattern
import io.embrace.android.embracesdk.internal.config.remote.NetworkSpanForwardingRemoteConfig
import io.embrace.android.embracesdk.recordSession
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
Expand All @@ -28,7 +30,12 @@ internal class PublicApiTest {
@Rule
@JvmField
val testRule: IntegrationTestRule = IntegrationTestRule {
IntegrationTestRule.Harness(startImmediately = false)
IntegrationTestRule.Harness(startImmediately = false).apply {
overriddenConfigService.networkSpanForwardingBehavior =
fakeNetworkSpanForwardingBehavior {
NetworkSpanForwardingRemoteConfig(100f)
}
}
}

@Test
Expand Down Expand Up @@ -92,7 +99,10 @@ internal class PublicApiTest {
with(testRule) {
startSdk(context = harness.overriddenCoreModule.context)
harness.recordSession {
assertEquals(embrace.currentSessionId, harness.overriddenOpenTelemetryModule.currentSessionSpan.getSessionId())
assertEquals(
embrace.currentSessionId,
harness.overriddenOpenTelemetryModule.currentSessionSpan.getSessionId()
)
assertNotNull(embrace.currentSessionId)
}
}
Expand All @@ -115,8 +125,9 @@ internal class PublicApiTest {
@Test
fun `ensure all generated W3C traceparent conforms to the expected format`() {
with(testRule) {
startSdk()
repeat(100) {
assertTrue(validPattern.matches(embrace.generateW3cTraceparent()))
assertTrue(validPattern.matches(checkNotNull(embrace.generateW3cTraceparent())))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public String getTraceIdHeader() {
return impl.getTraceIdHeader();
}

@NonNull
@Nullable
@Override
public String generateW3cTraceparent() {
return impl.generateW3cTraceparent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ public interface NetworkRequestApi {

public val traceIdHeader: String

public fun generateW3cTraceparent(): String
public fun generateW3cTraceparent(): String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ internal class NetworkRequestApiDelegate(
return NetworkBehaviorImpl.CONFIG_TRACE_ID_HEADER_DEFAULT_VALUE
}

override fun generateW3cTraceparent(): String = IdGenerator.generateW3CTraceparent()
override fun generateW3cTraceparent(): String? =
if (configService?.networkSpanForwardingBehavior?.isNetworkSpanForwardingEnabled() == true) {
IdGenerator.generateW3CTraceparent()
} else {
null
}

private fun logNetworkRequest(request: EmbraceNetworkRequest) {
if (configService?.networkBehavior?.isUrlEnabled(request.url) == true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import io.embrace.android.embracesdk.fakes.FakeNetworkLoggingService
import io.embrace.android.embracesdk.fakes.FakeSessionOrchestrator
import io.embrace.android.embracesdk.fakes.FakeTelemetryService
import io.embrace.android.embracesdk.fakes.fakeModuleInitBootstrapper
import io.embrace.android.embracesdk.fakes.fakeNetworkSpanForwardingBehavior
import io.embrace.android.embracesdk.internal.config.remote.NetworkSpanForwardingRemoteConfig
import io.embrace.android.embracesdk.internal.payload.AppFramework
import io.embrace.android.embracesdk.network.EmbraceNetworkRequest
import io.embrace.android.embracesdk.network.http.HttpMethod
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -60,7 +63,15 @@ internal class NetworkRequestApiDelegateTest {
}

@Test
fun testGenerateW3cTraceparent() {
fun testGenerateW3cTraceparentEnabled() {
configService.networkSpanForwardingBehavior = fakeNetworkSpanForwardingBehavior {
NetworkSpanForwardingRemoteConfig(100f)
}
assertNotNull(delegate.generateW3cTraceparent())
}

@Test
fun testGenerateW3cTraceparent() {
assertNull(delegate.generateW3cTraceparent())
}
}