Skip to content

Commit

Permalink
[extension/zpages] Add an option to enable expvar
Browse files Browse the repository at this point in the history
resolve open-telemetry#11081

Signed-off-by: Hong Chen <[email protected]>
  • Loading branch information
HongChenTW committed Dec 20, 2024
1 parent e23802a commit 2e556b3
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion extension/zpagesextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extensions:
zpages:
```
The full list of settings exposed for this exporter are documented [here](./config.go)
The full list of settings exposed for this extension are documented [here](./config.go)
with detailed sample configurations [here](./testdata/config.yaml).
## Exposed zPages routes
Expand Down
4 changes: 4 additions & 0 deletions extension/zpagesextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
// Config has the configuration for the extension enabling the zPages extension.
type Config struct {
confighttp.ServerConfig `mapstructure:",squash"`

// EnableExpvar indicates whether to enable expvar service.
// (default = false)
EnableExpvar bool `mapstructure:"enable_expvar"`
}

var _ component.Config = (*Config)(nil)
Expand Down
1 change: 1 addition & 0 deletions extension/zpagesextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ func TestUnmarshalConfig(t *testing.T) {
ServerConfig: confighttp.ServerConfig{
Endpoint: "localhost:56888",
},
EnableExpvar: false,
}, cfg)
}
1 change: 1 addition & 0 deletions extension/zpagesextension/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func createDefaultConfig() component.Config {
ServerConfig: confighttp.ServerConfig{
Endpoint: defaultEndpoint,
},
EnableExpvar: false,
}
}

Expand Down
9 changes: 8 additions & 1 deletion extension/zpagesextension/zpagesextension.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package zpagesextension // import "go.opentelemetry.io/collector/extension/zpage
import (
"context"
"errors"
"expvar"
"net/http"
"path"

Expand All @@ -18,7 +19,8 @@ import (
)

const (
tracezPath = "tracez"
tracezPath = "tracez"
expvarzPath = "expvarz"
)

type zpagesExtension struct {
Expand Down Expand Up @@ -56,6 +58,11 @@ func (zpe *zpagesExtension) Start(ctx context.Context, host component.Host) erro
zpe.telemetry.Logger.Warn("zPages span processor registration is not available")
}

if zpe.config.EnableExpvar {
zPagesMux.Handle(path.Join("/debug", expvarzPath), expvar.Handler())
zpe.telemetry.Logger.Info("Registered zPages expvar handler")
}

hostZPages, ok := host.(interface {
RegisterZPages(mux *http.ServeMux, pathPrefix string)
})
Expand Down
40 changes: 34 additions & 6 deletions extension/zpagesextension/zpagesextension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func newZpagesTelemetrySettings() component.TelemetrySettings {

func TestZPagesExtensionUsage(t *testing.T) {
cfg := &Config{
confighttp.ServerConfig{
ServerConfig: confighttp.ServerConfig{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
}
Expand All @@ -78,7 +78,7 @@ func TestZPagesExtensionUsage(t *testing.T) {

func TestZPagesExtensionBadAuthExtension(t *testing.T) {
cfg := &Config{
confighttp.ServerConfig{
ServerConfig: confighttp.ServerConfig{
Endpoint: "localhost:0",
Auth: &confighttp.AuthConfig{
Authentication: configauth.Authentication{
Expand All @@ -98,7 +98,7 @@ func TestZPagesExtensionPortAlreadyInUse(t *testing.T) {
defer ln.Close()

cfg := &Config{
confighttp.ServerConfig{
ServerConfig: confighttp.ServerConfig{
Endpoint: endpoint,
},
}
Expand All @@ -110,7 +110,7 @@ func TestZPagesExtensionPortAlreadyInUse(t *testing.T) {

func TestZPagesMultipleStarts(t *testing.T) {
cfg := &Config{
confighttp.ServerConfig{
ServerConfig: confighttp.ServerConfig{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
}
Expand All @@ -127,7 +127,7 @@ func TestZPagesMultipleStarts(t *testing.T) {

func TestZPagesMultipleShutdowns(t *testing.T) {
cfg := &Config{
confighttp.ServerConfig{
ServerConfig: confighttp.ServerConfig{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
}
Expand All @@ -142,7 +142,7 @@ func TestZPagesMultipleShutdowns(t *testing.T) {

func TestZPagesShutdownWithoutStart(t *testing.T) {
cfg := &Config{
confighttp.ServerConfig{
ServerConfig: confighttp.ServerConfig{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
}
Expand All @@ -152,3 +152,31 @@ func TestZPagesShutdownWithoutStart(t *testing.T) {

require.NoError(t, zpagesExt.Shutdown(context.Background()))
}

func TestZPagesEnableExpvar(t *testing.T) {
cfg := &Config{
ServerConfig: confighttp.ServerConfig{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
EnableExpvar: true,
}

zpagesExt := newServer(cfg, newZpagesTelemetrySettings())
require.NotNil(t, zpagesExt)

require.NoError(t, zpagesExt.Start(context.Background(), newZPagesHost()))
t.Cleanup(func() { require.NoError(t, zpagesExt.Shutdown(context.Background())) })

// Give a chance for the server goroutine to run.
runtime.Gosched()

_, zpagesPort, err := net.SplitHostPort(cfg.ServerConfig.Endpoint)
require.NoError(t, err)

client := &http.Client{}
resp, err := client.Get("http://localhost:" + zpagesPort + "/debug/expvarz")
require.NoError(t, err)
defer resp.Body.Close()

require.Equal(t, http.StatusOK, resp.StatusCode)
}

0 comments on commit 2e556b3

Please sign in to comment.