diff --git a/cmd/jaeger/internal/all-in-one.yaml b/cmd/jaeger/internal/all-in-one.yaml index dce7f44ddd4..db355a2d55f 100644 --- a/cmd/jaeger/internal/all-in-one.yaml +++ b/cmd/jaeger/internal/all-in-one.yaml @@ -46,7 +46,7 @@ extensions: grpc: expvar: - port: 27777 + endpoint: "${env:JAEGER_LISTEN_HOST:-localhost}:27777" receivers: otlp: diff --git a/cmd/jaeger/internal/extension/expvar/config.go b/cmd/jaeger/internal/extension/expvar/config.go index 8f793570b7f..30fcec1f1ee 100644 --- a/cmd/jaeger/internal/extension/expvar/config.go +++ b/cmd/jaeger/internal/extension/expvar/config.go @@ -5,10 +5,11 @@ package expvar import ( "github.com/asaskevich/govalidator" + "go.opentelemetry.io/collector/config/confighttp" ) type Config struct { - Port int `mapstructure:"port"` + confighttp.ServerConfig `mapstructure:",squash"` } func (cfg *Config) Validate() error { diff --git a/cmd/jaeger/internal/extension/expvar/extension.go b/cmd/jaeger/internal/extension/expvar/extension.go index 74dd41cc51c..4a90338e60f 100644 --- a/cmd/jaeger/internal/extension/expvar/extension.go +++ b/cmd/jaeger/internal/extension/expvar/extension.go @@ -8,8 +8,9 @@ import ( "errors" "expvar" "fmt" + "net" "net/http" - "time" + "sync" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componentstatus" @@ -27,6 +28,8 @@ type expvarExtension struct { config *Config server *http.Server telset component.TelemetrySettings + + shutdownWG sync.WaitGroup } func newExtension(config *Config, telset component.TelemetrySettings) *expvarExtension { @@ -36,20 +39,26 @@ func newExtension(config *Config, telset component.TelemetrySettings) *expvarExt } } -func (c *expvarExtension) Start(_ context.Context, host component.Host) error { - c.server = &http.Server{ - Addr: fmt.Sprintf(":%d", c.config.Port), - Handler: expvar.Handler(), - ReadHeaderTimeout: 3 * time.Second, +func (c *expvarExtension) Start(ctx context.Context, host component.Host) error { + server, err := c.config.ToServer(ctx, host, c.telset, expvar.Handler()) + if err != nil { + return err + } + c.server = server + var hln net.Listener + if hln, err = c.config.ToListener(ctx); err != nil { + return err } - c.telset.Logger.Info("Starting expvar server", zap.String("addr", c.server.Addr)) + c.telset.Logger.Info("Starting expvar server", zap.Stringer("addr", hln.Addr())) + c.shutdownWG.Add(1) go func() { - if err := c.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + defer c.shutdownWG.Done() + + if err := c.server.Serve(hln); err != nil && !errors.Is(err, http.ErrServerClosed) { err = fmt.Errorf("error starting expvar server: %w", err) componentstatus.ReportStatus(host, componentstatus.NewFatalErrorEvent(err)) } }() - return nil } @@ -58,6 +67,7 @@ func (c *expvarExtension) Shutdown(ctx context.Context) error { if err := c.server.Shutdown(ctx); err != nil { return fmt.Errorf("error shutting down expvar server: %w", err) } + c.shutdownWG.Wait() } return nil } diff --git a/cmd/jaeger/internal/extension/expvar/extension_test.go b/cmd/jaeger/internal/extension/expvar/extension_test.go index 5a61ebc66b7..057baf090cb 100644 --- a/cmd/jaeger/internal/extension/expvar/extension_test.go +++ b/cmd/jaeger/internal/extension/expvar/extension_test.go @@ -13,6 +13,8 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/extension/storage/storagetest" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/config/configauth" + "go.opentelemetry.io/collector/config/confighttp" "go.uber.org/zap/zaptest" ) @@ -30,7 +32,9 @@ func TestExpvarExtension(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { config := &Config{ - Port: Port, + ServerConfig: confighttp.ServerConfig{ + Endpoint: "0.0.0.0:27777", + }, } s := newExtension(config, component.TelemetrySettings{ Logger: zaptest.NewLogger(t), @@ -51,3 +55,21 @@ func TestExpvarExtension(t *testing.T) { }) } } + +func TestExpvarExtension_StartError(t *testing.T) { + config := &Config{ + ServerConfig: confighttp.ServerConfig{ + Endpoint: "0.0.0.0:27777", + Auth: &confighttp.AuthConfig{ + Authentication: configauth.Authentication{ + AuthenticatorID: component.MustNewID("invalid_auth"), + }, + }, + }, + } + s := newExtension(config, component.TelemetrySettings{ + Logger: zaptest.NewLogger(t), + }) + err := s.Start(context.Background(), storagetest.NewStorageHost()) + require.ErrorContains(t, err, "invalid_auth") +} diff --git a/cmd/jaeger/internal/extension/expvar/factory.go b/cmd/jaeger/internal/extension/expvar/factory.go index 1236128a9d9..a70f94cf4bb 100644 --- a/cmd/jaeger/internal/extension/expvar/factory.go +++ b/cmd/jaeger/internal/extension/expvar/factory.go @@ -5,8 +5,10 @@ package expvar import ( "context" + "fmt" "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/config/confighttp" "go.opentelemetry.io/collector/extension" ) @@ -27,7 +29,9 @@ func NewFactory() extension.Factory { func createDefaultConfig() component.Config { return &Config{ - Port: Port, + ServerConfig: confighttp.ServerConfig{ + Endpoint: fmt.Sprintf("0.0.0.0:%d", Port), + }, } }