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 #11964

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
25 changes: 25 additions & 0 deletions .chloggen/zpagesextension-add-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: []
12 changes: 11 additions & 1 deletion extension/zpagesextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ The following settings are required:
zPages. Use localhost:<port> to make it available only locally, or ":<port>" to
make it available on all network interfaces.

The following settings can be optionally configured:

- `enable_expvar` (default = false): Enable the expvar services. For detail see [ExpvarZ](#expvarz).

Example:
```yaml
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 Expand Up @@ -78,6 +82,12 @@ They also allow you to quickly examine error samples

Example URL: http://localhost:55679/debug/tracez

### ExpvarZ

The ExpvarZ exposes the useful information about Go runtime, OTEL components could levearge [expvar](https://pkg.go.dev/expvar) library to expose their own state.

Example URL: http://localhost:55679/debug/expvarz

## Warnings

This extension registers a SpanProcessor to record all the spans created inside
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
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)
}