Skip to content

Commit 95eff73

Browse files
Merge pull request #41 from theseus-rs/trace-instrumentation
feat: add tracing instrumentation
2 parents 1702bc3 + a672bef commit 95eff73

File tree

10 files changed

+64
-44
lines changed

10 files changed

+64
-44
lines changed

Cargo.toml

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,33 @@ repository = "https://github.com/theseus-rs/postgresql-embedded"
1313
version = "0.7.1"
1414

1515
[workspace.dependencies]
16-
anyhow = { version = "1.0" }
17-
async-trait = { version = "0.1" }
18-
bytes = { version = "1.5" }
19-
criterion = { version = "0.5" }
20-
flate2 = { version = "1.0" }
21-
hex = { version = "0.4" }
22-
home = { version = "0.5" }
23-
human_bytes = { version = "0.4", default-features = false }
24-
lazy_static = { version = "1.4" }
25-
num-format = { version = "0.4" }
26-
rand = { version = "0.8" }
27-
regex = { version = "1.10" }
28-
reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls-native-roots"] }
29-
reqwest-middleware = { version = "0.2.4" }
30-
reqwest-retry = { version = "0.4.0" }
31-
reqwest-tracing = { version = "0.4.0" }
32-
serde = { version = "1.0", features = ["derive"] }
33-
serde_json = { version = "1.0" }
34-
sha2 = { version = "0.10" }
35-
tar = { version = "0.4" }
36-
task-local-extensions = { version = "0.1" }
37-
target-triple = { version = "0.1" }
38-
test-log = { version = "0.2" }
39-
tempfile = { version = "3.10" }
40-
thiserror = { version = "1.0" }
41-
tokio = { version = "1.36", features = ["full"] }
42-
tracing = { version = "0.1", features = ["log"] }
16+
anyhow = "1.0.81"
17+
async-trait = "0.1.77"
18+
bytes = "1.5.0"
19+
criterion = "0.5.1"
20+
flate2 = "1.0.28"
21+
hex = "0.4.3"
22+
home = "0.5.9"
23+
human_bytes = { version = "0.4.3", default-features = false }
24+
lazy_static = "1.4.0"
25+
num-format = "0.4.4"
26+
rand = "0.8.5"
27+
regex = "1.10.3"
28+
reqwest = { version = "0.11.26", default-features = false }
29+
reqwest-middleware = "0.2.4"
30+
reqwest-retry = "0.4.0"
31+
reqwest-tracing = "0.4.0"
32+
serde = "1.0.197"
33+
serde_json = "1.0.114"
34+
sha2 = "0.10.8"
35+
tar = "0.4.40"
36+
task-local-extensions = "0.1.4"
37+
target-triple = "0.1.2"
38+
test-log = "0.2.15"
39+
tempfile = "3.10.1"
40+
thiserror = "1.0.58"
41+
tokio = "1.36.0"
42+
tracing = "0.1.40"
4343

4444
[workspace.metadata.release]
4545
shared-version = true

examples/archive_async/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ version.workspace = true
88
[dependencies]
99
postgresql_archive = { path = "../../postgresql_archive" }
1010
tempfile = { workspace = true }
11-
tokio = { workspace = true }
11+
tokio = { workspace = true, features = ["full"] }

examples/embedded_async/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ version.workspace = true
77

88
[dependencies]
99
postgresql_embedded = { path = "../../postgresql_embedded" }
10-
tokio = { workspace = true }
10+
tokio = { workspace = true, features = ["full"] }

examples/postgres_embedded/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ version.workspace = true
77

88
[dependencies]
99
anyhow = { workspace = true }
10-
postgres = { version = "0.19" }
10+
postgres = { version = "0.19.7" }
1111
postgresql_embedded = { path = "../../postgresql_embedded", features = ["blocking"] }

