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

Add response status to AWS and remote namespaces #4

Merged
merged 4 commits into from
Mar 13, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Add the following to your `Cargo.toml` file:

```toml
[dependencies]
xray-lite = { git = "https://github.com/codemonger-io/xray-lite.git", tag = "v0.0.5" }
xray-lite = { git = "https://github.com/codemonger-io/xray-lite.git", tag = "v0.0.6" }
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion xray-lite/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xray-lite"
version = "0.0.5"
version = "0.0.6"
authors = ["softprops <[email protected]>", "Kikuo Emoto <[email protected]>"]
edition = "2021"
description = "AWS X-Ray daemon client for Rust"
Expand Down
144 changes: 143 additions & 1 deletion xray-lite/src/namespace.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Namespace encapsulation for subsegments.

use crate::segment::{AwsOperation, Http, Request, Subsegment};
use crate::segment::{AwsOperation, Http, Request, Response, Subsegment};

/// Namespace.
pub trait Namespace {
Expand All @@ -19,6 +19,7 @@ pub struct AwsNamespace {
service: String,
operation: String,
request_id: Option<String>,
response_status: Option<u16>,
}

impl AwsNamespace {
Expand All @@ -28,6 +29,7 @@ impl AwsNamespace {
service: service.into(),
operation: operation.into(),
request_id: None,
response_status: None,
}
}

Expand All @@ -36,6 +38,12 @@ impl AwsNamespace {
self.request_id = Some(request_id.into());
self
}

/// Sets the response status.
pub fn response_status(&mut self, status: u16) -> &mut Self {
self.response_status = Some(status);
self
}
}

impl Namespace for AwsNamespace {
Expand All @@ -61,6 +69,28 @@ impl Namespace for AwsNamespace {
..AwsOperation::default()
});
}
if let Some(response_status) = self.response_status {
if let Some(http) = subsegment.http.as_mut() {
if let Some(response) = http.response.as_mut() {
if response.status.is_none() {
response.status = Some(response_status);
}
} else {
http.response = Some(Response {
status: Some(response_status),
..Response::default()
});
}
} else {
subsegment.http = Some(Http {
response: Some(Response {
status: Some(response_status),
..Response::default()
}),
..Http::default()
});
}
}
}
}

Expand All @@ -70,6 +100,7 @@ pub struct RemoteNamespace {
name: String,
method: String,
url: String,
response_status: Option<u16>,
}

impl RemoteNamespace {
Expand All @@ -79,8 +110,15 @@ impl RemoteNamespace {
name: name.into(),
method: method.into(),
url: url.into(),
response_status: None,
}
}

/// Sets the response status.
pub fn response_status(&mut self, status: u16) -> &mut Self {
self.response_status = Some(status);
self
}
}

impl Namespace for RemoteNamespace {
Expand Down Expand Up @@ -117,6 +155,19 @@ impl Namespace for RemoteNamespace {
..Http::default()
});
}
if let Some(response_status) = self.response_status {
let http = subsegment.http.as_mut().expect("http must have been set");
if let Some(response) = http.response.as_mut() {
if response.status.is_none() {
response.status = Some(response_status);
}
} else {
http.response = Some(Response {
status: Some(response_status),
..Response::default()
});
}
}
}
}

Expand All @@ -141,3 +192,94 @@ impl Namespace for CustomNamespace {
// does nothing
fn update_subsegment(&self, _subsegment: &mut Subsegment) {}
}

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

#[test]
fn aws_namespace_should_have_service_name_as_name() {
let namespace = AwsNamespace::new("S3", "GetObject");
assert_eq!(namespace.name(""), "S3");
assert_eq!(namespace.name("prefix"), "S3");
}

#[test]
fn aws_namespace_should_update_subsegment_with_aws_operation() {
let namespace = AwsNamespace::new("S3", "GetObject");
let mut subsegment = Subsegment::default();
namespace.update_subsegment(&mut subsegment);
assert_eq!(subsegment.namespace.unwrap(), "aws");
assert_eq!(subsegment.aws.unwrap().operation.unwrap(), "GetObject");
}

#[test]
fn aws_namespace_should_update_subsegment_with_request_id() {
let mut namespace = AwsNamespace::new("S3", "GetObject");
namespace.request_id("12345");
let mut subsegment = Subsegment::default();
namespace.update_subsegment(&mut subsegment);
assert_eq!(subsegment.aws.unwrap().request_id.unwrap(), "12345");
}

#[test]
fn aws_namespace_should_update_subsegment_with_response_status() {
let mut namespace = AwsNamespace::new("S3", "GetObject");
namespace.response_status(200);
let mut subsegment = Subsegment::default();
namespace.update_subsegment(&mut subsegment);
assert_eq!(
subsegment
.http
.expect("http")
.response
.expect("response")
.status
.expect("status"),
200,
);
}

#[test]
fn remote_namespace_should_have_name_as_name() {
let namespace = RemoteNamespace::new("codemonger.io", "GET", "https://codemonger.io/");
assert_eq!(namespace.name(""), "codemonger.io");
assert_eq!(namespace.name("prefix"), "codemonger.io");
}

#[test]
fn remote_namespace_should_update_subsegment_with_remote_service() {
let namespace = RemoteNamespace::new("codemonger.io", "GET", "https://codemonger.io/");
let mut subsegment = Subsegment::default();
namespace.update_subsegment(&mut subsegment);
assert_eq!(subsegment.namespace.unwrap(), "remote");
let request = subsegment.http.expect("http").request.expect("request");
assert_eq!(request.method.unwrap(), "GET");
assert_eq!(request.url.unwrap(), "https://codemonger.io/");
}

#[test]
fn remote_namespace_should_update_subsegment_with_response_status() {
let mut namespace = RemoteNamespace::new("codemonger.io", "GET", "https://codemonger.io/");
namespace.response_status(200);
let mut subsegment = Subsegment::default();
namespace.update_subsegment(&mut subsegment);
assert_eq!(
subsegment
.http
.expect("http")
.response
.expect("response")
.status
.expect("status"),
200,
);
}

#[test]
fn custom_namespace_should_have_prefixed_name() {
let namespace = CustomNamespace::new("TestSubsegment");
assert_eq!(namespace.name(""), "TestSubsegment");
assert_eq!(namespace.name("prefix"), "prefixTestSubsegment");
}
}