Skip to content

Commit 0a33952

Browse files
committed
implement helper for specifying per-request tracing
1 parent c80e768 commit 0a33952

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

src/qdrant_client/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,27 @@ impl Qdrant {
212212
Ok(result)
213213
}
214214

215+
/// Create a lightweight clone of this client with an additional header.
216+
///
217+
/// The returned client shares the same connection pool but sends
218+
/// the extra header with every request. Useful for per-request
219+
/// tracing IDs or other contextual metadata.
220+
///
221+
/// ```no_run
222+
/// use qdrant_client::Qdrant;
223+
///
224+
///# async fn example(client: &Qdrant) -> Result<(), qdrant_client::QdrantError> {
225+
/// let traced = client.with_header("x-request-id", "abc-123");
226+
/// // traced shares the same connection pool, but adds the header to every request
227+
///# Ok(())
228+
///# }
229+
/// ```
230+
pub fn with_header(&self, key: impl Into<String>, value: impl Into<String>) -> Self {
231+
let mut clone = self.clone();
232+
clone.config.custom_headers.push((key.into(), value.into()));
233+
clone
234+
}
235+
215236
/// Health check.
216237
///
217238
/// Do a health check and fetch server information such as the current version and commit.

tests/snippet_tests/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
mod test_batch_update;
22
mod test_clear_payload;
33
mod test_collection_exists;
4-
mod test_config_headers;
54
mod test_count_points;
65
mod test_create_collection;
76
mod test_create_collection_with_bq;
@@ -21,7 +20,6 @@ mod test_delete_snapshot;
2120
mod test_delete_vectors;
2221
mod test_discover_batch_points;
2322
mod test_discover_points;
24-
mod test_external_api_keys;
2523
mod test_facets;
2624
mod test_get_collection;
2725
mod test_get_collection_aliases;
@@ -59,3 +57,4 @@ mod test_upsert_points;
5957
mod test_upsert_points_fallback_shard_key;
6058
mod test_upsert_points_insert_only;
6159
mod test_upsert_points_with_condition;
60+
mod test_with_header;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
#[tokio::test]
3+
async fn test_with_header() {
4+
async fn with_header() -> Result<(), Box<dyn std::error::Error>> {
5+
// WARNING: This is a generated test snippet.
6+
// Please, modify the snippet in the `../snippets/with_header.rs` file
7+
use qdrant_client::Qdrant;
8+
9+
let client = Qdrant::from_url("http://localhost:6334")
10+
.header("x-base", "base-value")
11+
.skip_compatibility_check()
12+
.build()
13+
.unwrap();
14+
15+
let traced = client.with_header("x-request-id", "abc-123");
16+
17+
assert_eq!(
18+
traced.config.custom_headers,
19+
vec![
20+
("x-base".to_string(), "base-value".to_string()),
21+
("x-request-id".to_string(), "abc-123".to_string()),
22+
]
23+
);
24+
// Original still has only the base header
25+
assert_eq!(
26+
client.config.custom_headers,
27+
vec![("x-base".to_string(), "base-value".to_string())]
28+
);
29+
Ok(())
30+
}
31+
let _ = with_header().await;
32+
}

tests/snippets/with_header.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use qdrant_client::Qdrant;
2+
3+
let client = Qdrant::from_url("http://localhost:6334")
4+
.header("x-base", "base-value")
5+
.skip_compatibility_check()
6+
.build()
7+
.unwrap();
8+
9+
let traced = client.with_header("x-request-id", "abc-123");
10+
11+
assert_eq!(
12+
traced.config.custom_headers,
13+
vec![
14+
("x-base".to_string(), "base-value".to_string()),
15+
("x-request-id".to_string(), "abc-123".to_string()),
16+
]
17+
);
18+
// Original still has only the base header
19+
assert_eq!(
20+
client.config.custom_headers,
21+
vec![("x-base".to_string(), "base-value".to_string())]
22+
);

0 commit comments

Comments
 (0)