fix: Add system property substitution to declarative config#8073
fix: Add system property substitution to declarative config#8073MikeGoldsmith wants to merge 3 commits intoopen-telemetry:mainfrom
Conversation
Extends env var substitution to support Java system properties via
${sys:property.name} syntax. Maintains backward compatibility with
existing ${VAR} and ${env:VAR} patterns. All support :-default values.
Addresses open-telemetry#5926
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8073 +/- ##
=========================================
Coverage 90.20% 90.20%
- Complexity 7592 7595 +3
=========================================
Files 841 841
Lines 22911 22926 +15
Branches 2288 2293 +5
=========================================
+ Hits 20666 20681 +15
Misses 1529 1529
Partials 716 716 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Note: add a comment to open-telemetry/opentelemetry-java-instrumentation#15775 if this is accepted |
| // Matches ${VAR_NAME}, ${env:VAR_NAME}, or ${sys:property.name} with optional :-default | ||
| private static final Pattern ENV_VARIABLE_REFERENCE = | ||
| Pattern.compile("\\$\\{([a-zA-Z_][a-zA-Z0-9_]*)(:-([^\n}]*))?}"); | ||
| Pattern.compile("\\$\\{(?:(env|sys):)?([a-zA-Z_][a-zA-Z0-9_.]*)(?::-([^\\n}]*))?}"); |
There was a problem hiding this comment.
Technically this is non compliant with the spec. The syntax describes "Optionally followed by env:" with no affordance for alternative prefixes.
I would have preferred none of this prefix business at all, but env: allows for compatibility with the collector's syntax, which had a lot of momentum and was stabilizing.
The collector also supports alternative prefixes to substitute from other places besides env vars (things like other files, network locations), AND allows you to do things like substitute entire objects or array (where as declarative config explicitly limits substitution to primitive types). So collector substitution is a superset of declarative config substitution. The motivation behind this was simplicity: since declarative config is going to be implemented 10+ times, we want the implementation burden to be lower.
The question we need to answer in the java is whether system property support is important enough for us to either:
- Not complying with the spec
- Try to update the spec to reflect our use cases
Personally, I don't think spec non-compliance is that big of a deal here. But I also want to be use case motivated. @MikeGoldsmith (or anyone else) have you heard this requested from users?
There was a problem hiding this comment.
yeah, I think it's good to support both env vars and system properties in the Java ecosystem
There was a problem hiding this comment.
I've not heard anything directly that using system properties like this is a hard requirement but I definitely see the benefit for those who prefer them over env vars.
I've opened a spec issue and draft PR to add support for per-language prefixes.
| // Matches ${VAR_NAME}, ${env:VAR_NAME}, or ${sys:property.name} with optional :-default | ||
| private static final Pattern ENV_VARIABLE_REFERENCE = | ||
| Pattern.compile("\\$\\{([a-zA-Z_][a-zA-Z0-9_]*)(:-([^\n}]*))?}"); | ||
| Pattern.compile("\\$\\{(?:(env|sys):)?([a-zA-Z_][a-zA-Z0-9_.]*)(?::-([^\\n}]*))?}"); |
There was a problem hiding this comment.
Even if we don't add support for the sys prefix, we should add support for env for better interop with collector and other languages.
...a/io/opentelemetry/sdk/extension/incubator/fileconfig/DeclarativeConfigurationParseTest.java
Outdated
Show resolved
Hide resolved
jack-berg
left a comment
There was a problem hiding this comment.
Assuming the java SIG agrees on the direction, I like the implementation. Thanks for working on this.
Co-authored-by: Jack Berg <34418638+jack-berg@users.noreply.github.com>
|
Will leave open for a few days to give other @open-telemetry/java-approvers the chance to object. |
Summary
Adds support for Java system property substitution in declarative configuration files, extending the existing environment variable substitution functionality. Users can now reference system properties using
${sys:property.name}syntax alongside environment variables.Addresses #5926
Changes
${sys:property.name}in addition to existing${VAR}and${env:VAR}patterns:-defaultsyntax like env vars${sys:app.version})Testing
Example Usage