Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate opentelemetry tracing headers in requests to limitador #49

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ protobuf = { version = "2.27", features = ["with-serde"] }
thiserror = "1.0"
regex = "1"
radix_trie = "0.2.1"
strum = "0.26.2"
strum_macros = "0.26.2"

[dev-dependencies]
proxy-wasm-test-framework = { git = "https://github.com/Kuadrant/wasm-test-framework.git", branch = "kuadrant" }
Expand Down
37 changes: 35 additions & 2 deletions src/filter/http_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,38 @@ use crate::utils::tokenize_with_escaping;
use log::{debug, info, warn};
use protobuf::Message;
use proxy_wasm::traits::{Context, HttpContext};
use proxy_wasm::types::Action;
use proxy_wasm::types::{Action, Bytes};
use std::rc::Rc;
use std::time::Duration;
use strum::IntoEnumIterator;
use strum_macros::EnumIter;

const RATELIMIT_SERVICE_NAME: &str = "envoy.service.ratelimit.v3.RateLimitService";
const RATELIMIT_METHOD_NAME: &str = "ShouldRateLimit";

// tracing headers
#[derive(EnumIter)]
pub enum TracingHeader {
Traceparent,
Tracestate,
Baggage,
}

impl TracingHeader {
fn as_str(&self) -> &'static str {
match self {
TracingHeader::Traceparent => "traceparent",
TracingHeader::Tracestate => "tracestate",
TracingHeader::Baggage => "baggage",
}
}
}

pub struct Filter {
pub context_id: u32,
pub config: Rc<FilterConfig>,
pub response_headers_to_add: Vec<(String, String)>,
pub tracing_headers: Vec<(TracingHeader, Bytes)>,
}

impl Filter {
Expand Down Expand Up @@ -51,11 +72,17 @@ impl Filter {

let rl_req_serialized = Message::write_to_bytes(&rl_req).unwrap(); // TODO(rahulanand16nov): Error Handling

let rl_tracing_headers = self
.tracing_headers
.iter()
.map(|(header, value)| (header.as_str(), value.as_slice()))
.collect();

match self.dispatch_grpc_call(
rlp.service.as_str(),
RATELIMIT_SERVICE_NAME,
RATELIMIT_METHOD_NAME,
Vec::new(),
rl_tracing_headers,
Some(&rl_req_serialized),
Duration::from_secs(5),
) {
Expand Down Expand Up @@ -207,6 +234,12 @@ impl HttpContext for Filter {
fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
info!("on_http_request_headers #{}", self.context_id);

for header in TracingHeader::iter() {
match self.get_http_request_header_bytes(header.as_str()) {
Some(value) => self.tracing_headers.push((header, value)),
None => (),
}
}
match self
.config
.index
Expand Down
1 change: 1 addition & 0 deletions src/filter/root_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl RootContext for FilterRoot {
context_id,
config: Rc::clone(&self.config),
response_headers_to_add: Vec::default(),
tracing_headers: Vec::default(),
}))
}

Expand Down
Loading