-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSamplingE2ETest.kt
More file actions
224 lines (198 loc) · 8.1 KB
/
SamplingE2ETest.kt
File metadata and controls
224 lines (198 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.example.androidobservability
import android.app.Application
import androidx.test.core.app.ApplicationProvider
import com.example.androidobservability.TestUtils.TelemetryType
import com.example.androidobservability.TestUtils.waitForTelemetryData
import com.launchdarkly.observability.api.ObservabilityOptions
import com.launchdarkly.observability.client.TelemetryInspector
import com.launchdarkly.observability.sdk.LDObserve
import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.api.logs.Severity
import junit.framework.TestCase.assertEquals
import kotlinx.coroutines.test.runTest
import org.junit.Ignore
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
/**
* End-to-end tests for the LaunchDarkly Observability SDK's sampling functionality.
*
* This test suite validates that telemetry data (logs and spans) are correctly filtered
* based on sampling configuration rules. The sampling configuration is dynamically injected
* into the TestApplication through a mock web server that serves the configuration from
* `get_sampling_config_response.json` in the test resources, simulating real-world SDK behavior.
*
* **Sampling Logic:**
* - `samplingRatio: 0.0` = All matching telemetry is discarded (0% pass through)
* - `samplingRatio: 1.0` = All matching telemetry passes through (100% pass through)
* - Telemetry that doesn't match any sampling rules passes through by default
*
* **Test Coverage:**
* - Log sampling by severity, message content, regex patterns, and attributes
* - Span sampling by name, events, attributes, and complex rule combinations
* - Verification that only expected telemetry reaches the exporters
*
*/
@RunWith(RobolectricTestRunner::class)
@Config(application = TestApplication::class)
class SamplingE2ETest {
@get:Rule
val testCoroutineRule = TestCoroutineRule()
private val application = ApplicationProvider.getApplicationContext<Application>() as TestApplication
private var telemetryInspector: TelemetryInspector? = null
@Before
fun setUp() {
application.observabilityOptions = getOptionsAllEnabled()
application.initForTest()
telemetryInspector = application.telemetryInspector
}
@Test
fun `should avoid exporting logs matching sampling configuration for logs`() = runTest {
triggerLogs()
telemetryInspector?.logExporter?.flush()
waitForTelemetryData(telemetryInspector = application.telemetryInspector, telemetryType = TelemetryType.LOGS)
val logsExported = telemetryInspector?.logExporter?.finishedLogRecordItems?.map {
it.bodyValue?.value.toString()
}
// Only first and final logs should be exported
assertEquals(2, logsExported?.size)
assertEquals("first-log", logsExported?.get(0))
assertEquals("final-log", logsExported?.get(1))
}
@Test
@Ignore
fun `should avoid exporting spans matching sampling configuration for spans`() = runTest {
triggerSpans()
telemetryInspector?.spanExporter?.flush()
waitForTelemetryData(telemetryInspector = application.telemetryInspector, telemetryType = TelemetryType.SPANS)
val spansExported = telemetryInspector?.spanExporter?.finishedSpanItems?.map {
it.name
}
// Only first and final spans should be exported
assertEquals(2, spansExported?.size)
assertEquals("first-span", spansExported?.get(0))
assertEquals("final-span", spansExported?.get(1))
}
private fun triggerLogs() {
// Log 0: Should NOT be sampled (doesn't match any config)
LDObserve.recordLog(
message = "first-log",
severity = Severity.INFO,
attributes = Attributes.empty()
)
// Log 1: Matches severity "ERROR"
LDObserve.recordLog(
message = "log1",
severity = Severity.ERROR,
attributes = Attributes.empty()
)
// Log 2: Matches message "Connection failed"
LDObserve.recordLog(
message = "Connection failed",
severity = Severity.INFO,
attributes = Attributes.empty()
)
// Log 3: Matches message regex "Error: .*"
LDObserve.recordLog(
message = "Error: HTTP connection failed",
severity = Severity.INFO,
attributes = Attributes.empty()
)
// Log 4: Matches complex config with specific attributes
LDObserve.recordLog(
message = "Database connection failed",
severity = Severity.WARN,
attributes = Attributes.of(
AttributeKey.stringKey("service.name"), "db-primary",
AttributeKey.booleanKey("retry.enabled"), true,
AttributeKey.longKey("retry.count"), 3L,
AttributeKey.doubleKey("retry.timeout"), 15.5
)
)
// Log 5: Should NOT be sampled (doesn't match any config)
LDObserve.recordLog(
message = "final-log",
severity = Severity.INFO,
attributes = Attributes.empty()
)
}
private fun triggerSpans() {
// Span 0: Should NOT be sampled (doesn't match any config)
val span0 = LDObserve.startSpan(
name = "first-span",
attributes = Attributes.empty()
)
span0.end()
// Span 1: Matches name "test-span" with samplingRatio 0 (should be discarded)
val span1 = LDObserve.startSpan(
name = "test-span",
attributes = Attributes.empty()
)
span1.end()
// Span 2: Matches name regex "test-span-\\d+" with samplingRatio 0 (should be discarded)
val span2 = LDObserve.startSpan(
name = "test-span-1",
attributes = Attributes.empty()
)
span2.end()
// Span 3: Has event "test-event" with samplingRatio 0 (should be discarded)
val span3 = LDObserve.startSpan(
name = "span-with-event",
attributes = Attributes.empty()
)
span3.addEvent("test-event")
span3.end()
// Span 4: Has event matching regex "test-event-\\d+" with samplingRatio 0 (should be discarded)
val span4 = LDObserve.startSpan(
name = "span-with-numbered-event",
attributes = Attributes.empty()
)
span4.addEvent("test-event-42")
span4.end()
// Span 5: Has attribute http.method=POST with samplingRatio 0 (should be discarded)
val span5 = LDObserve.startSpan(
name = "http-request",
attributes = Attributes.of(
AttributeKey.stringKey("http.method"), "POST"
)
)
span5.end()
// Span 6: Has event with specific attributes with samplingRatio 0 (should be discarded)
val span6 = LDObserve.startSpan(
name = "error-span",
attributes = Attributes.empty()
)
span6.addEvent(
"error-occurred",
Attributes.of(
AttributeKey.stringKey("error.type"), "network",
AttributeKey.stringKey("db.error"), "Database connection failed",
AttributeKey.longKey("error.code"), 503L
)
)
span6.end()
// Span 7: Should NOT be sampled (doesn't match any config)
val span7 = LDObserve.startSpan(
name = "final-span",
attributes = Attributes.empty()
)
span7.end()
}
private fun getOptionsAllEnabled(): ObservabilityOptions {
return ObservabilityOptions(
debug = true,
logsApiLevel = ObservabilityOptions.LogLevel.TRACE,
tracesApi = ObservabilityOptions.TracesApi.enabled(),
metricsApi = ObservabilityOptions.MetricsApi.enabled(),
instrumentations = ObservabilityOptions.Instrumentations(
crashReporting = true,
activityLifecycle = true,
launchTime = true
)
)
}
}