diff --git a/build.rs b/build.rs index b6b4d1f21b9a0..fd18192091fdf 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,21 @@ use std::{collections::HashSet, env, fs::File, io::Write, path::Path, process::Command}; +#[cfg(feature = "protobuf-build")] +struct GrpcServiceGenerator; + +#[cfg(feature = "protobuf-build")] +impl prost_build::ServiceGenerator for GrpcServiceGenerator { + fn generate(&mut self, service: prost_build::Service, buf: &mut String) { + let mut builder = tonic_prost_build::configure(); + if service.package == "vector" && service.name == "Vector" { + builder = builder.codec_path("crate::proto::vector::CopyingProstCodec"); + } + let mut generator = builder.service_generator(); + generator.generate(service, buf); + generator.finalize(buf); + } +} + struct TrackedEnv { tracked: HashSet, } @@ -141,6 +157,8 @@ fn main() { println!("cargo:rerun-if-changed=proto/vector/ddsketch_full.proto"); println!("cargo:rerun-if-changed=proto/vector/vector.proto"); println!("cargo:rerun-if-changed=proto/vector/observability.proto"); + println!("cargo:rerun-if-changed=proto"); + println!("cargo:rerun-if-changed=lib/vector-core/proto"); // Create and store the "file descriptor set" from the compiled Protocol Buffers packages. // Used for gRPC reflection of Vector's own observability/API protos. @@ -154,10 +172,10 @@ fn main() { .file_descriptor_set_path(protobuf_fds_path) .extern_path(".event", "crate::event::proto"); // Use existing event types from vector-core - tonic_prost_build::configure() + prost_build + .service_generator(Box::new(GrpcServiceGenerator)) .protoc_arg("--experimental_allow_proto3_optional") - .compile_with_config( - prost_build, + .compile_protos( &[ "lib/vector-core/proto/event.proto", "proto/vector/ddsketch_full.proto", diff --git a/src/proto/vector.rs b/src/proto/vector.rs index 44936cfca46f4..5c00ac66a3501 100644 --- a/src/proto/vector.rs +++ b/src/proto/vector.rs @@ -3,5 +3,8 @@ tonic::include_proto!("vector"); +mod copying_codec; +use copying_codec::CopyingProstCodec; + pub use vector_client::VectorClient as Client; pub use vector_server::{Vector as Service, VectorServer as Server}; diff --git a/src/proto/vector/copying_codec.rs b/src/proto/vector/copying_codec.rs new file mode 100644 index 0000000000000..26ca5715a1e6b --- /dev/null +++ b/src/proto/vector/copying_codec.rs @@ -0,0 +1,61 @@ +use std::marker::PhantomData; + +use bytes::Buf; +use prost::Message; +use tonic::{ + Status, + codec::{BufferSettings, Codec, DecodeBuf, Decoder}, +}; +use tonic_prost::ProstCodec; + +#[derive(Debug, Default)] +pub(super) struct CopyingProstCodec(PhantomData<(T, U)>); + +impl Codec for CopyingProstCodec +where + T: Message + Send + 'static, + U: Message + Default + Send + 'static, +{ + type Encode = T; + type Decode = U; + type Encoder = as Codec>::Encoder; + type Decoder = CopyingProstDecoder; + + fn encoder(&mut self) -> Self::Encoder { + ProstCodec::::raw_encoder(BufferSettings::default()) + } + + fn decoder(&mut self) -> Self::Decoder { + CopyingProstDecoder(PhantomData) + } +} + +pub(super) struct CopyingProstDecoder(PhantomData); + +impl Decoder for CopyingProstDecoder { + type Item = U; + type Error = Status; + + fn decode(&mut self, buf: &mut DecodeBuf<'_>) -> Result, Status> { + U::decode(CopyingBuf(buf)) + .map(Some) + .map_err(|error| Status::internal(error.to_string())) + } +} + +struct CopyingBuf(B); + +impl Buf for CopyingBuf { + fn remaining(&self) -> usize { + self.0.remaining() + } + + fn chunk(&self) -> &[u8] { + self.0.chunk() + } + + fn advance(&mut self, count: usize) { + self.0.advance(count); + } + +}