Skip to content

Commit

Permalink
[refactor] Passing tracing_headers to RateLimitService
Browse files Browse the repository at this point in the history
Signed-off-by: dd di cesare <[email protected]>
  • Loading branch information
didierofrivia committed Aug 20, 2024
1 parent 87ddc45 commit 3011881
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
10 changes: 3 additions & 7 deletions src/filter/http_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use proxy_wasm::types::{Action, Bytes};
use std::rc::Rc;

// tracing headers
#[derive(Clone)]
pub enum TracingHeader {
Traceparent,
Tracestate,
Expand All @@ -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",
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 16 additions & 8 deletions src/service/rate_limit.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -49,9 +50,16 @@ fn grpc_call(
)
}

impl Service<RateLimitRequest> for RateLimitService<'_> {
impl Service<RateLimitRequest> for RateLimitService {
fn send(&self, message: RateLimitRequest) -> Result<u32, Status> {
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,
)
}
}

Expand Down

0 comments on commit 3011881

Please sign in to comment.