|
1 | 1 | package io.sentry |
2 | 2 |
|
3 | 3 | import kotlin.test.Test |
| 4 | +import kotlin.test.assertEquals |
4 | 5 | import kotlin.test.assertFalse |
| 6 | +import kotlin.test.assertNotEquals |
5 | 7 | import kotlin.test.assertNotNull |
6 | 8 | import kotlin.test.assertTrue |
7 | 9 |
|
@@ -42,4 +44,99 @@ class PropagationContextTest { |
42 | 44 | assertTrue(propagationContext.baggage.isMutable) |
43 | 45 | assertFalse(propagationContext.baggage.isShouldFreeze) |
44 | 46 | } |
| 47 | + |
| 48 | + // Decision matrix tests for shouldContinueTrace |
| 49 | + |
| 50 | + private val incomingTraceId = "bc6d53f15eb88f4320054569b8c553d4" |
| 51 | + private val sentryTrace = "bc6d53f15eb88f4320054569b8c553d4-b72fa28504b07285-1" |
| 52 | + |
| 53 | + private fun makeOptions(dsnOrgId: String?, explicitOrgId: String? = null, strict: Boolean = false): SentryOptions { |
| 54 | + val options = SentryOptions() |
| 55 | + if (dsnOrgId != null) { |
| 56 | + options.dsn = "https://key@o$dsnOrgId.ingest.sentry.io/123" |
| 57 | + } else { |
| 58 | + options.dsn = "https://key@sentry.io/123" |
| 59 | + } |
| 60 | + options.orgId = explicitOrgId |
| 61 | + options.isStrictTraceContinuation = strict |
| 62 | + return options |
| 63 | + } |
| 64 | + |
| 65 | + private fun makeBaggage(orgId: String?): String { |
| 66 | + val parts = mutableListOf("sentry-trace_id=$incomingTraceId") |
| 67 | + if (orgId != null) { |
| 68 | + parts.add("sentry-org_id=$orgId") |
| 69 | + } |
| 70 | + return parts.joinToString(",") |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun `strict=false, matching orgs - continues trace`() { |
| 75 | + val options = makeOptions(dsnOrgId = "1", strict = false) |
| 76 | + val pc = PropagationContext.fromHeaders(NoOpLogger.getInstance(), sentryTrace, makeBaggage("1"), options) |
| 77 | + assertEquals(incomingTraceId, pc.traceId.toString()) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun `strict=false, baggage missing org - continues trace`() { |
| 82 | + val options = makeOptions(dsnOrgId = "1", strict = false) |
| 83 | + val pc = PropagationContext.fromHeaders(NoOpLogger.getInstance(), sentryTrace, makeBaggage(null), options) |
| 84 | + assertEquals(incomingTraceId, pc.traceId.toString()) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun `strict=false, sdk missing org - continues trace`() { |
| 89 | + val options = makeOptions(dsnOrgId = null, strict = false) |
| 90 | + val pc = PropagationContext.fromHeaders(NoOpLogger.getInstance(), sentryTrace, makeBaggage("1"), options) |
| 91 | + assertEquals(incomingTraceId, pc.traceId.toString()) |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun `strict=false, both missing org - continues trace`() { |
| 96 | + val options = makeOptions(dsnOrgId = null, strict = false) |
| 97 | + val pc = PropagationContext.fromHeaders(NoOpLogger.getInstance(), sentryTrace, makeBaggage(null), options) |
| 98 | + assertEquals(incomingTraceId, pc.traceId.toString()) |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun `strict=false, mismatched orgs - starts new trace`() { |
| 103 | + val options = makeOptions(dsnOrgId = "2", strict = false) |
| 104 | + val pc = PropagationContext.fromHeaders(NoOpLogger.getInstance(), sentryTrace, makeBaggage("1"), options) |
| 105 | + assertNotEquals(incomingTraceId, pc.traceId.toString()) |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + fun `strict=true, matching orgs - continues trace`() { |
| 110 | + val options = makeOptions(dsnOrgId = "1", strict = true) |
| 111 | + val pc = PropagationContext.fromHeaders(NoOpLogger.getInstance(), sentryTrace, makeBaggage("1"), options) |
| 112 | + assertEquals(incomingTraceId, pc.traceId.toString()) |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + fun `strict=true, baggage missing org - starts new trace`() { |
| 117 | + val options = makeOptions(dsnOrgId = "1", strict = true) |
| 118 | + val pc = PropagationContext.fromHeaders(NoOpLogger.getInstance(), sentryTrace, makeBaggage(null), options) |
| 119 | + assertNotEquals(incomingTraceId, pc.traceId.toString()) |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun `strict=true, sdk missing org - starts new trace`() { |
| 124 | + val options = makeOptions(dsnOrgId = null, strict = true) |
| 125 | + val pc = PropagationContext.fromHeaders(NoOpLogger.getInstance(), sentryTrace, makeBaggage("1"), options) |
| 126 | + assertNotEquals(incomingTraceId, pc.traceId.toString()) |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + fun `strict=true, both missing org - continues trace`() { |
| 131 | + val options = makeOptions(dsnOrgId = null, strict = true) |
| 132 | + val pc = PropagationContext.fromHeaders(NoOpLogger.getInstance(), sentryTrace, makeBaggage(null), options) |
| 133 | + assertEquals(incomingTraceId, pc.traceId.toString()) |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + fun `strict=true, mismatched orgs - starts new trace`() { |
| 138 | + val options = makeOptions(dsnOrgId = "2", strict = true) |
| 139 | + val pc = PropagationContext.fromHeaders(NoOpLogger.getInstance(), sentryTrace, makeBaggage("1"), options) |
| 140 | + assertNotEquals(incomingTraceId, pc.traceId.toString()) |
| 141 | + } |
45 | 142 | } |
0 commit comments