Skip to content

Commit

Permalink
[extension/zpages] use confighttp.ServerConfig as part of the configu…
Browse files Browse the repository at this point in the history
…ration of the zpagesextension
  • Loading branch information
atoulme committed May 31, 2024
1 parent 4bbb604 commit 2293c1a
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 34 deletions.
25 changes: 25 additions & 0 deletions .chloggen/zpagesextension_confighttp-user.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: confighttp

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Use `confighttp.ServerConfig` as part of zpagesextension. See [https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/confighttp/README.md#server-configuration](server configuration) options.

# One or more tracking issues or pull requests related to the change
issues: [9368]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
25 changes: 25 additions & 0 deletions .chloggen/zpagesextension_confighttp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: confighttp

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Use `confighttp.ServerConfig` as part of zpagesextension.Config. Previously the extension used `confignet.TCPAddrConfig`

# One or more tracking issues or pull requests related to the change
issues: [9368]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
9 changes: 3 additions & 6 deletions extension/zpagesextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ import (
"errors"

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

// Config has the configuration for the extension enabling the zPages extension.
type Config struct {
// TCPAddr is the address and port in which the zPages will be listening to.
// Use localhost:<port> to make it available only locally, or ":<port>" to
// make it available on all network interfaces.
TCPAddr confignet.TCPAddrConfig `mapstructure:",squash"`
confighttp.ServerConfig `mapstructure:",squash"`
}

var _ component.Config = (*Config)(nil)

// Validate checks if the extension configuration is valid
func (cfg *Config) Validate() error {
if cfg.TCPAddr.Endpoint == "" {
if cfg.ServerConfig.Endpoint == "" {
return errors.New("\"endpoint\" is required when using the \"zpages\" extension")
}
return nil
Expand Down
8 changes: 6 additions & 2 deletions extension/zpagesextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
)
Expand All @@ -22,6 +22,10 @@ func TestUnmarshalDefaultConfig(t *testing.T) {
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
}

func TestInvalidConfig(t *testing.T) {
assert.Error(t, (&Config{}).Validate())
}

func TestUnmarshalConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
Expand All @@ -30,7 +34,7 @@ func TestUnmarshalConfig(t *testing.T) {
assert.NoError(t, cm.Unmarshal(&cfg))
assert.Equal(t,
&Config{
TCPAddr: confignet.TCPAddrConfig{
ServerConfig: confighttp.ServerConfig{
Endpoint: "localhost:56888",
},
}, cfg)
Expand Down
4 changes: 2 additions & 2 deletions extension/zpagesextension/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/extension"
"go.opentelemetry.io/collector/extension/zpagesextension/internal/metadata"
)
Expand All @@ -23,7 +23,7 @@ func NewFactory() extension.Factory {

func createDefaultConfig() component.Config {
return &Config{
TCPAddr: confignet.TCPAddrConfig{
ServerConfig: confighttp.ServerConfig{
Endpoint: defaultEndpoint,
},
}
Expand Down
6 changes: 3 additions & 3 deletions extension/zpagesextension/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/extension/extensiontest"
"go.opentelemetry.io/collector/internal/testutil"
)

func TestFactory_CreateDefaultConfig(t *testing.T) {
cfg := createDefaultConfig()
assert.Equal(t, &Config{
TCPAddr: confignet.TCPAddrConfig{
ServerConfig: confighttp.ServerConfig{
Endpoint: "localhost:55679",
},
},
Expand All @@ -33,7 +33,7 @@ func TestFactory_CreateDefaultConfig(t *testing.T) {

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

ext, err := createExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
Expand Down
32 changes: 29 additions & 3 deletions extension/zpagesextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ require (
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector v0.101.0
go.opentelemetry.io/collector/component v0.101.0
go.opentelemetry.io/collector/config/confignet v0.101.0
go.opentelemetry.io/collector/config/configauth v0.101.0
go.opentelemetry.io/collector/config/confighttp v0.101.0
go.opentelemetry.io/collector/confmap v0.101.0
go.opentelemetry.io/collector/extension v0.101.0
go.opentelemetry.io/contrib/zpages v0.52.0
Expand All @@ -21,12 +22,17 @@ require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.1.1 // indirect
Expand All @@ -37,9 +43,17 @@ require (
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.53.0 // indirect
github.com/prometheus/procfs v0.15.0 // indirect
github.com/rs/cors v1.10.1 // indirect
go.opentelemetry.io/collector/config/configcompression v1.8.0 // indirect
go.opentelemetry.io/collector/config/configopaque v1.8.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.101.0 // indirect
go.opentelemetry.io/collector/config/configtls v0.101.0 // indirect
go.opentelemetry.io/collector/config/internal v0.101.0 // indirect
go.opentelemetry.io/collector/extension/auth v0.101.0 // indirect
go.opentelemetry.io/collector/featuregate v1.8.0 // indirect
go.opentelemetry.io/collector/pdata v1.8.0 // indirect
go.opentelemetry.io/contrib/config v0.7.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.52.0 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.27.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.27.0 // indirect
Expand Down Expand Up @@ -67,8 +81,6 @@ replace go.opentelemetry.io/collector => ../../

replace go.opentelemetry.io/collector/component => ../../component

replace go.opentelemetry.io/collector/config/confignet => ../../config/confignet

replace go.opentelemetry.io/collector/confmap => ../../confmap

replace go.opentelemetry.io/collector/extension => ../
Expand All @@ -87,3 +99,17 @@ retract (
replace go.opentelemetry.io/collector/config/configtelemetry => ../../config/configtelemetry

replace go.opentelemetry.io/collector/pdata/testdata => ../../pdata/testdata

replace go.opentelemetry.io/collector/config/configopaque => ../../config/configopaque

replace go.opentelemetry.io/collector/config/internal => ../../config/internal

replace go.opentelemetry.io/collector/config/configtls => ../../config/configtls

replace go.opentelemetry.io/collector/config/configcompression => ../../config/configcompression

replace go.opentelemetry.io/collector/config/configauth => ../../config/configauth

replace go.opentelemetry.io/collector/extension/auth => ../auth

replace go.opentelemetry.io/collector/config/confighttp => ../../config/confighttp
20 changes: 20 additions & 0 deletions extension/zpagesextension/go.sum

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

15 changes: 11 additions & 4 deletions extension/zpagesextension/zpagesextension.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type zpagesExtension struct {
config *Config
telemetry component.TelemetrySettings
zpagesSpanProcessor *zpages.SpanProcessor
server http.Server
server *http.Server
stopCh chan struct{}
}

Expand All @@ -43,7 +43,8 @@ type registerableTracerProvider interface {
UnregisterSpanProcessor(SpanProcessor trace.SpanProcessor)
}

func (zpe *zpagesExtension) Start(_ context.Context, host component.Host) error {
func (zpe *zpagesExtension) Start(ctx context.Context, host component.Host) error {

zPagesMux := http.NewServeMux()

sdktracer, ok := zpe.telemetry.TracerProvider.(registerableTracerProvider)
Expand All @@ -67,13 +68,16 @@ func (zpe *zpagesExtension) Start(_ context.Context, host component.Host) error

// Start the listener here so we can have earlier failure if port is
// already in use.
ln, err := zpe.config.TCPAddr.Listen(context.Background())
ln, err := zpe.config.ToListener(ctx)
if err != nil {
return err
}

zpe.telemetry.Logger.Info("Starting zPages extension", zap.Any("config", zpe.config))
zpe.server = http.Server{Handler: zPagesMux}
zpe.server, err = zpe.config.ToServer(ctx, host, zpe.telemetry, zPagesMux)
if err != nil {
return err
}
zpe.stopCh = make(chan struct{})
go func() {
defer close(zpe.stopCh)
Expand All @@ -87,6 +91,9 @@ func (zpe *zpagesExtension) Start(_ context.Context, host component.Host) error
}

func (zpe *zpagesExtension) Shutdown(context.Context) error {
if zpe.server == nil {
return nil
}
err := zpe.server.Close()
if zpe.stopCh != nil {
<-zpe.stopCh
Expand Down
Loading

0 comments on commit 2293c1a

Please sign in to comment.