@@ -6,13 +6,13 @@ import (
66 "testing"
77 "time"
88
9+ nodev1 "github.com/wundergraph/cosmo/router/gen/proto/wg/cosmo/node/v1"
910 "go.opentelemetry.io/otel/attribute"
1011 "go.opentelemetry.io/otel/sdk/metric"
11- "go.opentelemetry.io/otel/sdk/metric/metricdata"
1212 "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
1313
14- nodev1 "github.com/wundergraph/cosmo/router/gen/proto/wg/cosmo/node/v1"
1514 "github.com/wundergraph/cosmo/router/pkg/otel"
15+ "go.opentelemetry.io/otel/sdk/metric/metricdata"
1616
1717 "github.com/stretchr/testify/require"
1818 "go.uber.org/zap"
@@ -790,10 +790,7 @@ func TestCacheWarmup(t *testing.T) {
790790 })
791791 })
792792 })
793- }
794793
795- // Is set as Flaky so that when running the tests it will be run separately and retried if it fails
796- func TestFlakyCacheWarmupMetrics (t * testing.T ) {
797794 t .Run ("should emit planning times metrics during warmup" , func (t * testing.T ) {
798795 t .Parallel ()
799796
@@ -896,17 +893,36 @@ func TestFlakyCacheWarmupMetrics(t *testing.T) {
896893 // One when warming up the operation and one when executing the operation
897894 require .Len (t , m .Data .(metricdata.Histogram [float64 ]).DataPoints , 2 )
898895
896+ // Find data-points by matching their cache hit attribute
897+ warmupDataPoint := findDataPoint (t , m .Data .(metricdata.Histogram [float64 ]).DataPoints , false )
898+ executionDataPoint := findDataPoint (t , m .Data .(metricdata.Histogram [float64 ]).DataPoints , true )
899+
899900 // Warming up the operation
900- require .Equal (t , m . Data .(metricdata. Histogram [ float64 ]). DataPoints [ 0 ]. Count , uint64 (1 ))
901+ require .Equal (t , uint64 (1 ), warmupDataPoint . Count )
901902
902903 // Executing the operation. Is exported as an aggregated value
903- require .Equal (t , m . Data .(metricdata. Histogram [ float64 ]). DataPoints [ 1 ]. Count , uint64 (2 ))
904+ require .Equal (t , uint64 (2 ), executionDataPoint . Count )
904905
905906 // Ensure we collected non-zero planning times
906- require .Greater (t , m . Data .(metricdata. Histogram [ float64 ]). DataPoints [ 0 ] .Sum , float64 (0 ))
907- require .Greater (t , m . Data .(metricdata. Histogram [ float64 ]). DataPoints [ 1 ] .Sum , float64 (0 ))
907+ require .Greater (t , warmupDataPoint .Sum , float64 (0 ))
908+ require .Greater (t , executionDataPoint .Sum , float64 (0 ))
908909
909910 metricdatatest .AssertEqual (t , operationPlanningTimeMetric , m , metricdatatest .IgnoreTimestamp (), metricdatatest .IgnoreValue ())
910911 })
911912 })
912913}
914+
915+ // findDataPoint finds a data point in a slice of histogram data points by matching
916+ // the value of WgEnginePlanCacheHit attribute
917+ func findDataPoint (t * testing.T , dataPoints []metricdata.HistogramDataPoint [float64 ], cacheHit bool ) metricdata.HistogramDataPoint [float64 ] {
918+ t .Helper ()
919+ for _ , dp := range dataPoints {
920+ // Get the value of the WgEnginePlanCacheHit attribute
921+ val , found := dp .Attributes .Value (otel .WgEnginePlanCacheHit )
922+ if found && val .AsBool () == cacheHit {
923+ return dp
924+ }
925+ }
926+ t .Fatalf ("Could not find data point with WgEnginePlanCacheHit=%v" , cacheHit )
927+ return metricdata.HistogramDataPoint [float64 ]{}
928+ }
0 commit comments