Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
feat: Add a logging system based on Tracing
Browse files Browse the repository at this point in the history
* Logs HTTP response data
* Includes HTTP request information in log context
* Can assert log lines during tests

Note, this is not compatible with MozLog yet.

ref #15
  • Loading branch information
mythmon committed May 13, 2021
1 parent feae5ec commit 1eb4359
Show file tree
Hide file tree
Showing 26 changed files with 1,132 additions and 226 deletions.
234 changes: 180 additions & 54 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions config/base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ adm:
remote_settings:
storage_path: "./rs-cache"
collection: "quicksuggest"

logging:
levels: [INFO]
4 changes: 1 addition & 3 deletions merino-adm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ serde = "1.0.125"
serde_derive = "1.0.125"
serde_json = "1.0.64"
serde_qs = "0.8.3"
serde_url_params = "0.2.1"
remote-settings-client = { git = "https://github.com/mozilla-services/remote-settings-client", rev = "06fbe92e3e45c42ff1bdd9b71ceda6e9e908a656" }
viaduct = { git = "https://github.com/mozilla/application-services", rev = "v75.0.0"}
viaduct-reqwest = { git = "https://github.com/mozilla/application-services", rev = "v75.0.0"}
anyhow = "1.0.40"
env_logger = "0.8.3"
reqwest = { version = "0.11.3", features = ["json"] }
tokio = { version = "1.5.0", features = ["rt", "macros", "rt-multi-thread"] }
radix_trie = "0.2.1"
merino-suggest = { path = "../merino-suggest" }
futures = "0.3.14"
merino-settings = { path = "../merino-settings" }
tracing = "0.1.26"

[dev-dependencies]
pretty_assertions = "0.7.1"
5 changes: 4 additions & 1 deletion merino-adm/src/remote_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ impl RemoteSettingsSuggester {
/// Download suggestions from Remote Settings
///
/// This must be called at least once before any suggestions will be provided
#[tracing::instrument(skip(self, settings))]
pub async fn sync(&mut self, settings: &Settings) -> Result<()> {
tracing::info!("Syncing quicksuggest records from Remote Settings");
let reqwest_client = reqwest::Client::new();

// Set up and sync a Remote Settings client for the quicksuggest collection.
Expand Down Expand Up @@ -112,10 +114,11 @@ impl RemoteSettingsSuggester {
}

if suggestions.is_empty() {
println!("Warn: No suggestion records found on Remote Settings");
tracing::warn!("No suggestion records found on Remote Settings");
}

self.suggestions = suggestions;
tracing::info!("Completed syncing quicksuggest records from Remote Settings");

Ok(())
}
Expand Down
5 changes: 5 additions & 0 deletions merino-integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ edition = "2018"
[dependencies]
actix-rt = "2.2.0"
tokio = "1.5.0"
tokio-test = "0.4.1"
reqwest = { version = "0.11.3", features = ["json"] }
anyhow = "1.0.40"
serde = { version = "1.0.125", features = ["derive"] }
serde_json = "1.0.64"
serde_with = "1.8.1"
merino-web = { path = "../merino-web" }
merino-settings = { path = "../merino-settings" }
httpmock = "0.5.8"
viaduct = { git = "https://github.com/mozilla/application-services", rev = "v75.0.0"}
viaduct-reqwest = { git = "https://github.com/mozilla/application-services", rev = "v75.0.0"}
tracing = "0.1.26"
tracing-subscriber = "0.2.18"
maplit = "1.0.2"
63 changes: 35 additions & 28 deletions merino-integration-tests/src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
//! Tests Merino's debug pages.
#![cfg(test)]

use crate::{merino_test, TestingTools};
use merino_settings::Settings;
use reqwest::StatusCode;

use crate::TestingTools;

#[actix_rt::test]
async fn cant_use_debug_settings_route_when_debug_is_false() {
let TestingTools { test_client, .. } = TestingTools::new(|settings| settings.debug = false);

let response = test_client
.get("/debug/settings")
.send()
.await
.expect("failed to execute request");

assert_eq!(response.status(), StatusCode::NOT_FOUND);
assert_eq!(response.content_length(), Some(0));
merino_test(
|settings: &mut Settings| settings.debug = false,
|TestingTools { test_client, .. }| async move {
let response = test_client
.get("/debug/settings")
.send()
.await
.expect("failed to execute request");

assert_eq!(response.status(), StatusCode::NOT_FOUND);
assert_eq!(response.content_length(), Some(0));
},
)
.await
}

#[actix_rt::test]
async fn can_use_debug_settings_route_when_debug_is_true() {
let TestingTools { test_client, .. } = TestingTools::new(|settings| {
settings.debug = true;
});

let response = test_client
.get("/debug/settings")
.send()
.await
.expect("failed to execute request");

assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.headers().get("content-type").unwrap(),
&"application/json"
);
assert!(response.json::<serde_json::Value>().await.is_ok());
merino_test(
|settings: &mut Settings| settings.debug = true,
|TestingTools { test_client, .. }| async move {
let response = test_client
.get("/debug/settings")
.send()
.await
.expect("failed to execute request");

assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.headers().get("content-type").unwrap(),
&"application/json"
);
assert!(response.json::<serde_json::Value>().await.is_ok());
},
)
.await
}
158 changes: 87 additions & 71 deletions merino-integration-tests/src/dockerflow.rs
Original file line number Diff line number Diff line change
@@ -1,92 +1,108 @@
//! Tests that Merino conforms to [Dockerflow](https://github.com/mozilla-services/dockerflow).
#![cfg(test)]

