Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[extension/zpages] zpagesextension using confighttp.ServerConfig #10276

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 32 additions & 6 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.102.1
go.opentelemetry.io/collector/component v0.102.1
go.opentelemetry.io/collector/config/confignet v0.102.1
go.opentelemetry.io/collector/config/configauth v0.102.1
go.opentelemetry.io/collector/config/confighttp v0.102.1
go.opentelemetry.io/collector/confmap v0.102.1
go.opentelemetry.io/collector/extension v0.102.1
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.54.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.9.0 // indirect
go.opentelemetry.io/collector/config/configopaque v1.9.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.102.1 // indirect
go.opentelemetry.io/collector/config/configtls v0.102.1 // indirect
go.opentelemetry.io/collector/config/internal v0.102.1 // indirect
go.opentelemetry.io/collector/extension/auth v0.102.1 // indirect
go.opentelemetry.io/collector/featuregate v1.9.0 // indirect
go.opentelemetry.io/collector/pdata v1.9.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 All @@ -53,9 +67,9 @@ require (
go.opentelemetry.io/otel/sdk/metric v1.27.0 // indirect
go.opentelemetry.io/proto/otlp v1.2.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240520151616-dc85e6b867a5 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240520151616-dc85e6b867a5 // indirect
google.golang.org/grpc v1.64.0 // indirect
Expand All @@ -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
32 changes: 26 additions & 6 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
Loading