@@ -18,7 +18,6 @@ import (
18
18
"bytes"
19
19
"encoding/json"
20
20
"fmt"
21
- "net"
22
21
"reflect"
23
22
"sort"
24
23
"strconv"
@@ -269,7 +268,7 @@ func (c *Config) getEnvironmentVariablesForComponentKinds(logger logr.Logger, co
269
268
270
269
// applyDefaultForComponentKinds applies defaults to the endpoints for the given ComponentKind(s).
271
270
func (c * Config ) applyDefaultForComponentKinds (logger logr.Logger , componentKinds ... ComponentKind ) error {
272
- if err := c .Service .ApplyDefaults (); err != nil {
271
+ if err := c .Service .ApplyDefaults (logger ); err != nil {
273
272
return err
274
273
}
275
274
enabledComponents := c .GetEnabledComponents ()
@@ -427,59 +426,46 @@ type Service struct {
427
426
Pipelines map [string ]* Pipeline `json:"pipelines" yaml:"pipelines"`
428
427
}
429
428
430
- const defaultServicePort int32 = 8888
429
+ type serviceParseError string
431
430
432
- // MetricsEndpoint gets the port number and host address for the metrics endpoint from the collector config if it has been set.
433
- func (s * Service ) MetricsEndpoint () (string , int32 , error ) {
434
- defaultAddr := "0.0.0.0"
435
- if s .GetTelemetry () == nil {
436
- // telemetry isn't set, use the default
437
- return defaultAddr , defaultServicePort , nil
438
- }
439
- host , port , netErr := net .SplitHostPort (s .GetTelemetry ().Metrics .Address )
440
- if netErr != nil && strings .Contains (netErr .Error (), "missing port in address" ) {
441
- return defaultAddr , defaultServicePort , nil
442
- } else if netErr != nil {
443
- return "" , 0 , netErr
444
- }
445
- i64 , err := strconv .ParseInt (port , 10 , 32 )
446
- if err != nil {
447
- return "" , 0 , err
448
- }
449
-
450
- if host == "" {
451
- host = defaultAddr
452
- }
453
-
454
- return host , int32 (i64 ), nil
431
+ func (s serviceParseError ) Error () string {
432
+ return string (s )
455
433
}
456
434
457
- // NaivePort attempts gets the port number from the host address without doing any validation regarding the address itself.
435
+ const (
436
+ defaultServicePort int32 = 8888
437
+ defaultServiceHost = "0.0.0.0"
438
+ )
439
+
440
+ // MetricsEndpoint attempts gets the host and port number from the host address without doing any validation regarding the
441
+ // address itself.
458
442
// It works even before env var expansion happens, when a simple `net.SplitHostPort` would fail because of the extra colon
459
443
// from the env var, i.e. the address looks like "${env:POD_IP}:4317" or "${env:POD_IP}".
460
444
// It does not work in cases which the port itself is a variable, i.e. "${env:POD_IP}:${env:PORT}".
461
- func (s * Service ) NaivePort () (int32 , error ) {
445
+ // It does not work for IPv6 hostnames/addresses.
446
+ func (s * Service ) MetricsEndpoint (logger logr.Logger ) (string , int32 ) {
462
447
telemetry := s .GetTelemetry ()
463
448
if telemetry == nil {
464
- return defaultServicePort , nil
449
+ return defaultServiceHost , defaultServicePort
465
450
}
466
451
splitAddress := strings .Split (telemetry .Metrics .Address , ":" )
467
452
if len (splitAddress ) == 1 {
468
- return defaultServicePort , nil
453
+ return defaultServiceHost , defaultServicePort
469
454
}
470
- port , err := strconv .Atoi (splitAddress [len (splitAddress )- 1 ])
455
+ port , err := strconv .ParseInt (splitAddress [len (splitAddress )- 1 ], 10 , 32 )
471
456
if err != nil {
472
- return 0 , err
457
+ errMsg := fmt .Sprintf ("couldn't determine metrics port from configuration, using default: %s:%d" ,
458
+ defaultServiceHost , defaultServicePort )
459
+ logger .Info (errMsg , "error" , err )
460
+ return defaultServiceHost , defaultServicePort
473
461
}
474
- return int32 (port ), nil
462
+ host := strings .Join (splitAddress [0 :len (splitAddress )- 1 ], ":" )
463
+ return host , int32 (port )
475
464
}
476
465
477
466
// ApplyDefaults inserts configuration defaults if it has not been set.
478
- func (s * Service ) ApplyDefaults () error {
479
- telemetryAddr , telemetryPort , err := s .MetricsEndpoint ()
480
- if err != nil {
481
- return err
482
- }
467
+ func (s * Service ) ApplyDefaults (logger logr.Logger ) error {
468
+ telemetryAddr , telemetryPort := s .MetricsEndpoint (logger )
483
469
tm := & AnyConfig {
484
470
Object : map [string ]interface {}{
485
471
"metrics" : map [string ]interface {}{
0 commit comments