Skip to content

Add internal profiling to debug tool #32423

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
merged 11 commits into from
May 15, 2025
4 changes: 3 additions & 1 deletion Cargo.lock

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

16 changes: 4 additions & 12 deletions doc/user/data/mz-debug/emulator_options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@ columns:
- column: "Option"
- column: "Description"
rows:
- Option: "`--dump-docker <boolean>`"
Description: |

<a name="dump-docker"></a> If `true`, dump debug information from the Docker container.

Defaults to `true`.

- Option: "`--docker-container-id <ID>`"
Description: |

<a name="docker-container-id"></a> The Docker container to dump.

Required if [`--dump-docker`](#dump-docker) is true.
Required.

- Option: "`--mz-connection-url <URL>`"
- Option: "`--dump-docker <boolean>`"
Description: |

<a name="mz-connection-url"></a>The URL of the Materialize's SQL
connection.
<a name="dump-docker"></a> If `true`, dump debug information from the Docker container.

Defaults to `postgres://127.0.0.1:6875/materialize?sslmode=prefer`.
Defaults to `true`.
26 changes: 26 additions & 0 deletions doc/user/data/mz-debug/mz_debug_option.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,36 @@ columns:
- column: "Option"
- column: "Description"
rows:
- Option: "`--dump-heap-profiles <boolean>`"
Description: |

<a name="dump-heap-profiles"></a> If `true`, dump heap profiles (.pprof.gz) from
your Materialize instance.

Defaults to `true`.
- Option: "`--dump-prometheus-metrics <boolean>`"
Description: |

<a name="dump-prometheus-metrics"></a> If `true`, dump prometheus metrics from
your Materialize instance.

Defaults to `true`.


- Option: "`--dump-system-catalog <boolean>`"
Description: |

<a name="dump-system-catalog"></a> If `true`, dump the system catalog from
your Materialize instance.

Defaults to `true`.


- Option: "`--mz-connection-url <URL>`"
Description: |

<a name="mz-connection-url"></a>The Materialize instance's [PostgreSQL
connection
URL](https://www.postgresql.org/docs/14/libpq-connect.html#LIBPQ-CONNSTRING).

Defaults to `postgres://127.0.0.1:6875/materialize?sslmode=prefer`.
12 changes: 0 additions & 12 deletions doc/user/data/mz-debug/self_managed_options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,6 @@ rows:

Defaults to `6875`.

- Option: "`--mz-connection-url <URL>`"
Description: |

<a name="mz-connection-url"></a>The Materialize instance's [PostgreSQL
connection
URL](https://www.postgresql.org/docs/14/libpq-connect.html#LIBPQ-CONNSTRING).

Defaults to a connection URL constructed from:

[`--port-forward-local-address`](#port-forward-local-address) and
[`--port-forward-local-port`](#port-forward-local-port) values.

- Option: "`-h`, `--help`"
Description: |

Expand Down
2 changes: 1 addition & 1 deletion src/mz-debug/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ rust_binary(
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.1.0",
version = "0.2.0",
deps = [
"//src/build-info:mz_build_info",
"//src/cloud-resources:mz_cloud_resources",
Expand Down
4 changes: 3 additions & 1 deletion src/mz-debug/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "mz-debug"
description = "Debug tool for self-managed Materialize."
version = "0.1.0"
version = "0.2.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bumping the version like this is a good habit, but out of curiosity is it necessary? do we publish it anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary, just something manual I do whenever I decide to publish haha. We do publish it as a curlable binary that we expose via the docs!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And technically one can download old versions, though we use the version via a git tag, so it never actually reads this cargo.toml. We have instructions to update it here /Users/sangjunbak/materialize/ci/deploy_mz-debug/README.md but kinda upto the developer to make sure both are in sync 😬

edition.workspace = true
rust-version.workspace = true
publish = false
Expand All @@ -22,13 +22,15 @@ mz-cloud-resources = { path = "../cloud-resources"}
mz-ore = { path = "../ore", features = ["cli", "test"] }
mz-tls-util = { path = "../tls-util" }
postgres-openssl = { version = "0.5.0" }
reqwest = { version = "0.11", features = ["stream"] }
serde = "1.0.219"
serde_yaml = "0.9.34"
tokio = "1.44.1"
tokio-postgres = { version = "0.7.8" }
tokio-util = { version = "0.7.15", features = ["io"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.19", default-features = false, features = ["env-filter", "fmt"] }
url = { version = "2.3.1", features = ["serde"] }
walkdir = "2.5"
workspace-hack = { version = "0.0.0", path = "../workspace-hack", optional = true }
zip = { version = "2.6", default-features = false, features=["deflate-flate2", "flate2"]}
Expand Down
38 changes: 34 additions & 4 deletions src/mz-debug/src/docker_dumper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ use std::io::Write;
use std::path::PathBuf;
use std::time::Duration;

use anyhow::Context as AnyhowContext;
use mz_ore::retry::{self, RetryResult};
use tracing::{info, warn};

use crate::utils::format_base_path;
use crate::{ContainerDumper, Context};

static DOCKER_DUMP_DIR: &str = "docker";
static DOCKER_RESOURCE_DUMP_TIMEOUT: Duration = Duration::from_secs(30);

pub struct DockerDumper {
Expand All @@ -36,9 +37,7 @@ pub struct DockerDumper {
impl DockerDumper {
pub fn new(context: &Context, container_id: String) -> Self {
Self {
directory_path: format_base_path(context.start_time)
.join("docker")
.join(&container_id),
directory_path: context.base_path.join(DOCKER_DUMP_DIR).join(&container_id),
container_id,
}
}
Expand Down Expand Up @@ -149,3 +148,34 @@ fn write_output(
info!("Exported {}", file_path.display());
Ok(())
}

/// Gets the IP address of a Docker container using the container ID.
pub async fn get_container_ip(container_id: &str) -> Result<String, anyhow::Error> {
let output = tokio::process::Command::new("docker")
.args([
"inspect",
"-f",
"{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}",
container_id,
])
.output()
.await
.with_context(|| format!("Failed to get container IP address for {}", container_id))?;

if !output.status.success() {
return Err(anyhow::anyhow!(
"Docker command failed: {}",
String::from_utf8_lossy(&output.stderr)
));
}

let ip = String::from_utf8(output.stdout)
.with_context(|| "Failed to convert container IP address to string")?
.trim()
.to_string();
if ip.is_empty() {
return Err(anyhow::anyhow!("Container IP address not found"));
}

Ok(ip)
}
Loading