11package main
22
33import (
4+ "bytes"
45 "cmp"
56 "encoding/json"
67 "io"
@@ -13,6 +14,8 @@ import (
1314 "time"
1415
1516 "github.com/mccutchen/go-httpbin/v2/httpbin"
17+ io_prometheus_client "github.com/prometheus/client_model/go"
18+ "github.com/prometheus/common/expfmt"
1619 "github.com/stretchr/testify/require"
1720)
1821
@@ -371,22 +374,33 @@ func TestIntegration(t *testing.T) {
371374 body , err := io .ReadAll (resp .Body )
372375 require .NoError (t , err )
373376 lastStatsOutput = string (body )
374- for line := range strings .Lines (lastStatsOutput ) {
375- line = strings .TrimSpace (line )
376- if ! strings .HasPrefix (line , "route_latency_ms_count" ) {
377+
378+ decoder := expfmt .NewDecoder (bytes .NewReader (body ), expfmt .NewFormat (expfmt .TypeTextPlain ))
379+ for {
380+ var metricFamily io_prometheus_client.MetricFamily
381+ err := decoder .Decode (& metricFamily )
382+ if err == io .EOF {
383+ break
384+ }
385+ require .NoError (t , err )
386+
387+ if metricFamily .GetName () != "route_latency_ms" {
377388 continue
378389 }
379- t .Logf ("found route_latency_ms metric line: %s" , line )
380- parts := strings .Fields (line )
381- require .Equal (t , 2 , len (parts ), "expected 2 fields in route_latency_ms metric line" )
382- require .Equal (t , `route_latency_ms_count{version="v1.0.0",route_name="catch_all"}` , parts [0 ], "unexpected metric name and labels" )
383- count , err := strconv .Atoi (parts [1 ])
384- require .NoError (t , err , "expected integer count in route_latency_ms metric line" )
385- if count >= 1 {
386- return true
390+ for _ , metric := range metricFamily .GetMetric () {
391+ hist := metric .GetHistogram ()
392+ require .NotNil (t , hist )
393+ labels := make (map [string ]string )
394+ for _ , label := range metric .GetLabel () {
395+ labels [label .GetName ()] = label .GetValue ()
396+ }
397+ require .Equal (t , map [string ]string {"version" : "v1.0.0" , "route_name" : "catch_all" }, labels )
398+ if hist .GetSampleCount () > 0 {
399+ return true
400+ }
387401 }
388402 }
389- t .Logf ("route_latency_ms metric not found" )
403+ t .Logf ("route_latency_ms metric not found or no samples yet " )
390404 return false
391405 }, 5 * time .Second , 200 * time .Millisecond )
392406 })
0 commit comments