examples/sqlx_embedded/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ version.workspace = true
88
[dependencies]
99
anyhow = { workspace = true }
1010
postgresql_embedded = { path = "../../postgresql_embedded" }
11-
sqlx = { version = "0.7", default-features = true, features = ["postgres", "runtime-tokio"] }
12-
tokio = { workspace = true }
11+
sqlx = { version = "0.7.4", default-features = true, features = ["postgres", "runtime-tokio"] }
12+
tokio = { workspace = true, features = ["full"] }

postgresql_archive/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@ async-trait = { workspace = true }
1515
bytes = { workspace = true }
1616
flate2 = { workspace = true }
1717
hex = { workspace = true }
18-
human_bytes = { workspace = true }
18+
human_bytes = { workspace = true, default-features = false }
1919
lazy_static = { workspace = true }
2020
num-format = { workspace = true }
2121
regex = { workspace = true }
22-
reqwest = { workspace = true }
22+
reqwest = { workspace = true, default-features = false, features = ["json", "rustls-tls-native-roots"] }
2323
reqwest-middleware = { workspace = true }
2424
reqwest-retry = { workspace = true }
2525
reqwest-tracing = { workspace = true }
26-
serde = { workspace = true }
26+
serde = { workspace = true, features = ["derive"] }
2727
serde_json = { workspace = true }
2828
sha2 = { workspace = true }
2929
tar = { workspace = true }
3030
target-triple = { workspace = true }
3131
task-local-extensions = { workspace = true }
3232
tempfile = { workspace = true }
3333
thiserror = { workspace = true }
34-
tokio = { workspace = true, optional = true }
35-
tracing = { workspace = true }
34+
tokio = { workspace = true, features = ["full"], optional = true }
35+
tracing = { workspace = true, features = ["log"] }
3636

3737
[dev-dependencies]
3838
criterion = { workspace = true }

postgresql_archive/src/archive.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::thread::sleep;
2525
use std::time::Duration;
2626
use tar::Archive;
2727
use task_local_extensions::Extensions;
28-
use tracing::{debug, warn};
28+
use tracing::{debug, instrument, warn};
2929