use crate::{merino_test, TestingTools};
use anyhow::Result;
use reqwest::StatusCode;
use serde::Deserialize;

use crate::TestingTools;

#[actix_rt::test]
async fn lbheartbeat_works() {
let TestingTools { test_client, .. } = TestingTools::new(|_| ());

let response = test_client
.get("/__lbheartbeat__")
.send()
.await
.expect("failed to execute request");

assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.content_length(), Some(0));
merino_test(
|_| (),
|TestingTools { test_client, .. }| async move {
let response = test_client
.get("/__lbheartbeat__")
.send()
.await
.expect("failed to execute request");

assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.content_length(), Some(0));
},
)
.await
}

#[actix_rt::test]
async fn heartbeat_works() -> Result<()> {
let TestingTools { test_client, .. } = TestingTools::new(|_| ());

let response = test_client
.get("/__heartbeat__")
.send()
.await
.expect("failed to execute request");

assert!(response.status().is_success());
assert_eq!(
response
.headers()
.get_all("content-type")
.iter()
.collect::<Vec<_>>(),
vec!["application/json"]
);
Ok(())
merino_test(
|_| (),
|TestingTools { test_client, .. }| async move {
let response = test_client
.get("/__heartbeat__")
.send()
.await
.expect("failed to execute request");

assert!(response.status().is_success());
assert_eq!(
response
.headers()
.get_all("content-type")
.iter()
.collect::<Vec<_>>(),
vec!["application/json"]
);
Ok(())
},
)
.await
}

#[actix_rt::test]
async fn version_works() -> Result<()> {
let TestingTools { test_client, .. } = TestingTools::new(|_| ());

let response = test_client
.get("/__version__")
.send()
.await
.expect("failed to execute request");

assert!(response.status().is_success());
assert_eq!(
response
.headers()
.get_all("content-type")
.iter()
.collect::<Vec<_>>(),
vec!["application/json"]
);

#[derive(Deserialize, Debug)]
#[allow(dead_code)]
struct VersionInfo {
source: String,
version: String,
commit: String,
build: String,
}
let body: Result<VersionInfo, _> = response.json().await;
assert!(body.is_ok());

Ok(())
merino_test(
|_| (),
|TestingTools { test_client, .. }| async move {
let response = test_client
.get("/__version__")
.send()
.await
.expect("failed to execute request");

assert!(response.status().is_success());
assert_eq!(
response
.headers()
.get_all("content-type")
.iter()
.collect::<Vec<_>>(),
vec!["application/json"]
);

#[derive(Deserialize, Debug)]
#[allow(dead_code)]
struct VersionInfo {
source: String,
version: String,
commit: String,
build: String,
}
let body: Result<VersionInfo, _> = response.json().await;
assert!(body.is_ok());

Ok(())
},
)
.await
}

#[actix_rt::test]
async fn error_works() -> Result<()> {
let TestingTools { test_client, .. } = TestingTools::new(|_| ());

let response = test_client
.get("/__error__")
.send()
.await
.expect("failed to execute request");

assert!(response.status().is_server_error());

Ok(())
merino_test(
|_| (),
|TestingTools { test_client, .. }| async move {
let response = test_client
.get("/__error__")
.send()
.await
.expect("failed to execute request");

assert!(response.status().is_server_error());

Ok(())
},
)
.await
}
Loading

0 comments on commit 1eb4359

Please sign in to comment.