use super::convert::json_to_event_wrapper;
use super::proto::vector;
use crate::transport::error::{TransportError, TransportResult};
pub struct VectorCompatClient {
client: vector::vector_client::VectorClient<tonic::transport::Channel>,
}
impl VectorCompatClient {
pub fn connect_lazy(endpoint: &str) -> TransportResult<Self> {
const MAX_DECODE_BYTES: usize = 64 * 1024 * 1024;
let channel = tonic::transport::Channel::from_shared(endpoint.to_string())
.map_err(|e| TransportError::Config(format!("invalid Vector endpoint: {e}")))?
.connect_lazy();
let client = vector::vector_client::VectorClient::new(channel)
.max_decoding_message_size(MAX_DECODE_BYTES)
.accept_compressed(tonic::codec::CompressionEncoding::Gzip)
.send_compressed(tonic::codec::CompressionEncoding::Gzip);
Ok(Self { client })
}
pub async fn send_events(&self, values: &[serde_json::Value]) -> TransportResult<()> {
let events: Vec<_> = values.iter().map(json_to_event_wrapper).collect();
let request = vector::PushEventsRequest { events };
self.client
.clone()
.push_events(request)
.await
.map_err(|e| TransportError::Send(format!("Vector PushEvents failed: {e}")))?;
Ok(())
}
pub async fn health_check(&self) -> TransportResult<bool> {
let response = self
.client
.clone()
.health_check(vector::HealthCheckRequest {})
.await
.map_err(|e| TransportError::Connection(format!("Vector health check failed: {e}")))?;
Ok(response.into_inner().status == vector::ServingStatus::Serving as i32)
}
}