|
| 1 | +package io.sentry.opentelemetry.agentless |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import io.opentelemetry.api.GlobalOpenTelemetry |
| 5 | +import io.opentelemetry.sdk.OpenTelemetrySdk |
| 6 | +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk |
| 7 | +import io.sentry.Breadcrumb |
| 8 | +import io.sentry.EventProcessor |
| 9 | +import io.sentry.Hint |
| 10 | +import io.sentry.Sentry |
| 11 | +import io.sentry.SentryEvent |
| 12 | +import io.sentry.SentryLevel |
| 13 | +import io.sentry.protocol.Message |
| 14 | +import io.sentry.test.MockSentryServer |
| 15 | +import io.sentry.test.event |
| 16 | +import io.sentry.test.hasFlag |
| 17 | +import io.sentry.test.hasMetric |
| 18 | +import io.sentry.test.hasMetricWithAttribute |
| 19 | +import io.sentry.test.metrics |
| 20 | +import io.sentry.test.profileChunk |
| 21 | +import io.sentry.test.transaction |
| 22 | +import java.util.concurrent.TimeUnit |
| 23 | +import kotlin.test.AfterTest |
| 24 | +import kotlin.test.Test |
| 25 | + |
| 26 | +/** |
| 27 | + * Sets Sentry up the way an app without the agent does: the OpenTelemetry SDK's autoconfiguration |
| 28 | + * loads Sentry's customizer, which initializes Sentry and has it trace through OpenTelemetry. |
| 29 | + */ |
| 30 | +class AgentlessEndToEndTest { |
| 31 | + private val server = MockSentryServer() |
| 32 | + private var openTelemetry: OpenTelemetrySdk? = null |
| 33 | + |
| 34 | + @AfterTest |
| 35 | + fun teardown() { |
| 36 | + openTelemetry?.close() |
| 37 | + GlobalOpenTelemetry.resetForTest() |
| 38 | + Sentry.close() |
| 39 | + server.close() |
| 40 | + SENTRY_PROPERTIES.forEach { System.clearProperty(it) } |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `Sentry set up by OpenTelemetry reports events, a profiled transaction and metrics`() { |
| 45 | + // The customizer's Sentry.init reads these as external configuration. |
| 46 | + System.setProperty("sentry.dsn", server.dsn) |
| 47 | + System.setProperty("sentry.traces-sample-rate", "1.0") |
| 48 | + System.setProperty("sentry.profile-session-sample-rate", "1.0") |
| 49 | + System.setProperty("sentry.profile-lifecycle", "TRACE") |
| 50 | + // The profiler rejects a traces path with a dot anywhere in it, which the module's absolute |
| 51 | + // path can have. |
| 52 | + System.setProperty( |
| 53 | + "sentry.profiling-traces-dir-path", |
| 54 | + "${System.getProperty("java.io.tmpdir")}/sentry-agentless-end-to-end-test", |
| 55 | + ) |
| 56 | + val openTelemetry = |
| 57 | + AutoConfiguredOpenTelemetrySdk.builder() |
| 58 | + .setResultAsGlobal() |
| 59 | + .addPropertiesSupplier { |
| 60 | + mapOf( |
| 61 | + "otel.logs.exporter" to "none", |
| 62 | + "otel.metrics.exporter" to "none", |
| 63 | + "otel.traces.exporter" to "none", |
| 64 | + ) |
| 65 | + } |
| 66 | + .build() |
| 67 | + .openTelemetrySdk |
| 68 | + this.openTelemetry = openTelemetry |
| 69 | + |
| 70 | + Sentry.configureScope { |
| 71 | + it.addEventProcessor( |
| 72 | + object : EventProcessor { |
| 73 | + override fun process(event: SentryEvent, hint: Hint): SentryEvent = event.apply { |
| 74 | + if ((level?.ordinal ?: 0) > SentryLevel.INFO.ordinal) { |
| 75 | + addBreadcrumb(Breadcrumb("Processed by the event processor")) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + ) |
| 80 | + } |
| 81 | + Sentry.withScope { |
| 82 | + it.level = SentryLevel.FATAL |
| 83 | + Sentry.captureMessage("Fatal message!") |
| 84 | + } |
| 85 | + Sentry.captureMessage("Some warning!", SentryLevel.WARNING) |
| 86 | + Sentry.addFeatureFlag("my-feature-flag", true) |
| 87 | + Sentry.setAttribute("user.type", "admin") |
| 88 | + Sentry.setAttribute("feature.version", 2) |
| 89 | + Sentry.metrics().count("countMetric") |
| 90 | + Sentry.metrics().gauge("gaugeMetric", 5.0) |
| 91 | + Sentry.metrics().distribution("distributionMetric", 7.0) |
| 92 | + Sentry.captureException(RuntimeException("Some error!")) |
| 93 | + Sentry.captureEvent( |
| 94 | + SentryEvent().apply { |
| 95 | + message = Message().apply { message = "Detailed event" } |
| 96 | + fingerprints = listOf("NewClientDebug") |
| 97 | + level = SentryLevel.DEBUG |
| 98 | + } |
| 99 | + ) |
| 100 | + repeat(10) { i -> |
| 101 | + Sentry.captureEvent( |
| 102 | + SentryEvent().apply { message = Message().apply { message = "event $i" } } |
| 103 | + ) |
| 104 | + } |
| 105 | + val transaction = Sentry.startTransaction("transaction name", "op") |
| 106 | + val span = transaction.startChild("child") |
| 107 | + Thread.sleep(100) |
| 108 | + span.finish() |
| 109 | + transaction.finish() |
| 110 | + Sentry.flush(5000) |
| 111 | + // Sentry exports OpenTelemetry spans in batches every 5 seconds. |
| 112 | + openTelemetry.sdkTracerProvider.forceFlush().join(5, TimeUnit.SECONDS) |
| 113 | + |
| 114 | + val fatal = |
| 115 | + server.awaitEnvelope { it.event()?.message?.formatted == "Fatal message!" }.event()!! |
| 116 | + assertThat(fatal.level).isEqualTo(SentryLevel.FATAL) |
| 117 | + assertThat(fatal.breadcrumbs!!.map { it.message }).contains("Processed by the event processor") |
| 118 | + |
| 119 | + val warning = |
| 120 | + server.awaitEnvelope { it.event()?.message?.formatted == "Some warning!" }.event()!! |
| 121 | + assertThat(warning.level).isEqualTo(SentryLevel.WARNING) |
| 122 | + |
| 123 | + val error = |
| 124 | + server |
| 125 | + .awaitEnvelope { it.event()?.exceptions?.any { e -> e.value == "Some error!" } == true } |
| 126 | + .event()!! |
| 127 | + assertThat(error.hasFlag("my-feature-flag", true)).isTrue() |
| 128 | + |
| 129 | + val detailed = |
| 130 | + server.awaitEnvelope { it.event()?.message?.message == "Detailed event" }.event()!! |
| 131 | + assertThat(detailed.fingerprints).containsExactly("NewClientDebug") |
| 132 | + assertThat(detailed.level).isEqualTo(SentryLevel.DEBUG) |
| 133 | + |
| 134 | + repeat(10) { i -> server.awaitEnvelope { it.event()?.message?.message == "event $i" } } |
| 135 | + |
| 136 | + val sent = |
| 137 | + server.awaitEnvelope { it.transaction()?.transaction == "transaction name" }.transaction()!! |
| 138 | + assertThat(sent.spans.map { it.op }).contains("child") |
| 139 | + val profilerId = sent.contexts.profile?.profilerId |
| 140 | + assertThat(profilerId).isNotNull() |
| 141 | + server.awaitEnvelope(timeoutMillis = 30_000) { it.profileChunk()?.profilerId == profilerId } |
| 142 | + |
| 143 | + val metrics = server.awaitEnvelope { it.metrics() != null }.metrics()!! |
| 144 | + assertThat(metrics.hasMetric("countMetric", "counter", 1.0)).isTrue() |
| 145 | + assertThat(metrics.hasMetric("gaugeMetric", "gauge", 5.0)).isTrue() |
| 146 | + assertThat(metrics.hasMetric("distributionMetric", "distribution", 7.0)).isTrue() |
| 147 | + assertThat(metrics.hasMetricWithAttribute("countMetric", "user.type", "admin")).isTrue() |
| 148 | + assertThat(metrics.hasMetricWithAttribute("countMetric", "feature.version", 2)).isTrue() |
| 149 | + } |
| 150 | + |
| 151 | + private companion object { |
| 152 | + val SENTRY_PROPERTIES = |
| 153 | + listOf( |
| 154 | + "sentry.dsn", |
| 155 | + "sentry.traces-sample-rate", |
| 156 | + "sentry.profile-session-sample-rate", |
| 157 | + "sentry.profile-lifecycle", |
| 158 | + "sentry.profiling-traces-dir-path", |
| 159 | + ) |
| 160 | + } |
| 161 | +} |
0 commit comments