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] Add an option to enable expvar #11217

Closed
Show file tree
Hide file tree
Changes from all 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-register-expvar.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: zpagesextension

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add expvar handler to zpages extension.

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

# (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: []
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mention new endpoint in the README?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expvar route added to README in PR #11964

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ineffectual, false is the default value fr bools

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in another PR #11964

}, 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 @@ -57,6 +59,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 @@ -49,7 +49,7 @@ func newZpagesTelemetrySettings() component.TelemetrySettings {

func TestZPagesExtensionUsage(t *testing.T) {
cfg := &Config{
confighttp.ServerConfig{
ServerConfig: confighttp.ServerConfig{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
}
Expand All @@ -76,7 +76,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 @@ -96,7 +96,7 @@ func TestZPagesExtensionPortAlreadyInUse(t *testing.T) {
defer ln.Close()

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

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

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

func TestZPagesShutdownWithoutStart(t *testing.T) {
cfg := &Config{
confighttp.ServerConfig{
ServerConfig: confighttp.ServerConfig{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
}
Expand All @@ -150,3 +150,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)
}