elastic / apm-data

apm-data holds definitions and code for manipulating Elastic APM data
Apache License 2.0
12 stars 25 forks source link

Fix flaky TestConsumeMetricsWithOTelRemapper #312

Closed lahsivjar closed 2 months ago

lahsivjar commented 2 months ago

The flakiness in the test TestConsumeMetricsWithOTelRemapper was introduced in https://github.com/elastic/apm-data/pull/309 when we added another scope in the metric.

The root cause of the flakiness is the function eventsMatch. This function sorts the APMEvents by converting it into strings and later compares them at which point it uses protocmp.SortRepeated for metricset samples. The metricsets are created using maps (ref) which makes the order of metricsets undefined. While the tests use sorting on the metricset samples to get around this we still end up in an indeterministic state since the first sort doesn't sort on metricset samples (ref).

We can fix the eventsMatch function by first sorting the samples in each APMEvent and then sorting the APMEvent's themselves and this is what the PR does. In addition the PR also refactors the test TestConsumeMetricsWithOTelRemapper which would avoid the special issue anyway.