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

Retrieve opentelemetry parent span from headers #275

Merged
merged 2 commits into from
Apr 3, 2024
Merged
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
33 changes: 32 additions & 1 deletion limitador-server/src/envoy_rls/server.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use opentelemetry::global;
use opentelemetry::propagation::Extractor;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::sync::Arc;

use tonic::codegen::http::HeaderMap;
use tonic::{transport, transport::Server, Request, Response, Status};
use tracing::Span;
use tracing_opentelemetry::OpenTelemetrySpanExt;

use limitador::counter::Counter;

Expand Down Expand Up @@ -48,8 +53,13 @@ impl RateLimitService for MyRateLimiter {
debug!("Request received: {:?}", request);

let mut values: HashMap<String, String> = HashMap::new();
let req = request.into_inner();
let (metadata, _ext, req) = request.into_parts();
let namespace = req.domain;
let rl_headers = RateLimitRequestHeaders::new(metadata.into_headers());
let parent_context =
global::get_text_map_propagator(|propagator| propagator.extract(&rl_headers));
let span = Span::current();
span.set_parent(parent_context);

if namespace.is_empty() {
return Ok(Response::new(RateLimitResponse {
Expand Down Expand Up @@ -195,6 +205,27 @@ pub fn to_response_header(
headers
}

struct RateLimitRequestHeaders {
inner: HeaderMap,
}
impl RateLimitRequestHeaders {
pub fn new(inner: HeaderMap) -> Self {
Self { inner }
}
}
impl Extractor for RateLimitRequestHeaders {
fn get(&self, key: &str) -> Option<&str> {
match self.inner.get(key) {
Some(v) => v.to_str().ok(),
None => None,
}
}

fn keys(&self) -> Vec<&str> {
self.inner.keys().map(|k| k.as_str()).collect()
}
}

mod rls_proto {
pub(crate) const RLS_DESCRIPTOR_SET: &[u8] = tonic::include_file_descriptor_set!("rls");
}
Expand Down
6 changes: 4 additions & 2 deletions limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ use limitador::{
};
use notify::event::{ModifyKind, RenameMode};
use notify::{Error, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use opentelemetry::KeyValue;
use opentelemetry::{global, KeyValue};
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::propagation::TraceContextPropagator;
use opentelemetry_sdk::{trace, Resource};
use std::env::VarError;
use std::fmt::Display;
Expand Down Expand Up @@ -304,6 +305,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
};

if !config.tracing_endpoint.is_empty() {
global::set_text_map_propagator(TraceContextPropagator::new());
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
Expand All @@ -312,7 +314,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_endpoint(config.tracing_endpoint.clone()),
)
.with_trace_config(trace::config().with_resource(Resource::new(vec![
KeyValue::new("service.name", "limitador-server"),
KeyValue::new("service.name", "limitador"),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the name here to match authorino spans

])))
.install_batch(opentelemetry_sdk::runtime::Tokio)?;
let telemetry_layer = tracing_opentelemetry::layer().with_tracer(tracer);
Expand Down
Loading