Skip to content

Commit 76e91b4

Browse files
committed
worked on review comments
1 parent b0bbb36 commit 76e91b4

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
lines changed

internal/collector/otel_collector_plugin.go

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ func (oc *Collector) bootup(ctx context.Context) error {
232232
return
233233
}
234234

235-
oc.setProxyIfNeeded(ctx)
235+
if oc.config.IsCommandServerProxyConfigured() {
236+
oc.setProxyIfNeeded(ctx)
237+
}
236238

237239
appErr := oc.service.Run(ctx)
238240
if appErr != nil {
@@ -392,7 +394,10 @@ func (oc *Collector) restartCollector(ctx context.Context) {
392394
if !oc.tryCreateCollector(ctx) {
393395
return
394396
}
395-
oc.setProxyIfNeeded(ctx)
397+
398+
if oc.config.IsCommandServerProxyConfigured() {
399+
oc.setProxyIfNeeded(ctx)
400+
}
396401
var runCtx context.Context
397402
runCtx, oc.cancel = context.WithCancel(ctx)
398403
if !oc.stopped {
@@ -778,29 +783,17 @@ func escapeString(input string) string {
778783
}
779784

780785
func (oc *Collector) setExporterProxyEnvVars(ctx context.Context) {
781-
// Validate proxy fields
782-
if oc.config.Command.Server.Proxy == nil {
783-
slog.InfoContext(ctx, "Proxy configuration is not setup; skipping Proxy setup")
784-
return
785-
}
786-
787786
proxy := oc.config.Command.Server.Proxy
788-
if proxy.URL == "" {
789-
slog.WarnContext(ctx, "Proxy URL is empty; skipping Proxy setup")
790-
return
791-
}
792787
proxyURL := proxy.URL
793-
u, err := url.Parse(proxyURL)
788+
parsedProxyURL, err := url.Parse(proxyURL)
794789
if err != nil {
795790
slog.ErrorContext(ctx, "Malformed proxy URL; skipping Proxy setup", "url", proxyURL, "error", err)
796791
return
797792
}
798793

799794
auth := ""
800-
if oc.config.Command.Server.Proxy.AuthMethod != "" &&
801-
strings.TrimSpace(oc.config.Command.Server.Proxy.AuthMethod) != "" {
802-
auth = strings.TrimSpace(oc.config.Command.Server.Proxy.AuthMethod)
803-
slog.DebugContext(ctx, "auth string", "auth", auth)
795+
if proxy.AuthMethod != "" && strings.TrimSpace(proxy.AuthMethod) != "" {
796+
auth = strings.TrimSpace(proxy.AuthMethod)
804797
}
805798

806799
// Use the standalone setProxyWithBasicAuth function
@@ -809,9 +802,8 @@ func (oc *Collector) setExporterProxyEnvVars(ctx context.Context) {
809802
return
810803
}
811804
authLower := strings.ToLower(auth)
812-
slog.DebugContext(ctx, "auth To Lower string", "authlower", authLower)
813805
if authLower == "basic" {
814-
setProxyWithBasicAuth(ctx, proxy, u)
806+
setProxyWithBasicAuth(ctx, proxy, parsedProxyURL)
815807
} else {
816808
slog.ErrorContext(ctx, "Unknown auth type for proxy; Aborting Proxy Setup", "auth", auth, "url", proxyURL)
817809
}
@@ -829,14 +821,14 @@ func setProxyEnvs(ctx context.Context, proxyEnvURL, msg string) {
829821
}
830822

831823
// setProxyWithBasicAuth sets the proxy environment variables with basic auth credentials.
832-
func setProxyWithBasicAuth(ctx context.Context, proxy *config.Proxy, u *url.URL) {
824+
func setProxyWithBasicAuth(ctx context.Context, proxy *config.Proxy, parsedProxyURL *url.URL) {
833825
username := proxy.Username
834826
password := proxy.Password
835827
if username == "" || password == "" {
836828
slog.ErrorContext(ctx, "Username or password missing for basic auth")
837829
return
838830
}
839-
u.User = url.UserPassword(username, password)
840-
proxyURL := u.String()
831+
parsedProxyURL.User = url.UserPassword(username, password)
832+
proxyURL := parsedProxyURL.String()
841833
setProxyEnvs(ctx, proxyURL, "Setting Proxy with basic auth")
842834
}

internal/collector/otel_collector_plugin_test.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -842,13 +842,7 @@ func TestSetExporterProxyEnvVars(t *testing.T) {
842842
{
843843
name: "No proxy config",
844844
proxy: nil,
845-
expectedLog: "Proxy configuration is not setup; skipping Proxy setup",
846-
setEnv: false,
847-
},
848-
{
849-
name: "Empty proxy URL",
850-
proxy: &config.Proxy{URL: ""},
851-
expectedLog: "Proxy URL is empty; skipping Proxy setup",
845+
expectedLog: "Proxy configuration is not setup. Unable to configure proxy for OTLP exporter",
852846
setEnv: false,
853847
},
854848
{
@@ -885,24 +879,38 @@ func TestSetExporterProxyEnvVars(t *testing.T) {
885879
for _, tt := range tests {
886880
t.Run(tt.name, func(t *testing.T) {
887881
logBuf.Reset()
882+
883+
_ = os.Unsetenv("HTTP_PROXY")
884+
_ = os.Unsetenv("HTTPS_PROXY")
885+
888886
tmpDir := t.TempDir()
889887
cfg := types.OTelConfig(t)
890888
cfg.Collector.Log.Path = filepath.Join(tmpDir, "otel-collector-test.log")
891889
cfg.Command.Server.Proxy = tt.proxy
890+
891+
// If the proxy is nil, the production code would never call the setter functions.
892+
// added this check to prevent the panic error in UT.
893+
if cfg.Command.Server.Proxy == nil {
894+
// For the nil proxy case, we expect nothing to happen.
895+
assert.Empty(t, os.Getenv("HTTP_PROXY"))
896+
assert.Empty(t, os.Getenv("HTTPS_PROXY"))
897+
898+
return
899+
}
900+
892901
collector, err := NewCollector(cfg)
893902
require.NoError(t, err)
903+
894904
collector.setExporterProxyEnvVars(ctx)
905+
895906
helpers.ValidateLog(t, tt.expectedLog, logBuf)
907+
896908
if tt.setEnv {
897-
// Check that HTTP_PROXY and HTTPS_PROXY are set
898-
httpProxy := os.Getenv("HTTP_PROXY")
899-
httpsProxy := os.Getenv("HTTPS_PROXY")
900-
assert.NotEmpty(t, httpProxy)
901-
assert.NotEmpty(t, httpsProxy)
909+
assert.NotEmpty(t, os.Getenv("HTTP_PROXY"))
910+
assert.NotEmpty(t, os.Getenv("HTTPS_PROXY"))
902911
} else {
903-
// Unset for next test
904-
os.Unsetenv("HTTP_PROXY")
905-
os.Unsetenv("HTTPS_PROXY")
912+
assert.Empty(t, os.Getenv("HTTP_PROXY"))
913+
assert.Empty(t, os.Getenv("HTTPS_PROXY"))
906914
}
907915
})
908916
}

internal/config/types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,14 @@ func (c *Config) NewContextWithLabels(ctx context.Context) context.Context {
472472
return metadata.NewOutgoingContext(ctx, md)
473473
}
474474

475+
func (c *Config) IsCommandServerProxyConfigured() bool {
476+
if c.Command.Server.Proxy == nil {
477+
return false
478+
}
479+
480+
return c.Command.Server.Proxy.URL != ""
481+
}
482+
475483
// isAllowedDir checks if the given path is in the list of allowed directories.
476484
// It returns true if the path is allowed, false otherwise.
477485
// If the path is allowed but does not exist, it also logs a warning.

0 commit comments

Comments
 (0)