Skip to content

Commit 503f4bf

Browse files
authored
fix(agent-data-plane): ensure logs are flushed before exiting (#1201)
1 parent f208111 commit 503f4bf

File tree

5 files changed

+45
-22
lines changed

5 files changed

+45
-22
lines changed

.gitlab/e2e.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,17 @@ test-integration:
115115
retry: 2
116116
timeout: 10m
117117
image: "${SALUKI_BUILD_CI_IMAGE}"
118+
artifacts:
119+
expire_in: 1 week
120+
paths:
121+
- integration-logs/
122+
when: always
118123
variables:
119124
# Looks weird but we need to set this so that the test runner knows the right image path to pull the `alpine` image, which is required by
120125
# `airlock` to properly configure some permissions on the shared volume before the target container is started. We don't have access to
121126
# Docker Hub so the default of `alpine:latest` is insufficient.
122127
GROUND_TRUTH_ALPINE_IMAGE: registry.ddbuild.io/alpine:latest
128+
PANORAMIC_LOG_DIR: integration-logs
123129
script:
124130
# Copy the bundled Agent/ADP image we built to the local Docker instance so we can share the integration
125131
# test configurations between CI and local test runs.

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,13 @@ build-panoramic: ## Builds the panoramic binary (ADP integration test runner)
557557
test-integration: build-panoramic build-datadog-agent-image
558558
test-integration: ## Runs all ADP integration tests
559559
@echo "[*] Running ADP integration tests..."
560-
@target/release/panoramic run -d $(shell pwd)/test/integration/cases
560+
@target/release/panoramic run -d $(shell pwd)/test/integration/cases $(if $(PANORAMIC_LOG_DIR),-l $(PANORAMIC_LOG_DIR))
561561

562562
.PHONY: test-integration-quick
563563
test-integration-quick: build-panoramic
564564
test-integration-quick: ## Runs ADP integration tests (assumes images already built)
565565
@echo "[*] Running ADP integration tests (quick mode)..."
566-
@target/release/panoramic run -d $(shell pwd)/test/integration/cases
566+
@target/release/panoramic run -d $(shell pwd)/test/integration/cases $(if $(PANORAMIC_LOG_DIR),-l $(PANORAMIC_LOG_DIR))
567567

568568
.PHONY: list-integration-tests
569569
list-integration-tests: build-panoramic

bin/agent-data-plane/src/main.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
66
#![deny(warnings)]
77
#![deny(missing_docs)]
8-
use std::time::Instant;
8+
use std::{path::PathBuf, time::Instant};
99

1010
use saluki_app::bootstrap::AppBootstrapper;
11-
use saluki_config::ConfigurationLoader;
11+
use saluki_config::{ConfigurationLoader, GenericConfiguration};
1212
use saluki_error::{ErrorContext as _, GenericError};
1313
use tracing::{error, info, warn};
1414

@@ -64,25 +64,41 @@ async fn main() -> Result<(), GenericError> {
6464
.error_context("Failed to complete bootstrap phase.")?;
6565

6666
// Run the given subcommand.
67-
match cli.action {
67+
let maybe_exit_code = run_inner(cli.action, started, bootstrap_config_path, bootstrap_config).await?;
68+
69+
// Drop the bootstrap guard to ensure logs are flushed, etc.
70+
drop(_bootstrap_guard);
71+
72+
// Exit with the specific exit code, if one was provided.
73+
if let Some(exit_code) = maybe_exit_code {
74+
std::process::exit(exit_code);
75+
}
76+
77+
Ok(())
78+
}
79+
80+
async fn run_inner(
81+
action: Action, started: Instant, bootstrap_config_path: PathBuf, bootstrap_config: GenericConfiguration,
82+
) -> Result<Option<i32>, GenericError> {
83+
match action {
6884
Action::Run(cmd) => {
6985
// Populate our PID file, if configured.
7086
if let Some(pid_file) = &cmd.pid_file {
7187
let pid = std::process::id();
7288
if let Err(e) = std::fs::write(pid_file, pid.to_string()) {
7389
error!(error = %e, path = %pid_file.display(), "Failed to update PID file. Exiting.");
74-
std::process::exit(1);
90+
return Ok(Some(1));
7591
}
7692
}
7793

7894
let exit_code = match handle_run_command(started, bootstrap_config_path, bootstrap_config).await {
7995
Ok(()) => {
8096
info!("Agent Data Plane stopped.");
81-
0
97+
None
8298
}
8399
Err(e) => {
84100
error!("{:?}", e);
85-
1
101+
Some(1)
86102
}
87103
};
88104

@@ -93,12 +109,14 @@ async fn main() -> Result<(), GenericError> {
93109
}
94110
}
95111

96-
std::process::exit(exit_code);
112+
if let Some(exit_code) = exit_code {
113+
return Ok(Some(exit_code));
114+
}
97115
}
98116
Action::Debug(cmd) => handle_debug_command(&bootstrap_config, cmd).await,
99117
Action::Config(_) => handle_config_command(&bootstrap_config).await,
100118
Action::Dogstatsd(cmd) => handle_dogstatsd_command(&bootstrap_config, cmd).await,
101119
}
102120

103-
Ok(())
121+
Ok(None)
104122
}

bin/correctness/panoramic/src/cli.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ pub struct RunCommand {
4949
#[argh(switch)]
5050
pub no_tui: bool,
5151

52+
/// directory to write container logs to (default: auto-generated temp dir)
53+
#[argh(option, short = 'l')]
54+
pub log_dir: Option<PathBuf>,
55+
5256
/// skip writing container logs to disk
5357
#[argh(switch)]
5458
pub no_logs: bool,

bin/correctness/panoramic/src/main.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn initialize_logging() {
7474
.init();
7575
}
7676

77-
async fn run_tests(cmd: cli::RunCommand, use_tui: bool) -> ExitCode {
77+
async fn run_tests(mut cmd: cli::RunCommand, use_tui: bool) -> ExitCode {
7878
let mut test_cases = match discover_tests(&cmd.test_dir) {
7979
Ok(tests) => tests,
8080
Err(e) => {
@@ -115,8 +115,12 @@ async fn run_tests(cmd: cli::RunCommand, use_tui: bool) -> ExitCode {
115115
let log_dir = if cmd.no_logs {
116116
None
117117
} else {
118-
match create_log_dir() {
119-
Ok(dir) => Some(dir),
118+
let dir = cmd.log_dir.take().unwrap_or_else(|| {
119+
let timestamp = Local::now().format("%Y%m%d-%H%M%S");
120+
std::env::temp_dir().join(format!("panoramic-{}", timestamp))
121+
});
122+
match std::fs::create_dir_all(&dir) {
123+
Ok(()) => Some(dir),
120124
Err(e) => {
121125
if use_tui {
122126
eprintln!("Failed to create log directory: {}", e);
@@ -156,15 +160,6 @@ async fn run_tests(cmd: cli::RunCommand, use_tui: bool) -> ExitCode {
156160
}
157161
}
158162

159-
/// Create a unique temporary directory for container logs.
160-
fn create_log_dir() -> std::io::Result<PathBuf> {
161-
let timestamp = Local::now().format("%Y%m%d-%H%M%S");
162-
let dir_name = format!("panoramic-{}", timestamp);
163-
let log_dir = std::env::temp_dir().join(dir_name);
164-
std::fs::create_dir_all(&log_dir)?;
165-
Ok(log_dir)
166-
}
167-
168163
/// Run with the TUI consumer.
169164
async fn run_with_tui_consumer(
170165
rx: mpsc::UnboundedReceiver<TestEvent>, cancel_token: CancellationToken, log_dir: Option<PathBuf>,

0 commit comments

Comments
 (0)