Skip to content

[log] log(envutil): add debug logging to env var parse fallbacks#3825

Merged
lpcox merged 2 commits intomainfrom
log/envutil-debug-logging-c1ed1dded58a5e52
Apr 15, 2026
Merged

[log] log(envutil): add debug logging to env var parse fallbacks#3825
lpcox merged 2 commits intomainfrom
log/envutil-debug-logging-c1ed1dded58a5e52

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

Summary

Adds debug logging to internal/envutil/envutil.go to surface misconfiguration earlier.

What changed

Added var logEnvUtil = logger.New("envutil:envutil") and three targeted logEnvUtil.Printf calls — one in each type-converting function — that fire only when an environment variable is set but unusable:

Function Log condition
GetEnvInt Value is set but not a valid positive integer
GetEnvDuration Value is set but not a valid positive duration
GetEnvBool Value is set but not a recognised boolean (1/true/yes/on / 0/false/no/off)

The GetEnvString function is intentionally not logged — it has no parse step and adding a log there would be too noisy.

Why

When an operator sets e.g. MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD=512k (an invalid integer), the gateway silently falls back to the default with no indication of what happened. With this change, enabling DEBUG=envutil:* reveals the problem immediately:

envutil:envutil GetEnvInt: MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD="512k" is not a valid positive integer, using default=524288

Quality checklist

  • Exactly 1 file modified
  • No test files modified
  • Logger declaration added (var logEnvUtil = logger.New("envutil:envutil"))
  • Logger naming follows pkg:filename convention (matches logGitHub/logExpand in the same package)
  • Logger arguments use only in-scope variables — no side effects
  • Log messages are meaningful and actionable
  • No duplication of existing logs
  • Import statements follow Go formatting conventions

Generated by Go Logger Enhancement · ● 4.8M ·

Add a debug logger to internal/envutil/envutil.go to surface
misconfiguration earlier. The logger emits a debug message whenever
an environment variable is set but cannot be used (invalid integer,
unparseable duration, unrecognized boolean value), helping operators
quickly identify why a configured value is being silently ignored.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions bot added automation enhancement New feature or request labels Apr 15, 2026
@lpcox lpcox marked this pull request as ready for review April 15, 2026 16:52
Copilot AI review requested due to automatic review settings April 15, 2026 16:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds targeted debug logging in internal/envutil to surface invalid environment variable values (set-but-unusable) when parsing ints, durations, and booleans, helping operators detect misconfiguration earlier.

Changes:

  • Introduce a package-level envutil:envutil debug logger.
  • Emit debug logs when GetEnvInt, GetEnvDuration, or GetEnvBool fall back due to invalid (or non-positive) values.
Show a summary per file
File Description
internal/envutil/envutil.go Adds a logger and fallback debug logs for type-converting env helpers.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comments suppressed due to low confidence (2)

internal/envutil/envutil.go:46

  • These debug logs include the full env var value in the message. To avoid leaking secrets into stderr/log files, consider sanitizing/truncating envValue before logging (the repo already uses internal/logger/sanitize for env vars in other places).
		}
		logEnvUtil.Printf("GetEnvDuration: %s=%q is not a valid positive duration, using default=%v", envKey, envValue, defaultValue)
	}

internal/envutil/envutil.go:64

  • This log line emits the raw environment variable value. To reduce the risk of secret exposure (and because debug logs are also written to the file logger), consider sanitizing/truncating envValue before including it in the log message.
		}
		logEnvUtil.Printf("GetEnvBool: %s=%q is not a recognized boolean value, using default=%v", envKey, envValue, defaultValue)
	}
  • Files reviewed: 1/1 changed files
  • Comments generated: 2

Comment on lines 31 to 33
}
logEnvUtil.Printf("GetEnvInt: %s=%q is not a valid positive integer, using default=%d", envKey, envValue, defaultValue)
}
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

These debug logs print the raw environment variable value (%q). Since Logger.Printf also forwards to the file logger, this can persist potentially sensitive values if a secret-like env var is accidentally parsed via GetEnvInt. Consider sanitizing/truncating the value before logging (e.g., using internal/logger/sanitize helpers).

This issue also appears in the following locations of the same file:

  • line 44
  • line 62

Copilot uses AI. Check for mistakes.
Comment thread internal/envutil/envutil.go Outdated
Comment on lines +37 to +45
if d, err := time.ParseDuration(envValue); err == nil && d > 0 {
return d
}
logEnvUtil.Printf("GetEnvDuration: %s=%q is not a valid positive duration, using default=%v", envKey, envValue, defaultValue)
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

The doc comment says GetEnvDuration falls back only when unset/empty or ParseDuration fails, but the implementation also requires d > 0 (and the new log message refers to a “positive duration”). Update the comment to mention that zero/negative durations also fall back to defaultValue.

Copilot uses AI. Check for mistakes.
Address review feedback:

1. Sanitize raw env values using sanitize.TruncateSecret() in all 3
   debug log lines (GetEnvInt, GetEnvDuration, GetEnvBool). Env vars
   could contain secrets, and debug logs are also written to the file
   logger, so full values should never be persisted.

2. Update GetEnvDuration doc comment to mention that zero/negative
   durations also fall back to defaultValue (matching the d > 0
   condition in the code and the existing GetEnvInt doc pattern).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented Apr 15, 2026

Review + Fixes

Evaluated the Copilot review and pushed fixes in 9def5d0:

Addressed

  1. Sanitize env values in debug logs — All 3 log lines now use sanitize.TruncateSecret(envValue) instead of raw envValue. Env vars could contain secrets (API keys, tokens), and since debug logs also write to the file logger, full values should never be persisted. Output now shows e.g. ghp_... instead of the full token.

  2. Fix GetEnvDuration doc comment — Added mention that zero/negative durations also fall back to defaultValue, matching the d > 0 condition in the code and aligning with the existing GetEnvInt doc pattern.

Own review notes

The PR itself is clean and well-targeted:

  • Logger naming logEnvUtil follows the pkg:filename convention correctly
  • Logs fire only on the set-but-unusable path (not on unset/empty) — good signal-to-noise ratio
  • No unnecessary logging added to GetEnvString (which has no validation to fail)

@lpcox lpcox merged commit db92d58 into main Apr 15, 2026
14 checks passed
@lpcox lpcox deleted the log/envutil-debug-logging-c1ed1dded58a5e52 branch April 15, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants