From 2e556b386fb03c5380b739c780c533922269afab Mon Sep 17 00:00:00 2001 From: Hong Chen Date: Thu, 19 Sep 2024 22:53:39 +0800 Subject: [PATCH 1/6] [extension/zpages] Add an option to enable expvar resolve #11081 Signed-off-by: Hong Chen --- extension/zpagesextension/README.md | 2 +- extension/zpagesextension/config.go | 4 ++ extension/zpagesextension/config_test.go | 1 + extension/zpagesextension/factory.go | 1 + extension/zpagesextension/zpagesextension.go | 9 ++++- .../zpagesextension/zpagesextension_test.go | 40 ++++++++++++++++--- 6 files changed, 49 insertions(+), 8 deletions(-) diff --git a/extension/zpagesextension/README.md b/extension/zpagesextension/README.md index 859df8227ec..937f2073f39 100644 --- a/extension/zpagesextension/README.md +++ b/extension/zpagesextension/README.md @@ -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 diff --git a/extension/zpagesextension/config.go b/extension/zpagesextension/config.go index 7375f225ff8..4fe97147fb8 100644 --- a/extension/zpagesextension/config.go +++ b/extension/zpagesextension/config.go @@ -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) diff --git a/extension/zpagesextension/config_test.go b/extension/zpagesextension/config_test.go index cbbd324f008..49840becfec 100644 --- a/extension/zpagesextension/config_test.go +++ b/extension/zpagesextension/config_test.go @@ -37,5 +37,6 @@ func TestUnmarshalConfig(t *testing.T) { ServerConfig: confighttp.ServerConfig{ Endpoint: "localhost:56888", }, + EnableExpvar: false, }, cfg) } diff --git a/extension/zpagesextension/factory.go b/extension/zpagesextension/factory.go index 08e79541da1..3c4baba0710 100644 --- a/extension/zpagesextension/factory.go +++ b/extension/zpagesextension/factory.go @@ -26,6 +26,7 @@ func createDefaultConfig() component.Config { ServerConfig: confighttp.ServerConfig{ Endpoint: defaultEndpoint, }, + EnableExpvar: false, } } diff --git a/extension/zpagesextension/zpagesextension.go b/extension/zpagesextension/zpagesextension.go index 000022689cc..26dca09727a 100644 --- a/extension/zpagesextension/zpagesextension.go +++ b/extension/zpagesextension/zpagesextension.go @@ -6,6 +6,7 @@ package zpagesextension // import "go.opentelemetry.io/collector/extension/zpage import ( "context" "errors" + "expvar" "net/http" "path" @@ -18,7 +19,8 @@ import ( ) const ( - tracezPath = "tracez" + tracezPath = "tracez" + expvarzPath = "expvarz" ) type zpagesExtension struct { @@ -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) }) diff --git a/extension/zpagesextension/zpagesextension_test.go b/extension/zpagesextension/zpagesextension_test.go index 21b8df5f8ca..b89bc2b8670 100644 --- a/extension/zpagesextension/zpagesextension_test.go +++ b/extension/zpagesextension/zpagesextension_test.go @@ -51,7 +51,7 @@ func newZpagesTelemetrySettings() component.TelemetrySettings { func TestZPagesExtensionUsage(t *testing.T) { cfg := &Config{ - confighttp.ServerConfig{ + ServerConfig: confighttp.ServerConfig{ Endpoint: testutil.GetAvailableLocalAddress(t), }, } @@ -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{ @@ -98,7 +98,7 @@ func TestZPagesExtensionPortAlreadyInUse(t *testing.T) { defer ln.Close() cfg := &Config{ - confighttp.ServerConfig{ + ServerConfig: confighttp.ServerConfig{ Endpoint: endpoint, }, } @@ -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), }, } @@ -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), }, } @@ -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), }, } @@ -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) +} From 1f00ae273b526171389eb1beda8d37529eafe719 Mon Sep 17 00:00:00 2001 From: Hong Chen Date: Thu, 19 Sep 2024 23:07:33 +0800 Subject: [PATCH 2/6] Add the changelog entry Signed-off-by: Hong Chen --- .../zpagesextension-register-expvar.yaml | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .chloggen/zpagesextension-register-expvar.yaml diff --git a/.chloggen/zpagesextension-register-expvar.yaml b/.chloggen/zpagesextension-register-expvar.yaml new file mode 100644 index 00000000000..06556c241df --- /dev/null +++ b/.chloggen/zpagesextension-register-expvar.yaml @@ -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: [] From a5f27fd9b00a6b5c2859a6478661adfa57d23111 Mon Sep 17 00:00:00 2001 From: Hong Chen Date: Fri, 20 Dec 2024 22:05:26 +0800 Subject: [PATCH 3/6] Remove the ineffectual parameter Signed-off-by: Hong Chen --- extension/zpagesextension/config_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/extension/zpagesextension/config_test.go b/extension/zpagesextension/config_test.go index 49840becfec..cbbd324f008 100644 --- a/extension/zpagesextension/config_test.go +++ b/extension/zpagesextension/config_test.go @@ -37,6 +37,5 @@ func TestUnmarshalConfig(t *testing.T) { ServerConfig: confighttp.ServerConfig{ Endpoint: "localhost:56888", }, - EnableExpvar: false, }, cfg) } From cb04881c7e75c4c854bff6c7e8e735359f964e27 Mon Sep 17 00:00:00 2001 From: Hong Chen Date: Fri, 20 Dec 2024 22:09:45 +0800 Subject: [PATCH 4/6] Add expvarz route description to README Signed-off-by: Hong Chen --- extension/zpagesextension/README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/extension/zpagesextension/README.md b/extension/zpagesextension/README.md index 937f2073f39..b5642ea1316 100644 --- a/extension/zpagesextension/README.md +++ b/extension/zpagesextension/README.md @@ -27,6 +27,10 @@ The following settings are required: zPages. Use localhost: to make it available only locally, or ":" 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: @@ -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 From 25383d65e62be731d6b2eef3e9d650d747bbfff4 Mon Sep 17 00:00:00 2001 From: Hong Chen Date: Fri, 20 Dec 2024 22:13:37 +0800 Subject: [PATCH 5/6] Align the changlog name with branch name Signed-off-by: Hong Chen --- ...nsion-register-expvar.yaml => zpagesextension-add-expvar.yaml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .chloggen/{zpagesextension-register-expvar.yaml => zpagesextension-add-expvar.yaml} (100%) diff --git a/.chloggen/zpagesextension-register-expvar.yaml b/.chloggen/zpagesextension-add-expvar.yaml similarity index 100% rename from .chloggen/zpagesextension-register-expvar.yaml rename to .chloggen/zpagesextension-add-expvar.yaml From ffecfe5f8e3696e1ec8ed837e3b191c3cbf1e077 Mon Sep 17 00:00:00 2001 From: Hong Chen Date: Sat, 21 Dec 2024 06:09:53 +0800 Subject: [PATCH 6/6] Remove the ineffectual parameter Signed-off-by: Hong Chen --- extension/zpagesextension/factory.go | 1 - 1 file changed, 1 deletion(-) diff --git a/extension/zpagesextension/factory.go b/extension/zpagesextension/factory.go index 3c4baba0710..08e79541da1 100644 --- a/extension/zpagesextension/factory.go +++ b/extension/zpagesextension/factory.go @@ -26,7 +26,6 @@ func createDefaultConfig() component.Config { ServerConfig: confighttp.ServerConfig{ Endpoint: defaultEndpoint, }, - EnableExpvar: false, } }