-
Notifications
You must be signed in to change notification settings - Fork 10
enhancement(dogstatsd): read bind_host from config #1413
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
Open
webern
wants to merge
4
commits into
main
Choose a base branch
from
matt.briggs/issue-1331-pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eda033d
test(integration): add panoramic dynamic variable resolution init script
webern 8e2becc
test(integration): add dogstatsd bind_host test
webern 6dc0e24
test(integration): add dogstatsd default-bind and non-local-traffic p…
webern 02716cf
enhancement(dogstatsd): read bind_host from config
webern 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
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
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,141 @@ | ||
| //! Runtime-resolved dynamic variables for panoramic integration tests. | ||
| //! | ||
| //! Some integration tests need values that only exist at container runtime — for example, the | ||
| //! container's Docker-assigned IP address. These values aren't known when the test config is | ||
| //! written, so they can't be hardcoded in YAML. | ||
| //! | ||
| //! Dynamic variables solve this with a two-sided mechanism: | ||
| //! | ||
| //! ## Defining a dynamic variable | ||
| //! | ||
| //! In a test's `config.yaml`, add a `PANORAMIC_DYNAMIC_<KEY>` env var whose value is a shell | ||
| //! command. Reference the resolved value anywhere in the config with `{{PANORAMIC_DYNAMIC_<KEY>}}`: | ||
| //! | ||
| //! ```yaml | ||
| //! container: | ||
| //! env: | ||
| //! PANORAMIC_DYNAMIC_CONTAINER_IP: "hostname -i | awk '{print $1}'" | ||
| //! DD_BIND_HOST: "{{PANORAMIC_DYNAMIC_CONTAINER_IP}}" | ||
| //! | ||
| //! assertions: | ||
| //! - type: log_contains | ||
| //! pattern: "listen_addr:{{PANORAMIC_DYNAMIC_CONTAINER_IP}}:8125" | ||
| //! timeout: 15s | ||
| //! ``` | ||
| //! | ||
| //! ## How resolution works | ||
| //! | ||
| //! Two independent resolvers perform the same substitution: | ||
| //! | ||
| //! **Inside the container** — the `00-panoramic-dynamic.sh` cont-init.d script runs before any | ||
| //! services start. It evaluates each `PANORAMIC_DYNAMIC_*` command, writes the result to | ||
| //! `/airlock/dynamic/<KEY>`, resolves `{{PANORAMIC_DYNAMIC_*}}` references in `DD_*` env vars, and | ||
| //! writes the resolved values to `/run/adp/env/` for s6-envdir. ADP never sees placeholder strings. | ||
| //! | ||
| //! **Outside the container** — after the container starts, panoramic polls for | ||
| //! `/airlock/dynamic/.ready`, reads resolved values from `/airlock/dynamic/<KEY>`, and substitutes | ||
| //! `{{PANORAMIC_DYNAMIC_*}}` in assertion patterns before evaluating them. | ||
| //! | ||
| //! Both sides derive values from the same commands in the same container, so they match. | ||
| //! | ||
| //! ## Naming conventions | ||
| //! | ||
| //! - `PANORAMIC_DYNAMIC_*` — test infrastructure, not application config. Consumed by the init | ||
| //! script; never visible to ADP or the core agent. | ||
| //! - `DD_*` — Datadog Agent and ADP config keys. May contain `{{PANORAMIC_DYNAMIC_*}}` references | ||
| //! that get resolved before ADP starts. | ||
| //! | ||
| //! ## Error handling | ||
| //! | ||
| //! - If a `PANORAMIC_DYNAMIC_*` command produces an empty result, panoramic fails the test | ||
| //! immediately with a clear message (the shell command likely failed). | ||
| //! - If an assertion pattern still contains `{{PANORAMIC_DYNAMIC_*}}` after substitution, panoramic | ||
| //! fails the test (the variable was referenced but never defined). | ||
| //! - The init script writes `/airlock/dynamic/.ready` via a bash `trap EXIT`, so the sentinel is | ||
| //! always written regardless of how the script exits. | ||
|
|
||
| use std::{collections::HashMap, time::Duration, time::Instant}; | ||
|
|
||
| use airlock::driver::Driver; | ||
| use saluki_error::{generic_error, ErrorContext as _, GenericError}; | ||
| use tracing::debug; | ||
|
|
||
| use crate::config::TestCase; | ||
|
|
||
| /// Prefix for dynamic variable env vars in the test config. | ||
| pub const ENV_PREFIX: &str = "PANORAMIC_DYNAMIC_"; | ||
|
|
||
| /// Placeholder pattern: `{{PANORAMIC_DYNAMIC_<KEY>}}`. | ||
| const PLACEHOLDER_NEEDLE: &str = "{{PANORAMIC_DYNAMIC_"; | ||
|
|
||
| /// Returns `true` if the test case defines any `PANORAMIC_DYNAMIC_*` env vars. | ||
| pub fn has_dynamic_vars(test_case: &TestCase) -> bool { | ||
| test_case.container.env.keys().any(|k| k.starts_with(ENV_PREFIX)) | ||
| } | ||
|
|
||
| /// Reads resolved dynamic variable values from `/airlock/dynamic/` inside the container. | ||
| /// | ||
| /// Polls for the `/airlock/dynamic/.ready` sentinel (up to 30 seconds), then reads each key file. | ||
| pub async fn read_resolved_vars(driver: &Driver) -> Result<HashMap<String, String>, GenericError> { | ||
| let deadline = Instant::now() + Duration::from_secs(30); | ||
| loop { | ||
| let result = driver | ||
| .exec_in_container(vec!["cat".to_string(), "/airlock/dynamic/.ready".to_string()]) | ||
| .await; | ||
|
|
||
| if result.is_ok() { | ||
| break; | ||
| } | ||
|
|
||
| if Instant::now() > deadline { | ||
| return Err(generic_error!( | ||
| "Timed out waiting for /airlock/dynamic/.ready after 30s." | ||
| )); | ||
| } | ||
|
|
||
| tokio::time::sleep(Duration::from_millis(200)).await; | ||
| } | ||
|
|
||
| let listing = driver | ||
| .exec_in_container(vec!["ls".to_string(), "/airlock/dynamic/".to_string()]) | ||
| .await | ||
| .error_context("Failed to list /airlock/dynamic/.")?; | ||
|
|
||
| let mut vars = HashMap::new(); | ||
| for filename in listing.lines() { | ||
| let filename = filename.trim(); | ||
| if filename.is_empty() || filename == ".ready" { | ||
| continue; | ||
| } | ||
|
|
||
| let value = driver | ||
| .exec_in_container(vec!["cat".to_string(), format!("/airlock/dynamic/{}", filename)]) | ||
| .await | ||
| .error_context(format!("Failed to read /airlock/dynamic/{}.", filename))?; | ||
|
|
||
| debug!(key = filename, value = %value.trim(), "Resolved dynamic variable."); | ||
| vars.insert(filename.to_string(), value.trim().to_string()); | ||
| } | ||
|
|
||
| Ok(vars) | ||
| } | ||
|
|
||
| /// Replace all `{{PANORAMIC_DYNAMIC_*}}` placeholders in a string with resolved values. | ||
| pub fn resolve_placeholders(s: &mut String, vars: &HashMap<String, String>) { | ||
| for (key, value) in vars { | ||
| *s = s.replace(&format!("{{{{PANORAMIC_DYNAMIC_{key}}}}}"), value); | ||
| } | ||
| } | ||
|
|
||
| /// Collect any `{{PANORAMIC_DYNAMIC_*}}` placeholders still present in a string. | ||
| pub fn find_unresolved(s: &str, out: &mut Vec<String>) { | ||
| let mut remaining = s; | ||
| while let Some(start) = remaining.find(PLACEHOLDER_NEEDLE) { | ||
| if let Some(end) = remaining[start..].find("}}") { | ||
| out.push(remaining[start..start + end + 2].to_string()); | ||
| remaining = &remaining[start + end + 2..]; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why two impls?