Skip to content
Draft
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
24 changes: 21 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}
Expand Down Expand Up @@ -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.
Expand All @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/proto/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
61 changes: 61 additions & 0 deletions src/proto/vector/copying_codec.rs
Original file line number Diff line number Diff line change
@@ -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<T, U>(PhantomData<(T, U)>);

impl<T, U> Codec for CopyingProstCodec<T, U>
where
T: Message + Send + 'static,
U: Message + Default + Send + 'static,
{
type Encode = T;
type Decode = U;
type Encoder = <ProstCodec<T, U> as Codec>::Encoder;
type Decoder = CopyingProstDecoder<U>;

fn encoder(&mut self) -> Self::Encoder {
ProstCodec::<T, U>::raw_encoder(BufferSettings::default())
}

fn decoder(&mut self) -> Self::Decoder {
CopyingProstDecoder(PhantomData)
}
}

pub(super) struct CopyingProstDecoder<U>(PhantomData<U>);

impl<U: Message + Default> Decoder for CopyingProstDecoder<U> {
type Item = U;
type Error = Status;

fn decode(&mut self, buf: &mut DecodeBuf<'_>) -> Result<Option<U>, Status> {
U::decode(CopyingBuf(buf))
.map(Some)
.map_err(|error| Status::internal(error.to_string()))
}
}

struct CopyingBuf<B>(B);

impl<B: Buf> Buf for CopyingBuf<B> {
fn remaining(&self) -> usize {
self.0.remaining()
}

fn chunk(&self) -> &[u8] {
self.0.chunk()
}

fn advance(&mut self, count: usize) {
self.0.advance(count);
}

}
Loading