@@ -19,6 +19,7 @@ import (
19
19
"encoding/json"
20
20
"fmt"
21
21
"reflect"
22
+ "regexp"
22
23
"sort"
23
24
"strconv"
24
25
"strings"
@@ -440,26 +441,29 @@ const (
440
441
// MetricsEndpoint attempts gets the host and port number from the host address without doing any validation regarding the
441
442
// address itself.
442
443
// It works even before env var expansion happens, when a simple `net.SplitHostPort` would fail because of the extra colon
443
- // from the env var, i.e. the address looks like "${env:POD_IP}:4317" or "${env:POD_IP}".
444
+ // from the env var, i.e. the address looks like "${env:POD_IP}:4317", "${env:POD_IP}", or "${ POD_IP}".
444
445
// It does not work in cases which the port itself is a variable, i.e. "${env:POD_IP}:${env:PORT}".
445
- // It does not work for IPv6 hostnames/ addresses.
446
+ // It does not work for IPv6 addresses.
446
447
func (s * Service ) MetricsEndpoint (logger logr.Logger ) (string , int32 ) {
447
448
telemetry := s .GetTelemetry ()
448
- if telemetry == nil {
449
+ if telemetry == nil || telemetry . Metrics . Address == "" {
449
450
return defaultServiceHost , defaultServicePort
450
451
}
451
- splitAddress := strings .Split (telemetry .Metrics .Address , ":" )
452
- if len (splitAddress ) == 1 {
453
- return defaultServiceHost , defaultServicePort
452
+
453
+ explicitPortMatches := regexp .MustCompile (`:(\d+$)` ).FindStringSubmatch (telemetry .Metrics .Address )
454
+ if len (explicitPortMatches ) <= 1 {
455
+ return telemetry .Metrics .Address , defaultServicePort
454
456
}
455
- port , err := strconv .ParseInt (splitAddress [len (splitAddress )- 1 ], 10 , 32 )
457
+
458
+ port , err := strconv .ParseInt (explicitPortMatches [1 ], 10 , 32 )
456
459
if err != nil {
457
460
errMsg := fmt .Sprintf ("couldn't determine metrics port from configuration, using default: %s:%d" ,
458
461
defaultServiceHost , defaultServicePort )
459
462
logger .Info (errMsg , "error" , err )
460
463
return defaultServiceHost , defaultServicePort
461
464
}
462
- host := strings .Join (splitAddress [0 :len (splitAddress )- 1 ], ":" )
465
+
466
+ host , _ , _ := strings .Cut (telemetry .Metrics .Address , explicitPortMatches [0 ])
463
467
return host , int32 (port )
464
468
}
465
469
0 commit comments