Skip to content

Commit

Permalink
feat(integrations): parse and extract host and port from MongoDB URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
adriencaccia committed Dec 7, 2023
1 parent 8605a92 commit f482a84
Showing 1 changed file with 84 additions and 13 deletions.
97 changes: 84 additions & 13 deletions src/integrations/mongo_tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{

use reqwest::Client;
use tokio::fs;
use url::Url;

use crate::{helpers::get_env_variable, prelude::*};

Expand All @@ -15,50 +16,76 @@ pub struct MongoTracer {
process: Option<Child>,
server_address: String,
profile_folder: PathBuf,
proxy_host_port: String,
destination_host_port: String,
proxy_mongo_uri: String,
user_mongo_uri: String,
uri_env_name: String,
}

impl MongoTracer {
pub fn try_from(profile_folder: &PathBuf, mongodb_config: &MongoDBConfig) -> Result<Self> {
debug!(
"Retrieving the value of {} to patch the MongoDB URI",
"Retrieving the value of {} to patch the MongoDB URL",
mongodb_config.uri_env_name
);
let destination_host_port = get_env_variable(mongodb_config.uri_env_name.as_str())?;
let user_mongo_uri = get_env_variable(mongodb_config.uri_env_name.as_str())?;

Ok(Self {
process: None,
server_address: "0.0.0.0:55581".into(),
profile_folder: profile_folder.into(),
// TODO: later choose a random available port dynamically, and/or make it configurable
proxy_host_port: "mongodb://localhost:27018".into(),
destination_host_port,
proxy_mongo_uri: "mongodb://localhost:27018".into(),
user_mongo_uri,
uri_env_name: mongodb_config.uri_env_name.clone(),
})
}

fn get_host_port_from_uris(&self) -> Result<(String, String)> {
let proxy_uri = Url::parse(&self.proxy_mongo_uri)?;
let destination_uri = Url::parse(&self.user_mongo_uri)?;

let parsing_error_fn = || {
anyhow!(
"Failed to parse the Mongo URI: {}. Be sure to follow the MongoDB URI format described here: https://www.mongodb.com/docs/manual/reference/connection-string/#connection-string-formats",
self.proxy_mongo_uri
)
};

let proxy_host_port = format!(
"{}:{}",
proxy_uri.host_str().unwrap_or_default(),
proxy_uri.port().unwrap_or_default()
);
let destination_host_port = format!(
"{}:{}",
destination_uri.host_str().ok_or_else(parsing_error_fn)?,
destination_uri.port().ok_or_else(parsing_error_fn)?
);

Ok((proxy_host_port, destination_host_port))
}

pub async fn start(&mut self) -> Result<()> {
let mut command = Command::new("cs-mongo-tracer");
let (proxy_host_port, destination_host_port) = self
.get_host_port_from_uris()
.context("Failed to parse the mongo uris")?;

command.envs(vec![
(
"CODSPEED_MONGO_INSTR_SERVER_ADDRESS",
self.server_address.as_str(),
),
(
"CODSPEED_MONGO_PROXY_HOST_PORT",
self.proxy_host_port.as_str(),
),
("CODSPEED_MONGO_PROXY_HOST_PORT", proxy_host_port.as_str()),
(
"CODSPEED_MONGO_DEST_HOST_PORT",
self.destination_host_port.as_str(),
destination_host_port.as_str(),
),
]);
debug!("Start the MongoDB tracer: {:?}", command);
debug!(
"Proxy MongoDB from {} to {}",
self.proxy_host_port, self.destination_host_port
proxy_host_port, destination_host_port
);
let process = command.spawn()?;

Expand Down Expand Up @@ -100,7 +127,51 @@ impl MongoTracer {
"CODSPEED_MONGO_INSTR_SERVER_ADDRESS",
self.server_address.as_str(),
),
(self.uri_env_name.as_str(), self.proxy_host_port.as_str()),
(self.uri_env_name.as_str(), self.proxy_mongo_uri.as_str()),
]);
}
}

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

#[test]
fn test_get_host_port_from_uris() {
let tracer = MongoTracer {
process: None,
server_address: "".into(),
profile_folder: "".into(),
proxy_mongo_uri: "mongodb://localhost:27018".into(),
user_mongo_uri: "mongodb://localhost:27017".into(),
uri_env_name: "".into(),
};

let (proxy_host_port, destination_host_port) = tracer
.get_host_port_from_uris()
.expect("Failed to parse the mongo uris");

assert_eq!(proxy_host_port, "localhost:27018");
assert_eq!(destination_host_port, "localhost:27017");
}

#[test]
fn test_get_host_port_from_uris_error() {
let tracer = MongoTracer {
process: None,
server_address: "".into(),
profile_folder: "".into(),
proxy_mongo_uri: "mongodb://localhost:27018".into(),
user_mongo_uri: "localhost:27017".into(),
uri_env_name: "".into(),
};

let result = tracer.get_host_port_from_uris();

assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
"Failed to parse the Mongo URI: mongodb://localhost:27018. Be sure to follow the MongoDB URI format described here: https://www.mongodb.com/docs/manual/reference/connection-string/#connection-string-formats"
);
}
}

0 comments on commit f482a84

Please sign in to comment.