Skip to content

Commit

Permalink
Add log level and log destination for int proxy (#2247)
Browse files Browse the repository at this point in the history
* Add log level and log destination for int proxy

* schema
  • Loading branch information
aviramha authored Feb 18, 2024
1 parent 8d19bf6 commit 59c3c46
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 6 deletions.
25 changes: 23 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions changelog.d/2246.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add log level and log destination for int proxy
20 changes: 18 additions & 2 deletions mirrord-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@
"type": "object",
"properties": {
"idle_timeout": {
"title": "internal_proxy.idle_timeout {#agent-idle_timeout}",
"title": "internal_proxy.idle_timeout {#internal_proxy-idle_timeout}",
"description": "How much time to wait while we don't have any active connections before exiting.\n\nCommon cases would be running a chain of processes that skip using the layer and don't connect to the proxy.\n\n```json { \"internal_proxy\": { \"idle_timeout\": 30 } } ```",
"type": [
"integer",
Expand All @@ -805,8 +805,24 @@
"format": "uint64",
"minimum": 0.0
},
"log_destination": {
"title": "internal_proxy.log_destination {#internal_proxy-log_destination}",
"description": "Set the log file destination for the internal proxy.",
"type": [
"string",
"null"
]
},
"log_level": {
"title": "internal_proxy.log_level {#internal_proxy-log_level}",
"description": "Set the log level for the internal proxy. RUST_LOG convention (i.e `mirrord=trace`) will only beu sed if log_destination is set",
"type": [
"string",
"null"
]
},
"start_idle_timeout": {
"title": "internal_proxy.start_idle_timeout {#agent-start_idle_timeout}",
"title": "internal_proxy.start_idle_timeout {#internal_proxy-start_idle_timeout}",
"description": "How much time to wait for the first connection to the proxy in seconds.\n\nCommon cases would be running with dlv or any other debugger, which sets a breakpoint on process execution, delaying the layer startup and connection to proxy.\n\n```json { \"internal_proxy\": { \"start_idle_timeout\": 60 } } ```",
"type": [
"integer",
Expand Down
1 change: 1 addition & 0 deletions mirrord/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ tokio-util.workspace = true
socket2.workspace = true
drain.workspace = true
clap_complete = "4.4.1"
tracing-appender = "0.2"

[target.'cfg(target_os = "macos")'.dependencies]
mirrord-sip = { path = "../sip" }
Expand Down
3 changes: 3 additions & 0 deletions mirrord/cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ pub(crate) enum CliError {
r#"This is a bug. Please report it in our Discord or GitHub repository. {GENERAL_HELP}"#
))]
ConnectRequestBuildError(HttpError),
#[error("Failed to open log file for writing: {0:#?}")]
#[diagnostic(help(r#"Check that int proxy log file is in valid writable path"#))]
OpenIntProxyLogFile(std::io::Error),
}

impl From<OperatorApiError> for CliError {
Expand Down
17 changes: 17 additions & 0 deletions mirrord/cli/src/internal_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use nix::libc;
use tokio::{net::TcpListener, sync::mpsc, task::JoinHandle};
use tokio_util::sync::CancellationToken;
use tracing::{error, info, log::trace};
use tracing_subscriber::EnvFilter;

use crate::{
connection::AGENT_CONNECT_INFO_ENV_KEY,
Expand Down Expand Up @@ -144,6 +145,22 @@ fn get_agent_connect_info() -> Result<Option<AgentConnectInfo>> {
/// It listens for inbound layer connect and forwards to agent.
pub(crate) async fn proxy(watch: drain::Watch) -> Result<()> {
let config = LayerConfig::from_env()?;

if let Some(ref log_destination) = config.internal_proxy.log_destination {
let output_file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(log_destination)
.map_err(CliError::OpenIntProxyLogFile)?;
let tracing_registry = tracing_subscriber::fmt().with_writer(output_file);
if let Some(ref log_level) = config.internal_proxy.log_level {
tracing_registry
.with_env_filter(EnvFilter::builder().parse_lossy(log_level))
.init();
} else {
tracing_registry.init();
}
}
let agent_connect_info = get_agent_connect_info()?;

let mut analytics = AnalyticsReporter::new(config.telemetry, watch);
Expand Down
1 change: 1 addition & 0 deletions mirrord/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ fn init_ext_error_handler(commands: &Commands) -> bool {
let _ = miette::set_hook(Box::new(|_| Box::new(JSONReportHandler::new())));
true
}
Commands::InternalProxy => true,
_ => false,
}
}
Expand Down
14 changes: 12 additions & 2 deletions mirrord/config/src/internal_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::config::source::MirrordConfigSource;
#[config(map_to = "InternalProxyFileConfig", derive = "JsonSchema")]
#[cfg_attr(test, config(derive = "PartialEq"))]
pub struct InternalProxyConfig {
/// ### internal_proxy.start_idle_timeout {#agent-start_idle_timeout}
/// ### internal_proxy.start_idle_timeout {#internal_proxy-start_idle_timeout}
///
/// How much time to wait for the first connection to the proxy in seconds.
///
Expand All @@ -38,7 +38,7 @@ pub struct InternalProxyConfig {
#[config(default = 60)]
pub start_idle_timeout: u64,

/// ### internal_proxy.idle_timeout {#agent-idle_timeout}
/// ### internal_proxy.idle_timeout {#internal_proxy-idle_timeout}
///
/// How much time to wait while we don't have any active connections before exiting.
///
Expand All @@ -54,4 +54,14 @@ pub struct InternalProxyConfig {
/// ```
#[config(default = 5)]
pub idle_timeout: u64,

/// ### internal_proxy.log_level {#internal_proxy-log_level}
/// Set the log level for the internal proxy.
/// RUST_LOG convention (i.e `mirrord=trace`)
/// will only beu sed if log_destination is set
pub log_level: Option<String>,

/// ### internal_proxy.log_destination {#internal_proxy-log_destination}
/// Set the log file destination for the internal proxy.
pub log_destination: Option<String>,
}

0 comments on commit 59c3c46

Please sign in to comment.