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

Create OkHttp on background thread during initialization #1677

Merged
merged 3 commits into from
Nov 14, 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 @@ -15,7 +15,7 @@ internal class EmbraceApiUrlBuilder(
}

override val appId: String = checkNotNull(instrumentedConfig.project.getAppId())
private val coreBaseUrl = instrumentedConfig.baseUrls.getData() ?: "https://a-$appId.$DATA_DEFAULT/api"
private val coreBaseUrl = instrumentedConfig.baseUrls.getData() ?: "https://a-$appId.$DATA_DEFAULT"
private val configBaseUrl = instrumentedConfig.baseUrls.getConfig() ?: "https://a-$appId.$CONFIG_DEFAULT"
private val operatingSystemCode = Build.VERSION.SDK_INT.toString() + ".0.0"
override val baseDataUrl: String = resolveUrl(Endpoint.SESSIONS).split(Endpoint.SESSIONS.path).first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import java.util.concurrent.TimeUnit

class CombinedRemoteConfigSource(
private val store: RemoteConfigStore,
private val httpSource: RemoteConfigSource,
httpSource: Lazy<RemoteConfigSource>,
private val worker: BackgroundWorker,
private val intervalMs: Long = 60 * 60 * 1000
) {

private val httpSource: RemoteConfigSource by httpSource

// the remote config that is used for the lifetime of the process.
private val response by lazy {
Systrace.traceSynchronous("load-config-from-store") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ internal class ConfigModuleImpl(
}

override val okHttpClient by singleton {
OkHttpClient()
.newBuilder()
.protocols(listOf(Protocol.HTTP_2, Protocol.HTTP_1_1))
.connectTimeout(DEFAULT_CONNECTION_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.readTimeout(DEFAULT_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.build()
Systrace.traceSynchronous("okhttp-client-init") {
OkHttpClient()
.newBuilder()
.protocols(listOf(Protocol.HTTP_2, Protocol.HTTP_1_1))
.connectTimeout(DEFAULT_CONNECTION_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.readTimeout(DEFAULT_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.build()
}
}

override val combinedRemoteConfigSource: CombinedRemoteConfigSource? by singleton {
if (initModule.onlyOtelExportEnabled()) return@singleton null
CombinedRemoteConfigSource(
store = remoteConfigStore,
httpSource = remoteConfigSource ?: return@singleton null,
httpSource = lazy { checkNotNull(remoteConfigSource) },
worker = workerThreadModule.backgroundWorker(Worker.Background.IoRegWorker),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ internal class EmbraceApiServiceTest {
var finished = false
apiService.sendSession({ it.write(payload) }) { finished = true }
verifyOnlyRequest(
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/api/v2/spans",
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/v2/spans",
expectedPayload = payload
)
assertTrue(finished)
Expand Down Expand Up @@ -100,7 +100,7 @@ internal class EmbraceApiServiceTest {
val type: ParameterizedType = TypeUtils.parameterizedType(Envelope::class, LogPayload::class)

verifyOnlyRequest(
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/api/v2/logs",
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/v2/logs",
expectedPayload = getGenericsExpectedPayloadSerialized(logsEnvelope, type)
)
}
Expand Down Expand Up @@ -130,7 +130,7 @@ internal class EmbraceApiServiceTest {

val request = fakePendingApiCallsSender.retryQueue.single().first
val payload = fakePendingApiCallsSender.retryQueue.single().second
assertEquals("https://a-$fakeAppId.data.emb-api.com/api/v2/logs", request.url.url)
assertEquals("https://a-$fakeAppId.data.emb-api.com/v2/logs", request.url.url)
val type: ParameterizedType = TypeUtils.parameterizedType(Envelope::class, LogPayload::class)
assertArrayEquals(getGenericsExpectedPayloadSerialized(logsEnvelope, type), payload)
}
Expand All @@ -149,7 +149,7 @@ internal class EmbraceApiServiceTest {
apiService.sendLogEnvelope(logPayload)
assertEquals(0, fakeApiClient.sentRequests.size)
val request = fakePendingApiCallsSender.retryQueue.single().first
assertEquals("https://a-$fakeAppId.data.emb-api.com/api/v2/logs", request.url.url)
assertEquals("https://a-$fakeAppId.data.emb-api.com/v2/logs", request.url.url)
}

@Test
Expand All @@ -165,7 +165,7 @@ internal class EmbraceApiServiceTest {
apiService.sendLogEnvelope(logPayload)

verifyOnlyRequest(
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/api/v2/logs",
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/v2/logs",
expectedPayload = getExpectedPayloadSerialized(logPayload, logType)
)
assertEquals(1, fakePendingApiCallsSender.retryQueue.size)
Expand All @@ -183,7 +183,7 @@ internal class EmbraceApiServiceTest {
apiService.sendLogEnvelope(logPayload)

verifyOnlyRequest(
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/api/v2/logs",
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/v2/logs",
expectedPayload = getExpectedPayloadSerialized(logPayload, logType)
)
assertEquals(1, fakePendingApiCallsSender.retryQueue.size)
Expand All @@ -204,7 +204,7 @@ internal class EmbraceApiServiceTest {
apiService.sendLogEnvelope(logPayload)

verifyOnlyRequest(
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/api/v2/logs",
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/v2/logs",
expectedPayload = getExpectedPayloadSerialized(logPayload, logType)
)
assertEquals(0, fakePendingApiCallsSender.retryQueue.size)
Expand All @@ -222,7 +222,7 @@ internal class EmbraceApiServiceTest {
apiService.sendLogEnvelope(logPayload)

verifyOnlyRequest(
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/api/v2/logs",
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/v2/logs",
expectedPayload = getExpectedPayloadSerialized(logPayload, logType)
)
assertEquals(0, fakePendingApiCallsSender.retryQueue.size)
Expand All @@ -243,7 +243,7 @@ internal class EmbraceApiServiceTest {
apiService.sendLogEnvelope(logPayload)

verifyOnlyRequest(
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/api/v2/logs",
expectedUrl = "https://a-$fakeAppId.data.emb-api.com/v2/logs",
expectedPayload = getExpectedPayloadSerialized(logPayload, logType)
)
assertEquals(0, fakePendingApiCallsSender.retryQueue.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ internal class EmbraceApiUrlBuilderTest {
apiUrlBuilder.resolveUrl(Endpoint.CONFIG)
)
assertEquals(
"https://a-$APP_ID.data.emb-api.com/api/v2/logs",
"https://a-$APP_ID.data.emb-api.com/v2/logs",
apiUrlBuilder.resolveUrl(Endpoint.LOGS)
)
assertEquals(
"https://a-$APP_ID.data.emb-api.com/api/v2/spans",
"https://a-$APP_ID.data.emb-api.com/v2/spans",
apiUrlBuilder.resolveUrl(Endpoint.SESSIONS)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,11 @@ internal class EmbracePendingApiCallsSenderTest {

// verify logs were added to the queue, and oldest added requests are dropped
assertEquals(
"https://a-abcde.data.emb-api.com/api/v2/logs",
"https://a-abcde.data.emb-api.com/v2/logs",
queue.pollNextPendingApiCall()?.apiRequest?.url?.url
)
assertEquals(
"https://a-abcde.data.emb-api.com/api/v2/logs",
"https://a-abcde.data.emb-api.com/v2/logs",
queue.pollNextPendingApiCall()?.apiRequest?.url?.url
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CombinedRemoteConfigSourceTest {
remoteConfigStore = FakeRemoteConfigStore()
source = CombinedRemoteConfigSource(
remoteConfigStore,
remoteConfigSource,
lazy { remoteConfigSource },
BackgroundWorker(executorService)
)
}
Expand All @@ -41,7 +41,7 @@ class CombinedRemoteConfigSourceTest {
val cfg = RemoteConfig(100)
source = CombinedRemoteConfigSource(
FakeRemoteConfigStore(ConfigHttpResponse(cfg, null)),
remoteConfigSource,
lazy { remoteConfigSource },
BackgroundWorker(executorService)
)
assertEquals(cfg, source.getConfig())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ internal class EmbraceNetworkCaptureServiceTest {

@Test
fun `test capture rule doesn't capture Embrace endpoints`() {
val rule = getDefaultRule(urlRegex = "https://a-abcde.data.emb-api.com/api/v2")
val rule = getDefaultRule(urlRegex = "https://a-abcde.data.emb-api.com/v2")
cfg = RemoteConfig(networkCaptureRules = setOf(rule))
val result = getService().getNetworkCaptureRules("https://a-abcde.data.emb-api.com/api/v2/spans", "GET")
val result = getService().getNetworkCaptureRules("https://a-abcde.data.emb-api.com/v2/spans", "GET")
assertEquals(0, result.size)
}

Expand Down
Loading