Skip to content

Commit 2d8b5d9

Browse files
committed
[extension/zpages] use confighttp.ServerConfig as part of the configuration of the zpagesextension
1 parent 4bbb604 commit 2d8b5d9

File tree

12 files changed

+167
-25
lines changed

12 files changed

+167
-25
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: breaking
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: confighttp
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Use confighttp.ServerConfig as part of zpagesextension
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [9368]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

extension/zpagesextension/config.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@ import (
77
"errors"
88

99
"go.opentelemetry.io/collector/component"
10-
"go.opentelemetry.io/collector/config/confignet"
10+
"go.opentelemetry.io/collector/config/confighttp"
1111
)
1212

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

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

2320
// Validate checks if the extension configuration is valid
2421
func (cfg *Config) Validate() error {
25-
if cfg.TCPAddr.Endpoint == "" {
22+
if cfg.ServerConfig.Endpoint == "" {
2623
return errors.New("\"endpoint\" is required when using the \"zpages\" extension")
2724
}
2825
return nil

extension/zpagesextension/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
"github.com/stretchr/testify/require"
1212

13-
"go.opentelemetry.io/collector/config/confignet"
13+
"go.opentelemetry.io/collector/config/confighttp"
1414
"go.opentelemetry.io/collector/confmap"
1515
"go.opentelemetry.io/collector/confmap/confmaptest"
1616
)
@@ -30,7 +30,7 @@ func TestUnmarshalConfig(t *testing.T) {
3030
assert.NoError(t, cm.Unmarshal(&cfg))
3131
assert.Equal(t,
3232
&Config{
33-
TCPAddr: confignet.TCPAddrConfig{
33+
ServerConfig: confighttp.ServerConfig{
3434
Endpoint: "localhost:56888",
3535
},
3636
}, cfg)

extension/zpagesextension/factory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"context"
88

99
"go.opentelemetry.io/collector/component"
10-
"go.opentelemetry.io/collector/config/confignet"
10+
"go.opentelemetry.io/collector/config/confighttp"
1111
"go.opentelemetry.io/collector/extension"
1212
"go.opentelemetry.io/collector/extension/zpagesextension/internal/metadata"
1313
)
@@ -23,7 +23,7 @@ func NewFactory() extension.Factory {
2323

2424
func createDefaultConfig() component.Config {
2525
return &Config{
26-
TCPAddr: confignet.TCPAddrConfig{
26+
ServerConfig: confighttp.ServerConfig{
2727
Endpoint: defaultEndpoint,
2828
},
2929
}

extension/zpagesextension/factory_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111
"github.com/stretchr/testify/require"
1212

1313
"go.opentelemetry.io/collector/component/componenttest"
14-
"go.opentelemetry.io/collector/config/confignet"
14+
"go.opentelemetry.io/collector/config/confighttp"
1515
"go.opentelemetry.io/collector/extension/extensiontest"
1616
"go.opentelemetry.io/collector/internal/testutil"
1717
)
1818

1919
func TestFactory_CreateDefaultConfig(t *testing.T) {
2020
cfg := createDefaultConfig()
2121
assert.Equal(t, &Config{
22-
TCPAddr: confignet.TCPAddrConfig{
22+
ServerConfig: confighttp.ServerConfig{
2323
Endpoint: "localhost:55679",
2424
},
2525
},
@@ -33,7 +33,7 @@ func TestFactory_CreateDefaultConfig(t *testing.T) {
3333

3434
func TestFactory_CreateExtension(t *testing.T) {
3535
cfg := createDefaultConfig().(*Config)
36-
cfg.TCPAddr.Endpoint = testutil.GetAvailableLocalAddress(t)
36+
cfg.ServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
3737

3838
ext, err := createExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg)
3939
require.NoError(t, err)

extension/zpagesextension/go.mod

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/stretchr/testify v1.9.0
77
go.opentelemetry.io/collector v0.101.0
88
go.opentelemetry.io/collector/component v0.101.0
9-
go.opentelemetry.io/collector/config/confignet v0.101.0
9+
go.opentelemetry.io/collector/config/confighttp v0.101.0
1010
go.opentelemetry.io/collector/confmap v0.101.0
1111
go.opentelemetry.io/collector/extension v0.101.0
1212
go.opentelemetry.io/contrib/zpages v0.52.0
@@ -21,12 +21,17 @@ require (
2121
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
2222
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2323
github.com/davecgh/go-spew v1.1.1 // indirect
24+
github.com/felixge/httpsnoop v1.0.4 // indirect
25+
github.com/fsnotify/fsnotify v1.7.0 // indirect
2426
github.com/go-logr/logr v1.4.1 // indirect
2527
github.com/go-logr/stdr v1.2.2 // indirect
2628
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
2729
github.com/gogo/protobuf v1.3.2 // indirect
30+
github.com/golang/snappy v0.0.4 // indirect
2831
github.com/google/uuid v1.6.0 // indirect
2932
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
33+
github.com/hashicorp/go-version v1.7.0 // indirect
34+
github.com/klauspost/compress v1.17.8 // indirect
3035
github.com/knadh/koanf/maps v0.1.1 // indirect
3136
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
3237
github.com/knadh/koanf/v2 v2.1.1 // indirect
@@ -37,9 +42,18 @@ require (
3742
github.com/prometheus/client_model v0.6.1 // indirect
3843
github.com/prometheus/common v0.53.0 // indirect
3944
github.com/prometheus/procfs v0.15.0 // indirect
45+
github.com/rs/cors v1.10.1 // indirect
46+
go.opentelemetry.io/collector/config/configauth v0.101.0 // indirect
47+
go.opentelemetry.io/collector/config/configcompression v1.8.0 // indirect
48+
go.opentelemetry.io/collector/config/configopaque v1.8.0 // indirect
4049
go.opentelemetry.io/collector/config/configtelemetry v0.101.0 // indirect
50+
go.opentelemetry.io/collector/config/configtls v0.101.0 // indirect
51+
go.opentelemetry.io/collector/config/internal v0.101.0 // indirect
52+
go.opentelemetry.io/collector/extension/auth v0.101.0 // indirect
53+
go.opentelemetry.io/collector/featuregate v1.8.0 // indirect
4154
go.opentelemetry.io/collector/pdata v1.8.0 // indirect
4255
go.opentelemetry.io/contrib/config v0.7.0 // indirect
56+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect
4357
go.opentelemetry.io/otel v1.27.0 // indirect
4458
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.27.0 // indirect
4559
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.27.0 // indirect

extension/zpagesextension/go.sum

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extension/zpagesextension/zpagesextension.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type zpagesExtension struct {
2424
config *Config
2525
telemetry component.TelemetrySettings
2626
zpagesSpanProcessor *zpages.SpanProcessor
27-
server http.Server
27+
server *http.Server
2828
stopCh chan struct{}
2929
}
3030

@@ -43,7 +43,8 @@ type registerableTracerProvider interface {
4343
UnregisterSpanProcessor(SpanProcessor trace.SpanProcessor)
4444
}
4545

46-
func (zpe *zpagesExtension) Start(_ context.Context, host component.Host) error {
46+
func (zpe *zpagesExtension) Start(ctx context.Context, host component.Host) error {
47+
4748
zPagesMux := http.NewServeMux()
4849

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

6869
// Start the listener here so we can have earlier failure if port is
6970
// already in use.
70-
ln, err := zpe.config.TCPAddr.Listen(context.Background())
71+
ln, err := zpe.config.ToListener(ctx)
7172
if err != nil {
7273
return err
7374
}
7475

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

8993
func (zpe *zpagesExtension) Shutdown(context.Context) error {
94+
if zpe.server == nil {
95+
return nil
96+
}
9097
err := zpe.server.Close()
9198
if zpe.stopCh != nil {
9299
<-zpe.stopCh

extension/zpagesextension/zpagesextension_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
"go.opentelemetry.io/collector/component"
1818
"go.opentelemetry.io/collector/component/componenttest"
19-
"go.opentelemetry.io/collector/config/confignet"
19+
"go.opentelemetry.io/collector/config/confighttp"
2020
"go.opentelemetry.io/collector/internal/testutil"
2121
)
2222

@@ -48,7 +48,7 @@ func newZpagesTelemetrySettings() component.TelemetrySettings {
4848

4949
func TestZPagesExtensionUsage(t *testing.T) {
5050
cfg := &Config{
51-
TCPAddr: confignet.TCPAddrConfig{
51+
confighttp.ServerConfig{
5252
Endpoint: testutil.GetAvailableLocalAddress(t),
5353
},
5454
}
@@ -62,7 +62,7 @@ func TestZPagesExtensionUsage(t *testing.T) {
6262
// Give a chance for the server goroutine to run.
6363
runtime.Gosched()
6464

65-
_, zpagesPort, err := net.SplitHostPort(cfg.TCPAddr.Endpoint)
65+
_, zpagesPort, err := net.SplitHostPort(cfg.ServerConfig.Endpoint)
6666
require.NoError(t, err)
6767

6868
client := &http.Client{}
@@ -80,7 +80,7 @@ func TestZPagesExtensionPortAlreadyInUse(t *testing.T) {
8080
defer ln.Close()
8181

8282
cfg := &Config{
83-
TCPAddr: confignet.TCPAddrConfig{
83+
confighttp.ServerConfig{
8484
Endpoint: endpoint,
8585
},
8686
}
@@ -92,7 +92,7 @@ func TestZPagesExtensionPortAlreadyInUse(t *testing.T) {
9292

9393
func TestZPagesMultipleStarts(t *testing.T) {
9494
cfg := &Config{
95-
TCPAddr: confignet.TCPAddrConfig{
95+
confighttp.ServerConfig{
9696
Endpoint: testutil.GetAvailableLocalAddress(t),
9797
},
9898
}
@@ -109,7 +109,7 @@ func TestZPagesMultipleStarts(t *testing.T) {
109109

110110
func TestZPagesMultipleShutdowns(t *testing.T) {
111111
cfg := &Config{
112-
TCPAddr: confignet.TCPAddrConfig{
112+
confighttp.ServerConfig{
113113
Endpoint: testutil.GetAvailableLocalAddress(t),
114114
},
115115
}
@@ -124,7 +124,7 @@ func TestZPagesMultipleShutdowns(t *testing.T) {
124124

125125
func TestZPagesShutdownWithoutStart(t *testing.T) {
126126
cfg := &Config{
127-
TCPAddr: confignet.TCPAddrConfig{
127+
confighttp.ServerConfig{
128128
Endpoint: testutil.GetAvailableLocalAddress(t),
129129
},
130130
}

0 commit comments

Comments
 (0)