Skip to content

Commit 4487749

Browse files
authored
Add internal profiling to debug tool (#32423)
<!-- Describe the contents of the PR briefly but completely. If you write detailed commit messages, it is acceptable to copy/paste them here, or write "see commit messages for details." If there is only one commit in the PR, GitHub will have already added its commit message above. --> See commit messages for details ### Motivation * This PR adds a known-desirable feature. "Scrape prometheus metrics" and "Scrape profiling" from MaterializeInc/database-issues#8908 <!-- Which of the following best describes the motivation behind this PR? * This PR fixes a recognized bug. [Ensure issue is linked somewhere.] * This PR adds a known-desirable feature. [Ensure issue is linked somewhere.] * This PR fixes a previously unreported bug. [Describe the bug in detail, as if you were filing a bug report.] * This PR adds a feature that has not yet been specified. [Write a brief specification for the feature, including justification for its inclusion in Materialize, as if you were writing the original feature specification.] * This PR refactors existing code. [Describe what was wrong with the existing code, if it is not obvious.] --> ### Tips for reviewer <!-- Leave some tips for your reviewer, like: * The diff is much smaller if viewed with whitespace hidden. * [Some function/module/file] deserves extra attention. * [Some function/module/file] is pure code movement and only needs a skim. Delete this section if no tips. --> ### Checklist - [ ] This PR has adequate test coverage / QA involvement has been duly considered. ([trigger-ci for additional test/nightly runs](https://trigger-ci.dev.materialize.com/)) - [ ] This PR has an associated up-to-date [design doc](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/design/README.md), is a design doc ([template](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/design/00000000_template.md)), or is sufficiently small to not require a design. <!-- Reference the design in the description. --> - [ ] If this PR evolves [an existing `$T ⇔ Proto$T` mapping](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/command-and-response-binary-encoding.md) (possibly in a backwards-incompatible way), then it is tagged with a `T-proto` label. - [ ] If this PR will require changes to cloud orchestration or tests, there is a companion cloud PR to account for those changes that is tagged with the release-blocker label ([example](MaterializeInc/cloud#5021)). <!-- Ask in #team-cloud on Slack if you need help preparing the cloud PR. --> - [ ] If this PR includes major [user-facing behavior changes](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/guide-changes.md#what-changes-require-a-release-note), I have pinged the relevant PM to schedule a changelog post.
1 parent 62ef02d commit 4487749

13 files changed

+803
-322
lines changed

Cargo.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/user/data/mz-debug/emulator_options.yml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,16 @@ columns:
22
- column: "Option"
33
- column: "Description"
44
rows:
5-
- Option: "`--dump-docker <boolean>`"
6-
Description: |
7-
8-
<a name="dump-docker"></a> If `true`, dump debug information from the Docker container.
9-
10-
Defaults to `true`.
11-
125
- Option: "`--docker-container-id <ID>`"
136
Description: |
147
158
<a name="docker-container-id"></a> The Docker container to dump.
169
17-
Required if [`--dump-docker`](#dump-docker) is true.
10+
Required.
1811
19-
- Option: "`--mz-connection-url <URL>`"
12+
- Option: "`--dump-docker <boolean>`"
2013
Description: |
2114
22-
<a name="mz-connection-url"></a>The URL of the Materialize's SQL
23-
connection.
15+
<a name="dump-docker"></a> If `true`, dump debug information from the Docker container.
2416
25-
Defaults to `postgres://127.0.0.1:6875/materialize?sslmode=prefer`.
17+
Defaults to `true`.

doc/user/data/mz-debug/mz_debug_option.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,36 @@ columns:
22
- column: "Option"
33
- column: "Description"
44
rows:
5+
- Option: "`--dump-heap-profiles <boolean>`"
6+
Description: |
7+
8+
<a name="dump-heap-profiles"></a> If `true`, dump heap profiles (.pprof.gz) from
9+
your Materialize instance.
10+
11+
Defaults to `true`.
12+
- Option: "`--dump-prometheus-metrics <boolean>`"
13+
Description: |
14+
15+
<a name="dump-prometheus-metrics"></a> If `true`, dump prometheus metrics from
16+
your Materialize instance.
17+
18+
Defaults to `true`.
19+
20+
521
- Option: "`--dump-system-catalog <boolean>`"
622
Description: |
723
824
<a name="dump-system-catalog"></a> If `true`, dump the system catalog from
925
your Materialize instance.
1026
1127
Defaults to `true`.
28+
29+
30+
- Option: "`--mz-connection-url <URL>`"
31+
Description: |
32+
33+
<a name="mz-connection-url"></a>The Materialize instance's [PostgreSQL
34+
connection
35+
URL](https://www.postgresql.org/docs/14/libpq-connect.html#LIBPQ-CONNSTRING).
36+
37+
Defaults to `postgres://127.0.0.1:6875/materialize?sslmode=prefer`.

doc/user/data/mz-debug/self_managed_options.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,6 @@ rows:
5757
5858
Defaults to `6875`.
5959
60-
- Option: "`--mz-connection-url <URL>`"
61-
Description: |
62-
63-
<a name="mz-connection-url"></a>The Materialize instance's [PostgreSQL
64-
connection
65-
URL](https://www.postgresql.org/docs/14/libpq-connect.html#LIBPQ-CONNSTRING).
66-
67-
Defaults to a connection URL constructed from:
68-
69-
[`--port-forward-local-address`](#port-forward-local-address) and
70-
[`--port-forward-local-port`](#port-forward-local-port) values.
71-
7260
- Option: "`-h`, `--help`"
7361
Description: |
7462

src/mz-debug/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ rust_binary(
3434
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
3535
"//conditions:default": [],
3636
}),
37-
version = "0.1.0",
37+
version = "0.2.0",
3838
deps = [
3939
"//src/build-info:mz_build_info",
4040
"//src/cloud-resources:mz_cloud_resources",

src/mz-debug/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "mz-debug"
33
description = "Debug tool for self-managed Materialize."
4-
version = "0.1.0"
4+
version = "0.2.0"
55
edition.workspace = true
66
rust-version.workspace = true
77
publish = false
@@ -22,13 +22,15 @@ mz-cloud-resources = { path = "../cloud-resources"}
2222
mz-ore = { path = "../ore", features = ["cli", "test"] }
2323
mz-tls-util = { path = "../tls-util" }
2424
postgres-openssl = { version = "0.5.0" }
25+
reqwest = { version = "0.11", features = ["stream"] }
2526
serde = "1.0.219"
2627
serde_yaml = "0.9.34"
2728
tokio = "1.44.1"
2829
tokio-postgres = { version = "0.7.8" }
2930
tokio-util = { version = "0.7.15", features = ["io"] }
3031
tracing = "0.1.37"
3132
tracing-subscriber = { version = "0.3.19", default-features = false, features = ["env-filter", "fmt"] }
33+
url = { version = "2.3.1", features = ["serde"] }
3234
walkdir = "2.5"
3335
workspace-hack = { version = "0.0.0", path = "../workspace-hack", optional = true }
3436
zip = { version = "2.6", default-features = false, features=["deflate-flate2", "flate2"]}

src/mz-debug/src/docker_dumper.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ use std::io::Write;
2020
use std::path::PathBuf;
2121
use std::time::Duration;
2222

23+
use anyhow::Context as AnyhowContext;
2324
use mz_ore::retry::{self, RetryResult};
2425
use tracing::{info, warn};
2526

26-
use crate::utils::format_base_path;
2727
use crate::{ContainerDumper, Context};
2828

29+
static DOCKER_DUMP_DIR: &str = "docker";
2930
static DOCKER_RESOURCE_DUMP_TIMEOUT: Duration = Duration::from_secs(30);
3031

3132
pub struct DockerDumper {
@@ -36,9 +37,7 @@ pub struct DockerDumper {
3637
impl DockerDumper {
3738
pub fn new(context: &Context, container_id: String) -> Self {
3839
Self {
39-
directory_path: format_base_path(context.start_time)
40-
.join("docker")
41-
.join(&container_id),
40+
directory_path: context.base_path.join(DOCKER_DUMP_DIR).join(&container_id),
4241
container_id,
4342
}
4443
}
@@ -149,3 +148,34 @@ fn write_output(
149148
info!("Exported {}", file_path.display());
150149
Ok(())
151150
}
151+
152+
/// Gets the IP address of a Docker container using the container ID.
153+
pub async fn get_container_ip(container_id: &str) -> Result<String, anyhow::Error> {
154+
let output = tokio::process::Command::new("docker")
155+
.args([
156+
"inspect",
157+
"-f",
158+
"{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}",
159+
container_id,
160+
])
161+
.output()
162+
.await
163+
.with_context(|| format!("Failed to get container IP address for {}", container_id))?;
164+
165+
if !output.status.success() {
166+
return Err(anyhow::anyhow!(
167+
"Docker command failed: {}",
168+
String::from_utf8_lossy(&output.stderr)
169+
));
170+
}
171+
172+
let ip = String::from_utf8(output.stdout)
173+
.with_context(|| "Failed to convert container IP address to string")?
174+
.trim()
175+
.to_string();
176+
if ip.is_empty() {
177+
return Err(anyhow::anyhow!("Container IP address not found"));
178+
}
179+
180+
Ok(ip)
181+
}

0 commit comments

Comments
 (0)