@@ -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"
@@ -77,17 +80,15 @@ func FetchCatalogdMetricsExportedEndpoint(t *testing.T) {
7780}
7881
7982// fetchMetrics retrieves Prometheus metrics from the endpoint
80- func (c * MetricsTestConfig ) fetchMetrics (ctx context.Context , token string ) string {
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,60 @@ 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+ "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+ lines := strings .Split (metricsText , "\n " )
136+ for _ , line := range lines {
137+ // Check for partial matches
138+ for _ , partial := range relevantMetrics {
139+ if strings .Contains (line , partial ) {
140+ re := regexp .MustCompile (`(\S+)\s+([0-9.]+)` )
141+ match := re .FindStringSubmatch (line )
142+ if len (match ) == 3 {
143+ metricName := match [1 ]
144+ value , err := strconv .ParseFloat (match [2 ], 64 )
145+ if err == nil {
146+ mapMetrics [metricName ] = value
147+ }
148+ }
149+ }
150+ }
151+ }
152+
153+ return mapMetrics
154+ }
155+
156+ // storeMetricsResult writes only relevant metrics to a JSON file
157+ func (c * MetricsTestConfig ) storeMetricsResult (metrics map [string ]float64 ) {
123158 dir := "results"
124159 if err := os .MkdirAll (dir , os .ModePerm ); err != nil {
125160 c .t .Fatalf ("Failed to create directory %s: %v" , dir , err )
126161 }
127162
128163 filePath := fmt .Sprintf ("%s/metrics_%s_%s.txt" , dir , c .name , c .t .Name ())
129- err := os .WriteFile (filePath , []byte (metrics ), 0644 )
164+ jsonData , err := json .MarshalIndent (metrics , "" , " " )
165+ if err != nil {
166+ c .t .Fatalf ("Failed to encode metrics as JSON: %v" , err )
167+ }
168+
169+ err = os .WriteFile (filePath , jsonData , 0644 )
130170 require .NoError (c .t , err , "Failed to save metrics to file" )
131171
132- c .t .Logf ("Metrics saved to: %s" , filePath )
172+ c .t .Logf ("Filtered metrics saved to: %s" , filePath )
133173}
134174
135175func findK8sClient (t * testing.T ) (kubernetes.Interface , * rest.Config ) {
@@ -193,8 +233,7 @@ func NewMetricsTestConfig(
193233// run executes the entire test flow
194234func (c * MetricsTestConfig ) run () {
195235 ctx := context .Background ()
196- // To speed up
197- // defer c.cleanup(ctx)
236+ defer c .cleanup (ctx )
198237 c .createMetricsClusterRoleBinding (ctx )
199238 token := c .getServiceAccountToken (ctx )
200239 c .createCurlMetricsPod (ctx )
@@ -204,7 +243,7 @@ func (c *MetricsTestConfig) run() {
204243
205244 // Fetch and save Prometheus metrics after test execution
206245 metrics := c .fetchMetrics (ctx , token )
207- c .saveMetricsToFile (metrics )
246+ c .storeMetricsResult (metrics )
208247}
209248
210249// createMetricsClusterRoleBinding to bind the cluster role so metrics are accessible
0 commit comments