Skip to content

Commit

Permalink
[extension/healthcheck] Use HTTPServerSettings and remove deprecate…
Browse files Browse the repository at this point in the history
…d `port` field (#10853)

Make healthcheck use HTTPServerSettings and remove deprecated port field
  • Loading branch information
neelayu committed Aug 1, 2022
1 parent 5b2e46c commit 37d94f6
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 69 deletions.
7 changes: 5 additions & 2 deletions extension/healthcheckextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ processors in the future.

The following settings are required:

- `endpoint` (default = 0.0.0.0:13133): Address to publish the health check status to
- `port` (default = 13133): [deprecated] What port to expose HTTP health information.
- `endpoint` (default = 0.0.0.0:13133): Address to publish the health check status. For full list of `HTTPServerSettings` refer [here](https://github.com/open-telemetry/opentelemetry-collector/tree/main/config/confighttp).
- `path` (default = "/"): Specifies the path to be configured for the health check server.
- `check_collector_pipeline:` (optional): Settings of collector pipeline health check
- `enabled` (default = false): Whether enable collector pipeline check or not
Expand All @@ -33,6 +32,10 @@ extensions:
health_check:
health_check/1:
endpoint: "localhost:13"
tls:
ca_file: "/path/to/ca.crt"
cert_file: "/path/to/cert.crt"
key_file: "/path/to/key.key"
path: "/health/status"
check_collector_pipeline:
enabled: true
Expand Down
17 changes: 4 additions & 13 deletions extension/healthcheckextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,14 @@ import (
"time"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/confighttp"
)

// Config has the configuration for the extension enabling the health check
// extension, used to report the health status of the service.
type Config struct {
config.ExtensionSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct

// Port is the port used to publish the health check status.
// The default value is 13133.
// Deprecated: use Endpoint instead.
Port uint16 `mapstructure:"port"`

// TCPAddr represents a tcp endpoint address that is to publish the health
// check status.
// The default endpoint is "0.0.0.0:13133".
TCPAddr confignet.TCPAddr `mapstructure:",squash"`
config.ExtensionSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
confighttp.HTTPServerSettings `mapstructure:",squash"`

// Path represents the path the health check service will serve.
// The default path is "/".
Expand All @@ -59,7 +50,7 @@ func (cfg *Config) Validate() error {
if err != nil {
return err
}
if cfg.TCPAddr.Endpoint == "" {
if cfg.Endpoint == "" {
return errNoEndpointProvided
}
if cfg.CheckCollectorPipeline.ExporterFailureThreshold <= 0 {
Expand Down
12 changes: 10 additions & 2 deletions extension/healthcheckextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/service/servicetest"
)

Expand All @@ -44,8 +45,15 @@ func TestLoadConfig(t *testing.T) {
assert.Equal(t,
&Config{
ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "1")),
TCPAddr: confignet.TCPAddr{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "localhost:13",
TLSSetting: &configtls.TLSServerSetting{
TLSSetting: configtls.TLSSetting{
CAFile: "/path/to/ca",
CertFile: "/path/to/cert",
KeyFile: "/path/to/key",
},
},
},
CheckCollectorPipeline: defaultCheckCollectorPipelineSettings(),
Path: "/",
Expand Down
6 changes: 3 additions & 3 deletions extension/healthcheckextension/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/confighttp"
)

const (
Expand All @@ -42,7 +42,7 @@ func NewFactory() component.ExtensionFactory {
func createDefaultConfig() config.Extension {
return &Config{
ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)),
TCPAddr: confignet.TCPAddr{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: defaultEndpoint,
},
CheckCollectorPipeline: defaultCheckCollectorPipelineSettings(),
Expand All @@ -53,7 +53,7 @@ func createDefaultConfig() config.Extension {
func createExtension(_ context.Context, set component.ExtensionCreateSettings, cfg config.Extension) (component.Extension, error) {
config := cfg.(*Config)

return newServer(*config, set.Logger), nil
return newServer(*config, set.TelemetrySettings), nil
}

// defaultCheckCollectorPipelineSettings returns the default settings for CheckCollectorPipeline.
Expand Down
6 changes: 3 additions & 3 deletions extension/healthcheckextension/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configtest"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil"
Expand All @@ -32,7 +32,7 @@ func TestFactory_CreateDefaultConfig(t *testing.T) {
cfg := createDefaultConfig()
assert.Equal(t, &Config{
ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)),
TCPAddr: confignet.TCPAddr{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: defaultEndpoint,
},
CheckCollectorPipeline: defaultCheckCollectorPipelineSettings(),
Expand All @@ -47,7 +47,7 @@ func TestFactory_CreateDefaultConfig(t *testing.T) {

func TestFactory_CreateExtension(t *testing.T) {
cfg := createDefaultConfig().(*Config)
cfg.TCPAddr.Endpoint = testutil.GetAvailableLocalAddress(t)
cfg.Endpoint = testutil.GetAvailableLocalAddress(t)

ext, err := createExtension(context.Background(), componenttest.NewNopExtensionCreateSettings(), cfg)
require.NoError(t, err)
Expand Down
7 changes: 7 additions & 0 deletions extension/healthcheckextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.8 // indirect
github.com/knadh/koanf v1.4.2 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.8.2 // indirect
go.opentelemetry.io/collector/pdata v0.56.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.33.0 // indirect
go.opentelemetry.io/otel v1.8.0 // indirect
go.opentelemetry.io/otel/metric v0.31.0 // indirect
go.opentelemetry.io/otel/trace v1.8.0 // indirect
Expand Down
15 changes: 15 additions & 0 deletions extension/healthcheckextension/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 17 additions & 21 deletions extension/healthcheckextension/healthcheckextension.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ package healthcheckextension // import "github.com/open-telemetry/opentelemetry-
import (
"context"
"errors"
"net"
"fmt"
"net/http"
"strconv"
"time"

"github.com/jaegertracing/jaeger/pkg/healthcheck"
Expand All @@ -32,29 +31,23 @@ type healthCheckExtension struct {
config Config
logger *zap.Logger
state *healthcheck.HealthCheck
server http.Server
server *http.Server
stopCh chan struct{}
exporter *healthCheckExporter
settings component.TelemetrySettings
}

var _ component.PipelineWatcher = (*healthCheckExtension)(nil)

func (hc *healthCheckExtension) Start(_ context.Context, host component.Host) error {

hc.logger.Info("Starting health_check extension", zap.Any("config", hc.config))

// Initialize listener
var (
ln net.Listener
err error
)
if hc.config.Port != 0 && hc.config.TCPAddr.Endpoint == defaultEndpoint {
hc.logger.Warn("`Port` is deprecated, use `Endpoint` instead")
portStr := ":" + strconv.Itoa(int(hc.config.Port))
ln, err = net.Listen("tcp", portStr)
} else {
ln, err = hc.config.TCPAddr.Listen()
ln, err := hc.config.ToListener()
if err != nil {
return fmt.Errorf("failed to bind to address %s: %w", hc.config.Endpoint, err)
}

hc.server, err = hc.config.ToServer(host, hc.settings, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -131,6 +124,9 @@ func (hc *healthCheckExtension) check() bool {
}

func (hc *healthCheckExtension) Shutdown(context.Context) error {
if hc.server == nil {
return nil
}
err := hc.server.Close()
if hc.stopCh != nil {
<-hc.stopCh
Expand All @@ -148,15 +144,15 @@ func (hc *healthCheckExtension) NotReady() error {
return nil
}

func newServer(config Config, logger *zap.Logger) *healthCheckExtension {
func newServer(config Config, settings component.TelemetrySettings) *healthCheckExtension {
hc := &healthCheckExtension{
config: config,
logger: logger,
state: healthcheck.New(),
server: http.Server{},
config: config,
logger: settings.Logger,
state: healthcheck.New(),
settings: settings,
}

hc.state.SetLogger(logger)
hc.state.SetLogger(settings.Logger)

return hc
}
Loading

0 comments on commit 37d94f6

Please sign in to comment.