|
| 1 | +package com.example.androidobservability |
| 2 | + |
| 3 | +import android.app.Application |
| 4 | +import androidx.test.core.app.ApplicationProvider |
| 5 | +import com.example.androidobservability.TestUtils.waitForTelemetryData |
| 6 | +import com.launchdarkly.observability.interfaces.Metric |
| 7 | +import com.launchdarkly.observability.sdk.LDObserve |
| 8 | +import io.opentelemetry.api.common.AttributeKey |
| 9 | +import io.opentelemetry.api.common.Attributes |
| 10 | +import io.opentelemetry.api.logs.Severity |
| 11 | +import com.example.androidobservability.TestUtils.TelemetryType |
| 12 | +import junit.framework.TestCase.assertEquals |
| 13 | +import junit.framework.TestCase.assertFalse |
| 14 | +import junit.framework.TestCase.assertNotNull |
| 15 | +import junit.framework.TestCase.assertNull |
| 16 | +import junit.framework.TestCase.assertTrue |
| 17 | +import org.junit.Test |
| 18 | +import org.junit.runner.RunWith |
| 19 | +import org.robolectric.RobolectricTestRunner |
| 20 | +import org.robolectric.annotation.Config |
| 21 | +import java.util.concurrent.TimeUnit |
| 22 | + |
| 23 | +@RunWith(RobolectricTestRunner::class) |
| 24 | +@Config(application = TestApplication::class) |
| 25 | +class DisablingConfigOptionsE2ETest { |
| 26 | + |
| 27 | + private val application = ApplicationProvider.getApplicationContext<Application>() as TestApplication |
| 28 | + |
| 29 | + @Test |
| 30 | + fun `Logs should not be exported when disableLogs is set to true`() { |
| 31 | + application.pluginOptions = application.pluginOptions.copy(disableLogs = true) |
| 32 | + application.initForTest() |
| 33 | + val logsUrl = "http://localhost:${application.mockWebServer?.port}/v1/logs" |
| 34 | + |
| 35 | + triggerTestLog() |
| 36 | + LDObserve.flush() |
| 37 | + waitForTelemetryData(telemetryInspector = application.telemetryInspector, telemetryType = TelemetryType.LOGS) |
| 38 | + |
| 39 | + assertNull(application.telemetryInspector?.logExporter) |
| 40 | + assertFalse(requestsContainsUrl(logsUrl)) |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `Logs should be exported when disableLogs is set to false`() { |
| 45 | + application.pluginOptions = application.pluginOptions.copy(disableLogs = false) |
| 46 | + application.initForTest() |
| 47 | + val logsUrl = "http://localhost:${application.mockWebServer?.port}/v1/logs" |
| 48 | + |
| 49 | + triggerTestLog() |
| 50 | + LDObserve.flush() |
| 51 | + waitForTelemetryData(telemetryInspector = application.telemetryInspector, telemetryType = TelemetryType.LOGS) |
| 52 | + |
| 53 | + assertNotNull(application.telemetryInspector?.logExporter) |
| 54 | + assertTrue(requestsContainsUrl(logsUrl)) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun `Spans should NOT be exported when disableTraces is set to true`() { |
| 59 | + application.pluginOptions = application.pluginOptions.copy(disableTraces = true) |
| 60 | + application.initForTest() |
| 61 | + val tracesUrl = "http://localhost:${application.mockWebServer?.port}/v1/traces" |
| 62 | + |
| 63 | + triggerTestSpan() |
| 64 | + LDObserve.flush() |
| 65 | + waitForTelemetryData(telemetryInspector = application.telemetryInspector, telemetryType = TelemetryType.SPANS) |
| 66 | + |
| 67 | + assertNull(application.telemetryInspector?.spanExporter) |
| 68 | + assertFalse(requestsContainsUrl(tracesUrl)) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun `Spans should be exported when disableTraces is set to false`() { |
| 73 | + application.pluginOptions = application.pluginOptions.copy(disableTraces = false) |
| 74 | + application.initForTest() |
| 75 | + val tracesUrl = "http://localhost:${application.mockWebServer?.port}/v1/traces" |
| 76 | + |
| 77 | + triggerTestSpan() |
| 78 | + LDObserve.flush() |
| 79 | + waitForTelemetryData(telemetryInspector = application.telemetryInspector, telemetryType = TelemetryType.SPANS) |
| 80 | + |
| 81 | + assertNotNull(application.telemetryInspector?.spanExporter) |
| 82 | + assertTrue(requestsContainsUrl(tracesUrl)) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `Metrics should NOT be exported when disableMetrics is set to true`() { |
| 87 | + application.pluginOptions = application.pluginOptions.copy(disableMetrics = true) |
| 88 | + application.initForTest() |
| 89 | + val metricsUrl = "http://localhost:${application.mockWebServer?.port}/v1/metrics" |
| 90 | + |
| 91 | + triggerTestMetric() |
| 92 | + LDObserve.flush() |
| 93 | + waitForTelemetryData(telemetryInspector = application.telemetryInspector, telemetryType = TelemetryType.METRICS) |
| 94 | + |
| 95 | + assertNull(application.telemetryInspector?.metricExporter) |
| 96 | + assertFalse(requestsContainsUrl(metricsUrl)) |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + fun `Metrics should be exported when disableMetrics is set to false`() { |
| 101 | + application.pluginOptions = application.pluginOptions.copy(disableMetrics = false) |
| 102 | + application.initForTest() |
| 103 | + val metricsUrl = "http://localhost:${application.mockWebServer?.port}/v1/metrics" |
| 104 | + |
| 105 | + triggerTestMetric() |
| 106 | + LDObserve.flush() |
| 107 | + waitForTelemetryData(telemetryInspector = application.telemetryInspector, telemetryType = TelemetryType.METRICS) |
| 108 | + |
| 109 | + assertNotNull(application.telemetryInspector?.metricExporter) |
| 110 | + assertTrue(requestsContainsUrl(metricsUrl)) |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + fun `Errors should NOT be exported when disableErrorTracking is set to true`() { |
| 115 | + application.pluginOptions = application.pluginOptions.copy(disableErrorTracking = true) |
| 116 | + application.initForTest() |
| 117 | + val tracesUrl = "http://localhost:${application.mockWebServer?.port}/v1/traces" |
| 118 | + |
| 119 | + triggerError() |
| 120 | + LDObserve.flush() |
| 121 | + waitForTelemetryData(telemetryInspector = application.telemetryInspector, telemetryType = TelemetryType.SPANS) |
| 122 | + |
| 123 | + val spansExported = application.telemetryInspector?.spanExporter?.finishedSpanItems |
| 124 | + |
| 125 | + assertFalse(requestsContainsUrl(tracesUrl)) |
| 126 | + assertEquals(0, spansExported?.size) |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + fun `Errors should be exported when disableErrorTracking is set to false`() { |
| 131 | + application.pluginOptions = application.pluginOptions.copy(disableErrorTracking = false) |
| 132 | + application.initForTest() |
| 133 | + val tracesUrl = "http://localhost:${application.mockWebServer?.port}/v1/traces" |
| 134 | + |
| 135 | + triggerError() |
| 136 | + LDObserve.flush() |
| 137 | + waitForTelemetryData(telemetryInspector = application.telemetryInspector, telemetryType = TelemetryType.SPANS) |
| 138 | + |
| 139 | + val spansExported = application.telemetryInspector?.spanExporter?.finishedSpanItems |
| 140 | + |
| 141 | + assertTrue(requestsContainsUrl(tracesUrl)) |
| 142 | + assertEquals("highlight.error", spansExported?.get(0)?.name) |
| 143 | + assertEquals( |
| 144 | + "Test error", |
| 145 | + spansExported?.get(0)?.events?.get(0)?.attributes?.get(AttributeKey.stringKey("exception.message")) |
| 146 | + ) |
| 147 | + } |
| 148 | + |
| 149 | + private fun requestsContainsUrl(url: String): Boolean { |
| 150 | + while (true) { |
| 151 | + val request = application.mockWebServer?.takeRequest(100, TimeUnit.MILLISECONDS) |
| 152 | + if (request == null) return false |
| 153 | + if (request.requestUrl.toString() == url) return true |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private fun triggerTestLog() { |
| 158 | + LDObserve.recordLog( |
| 159 | + message = "test-log", |
| 160 | + severity = Severity.INFO, |
| 161 | + attributes = Attributes.empty() |
| 162 | + ) |
| 163 | + } |
| 164 | + |
| 165 | + private fun triggerTestSpan() { |
| 166 | + val span0 = LDObserve.startSpan( |
| 167 | + name = "test-span", |
| 168 | + attributes = Attributes.empty() |
| 169 | + ) |
| 170 | + span0.end() |
| 171 | + } |
| 172 | + |
| 173 | + private fun triggerError() { |
| 174 | + LDObserve.recordError( |
| 175 | + Error("Test error"), |
| 176 | + Attributes.of(AttributeKey.stringKey("test"), "crash_test") |
| 177 | + ) |
| 178 | + } |
| 179 | + |
| 180 | + private fun triggerTestMetric() { |
| 181 | + LDObserve.recordMetric(Metric("test", 50.0)) |
| 182 | + } |
| 183 | +} |
0 commit comments