Skip to content

Commit

Permalink
Merge branch 'feat/add-opentelemetry-mode-option' into feat/otel-mode…
Browse files Browse the repository at this point in the history
…-off
  • Loading branch information
adinauer authored Dec 20, 2024
2 parents dd37382 + 68019c1 commit cb4ede3
Show file tree
Hide file tree
Showing 25 changed files with 215 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class InternalSentrySdkTest {
lateinit var options: SentryOptions

fun init(context: Context) {
SentryAndroid.init(context) { options ->
initForTest(context) { options ->
this@Fixture.options = options
options.dsn = "https://key@host/proj"
options.setTransportFactory { _, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.sentry.Hint
import io.sentry.ILogger
import io.sentry.ISentryClient
import io.sentry.Sentry
import io.sentry.Sentry.OptionsConfiguration
import io.sentry.SentryEnvelope
import io.sentry.SentryLevel
import io.sentry.SentryLevel.DEBUG
Expand All @@ -40,6 +41,8 @@ import io.sentry.cache.PersistingScopeObserver
import io.sentry.cache.PersistingScopeObserver.BREADCRUMBS_FILENAME
import io.sentry.cache.PersistingScopeObserver.SCOPE_CACHE
import io.sentry.cache.PersistingScopeObserver.TRANSACTION_FILENAME
import io.sentry.test.applyTestOptions
import io.sentry.test.initForTest
import io.sentry.transport.NoOpEnvelopeCache
import io.sentry.util.StringUtils
import org.awaitility.kotlin.await
Expand Down Expand Up @@ -100,9 +103,9 @@ class SentryAndroidTest {
}
val mockContext = context ?: ContextUtilsTestHelper.mockMetaData(metaData = metadata)
when {
logger != null -> SentryAndroid.init(mockContext, logger)
options != null -> SentryAndroid.init(mockContext, options)
else -> SentryAndroid.init(mockContext)
logger != null -> initForTest(mockContext, logger)
options != null -> initForTest(mockContext, options)
else -> initForTest(mockContext)
}
}

Expand Down Expand Up @@ -290,7 +293,7 @@ class SentryAndroidTest {

val mockContext = ContextUtilsTestHelper.createMockContext(true)
val cacheDirPath = Files.createTempDirectory("new_cache").absolutePathString()
SentryAndroid.init(mockContext) {
initForTest(mockContext) {
it.dsn = "https://key@sentry.io/123"
it.cacheDirPath = cacheDirPath
options = it
Expand Down Expand Up @@ -354,7 +357,7 @@ class SentryAndroidTest {
@Test
fun `When initializing Sentry a callback is added to application by appStartMetrics`() {
val mockContext = ContextUtilsTestHelper.createMockContext(true)
SentryAndroid.init(mockContext) {
initForTest(mockContext) {
it.dsn = "https://key@sentry.io/123"
}
verify(mockContext.applicationContext as Application).registerActivityLifecycleCallbacks(eq(AppStartMetrics.getInstance()))
Expand All @@ -368,7 +371,7 @@ class SentryAndroidTest {
Mockito.mockStatic(ContextUtils::class.java, Mockito.CALLS_REAL_METHODS).use { mockedContextUtils ->
mockedContextUtils.`when`<Any> { ContextUtils.isForegroundImportance() }
.thenReturn(inForeground)
SentryAndroid.init(context) { options ->
initForTest(context) { options ->
options.release = "prod"
options.dsn = "https://key@sentry.io/123"
options.isEnableAutoSessionTracking = true
Expand Down Expand Up @@ -558,3 +561,18 @@ class SentryAndroidTest {
override fun discard(envelope: SentryEnvelope) = Unit
}
}

fun initForTest(context: Context, optionsConfiguration: OptionsConfiguration<SentryAndroidOptions>) {
SentryAndroid.init(context) {
applyTestOptions(it)
optionsConfiguration.configure(it)
}
}

fun initForTest(context: Context, logger: ILogger) {
SentryAndroid.init(context, logger)
}

fun initForTest(context: Context) {
SentryAndroid.init(context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class SentryLogcatAdapterTest {
}
val mockContext = ContextUtilsTestHelper.mockMetaData(metaData = metadata)
when {
options != null -> SentryAndroid.init(mockContext, options)
else -> SentryAndroid.init(mockContext)
options != null -> initForTest(mockContext, options)
else -> initForTest(mockContext)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SessionTrackingIntegrationTest {
@Test
fun `session tracking works properly with multiple backgrounds and foregrounds`() {
lateinit var options: SentryAndroidOptions
SentryAndroid.init(context) {
initForTest(context) {
it.dsn = "https://key@sentry.io/proj"
it.release = "io.sentry.samples@2.3.0"
it.environment = "production"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ dependencies {
errorprone(Config.CompileOnly.errorprone)
errorprone(Config.CompileOnly.errorProneNullAway)

androidTestImplementation(projects.sentryTestSupport)
androidTestImplementation(Config.TestLibs.kotlinTestJunit)
androidTestImplementation(Config.TestLibs.espressoCore)
androidTestImplementation(Config.TestLibs.androidxTestCoreKtx)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry.uitest.android.benchmark

import android.content.Context
import android.os.Bundle
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.launchActivity
Expand All @@ -12,8 +13,11 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.runner.AndroidJUnitRunner
import io.sentry.ITransaction
import io.sentry.Sentry
import io.sentry.Sentry.OptionsConfiguration
import io.sentry.SentryOptions
import io.sentry.android.core.SentryAndroid
import io.sentry.android.core.SentryAndroidOptions
import io.sentry.test.applyTestOptions
import io.sentry.uitest.android.benchmark.util.BenchmarkOperation
import org.junit.runner.RunWith
import kotlin.test.AfterTest
Expand Down Expand Up @@ -62,7 +66,7 @@ class SentryBenchmarkTest : BaseBenchmarkTest() {
choreographer,
before = {
runner.runOnMainSync {
SentryAndroid.init(context) { options: SentryOptions ->
initForTest(context) { options: SentryOptions ->
options.dsn = "https://key@uri/1234567"
options.tracesSampleRate = 1.0
options.profilesSampleRate = 1.0
Expand Down Expand Up @@ -127,3 +131,10 @@ class SentryBenchmarkTest : BaseBenchmarkTest() {
}
}
}

fun initForTest(context: Context, optionsConfiguration: OptionsConfiguration<SentryAndroidOptions>) {
SentryAndroid.init(context) {
applyTestOptions(it)
optionsConfiguration.configure(it)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import androidx.test.espresso.idling.CountingIdlingResource
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.runner.AndroidJUnitRunner
import io.sentry.Sentry
import io.sentry.Sentry.OptionsConfiguration
import io.sentry.android.core.SentryAndroid
import io.sentry.android.core.SentryAndroidOptions
import io.sentry.test.applyTestOptions
import io.sentry.test.initForTest
import io.sentry.uitest.android.mockservers.MockRelay
import java.io.FileInputStream
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -84,7 +87,7 @@ abstract class BaseUiTest {
if (relayWaitForRequests) {
IdlingRegistry.getInstance().register(relayIdlingResource)
}
SentryAndroid.init(context) {
initForTest(context) {
it.dsn = mockDsn
it.isDebug = true
// We don't use test orchestrator, due to problems with Saucelabs.
Expand All @@ -111,3 +114,10 @@ fun classExists(className: String): Boolean {
}
return false
}

fun initForTest(context: Context, optionsConfiguration: OptionsConfiguration<SentryAndroidOptions>) {
SentryAndroid.init(context) {
applyTestOptions(it)
optionsConfiguration.configure(it)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.sentry.SentryReplayEvent
import io.sentry.SentryReplayEvent.ReplayType
import io.sentry.SystemOutLogger
import io.sentry.android.core.SentryAndroid
import io.sentry.android.core.SentryAndroidOptions
import io.sentry.android.core.performance.AppStartMetrics
import io.sentry.android.replay.ReplayCache.Companion.ONGOING_SEGMENT
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_BIT_RATE
Expand All @@ -32,6 +33,7 @@ import io.sentry.protocol.Contexts
import io.sentry.protocol.SentryId
import io.sentry.rrweb.RRWebMetaEvent
import io.sentry.rrweb.RRWebVideoEvent
import io.sentry.test.applyTestOptions
import org.awaitility.kotlin.await
import org.awaitility.kotlin.withAlias
import org.junit.Rule
Expand Down Expand Up @@ -146,7 +148,7 @@ class AnrWithReplayIntegrationTest {
val replayId1 = SentryId()
val replayId2 = SentryId()

SentryAndroid.init(context) {
initForTest(context) {
it.dsn = "https://key@sentry.io/123"
it.cacheDirPath = cacheDir
it.isDebug = true
Expand Down Expand Up @@ -216,3 +218,10 @@ class AnrWithReplayIntegrationTest {
.untilTrue(asserted)
}
}

fun initForTest(context: Context, optionsConfiguration: Sentry.OptionsConfiguration<SentryAndroidOptions>) {
SentryAndroid.init(context) {
applyTestOptions(it)
optionsConfiguration.configure(it)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.sentry.Sentry
import io.sentry.SentryLevel
import io.sentry.SentryOptions
import io.sentry.checkEvent
import io.sentry.test.initForTest
import io.sentry.transport.ITransport
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.mock
Expand Down Expand Up @@ -60,7 +61,7 @@ class SentryHandlerTest {
@Test
fun `does not initialize Sentry if Sentry is already enabled with higher prio`() {
val transport = mock<ITransport>()
Sentry.init {
initForTest {
it.dsn = "http://key@localhost/proj"
it.environment = "manual-environment"
it.setTransportFactory { _, _ -> transport }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.sentry.kotlin

import io.sentry.ScopeType
import io.sentry.Sentry
import io.sentry.test.initForTest
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
Expand All @@ -22,7 +23,7 @@ class SentryContextTest {

@BeforeTest
fun init() {
Sentry.init("https://key@sentry.io/123")
initForTest("https://key@sentry.io/123")
}

@AfterTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.sentry.ScopesAdapter
import io.sentry.Sentry
import io.sentry.SentryLevel
import io.sentry.checkEvent
import io.sentry.test.initForTest
import io.sentry.transport.ITransport
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.LogManager
Expand Down Expand Up @@ -84,7 +85,7 @@ class SentryAppenderTest {

@Test
fun `does not initialize Sentry if Sentry is already enabled with higher prio`() {
Sentry.init {
initForTest {
it.dsn = "http://key@localhost/proj"
it.environment = "manual-environment"
it.setTransportFactory(fixture.transportFactory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import io.sentry.Sentry
import io.sentry.SentryLevel
import io.sentry.SentryOptions
import io.sentry.checkEvent
import io.sentry.test.initForTest
import io.sentry.transport.ITransport
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
Expand Down Expand Up @@ -96,7 +97,7 @@ class SentryAppenderTest {
it.setTag("only-present-if-logger-init-was-run", "another-value")
}
)
Sentry.init {
initForTest {
it.dsn = "http://key@localhost/proj"
it.environment = "manual-environment"
it.setTransportFactory(fixture.transportFactory)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sentry.spring.jakarta

import io.sentry.Sentry
import io.sentry.test.initForTest
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import kotlin.test.AfterTest
Expand All @@ -26,7 +27,7 @@ class SentryTaskDecoratorTest {

@Test
fun `scopes is reset to its state within the thread after decoration is done`() {
Sentry.init {
initForTest {
it.dsn = dsn
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sentry.spring.jakarta.webflux

import io.sentry.Sentry
import io.sentry.test.initForTest
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import kotlin.test.AfterTest
Expand All @@ -27,7 +28,7 @@ class SentryScheduleHookTest {

@Test
fun `scopes is reset to its state within the thread after hook is done`() {
Sentry.init {
initForTest {
it.dsn = dsn
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.sentry.ScopesAdapter
import io.sentry.Sentry
import io.sentry.checkEvent
import io.sentry.checkTransaction
import io.sentry.test.initForTest
import io.sentry.transport.ITransport
import org.assertj.core.api.Assertions.assertThat
import org.awaitility.kotlin.await
Expand Down Expand Up @@ -175,7 +176,7 @@ open class App {

@Bean
open fun sentryInitializer(transportFactory: ITransportFactory) = ApplicationRunner {
Sentry.init {
initForTest {
it.dsn = "http://key@localhost/proj"
it.setDebug(true)
it.setTransportFactory(transportFactory)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sentry.spring

import io.sentry.Sentry
import io.sentry.test.initForTest
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import kotlin.test.AfterTest
Expand All @@ -26,7 +27,7 @@ class SentryTaskDecoratorTest {

@Test
fun `scopes is reset to its state within the thread after decoration is done`() {
Sentry.init {
initForTest {
it.dsn = dsn
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sentry.spring.webflux

import io.sentry.Sentry
import io.sentry.test.initForTest
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import kotlin.test.AfterTest
Expand All @@ -27,7 +28,7 @@ class SentryScheduleHookTest {

@Test
fun `scopes is reset to its state within the thread after hook is done`() {
Sentry.init {
initForTest {
it.dsn = dsn
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.sentry.ScopesAdapter
import io.sentry.Sentry
import io.sentry.checkEvent
import io.sentry.checkTransaction
import io.sentry.test.initForTest
import io.sentry.transport.ITransport
import org.assertj.core.api.Assertions.assertThat
import org.awaitility.kotlin.await
Expand Down Expand Up @@ -175,7 +176,7 @@ open class App {

@Bean
open fun sentryInitializer(transportFactory: ITransportFactory) = ApplicationRunner {
Sentry.init {
initForTest {
it.dsn = "http://key@localhost/proj"
it.setDebug(true)
it.setTransportFactory(transportFactory)
Expand Down
Loading

0 comments on commit cb4ede3

Please sign in to comment.