-
Notifications
You must be signed in to change notification settings - Fork 21
feat: added environment capability [APMSP-3780] #2239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+93
−11
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0d17bb7
feat: added environment capability
Aaalibaba42 27390d0
feat!: make set and unset unsafe
Aaalibaba42 6b5cd46
revert: remove set/unset since they are too risky for libdatadog's ki…
Aaalibaba42 df2e0f1
fix: better error message
Aaalibaba42 5fedbee
docs: explain why set and unset are excluded
Aaalibaba42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Native environment-variable capability backed by `std::env`. | ||
|
|
||
| use std::env::VarError; | ||
|
|
||
| use libdd_capabilities::env::{EnvCapability, EnvError}; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct NativeEnvCapability; | ||
|
|
||
| impl EnvCapability for NativeEnvCapability { | ||
| fn new() -> Self { | ||
| Self | ||
| } | ||
|
|
||
| fn get(&self, name: &str) -> Result<Option<String>, EnvError> { | ||
| match std::env::var(name) { | ||
| Ok(v) => Ok(Some(v)), | ||
| Err(VarError::NotPresent) => Ok(None), | ||
| Err(VarError::NotUnicode(_)) => Err(EnvError::NotUnicode(name.to_owned())), | ||
|
Aaalibaba42 marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn get_absent_returns_none() { | ||
| let cap = NativeEnvCapability; | ||
| // A name unlikely to be set in any environment. | ||
| assert_eq!(cap.get("LIBDD_CAP_TEST_ABSENT_XYZZY").unwrap(), None); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Environment-variable capability trait and error types. | ||
| //! | ||
| //! Sync: env access is a single map lookup on both native (`std::env`) and | ||
| //! wasm (`process.env`). | ||
| //! | ||
| //! `set` and `unset` are intentionally absent from this trait. libdatadog is | ||
| //! embedded in many kinds of runtime, where mutating the process environment | ||
| //! very much unsafe. Exposing mutation here would make it trivially easy for | ||
| //! callers to corrupt the environment of a multi-threaded host process. | ||
|
|
||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum EnvError { | ||
| #[error("The value of the environment variable `{0}` is not valid UTF-8")] | ||
| NotUnicode(String), | ||
| #[error("IO error: {0}")] | ||
| Io(anyhow::Error), | ||
| } | ||
|
|
||
| pub trait EnvCapability: Clone + std::fmt::Debug { | ||
| fn new() -> Self; | ||
|
|
||
| /// Read an env var. | ||
| /// | ||
| /// `Ok(None)` means the variable is unset; `Err(NotUnicode)` means it is | ||
| /// set but its value is not valid UTF-8. Callers that treat "missing" and | ||
| /// "invalid" the same should collapse both branches explicitly. | ||
| fn get(&self, name: &str) -> Result<Option<String>, EnvError>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.