diff --git a/src/filter/http_context.rs b/src/filter/http_context.rs index aa02bdc4..281eab93 100644 --- a/src/filter/http_context.rs +++ b/src/filter/http_context.rs @@ -11,6 +11,7 @@ use proxy_wasm::types::{Action, Bytes}; use std::rc::Rc; // tracing headers +#[derive(Clone)] pub enum TracingHeader { Traceparent, Tracestate, @@ -22,7 +23,7 @@ impl TracingHeader { [Traceparent, Tracestate, Baggage] } - fn as_str(&self) -> &'static str { + pub fn as_str(&self) -> &'static str { match self { Traceparent => "traceparent", Tracestate => "tracestate", @@ -61,13 +62,8 @@ impl Filter { ); return Action::Continue; } - let rl_tracing_headers = self - .tracing_headers - .iter() - .map(|(header, value)| (header.as_str(), value.as_slice())) - .collect(); - let rls = RateLimitService::new(rlp.service.as_str(), rl_tracing_headers); + let rls = RateLimitService::new(rlp.service.as_str(), self.tracing_headers.clone()); let message = RateLimitService::message(rlp.domain.clone(), descriptors); match rls.send(message) { diff --git a/src/service/rate_limit.rs b/src/service/rate_limit.rs index 170d1bce..6c4726c5 100644 --- a/src/service/rate_limit.rs +++ b/src/service/rate_limit.rs @@ -1,22 +1,23 @@ use crate::envoy::{RateLimitDescriptor, RateLimitRequest}; +use crate::filter::http_context::TracingHeader; use crate::service::Service; use protobuf::{Message, RepeatedField}; use proxy_wasm::hostcalls::dispatch_grpc_call; -use proxy_wasm::types::Status; +use proxy_wasm::types::{Bytes, Status}; use std::time::Duration; const RATELIMIT_SERVICE_NAME: &str = "envoy.service.ratelimit.v3.RateLimitService"; const RATELIMIT_METHOD_NAME: &str = "ShouldRateLimit"; -pub struct RateLimitService<'a> { +pub struct RateLimitService { endpoint: String, - metadata: Vec<(&'a str, &'a [u8])>, + tracing_headers: Vec<(TracingHeader, Bytes)>, } -impl<'a> RateLimitService<'a> { - pub fn new(endpoint: &str, metadata: Vec<(&'a str, &'a [u8])>) -> RateLimitService<'a> { +impl RateLimitService { + pub fn new(endpoint: &str, metadata: Vec<(TracingHeader, Bytes)>) -> RateLimitService { Self { endpoint: String::from(endpoint), - metadata, + tracing_headers: metadata, } } pub fn message( @@ -49,9 +50,16 @@ fn grpc_call( ) } -impl Service for RateLimitService<'_> { +impl Service for RateLimitService { fn send(&self, message: RateLimitRequest) -> Result { - grpc_call(self.endpoint.as_str(), self.metadata.clone(), message) + grpc_call( + self.endpoint.as_str(), + self.tracing_headers + .iter() + .map(|(header, value)| (header.as_str(), value.as_slice())) + .collect(), + message, + ) } }