Skip to content

Commit dc9250a

Browse files
HongChenTWyurishkuroevan-bradley
authored
[extension/zpages] Add an option to enable expvar (#11964)
#### Description Add an option to enable expvar, default is `disable`. #### Link to tracking issue Resolve #11081 #### Testing Add a test case to verify the newly added option works. --------- Signed-off-by: Hong Chen <[email protected]> Co-authored-by: Yuri Shkuro <[email protected]> Co-authored-by: Evan Bradley <[email protected]>
1 parent af659fa commit dc9250a

File tree

5 files changed

+89
-7
lines changed

5 files changed

+89
-7
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: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: zpagesextension
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add expvar handler to zpages extension.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [11081]
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/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ The following settings are required:
2727
zPages. Use localhost:<port> to make it available only locally, or ":<port>" to
2828
make it available on all network interfaces.
2929

30+
The following settings can be optionally configured:
31+
32+
- `expvar`
33+
- `enabled` (default = false): Enable the expvar services. For detail see [ExpvarZ](#expvarz).
34+
3035
Example:
3136
```yaml
3237
extensions:
@@ -78,6 +83,12 @@ They also allow you to quickly examine error samples
7883

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

86+
### ExpvarZ
87+
88+
The ExpvarZ exposes the useful information about Go runtime, OTel components could leverage [expvar](https://pkg.go.dev/expvar) library to expose their own state.
89+
90+
Example URL: http://localhost:55679/debug/expvarz
91+
8192
## Warnings
8293

8394
This extension registers a SpanProcessor to record all the spans created inside

extension/zpagesextension/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ import (
1313
// Config has the configuration for the extension enabling the zPages extension.
1414
type Config struct {
1515
confighttp.ServerConfig `mapstructure:",squash"`
16+
17+
Expvar ExpvarConfig `mapstructure:"expvar"`
18+
}
19+
20+
// ExpvarConfig has the configuration for the expvar service.
21+
type ExpvarConfig struct {
22+
// Enabled indicates whether to enable expvar service.
23+
// (default = false)
24+
Enabled bool `mapstructure:"enabled"`
1625
}
1726

1827
var _ component.Config = (*Config)(nil)

extension/zpagesextension/zpagesextension.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package zpagesextension // import "go.opentelemetry.io/collector/extension/zpage
66
import (
77
"context"
88
"errors"
9+
"expvar"
910
"net/http"
1011
"path"
1112

@@ -18,7 +19,8 @@ import (
1819
)
1920

2021
const (
21-
tracezPath = "tracez"
22+
tracezPath = "tracez"
23+
expvarzPath = "expvarz"
2224
)
2325

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

61+
if zpe.config.Expvar.Enabled {
62+
zPagesMux.Handle(path.Join("/debug", expvarzPath), expvar.Handler())
63+
zpe.telemetry.Logger.Info("Registered zPages expvar handler")
64+
}
65+
5966
hostZPages, ok := host.(interface {
6067
RegisterZPages(mux *http.ServeMux, pathPrefix string)
6168
})

extension/zpagesextension/zpagesextension_test.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func newZpagesTelemetrySettings() component.TelemetrySettings {
5151

5252
func TestZPagesExtensionUsage(t *testing.T) {
5353
cfg := &Config{
54-
confighttp.ServerConfig{
54+
ServerConfig: confighttp.ServerConfig{
5555
Endpoint: testutil.GetAvailableLocalAddress(t),
5656
},
5757
}
@@ -78,7 +78,7 @@ func TestZPagesExtensionUsage(t *testing.T) {
7878

7979
func TestZPagesExtensionBadAuthExtension(t *testing.T) {
8080
cfg := &Config{
81-
confighttp.ServerConfig{
81+
ServerConfig: confighttp.ServerConfig{
8282
Endpoint: "localhost:0",
8383
Auth: &confighttp.AuthConfig{
8484
Authentication: configauth.Authentication{
@@ -98,7 +98,7 @@ func TestZPagesExtensionPortAlreadyInUse(t *testing.T) {
9898
defer ln.Close()
9999

100100
cfg := &Config{
101-
confighttp.ServerConfig{
101+
ServerConfig: confighttp.ServerConfig{
102102
Endpoint: endpoint,
103103
},
104104
}
@@ -110,7 +110,7 @@ func TestZPagesExtensionPortAlreadyInUse(t *testing.T) {
110110

111111
func TestZPagesMultipleStarts(t *testing.T) {
112112
cfg := &Config{
113-
confighttp.ServerConfig{
113+
ServerConfig: confighttp.ServerConfig{
114114
Endpoint: testutil.GetAvailableLocalAddress(t),
115115
},
116116
}
@@ -127,7 +127,7 @@ func TestZPagesMultipleStarts(t *testing.T) {
127127

128128
func TestZPagesMultipleShutdowns(t *testing.T) {
129129
cfg := &Config{
130-
confighttp.ServerConfig{
130+
ServerConfig: confighttp.ServerConfig{
131131
Endpoint: testutil.GetAvailableLocalAddress(t),
132132
},
133133
}
@@ -142,7 +142,7 @@ func TestZPagesMultipleShutdowns(t *testing.T) {
142142

143143
func TestZPagesShutdownWithoutStart(t *testing.T) {
144144
cfg := &Config{
145-
confighttp.ServerConfig{
145+
ServerConfig: confighttp.ServerConfig{
146146
Endpoint: testutil.GetAvailableLocalAddress(t),
147147
},
148148
}
@@ -152,3 +152,33 @@ func TestZPagesShutdownWithoutStart(t *testing.T) {
152152

153153
require.NoError(t, zpagesExt.Shutdown(context.Background()))
154154
}
155+
156+
func TestZPagesEnableExpvar(t *testing.T) {
157+
cfg := &Config{
158+
ServerConfig: confighttp.ServerConfig{
159+
Endpoint: testutil.GetAvailableLocalAddress(t),
160+
},
161+
Expvar: ExpvarConfig{
162+
Enabled: true,
163+
},
164+
}
165+
166+
zpagesExt := newServer(cfg, newZpagesTelemetrySettings())
167+
require.NotNil(t, zpagesExt)
168+
169+
require.NoError(t, zpagesExt.Start(context.Background(), newZPagesHost()))
170+
t.Cleanup(func() { require.NoError(t, zpagesExt.Shutdown(context.Background())) })
171+
172+
// Give a chance for the server goroutine to run.
173+
runtime.Gosched()
174+
175+
_, zpagesPort, err := net.SplitHostPort(cfg.ServerConfig.Endpoint)
176+
require.NoError(t, err)
177+
178+
client := &http.Client{}
179+
resp, err := client.Get("http://localhost:" + zpagesPort + "/debug/expvarz")
180+
require.NoError(t, err)
181+
defer resp.Body.Close()
182+
183+
require.Equal(t, http.StatusOK, resp.StatusCode)
184+
}

0 commit comments

Comments
 (0)