Skip to content
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

Send client and rust version to server #200

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ reqwest = { version = "0.12.8", optional = true, default-features = false, featu
futures-util = { version = "0.3.31", optional = true }
derive_builder = { version = "0.20.2" }
thiserror = "1.0.64"
rustc_version = "0.4"

[dev-dependencies]
tonic-build = { version = "0.12.3", features = ["prost"] }
Expand Down
11 changes: 10 additions & 1 deletion src/client/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,26 @@ use crate::qdrant::{
SparseVectorConfig, UpdateCollection, UpdateCollectionClusterSetupRequest,
UpdateCollectionClusterSetupResponse, VectorsConfigDiff,
};
use crate::user_agent::UserAgentInterceptor;

impl QdrantClient {
// Access to raw collection API
pub async fn with_collections_client<T, O: Future<Output = anyhow::Result<T, Status>>>(
&self,
f: impl Fn(CollectionsClient<InterceptedService<Channel, TokenInterceptor>>) -> O,
f: impl Fn(
CollectionsClient<
InterceptedService<
InterceptedService<Channel, TokenInterceptor>,
UserAgentInterceptor,
>,
>,
) -> O,
) -> anyhow::Result<T, Status> {
self.channel
.with_channel(
|channel| {
let service = self.with_api_key(channel);
let service = InterceptedService::new(service, UserAgentInterceptor::new());
let mut client =
CollectionsClient::new(service).max_decoding_message_size(usize::MAX);
if let Some(compression) = self.cfg.compression {
Expand Down
11 changes: 10 additions & 1 deletion src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use crate::auth::TokenInterceptor;
use crate::channel_pool::ChannelPool;
pub use crate::payload::Payload;
use crate::qdrant::{qdrant_client, HealthCheckReply, HealthCheckRequest};
use crate::user_agent::UserAgentInterceptor;

/// A builder for `QdrantClient`s
#[deprecated(since = "1.10.0", note = "use `qdrant_client::QdrantBuilder` instead")]
Expand Down Expand Up @@ -81,12 +82,20 @@ impl QdrantClient {
// Access to raw root qdrant API
pub async fn with_root_qdrant_client<T, O: Future<Output = Result<T, Status>>>(
&self,
f: impl Fn(qdrant_client::QdrantClient<InterceptedService<Channel, TokenInterceptor>>) -> O,
f: impl Fn(
qdrant_client::QdrantClient<
InterceptedService<
InterceptedService<Channel, TokenInterceptor>,
UserAgentInterceptor,
>,
>,
) -> O,
) -> Result<T, Status> {
self.channel
.with_channel(
|channel| {
let service = self.with_api_key(channel);
let service = InterceptedService::new(service, UserAgentInterceptor::new());
let mut client = qdrant_client::QdrantClient::new(service)
.max_decoding_message_size(usize::MAX);
if let Some(compression) = self.cfg.compression {
Expand Down
11 changes: 10 additions & 1 deletion src/client/points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,26 @@ use crate::qdrant::{
UpdateBatchPoints, UpdateBatchResponse, UpdatePointVectors, UpsertPoints, VectorsSelector,
WithPayloadSelector, WithVectorsSelector, WriteOrdering,
};
use crate::user_agent::UserAgentInterceptor;

impl QdrantClient {
// Access to raw points API
pub async fn with_points_client<T, O: Future<Output = anyhow::Result<T, Status>>>(
&self,
f: impl Fn(PointsClient<InterceptedService<Channel, TokenInterceptor>>) -> O,
f: impl Fn(
PointsClient<
InterceptedService<
InterceptedService<Channel, TokenInterceptor>,
UserAgentInterceptor,
>,
>,
) -> O,
) -> anyhow::Result<T, Status> {
self.channel
.with_channel(
|channel| {
let service = self.with_api_key(channel);
let service = InterceptedService::new(service, UserAgentInterceptor::new());
let mut client =
PointsClient::new(service).max_decoding_message_size(usize::MAX);
if let Some(compression) = self.cfg.compression {
Expand Down
11 changes: 10 additions & 1 deletion src/client/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,25 @@ use crate::qdrant::{
DeleteFullSnapshotRequest, DeleteSnapshotRequest, DeleteSnapshotResponse,
ListFullSnapshotsRequest, ListSnapshotsRequest, ListSnapshotsResponse,
};
use crate::user_agent::UserAgentInterceptor;

impl QdrantClient {
pub async fn with_snapshot_client<T, O: Future<Output = anyhow::Result<T, Status>>>(
&self,
f: impl Fn(SnapshotsClient<InterceptedService<Channel, TokenInterceptor>>) -> O,
f: impl Fn(
SnapshotsClient<
InterceptedService<
InterceptedService<Channel, TokenInterceptor>,
UserAgentInterceptor,
>,
>,
) -> O,
) -> anyhow::Result<T, Status> {
self.channel
.with_channel(
|channel| {
let service = self.with_api_key(channel);
let service = InterceptedService::new(service, UserAgentInterceptor::new());
let mut client =
SnapshotsClient::new(service).max_decoding_message_size(usize::MAX);
if let Some(compression) = self.cfg.compression {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ mod grpc_macros;
mod manual_builder;
mod payload;
mod qdrant_client;
mod user_agent;
// Deprecated modules
/// Deprecated Qdrant client
#[deprecated(
Expand Down
11 changes: 10 additions & 1 deletion src/qdrant_client/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::qdrant::{
UpdateCollection, UpdateCollectionClusterSetupRequest, UpdateCollectionClusterSetupResponse,
};
use crate::qdrant_client::{Qdrant, QdrantResult};
use crate::user_agent::UserAgentInterceptor;

/// # Collection operations
///
Expand All @@ -25,13 +26,21 @@ use crate::qdrant_client::{Qdrant, QdrantResult};
impl Qdrant {
pub(super) async fn with_collections_client<T, O: Future<Output = Result<T, Status>>>(
&self,
f: impl Fn(CollectionsClient<InterceptedService<Channel, TokenInterceptor>>) -> O,
f: impl Fn(
CollectionsClient<
InterceptedService<
InterceptedService<Channel, TokenInterceptor>,
UserAgentInterceptor,
>,
>,
) -> O,
) -> QdrantResult<T> {
let result = self
.channel
.with_channel(
|channel| {
let service = self.with_api_key(channel);
let service = InterceptedService::new(service, UserAgentInterceptor::new());
let mut client =
CollectionsClient::new(service).max_decoding_message_size(usize::MAX);
if let Some(compression) = self.config.compression {
Expand Down
11 changes: 10 additions & 1 deletion src/qdrant_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::auth::TokenInterceptor;
use crate::channel_pool::ChannelPool;
use crate::qdrant::{qdrant_client, HealthCheckReply, HealthCheckRequest};
use crate::qdrant_client::config::QdrantConfig;
use crate::user_agent::UserAgentInterceptor;
use crate::QdrantError;

/// [`Qdrant`] client result
Expand Down Expand Up @@ -132,13 +133,21 @@ impl Qdrant {
// Access to raw root qdrant API
async fn with_root_qdrant_client<T, O: Future<Output = Result<T, Status>>>(
&self,
f: impl Fn(qdrant_client::QdrantClient<InterceptedService<Channel, TokenInterceptor>>) -> O,
f: impl Fn(
qdrant_client::QdrantClient<
InterceptedService<
InterceptedService<Channel, TokenInterceptor>,
UserAgentInterceptor,
>,
>,
) -> O,
) -> QdrantResult<T> {
let result = self
.channel
.with_channel(
|channel| {
let service = self.with_api_key(channel);
let service = InterceptedService::new(service, UserAgentInterceptor::new());
let mut client = qdrant_client::QdrantClient::new(service)
.max_decoding_message_size(usize::MAX);
if let Some(compression) = self.config.compression {
Expand Down
11 changes: 10 additions & 1 deletion src/qdrant_client/points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::qdrant::{
UpdateBatchResponse, UpdatePointVectors, UpsertPoints,
};
use crate::qdrant_client::{Qdrant, QdrantResult};
use crate::user_agent::UserAgentInterceptor;

/// # Point operations
///
Expand All @@ -22,13 +23,21 @@ use crate::qdrant_client::{Qdrant, QdrantResult};
impl Qdrant {
pub(crate) async fn with_points_client<T, O: Future<Output = Result<T, Status>>>(
&self,
f: impl Fn(PointsClient<InterceptedService<Channel, TokenInterceptor>>) -> O,
f: impl Fn(
PointsClient<
InterceptedService<
InterceptedService<Channel, TokenInterceptor>,
UserAgentInterceptor,
>,
>,
) -> O,
) -> QdrantResult<T> {
let result = self
.channel
.with_channel(
|channel| {
let service = self.with_api_key(channel);
let service = InterceptedService::new(service, UserAgentInterceptor::new());
let mut client =
PointsClient::new(service).max_decoding_message_size(usize::MAX);
if let Some(compression) = self.config.compression {
Expand Down
11 changes: 10 additions & 1 deletion src/qdrant_client/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::qdrant::{
ListFullSnapshotsRequest, ListSnapshotsRequest, ListSnapshotsResponse,
};
use crate::qdrant_client::{Qdrant, QdrantResult};
use crate::user_agent::UserAgentInterceptor;

/// # Snapshot operations
///
Expand All @@ -21,13 +22,21 @@ use crate::qdrant_client::{Qdrant, QdrantResult};
impl Qdrant {
async fn with_snapshot_client<T, O: Future<Output = Result<T, Status>>>(
&self,
f: impl Fn(SnapshotsClient<InterceptedService<Channel, TokenInterceptor>>) -> O,
f: impl Fn(
SnapshotsClient<
InterceptedService<
InterceptedService<Channel, TokenInterceptor>,
UserAgentInterceptor,
>,
>,
) -> O,
) -> QdrantResult<T> {
let result = self
.channel
.with_channel(
|channel| {
let service = self.with_api_key(channel);
let service = InterceptedService::new(service, UserAgentInterceptor::new());
let mut client =
SnapshotsClient::new(service).max_decoding_message_size(usize::MAX);
if let Some(compression) = self.config.compression {
Expand Down
46 changes: 46 additions & 0 deletions src/user_agent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use rustc_version::version_meta;
use tonic::service::Interceptor;
use tonic::{Request, Status};

pub struct UserAgentInterceptor {
rust_version: Option<String>,
rust_client_version: Option<String>,
}

impl Default for UserAgentInterceptor {
fn default() -> Self {
Self::new()
}
}

impl UserAgentInterceptor {
pub fn new() -> Self {
let rust_version = Some(version_meta().unwrap().semver.to_string());
let rust_client_version = Some(env!("CARGO_PKG_VERSION").to_string());

Self {
rust_version,
rust_client_version,
}
}
}

impl Interceptor for UserAgentInterceptor {
fn call(&mut self, mut req: Request<()>) -> Result<Request<()>, Status> {
let user_agent_value = format!(
"rust-client/{} rust/{}",
self.rust_version.clone().unwrap_or_default(),
self.rust_client_version.clone().unwrap_or_default()
);
req.metadata_mut().insert(
"x-user-agent",
user_agent_value.parse().map_err(|_| {
Status::invalid_argument(format!(
"Malformed user-agent value: {}",
user_agent_value
))
})?,
);
Ok(req)
}
}
Loading