3030
const GITHUB_API_VERSION_HEADER: &str = "X-GitHub-Api-Version";
3131
const GITHUB_API_VERSION: &str = "2022-11-28";
@@ -101,6 +101,7 @@ fn reqwest_client() -> ClientWithMiddleware {
101101

102102
/// Gets a release from GitHub for a given [version](Version) of PostgreSQL. If a release for the
103103
/// [version](Version) is not found, then a [ReleaseNotFound] error is returned.
104+
#[instrument(level = "debug")]
104105
async fn get_release(version: &Version) -> Result<Release> {
105106
let url = "https://api.github.com/repos/theseus-rs/postgresql-binaries/releases";
106107
let client = reqwest_client();
@@ -169,6 +170,7 @@ async fn get_release(version: &Version) -> Result<Release> {
169170
/// Gets the version of PostgreSQL for the specified [version](Version). If the version minor or release is not
170171
/// specified, then the latest version is returned. If a release for the [version](Version) is not found, then a
171172
/// [ReleaseNotFound] error is returned.
173+
#[instrument(level = "debug")]
172174
pub async fn get_version(version: &Version) -> Result<Version> {
173175
let release = get_release(version).await?;
174176
Version::from_str(&release.tag_name)
@@ -180,6 +182,7 @@ pub async fn get_version(version: &Version) -> Result<Version> {
180182
/// is not found, then an [error](crate::error::Error) is returned.
181183
///
182184
/// Two assets are returned. The first [asset](Asset) is the archive, and the second [asset](Asset) is the archive hash.
185+
#[instrument(level = "debug", skip(target))]
183186
async fn get_asset<S: AsRef<str>>(version: &Version, target: S) -> Result<(Version, Asset, Asset)> {
184187
let release = get_release(version).await?;
185188
let asset_version = Version::from_str(&release.tag_name)?;
@@ -212,6 +215,7 @@ async fn get_asset<S: AsRef<str>>(version: &Version, target: S) -> Result<(Versi
212215
/// [error](crate::error::Error) is returned.
213216
///
214217
/// Returns the archive version and bytes.
218+
#[instrument]
215219
pub async fn get_archive(version: &Version) -> Result<(Version, Bytes)> {
216220
get_archive_for_target(version, target_triple::TARGET).await
217221
}
@@ -222,6 +226,7 @@ pub async fn get_archive(version: &Version) -> Result<(Version, Bytes)> {
222226
/// is not found, then an [error](crate::error::Error) is returned.
223227
///
224228
/// Returns the archive version and bytes.
229+
#[instrument(level = "debug", skip(target))]
225230
pub async fn get_archive_for_target<S: AsRef<str>>(
226231
version: &Version,
227232
target: S,
@@ -270,6 +275,7 @@ pub async fn get_archive_for_target<S: AsRef<str>>(
270275

271276
/// Acquires a lock file in the [out_dir](Path) to prevent multiple processes from extracting the
272277
/// archive at the same time.
278+
#[instrument(level = "debug")]
273279
fn acquire_lock(out_dir: &Path) -> Result<PathBuf> {
274280
let lock_file = out_dir.join("postgresql-archive.lock");
275281

@@ -313,6 +319,7 @@ fn acquire_lock(out_dir: &Path) -> Result<PathBuf> {
313319
}
314320

315321
/// Extracts the compressed tar [bytes](Bytes) to the [out_dir](Path).
322+
#[instrument]
316323
pub async fn extract(bytes: &Bytes, out_dir: &Path) -> Result<()> {
317324
let input = BufReader::new(Cursor::new(bytes));
318325
let decoder = GzDecoder::new(input);

postgresql_embedded/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ version.workspace = true
1313
[build-dependencies]
1414
anyhow = { workspace = true }
1515
postgresql_archive = { path = "../postgresql_archive", version = "0.7.1" }
16-
tokio = { workspace = true }
16+
tokio = { workspace = true, features = ["full"] }
1717

1818
[dependencies]
1919
anyhow = { workspace = true }
@@ -24,13 +24,13 @@ postgresql_archive = { path = "../postgresql_archive", version = "0.7.1" }
2424
rand = { workspace = true }
2525
tempfile = { workspace = true }
2626
thiserror = { workspace = true }
27-
tokio = { workspace = true, optional = true }
28-
tracing = { workspace = true }
27+
tokio = { workspace = true, features = ["full"], optional = true }
28+
tracing = { workspace = true, features = ["log"] }
2929

3030
[dev-dependencies]
3131
criterion = { workspace = true }
3232
test-log = { workspace = true }
33-
tokio = { workspace = true }
33+
tokio = { workspace = true, features = ["full"] }
3434

3535
[features]
3636
default = []

postgresql_embedded/src/command/traits.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::error::Result;
22
use std::ffi::{OsStr, OsString};
3+
use std::fmt::Debug;
34
use std::path::PathBuf;
45
use std::time::Duration;
56
use tracing::debug;
67

78
/// Trait to build a command
8-
pub trait CommandBuilder {
9+
pub trait CommandBuilder: Debug {
910
/// Get the program name
1011
fn get_program(&self) -> &'static OsStr;
1112

@@ -151,6 +152,7 @@ mod test {
151152

152153
#[test]
153154
fn test_command_builder_defaults() {
155+
#[derive(Debug)]
154156
struct DefaultCommandBuilder {
155157
program_dir: Option<PathBuf>,
156158
}
@@ -171,6 +173,7 @@ mod test {
171173
assert_eq!(r#""test""#, command.to_command_string());
172174
}
173175

176+
#[derive(Debug)]
174177
struct TestCommandBuilder {
175178
program_dir: Option<PathBuf>,
176179
args: Vec<OsString>,

postgresql_embedded/src/postgresql.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::net::TcpListener;
1515
use std::ops::Deref;
1616
#[cfg(feature = "bundled")]
1717
use std::str::FromStr;
18-
use tracing::debug;
18+
use tracing::{debug, instrument};
1919

2020
use crate::command::psql::PsqlBuilder;
2121
use crate::Error::{CreateDatabaseError, DatabaseExistsError, DropDatabaseError};
@@ -90,6 +90,7 @@ impl PostgreSQL {
9090
}
9191

9292
/// Get the [status](Status) of the PostgreSQL server
93+
#[instrument(level = "debug")]
9394
pub fn status(&self) -> Status {
9495
if self.is_running() {
9596
Status::Started
@@ -136,6 +137,7 @@ impl PostgreSQL {
136137
/// Set up the database by extracting the archive and initializing the database.
137138
/// If the installation directory already exists, the archive will not be extracted.
138139
/// If the data directory already exists, the database will not be initialized.
140+
#[instrument]
139141
pub async fn setup(&mut self) -> Result<()> {
140142
if !self.is_installed() {
141143
self.install().await?;
@@ -153,6 +155,7 @@ impl PostgreSQL {
153155
/// hash does not match the expected hash, an error will be returned. If the installation directory
154156
/// already exists, the archive will not be extracted. If the archive is not found, an error will be
155157
/// returned.
158+
#[instrument]
156159
async fn install(&mut self) -> Result<()> {
157160
debug!("Starting installation process for version {}", self.version);
158161

@@ -201,6 +204,7 @@ impl PostgreSQL {
201204

202205
/// Initialize the database in the data directory. This will create the necessary files and
203206
/// directories to start the database.
207+
#[instrument]
204208
async fn initialize(&mut self) -> Result<()> {
205209
if !self.settings.password_file.exists() {
206210
let mut file = std::fs::File::create(&self.settings.password_file)?;
@@ -234,6 +238,7 @@ impl PostgreSQL {
234238

235239
/// Start the database and wait for the startup to complete.
236240
/// If the port is set to `0`, the database will be started on a random port.
241+
#[instrument]
237242
pub async fn start(&mut self) -> Result<()> {
238243
if self.settings.port == 0 {
239244
let listener = TcpListener::bind(("0.0.0.0", 0))?;
@@ -269,6 +274,7 @@ impl PostgreSQL {
269274
}
270275

271276
/// Stop the database gracefully (smart mode) and wait for the shutdown to complete.
277+
#[instrument]
272278
pub async fn stop(&self) -> Result<()> {
273279
debug!(
274280
"Stopping database {}",
@@ -294,6 +300,7 @@ impl PostgreSQL {
294300
}
295301

296302
/// Create a new database with the given name.
303+
#[instrument(skip(database_name))]
297304
pub async fn create_database<S: AsRef<str>>(&self, database_name: S) -> Result<()> {
298305
debug!(
299306
"Creating database {} for {}:{}",
@@ -327,6 +334,7 @@ impl PostgreSQL {
327334
}
328335

329336
/// Check if a database with the given name exists.
337+
#[instrument(skip(database_name))]
330338
pub async fn database_exists<S: AsRef<str>>(&self, database_name: S) -> Result<bool> {
331339
debug!(
332340
"Checking if database {} exists for {}:{}",
@@ -358,6 +366,7 @@ impl PostgreSQL {
358366
}
359367

360368
/// Drop a database with the given name.
369+
#[instrument(skip(database_name))]
361370
pub async fn drop_database<S: AsRef<str>>(&self, database_name: S) -> Result<()> {
362371
debug!(
363372
"Dropping database {} for {}:{}",
@@ -405,6 +414,7 @@ impl PostgreSQL {
405414

406415
#[cfg(feature = "tokio")]
407416
/// Execute a command and return the stdout and stderr as strings.
417+
#[instrument(level = "debug")]
408418
async fn execute_command<B: CommandBuilder>(
409419
&self,
410420
command_builder: B,

0 commit comments

Comments
 (0)