feat(flagd): log error on invalid env var parsing#1773
feat(flagd): log error on invalid env var parsing#1773buildingisfun23 wants to merge 1 commit intoopen-feature:mainfrom
Conversation
When flagd options in environment variables cannot be parsed (e.g., non-numeric value for an integer option), the code silently falls back to default values. This adds ERROR-level log messages at each fallback point so users are informed about misconfigurations. Closes open-feature#1673
There was a problem hiding this comment.
Code Review
This pull request introduces error logging to the environment variable fallback methods in the Config class to improve observability when parsing fails. The review feedback suggests including the caught exception in the log.error calls to capture stack traces for better debugging and recommends caching the environment variable value to avoid redundant System.getenv calls.
| log.error( | ||
| "Failed to parse env variable '{}' with value '{}' as int, using default: {}", | ||
| key, | ||
| System.getenv(key), | ||
| defaultValue); |
There was a problem hiding this comment.
It is recommended to include the exception e in the log.error call. This allows the logger to capture the stack trace and the specific error message (e.g., why the integer parsing failed), which is invaluable for debugging. Additionally, note that System.getenv(key) is called multiple times in this method; while not a performance bottleneck in a configuration loader, it is generally better practice to retrieve the value once to ensure consistency and avoid redundant system lookups.
| log.error( | |
| "Failed to parse env variable '{}' with value '{}' as int, using default: {}", | |
| key, | |
| System.getenv(key), | |
| defaultValue); | |
| log.error( | |
| "Failed to parse env variable '{}' with value '{}' as int, using default: {}", | |
| key, | |
| System.getenv(key), | |
| defaultValue, | |
| e); |
| log.error( | ||
| "Failed to parse env variable '{}' with value '{}' as long, using default: {}", | ||
| key, | ||
| System.getenv(key), | ||
| defaultValue); |
There was a problem hiding this comment.
Including the exception e in the log output provides the root cause of the parsing failure, which aids in troubleshooting. Also, consider capturing the result of System.getenv(key) in a variable at the start of the method to avoid redundant calls and potential (though unlikely) consistency issues if the environment were to change between calls.
| log.error( | |
| "Failed to parse env variable '{}' with value '{}' as long, using default: {}", | |
| key, | |
| System.getenv(key), | |
| defaultValue); | |
| log.error( | |
| "Failed to parse env variable '{}' with value '{}' as long, using default: {}", | |
| key, | |
| System.getenv(key), | |
| defaultValue, | |
| e); |
| log.error( | ||
| "Failed to parse env variable '{}' with value '{}' as list, using default: {}", | ||
| key, | ||
| System.getenv(key), | ||
| defaultValue); |
There was a problem hiding this comment.
Adding the exception e to the log call ensures that any unexpected errors during the stream processing or splitting are properly documented with a stack trace. As with the other fallback methods, retrieving the environment variable once and reusing it would be more efficient and robust than multiple System.getenv(key) calls.
| log.error( | |
| "Failed to parse env variable '{}' with value '{}' as list, using default: {}", | |
| key, | |
| System.getenv(key), | |
| defaultValue); | |
| log.error( | |
| "Failed to parse env variable '{}' with value '{}' as list, using default: {}", | |
| key, | |
| System.getenv(key), | |
| defaultValue, | |
| e); |
Summary
Config.java@Slf4jlogger already present on the classFixes #1673