@@ -16,9 +16,12 @@ package e2e
1616import (
1717 "bytes"
1818 "context"
19+ "encoding/json"
1920 "errors"
2021 "fmt"
2122 "os"
23+ "regexp"
24+ "strconv"
2225 "strings"
2326 "testing"
2427 "time"
@@ -76,18 +79,16 @@ func FetchCatalogdMetricsExportedEndpoint(t *testing.T, testName string) {
7679 mtc .run ()
7780}
7881
79- // fetchMetrics retrieves Prometheus metrics from the endpoint
80- func (c * MetricsTestConfig ) fetchMetrics (ctx context.Context , token string ) string {
82+ // fetchMetrics retrieves only specific Prometheus metrics
83+ func (c * MetricsTestConfig ) fetchMetrics (ctx context.Context , token string ) map [ string ] float64 {
8184 c .t .Log ("Fetching Prometheus metrics after test execution" )
8285
83- // Command to execute inside the pod
8486 cmd := []string {
8587 "curl" , "-s" , "-k" ,
8688 "-H" , "Authorization: Bearer " + token ,
8789 c .metricsURL ,
8890 }
8991
90- // Execute command in pod
9192 req := c .kubeClient .CoreV1 ().RESTClient ().
9293 Post ().
9394 Resource ("pods" ).
@@ -115,21 +116,54 @@ func (c *MetricsTestConfig) fetchMetrics(ctx context.Context, token string) stri
115116 err = executor .StreamWithContext (ctx , streamOpts )
116117 require .NoError (c .t , err , "Error streaming exec request: %v" , stderr .String ())
117118
118- return stdout .String ()
119+ return parseMetrics ( stdout .String () )
119120}
120121
121- // saveMetricsToFile writes the fetched metrics to a text file
122- func (c * MetricsTestConfig ) saveMetricsToFile (metrics string ) {
122+ // parseMetrics extracts only the required metrics from Prometheus response
123+ func parseMetrics (metricsText string ) map [string ]float64 {
124+ relevantMetrics := []string {
125+ "controller_runtime_reconcile_time_seconds_sum" ,
126+ "catalogd_http_request_duration_seconds_sum" ,
127+ "controller_runtime_reconcile_total" ,
128+ "go_cpu_classes_gc_total_cpu_seconds_total" ,
129+ "go_cpu_classes_idle_cpu_seconds_total" ,
130+ "controller_runtime_reconcile_errors_total" ,
131+ "controller_runtime_webhook_latency_seconds" ,
132+ }
133+
134+ mapMetrics := make (map [string ]float64 )
135+ for _ , metric := range relevantMetrics {
136+ regex := regexp .MustCompile (metric + `\s+([0-9.]+)` )
137+ match := regex .FindStringSubmatch (metricsText )
138+
139+ if len (match ) == 2 {
140+ v , err := strconv .ParseFloat (match [1 ], 64 )
141+ if err == nil {
142+ mapMetrics [metric ] = v
143+ }
144+ }
145+ }
146+
147+ return mapMetrics
148+ }
149+
150+ // storeMetricsResult writes only relevant metrics to a JSON file
151+ func (c * MetricsTestConfig ) storeMetricsResult (metrics map [string ]float64 ) {
123152 dir := "results"
124153 if err := os .MkdirAll (dir , os .ModePerm ); err != nil {
125154 c .t .Fatalf ("Failed to create directory %s: %v" , dir , err )
126155 }
127156
128- filePath := fmt .Sprintf ("%s/metrics_%s.txt" , dir , c .name )
129- err := os .WriteFile (filePath , []byte (metrics ), 0644 )
157+ filePath := fmt .Sprintf ("%s/metrics_%s.json" , dir , c .name )
158+ jsonData , err := json .MarshalIndent (metrics , "" , " " )
159+ if err != nil {
160+ c .t .Fatalf ("Failed to encode metrics as JSON: %v" , err )
161+ }
162+
163+ err = os .WriteFile (filePath , jsonData , 0644 )
130164 require .NoError (c .t , err , "Failed to save metrics to file" )
131165
132- c .t .Logf ("Metrics saved to: %s" , filePath )
166+ c .t .Logf ("Filtered metrics saved to: %s" , filePath )
133167}
134168
135169func findK8sClient (t * testing.T ) (kubernetes.Interface , * rest.Config ) {
@@ -203,7 +237,7 @@ func (c *MetricsTestConfig) run() {
203237
204238 // Fetch and save Prometheus metrics after test execution
205239 metrics := c .fetchMetrics (ctx , token )
206- c .saveMetricsToFile (metrics )
240+ c .storeMetricsResult (metrics )
207241}
208242
209243// createMetricsClusterRoleBinding to bind the cluster role so metrics are accessible
0 commit comments