|
43 | 43 | import java.util.Collection; |
44 | 44 | import java.util.Iterator; |
45 | 45 | import java.util.List; |
| 46 | +import java.util.stream.Collectors; |
46 | 47 | import org.junit.jupiter.api.AfterEach; |
47 | 48 | import org.junit.jupiter.api.BeforeEach; |
48 | 49 | import org.junit.jupiter.api.Test; |
@@ -155,7 +156,7 @@ void verifyIntegration_defaultAutoDelete() throws InterruptedException { |
155 | 156 |
|
156 | 157 | assertThat(storedSpans).hasSize(2); |
157 | 158 | assertThat(storedLogs).hasSize(2); |
158 | | - assertThat(storedMetrics).hasSize(2); |
| 159 | + assertThat(filterTestMetrics(storedMetrics)).hasSize(2); |
159 | 160 |
|
160 | 161 | // Data is auto-deleted from disk |
161 | 162 | assertDirectoryFileCount(spansDir, 0); |
@@ -198,7 +199,7 @@ void verifyIntegration_withoutAutoDelete() throws InterruptedException { |
198 | 199 |
|
199 | 200 | assertThat(storedSpans).hasSize(2); |
200 | 201 | assertThat(storedLogs).hasSize(2); |
201 | | - assertThat(storedMetrics).hasSize(2); |
| 202 | + assertThat(filterTestMetrics(storedMetrics)).hasSize(2); |
202 | 203 |
|
203 | 204 | // Data stays on disk |
204 | 205 | assertDirectoryFileCount(spansDir, 2); |
@@ -253,7 +254,7 @@ void verifyIntegration_withoutAutoDelete_explicitRemove() throws InterruptedExce |
253 | 254 |
|
254 | 255 | assertThat(storedSpans).hasSize(2); |
255 | 256 | assertThat(storedLogs).hasSize(2); |
256 | | - assertThat(storedMetrics).hasSize(2); |
| 257 | + assertThat(filterTestMetrics(storedMetrics)).hasSize(2); |
257 | 258 |
|
258 | 259 | // Data explicitly cleared |
259 | 260 | assertDirectoryFileCount(spansDir, 0); |
@@ -284,6 +285,23 @@ private void createMetric() { |
284 | 285 | clearInvocations(metricCallback); |
285 | 286 | } |
286 | 287 |
|
| 288 | + private static final String TEST_METRIC_INSTRUMENTATION_SCOPE_NAME = "MetricInstrumentationScope"; |
| 289 | + |
| 290 | + /** |
| 291 | + * Filters out upstream's self-instrumentation metrics (e.g. {@code |
| 292 | + * otel.sdk.metric_reader.collection.duration}) that {@link PeriodicMetricReader} automatically |
| 293 | + * records, returning only metrics from the test's instrumentation scope. |
| 294 | + */ |
| 295 | + private static List<MetricData> filterTestMetrics(List<MetricData> metrics) { |
| 296 | + return metrics.stream() |
| 297 | + .filter( |
| 298 | + m -> |
| 299 | + m.getInstrumentationScopeInfo() |
| 300 | + .getName() |
| 301 | + .equals(TEST_METRIC_INSTRUMENTATION_SCOPE_NAME)) |
| 302 | + .collect(Collectors.toList()); |
| 303 | + } |
| 304 | + |
287 | 305 | private static void assertDirectoryFileCount(File directory, int fileCount) { |
288 | 306 | assertThat(directory).isDirectory(); |
289 | 307 | assertThat(directory.listFiles()).hasSize(fileCount); |
|
0 commit comments