Skip to content

Commit

Permalink
make sure to call attributes fn for its OP impl
Browse files Browse the repository at this point in the history
  • Loading branch information
GlenDC committed Sep 13, 2024
1 parent d394317 commit 2e4027a
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 17 deletions.
4 changes: 2 additions & 2 deletions rama-core/src/telemetry/opentelemetry/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<State, F> AttributesFactory<State> for F
where
F: Fn(usize, &Context<State>) -> Vec<KeyValue> + Send + Sync + 'static,
{
fn attributes(&self, size_hint: usize, _ctx: &Context<State>) -> Vec<KeyValue> {
Vec::with_capacity(size_hint)
fn attributes(&self, size_hint: usize, ctx: &Context<State>) -> Vec<KeyValue> {
(self)(size_hint, ctx)
}
}
125 changes: 125 additions & 0 deletions rama-http/src/layer/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,128 @@ where
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_default_svc_compute_attributes_default() {
let svc = RequestMetricsService::new(());
let mut ctx = Context::default();
let req = Request::builder()
.uri("http://www.example.com")
.body(())
.unwrap();

let attributes = svc.compute_attributes(&mut ctx, &req);
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_NAME));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_VERSION));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == HTTP_REQUEST_HOST));
}

#[test]
fn test_custom_svc_compute_attributes_default() {
let svc = RequestMetricsLayer::custom(MeterOptions {
service: Some(ServiceInfo {
name: "test".to_owned(),
version: "42".to_owned(),
}),
metric_prefix: Some("foo".to_owned()),
..Default::default()
})
.layer(());
let mut ctx = Context::default();
let req = Request::builder()
.uri("http://www.example.com")
.body(())
.unwrap();

let attributes = svc.compute_attributes(&mut ctx, &req);
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_NAME && attr.value.as_str() == "test"));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_VERSION && attr.value.as_str() == "42"));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == HTTP_REQUEST_HOST));
}

#[test]
fn test_custom_svc_compute_attributes_attributes_vec() {
let svc = RequestMetricsLayer::custom(MeterOptions {
service: Some(ServiceInfo {
name: "test".to_owned(),
version: "42".to_owned(),
}),
metric_prefix: Some("foo".to_owned()),
..Default::default()
})
.with_attributes(vec![KeyValue::new("test", "attribute_fn")])
.layer(());
let mut ctx = Context::default();
let req = Request::builder()
.uri("http://www.example.com")
.body(())
.unwrap();

let attributes = svc.compute_attributes(&mut ctx, &req);
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_NAME && attr.value.as_str() == "test"));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_VERSION && attr.value.as_str() == "42"));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == HTTP_REQUEST_HOST));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == "test" && attr.value.as_str() == "attribute_fn"));
}

#[test]
fn test_custom_svc_compute_attributes_attribute_fn() {
let svc = RequestMetricsLayer::custom(MeterOptions {
service: Some(ServiceInfo {
name: "test".to_owned(),
version: "42".to_owned(),
}),
metric_prefix: Some("foo".to_owned()),
..Default::default()
})
.with_attributes(|size_hint: usize, _ctx: &Context<()>| {
let mut attributes = Vec::with_capacity(size_hint + 1);
attributes.push(KeyValue::new("test", "attribute_fn"));
attributes
})
.layer(());
let mut ctx = Context::default();
let req = Request::builder()
.uri("http://www.example.com")
.body(())
.unwrap();

let attributes = svc.compute_attributes(&mut ctx, &req);
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_NAME && attr.value.as_str() == "test"));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_VERSION && attr.value.as_str() == "42"));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == HTTP_REQUEST_HOST));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == "test" && attr.value.as_str() == "attribute_fn"));
}
}
104 changes: 104 additions & 0 deletions rama-net/src/stream/layer/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,107 @@ where
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_default_svc_compute_attributes_default() {
let svc = NetworkMetricsService::new(());
let attributes = svc.compute_attributes(&Context::default());
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_NAME));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_VERSION));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == NETWORK_TRANSPORT));
}

