Skip to content

Commit

Permalink
Merge pull request #1137 from embrace-io/traceparent-generator
Browse files Browse the repository at this point in the history
Allow null for traceparent generator
  • Loading branch information
fractalwrench authored Jul 29, 2024
2 parents f477bc3 + 27e93e8 commit 46d593a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
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())
}
}

0 comments on commit 46d593a

Please sign in to comment.