Skip to content

Commit

Permalink
prevents crashes due to empty Telemetry config
Browse files Browse the repository at this point in the history
  • Loading branch information
Eclion committed Aug 19, 2022
1 parent 5baf864 commit 9fc25e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
18 changes: 16 additions & 2 deletions telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package telemetry

import (
"context"
"log"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -50,6 +51,8 @@ func ConfigureSinks(cfg *config.TelemetryConfig, memSink metrics.MetricSink) (me
addSink := func(fn func(*config.TelemetryConfig, string) (metrics.MetricSink, error)) {
s, err := fn(cfg, metricsConf.HostName)
if err != nil {
log.Println("error!!!")
log.Println(err)
errors = multierror.Append(errors, err)
return
}
Expand All @@ -68,12 +71,22 @@ func ConfigureSinks(cfg *config.TelemetryConfig, memSink metrics.MetricSink) (me
if len(sinks) > 0 {
sinks = append(sinks, memSink)
_, err := metrics.NewGlobal(metricsConf, sinks)
errors = multierror.Append(errors, err)
if err != nil {
errors = multierror.Append(errors, err)
}
} else {
metricsConf.EnableHostname = false
_, err := metrics.NewGlobal(metricsConf, memSink)
errors = multierror.Append(errors, err)
if err != nil {
errors = multierror.Append(errors, err)
}
}

// if no errors where collected, the method should not return
/*if len(errors.Errors) == 0 {
errors = nil
}*/

return sinks, errors
}

Expand All @@ -94,6 +107,7 @@ func Init(cfg *config.TelemetryConfig) (*Telemetry, error) {
}

_, errs := ConfigureSinks(cfg, memSink)

if errs != nil {
return nil, errs
}
Expand Down
8 changes: 8 additions & 0 deletions telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,11 @@ func TestPrometheusMetrics(t *testing.T) {
require.Emptyf(t, missingActualMetrics, "The following metrics are missing:\n - %s", strings.Join(missingActualMetrics, "\n - "))

}

func TestInitWithEmptyConfig(t *testing.T) {
cfg := &config.Config{}
cfg.Finalize()
_, err := Init(cfg.Telemetry)
require.NoError(t, err)

}

0 comments on commit 9fc25e6

Please sign in to comment.