#[test]
fn test_custom_svc_compute_attributes_default() {
let svc = NetworkMetricsLayer::custom(MeterOptions {
service: Some(ServiceInfo {
name: "test".to_owned(),
version: "42".to_owned(),
}),
metric_prefix: Some("foo".to_owned()),
..Default::default()
})
.layer(());

let attributes = svc.compute_attributes(&Context::default());
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_NAME && attr.value.as_str() == "test"));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_VERSION && attr.value.as_str() == "42"));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == NETWORK_TRANSPORT));
}

#[test]
fn test_custom_svc_compute_attributes_attributes_vec() {
let svc = NetworkMetricsLayer::custom(MeterOptions {
service: Some(ServiceInfo {
name: "test".to_owned(),
version: "42".to_owned(),
}),
metric_prefix: Some("foo".to_owned()),
..Default::default()
})
.with_attributes(vec![KeyValue::new("test", "attribute_fn")])
.layer(());

let attributes = svc.compute_attributes(&Context::default());
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_NAME && attr.value.as_str() == "test"));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_VERSION && attr.value.as_str() == "42"));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == NETWORK_TRANSPORT));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == "test" && attr.value.as_str() == "attribute_fn"));
}

#[test]
fn test_custom_svc_compute_attributes_attribute_fn() {
let svc = NetworkMetricsLayer::custom(MeterOptions {
service: Some(ServiceInfo {
name: "test".to_owned(),
version: "42".to_owned(),
}),
metric_prefix: Some("foo".to_owned()),
..Default::default()
})
.with_attributes(|size_hint: usize, _ctx: &Context<()>| {
let mut attributes = Vec::with_capacity(size_hint + 1);
attributes.push(KeyValue::new("test", "attribute_fn"));
attributes
})
.layer(());

let attributes = svc.compute_attributes(&Context::default());
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_NAME && attr.value.as_str() == "test"));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == SERVICE_VERSION && attr.value.as_str() == "42"));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == NETWORK_TRANSPORT));
assert!(attributes
.iter()
.any(|attr| attr.key.as_str() == "test" && attr.value.as_str() == "attribute_fn"));
}
}
36 changes: 21 additions & 15 deletions rama-net/src/user/credentials/bearer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ impl authorization::Credentials for Bearer {
#[cfg(test)]
mod tests {
use super::*;
use authorization::Credentials;

#[test]
fn bearer_parse_empty() {
Expand All @@ -106,6 +105,27 @@ mod tests {
assert!(value.is_err());
}

#[test]
fn bearer_header() {
let auth = Bearer::try_from_header_str("Bearer 123abc").unwrap();
assert_eq!(auth.token(), "123abc");
assert_eq!("Bearer 123abc", auth.as_header_string());
}

#[test]
fn bearer_clear() {
let auth = Bearer::try_from_clear_str("foobar".to_owned()).unwrap();
assert_eq!(auth.token(), "foobar");
assert_eq!("foobar", auth.as_clear_string());
}
}

#[cfg(feature = "http")]
#[cfg(test)]
mod http_tests {
use super::*;
use authorization::Credentials;

#[test]
fn bearer_encode() {
let auth = Bearer::try_from_clear_str("foobar").unwrap();
Expand Down Expand Up @@ -134,18 +154,4 @@ mod tests {
.unwrap();
assert_eq!(auth.token(), "QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
}

#[test]
fn bearer_header() {
let auth = Bearer::try_from_header_str("Bearer 123abc").unwrap();
assert_eq!(auth.token(), "123abc");
assert_eq!("Bearer 123abc", auth.as_header_string());
}

#[test]
fn bearer_clear() {
let auth = Bearer::try_from_clear_str("foobar".to_owned()).unwrap();
assert_eq!(auth.token(), "foobar");
assert_eq!("foobar", auth.as_clear_string());
}
}

0 comments on commit 2e4027a

Please sign in to comment.