Skip to content

Commit a8b4daa

Browse files
committed
NotifySink strip URL from logs for reqwest errors
1 parent 48ee3c1 commit a8b4daa

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

Cargo.lock

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ warp = { version = "0.4.2", features = ["server"] }
4141
opentelemetry = "0.31.0"
4242
opentelemetry-otlp = { version = "0.31.0", features = ["grpc-tonic", "logs"] }
4343
opentelemetry_sdk = "0.31.0"
44+
regex = "1.12.2"
4445

4546
[build-dependencies]
4647
shadow-rs = "1.4.0"

src/reporter/mod.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
mod norec;
22

33
use std::{
4+
borrow::Cow,
45
cmp::Ordering,
5-
sync::{Arc, Mutex},
6+
sync::{Arc, LazyLock, Mutex},
67
time::{Duration, UNIX_EPOCH},
78
};
89

910
use anyhow::anyhow;
1011
use norec::NoRec;
1112
use opentelemetry_otlp::WithExportConfig as _;
13+
use regex::Regex;
1214
use reqwest::Url;
1315
use serde::Deserialize;
1416
use spdlog::{
@@ -249,6 +251,12 @@ impl NotifySink {
249251
false
250252
}
251253
}
254+
255+
// Workaround: https://github.com/seanmonstar/reqwest/issues/2365
256+
fn strip_url(buf: &str) -> Cow<'_, str> {
257+
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r" for url \(.+\)").unwrap());
258+
RE.replace_all(buf, r" for url (<stripped>)")
259+
}
252260
}
253261

254262
impl GetSinkProp for NotifySink {
@@ -271,11 +279,26 @@ impl Sink for NotifySink {
271279
let mut ctx = FormatterContext::new();
272280
self.prop.formatter().format(record, &mut buf, &mut ctx)?;
273281

274-
self.notify_log(buf);
282+
self.notify_log(Self::strip_url(&buf));
275283
Ok(())
276284
}
277285

278286
fn flush(&self) -> spdlog::Result<()> {
279287
Ok(()) // No-op
280288
}
281289
}
290+
291+
#[cfg(test)]
292+
mod tests {
293+
use super::*;
294+
295+
#[test]
296+
fn strip_url_valid() {
297+
assert_eq!(
298+
NotifySink::strip_url(
299+
r"failed to sent request: error sending request for url (https://example.com/endpoint?a=%7Babc&_#)",
300+
),
301+
r"failed to sent request: error sending request for url (<stripped>)"
302+
);
303+
}
304+
}

0 commit comments

Comments
 (0)