Skip to content

Commit

Permalink
JSON log output (#9)
Browse files Browse the repository at this point in the history
* Allow outputting JSON logs from ingesters.

* Skip instrument for faktory.

* Use more specific timestamp format.

* Update logger for webhooks too.
  • Loading branch information
Syfaro authored Apr 21, 2021
1 parent dd7805d commit 3974f85
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 22 deletions.
29 changes: 14 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion fuzzysearch-ingest-e621/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ type Auth = (String, Option<String>);

#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
if matches!(std::env::var("LOG_FMT").as_deref(), Ok("json")) {
tracing_subscriber::fmt::Subscriber::builder()
.json()
.with_timer(tracing_subscriber::fmt::time::ChronoUtc::rfc3339())
.init();
} else {
tracing_subscriber::fmt::init();
}

create_metrics_server().await;

Expand Down
9 changes: 8 additions & 1 deletion fuzzysearch-ingest-furaffinity/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,14 @@ async fn process_submission(

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
if matches!(std::env::var("LOG_FMT").as_deref(), Ok("json")) {
tracing_subscriber::fmt::Subscriber::builder()
.json()
.with_timer(tracing_subscriber::fmt::time::ChronoUtc::rfc3339())
.init();
} else {
tracing_subscriber::fmt::init();
}

let (cookie_a, cookie_b) = (
std::env::var("FA_A").expect_or_log("Missing FA_A"),
Expand Down
3 changes: 3 additions & 0 deletions fuzzysearch-ingest-weasyl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ edition = "2018"
[dependencies]
anyhow = "1"

tracing = "0.1"
tracing-subscriber = "0.2"

reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }

Expand Down
20 changes: 16 additions & 4 deletions fuzzysearch-ingest-weasyl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ async fn load_frontpage(client: &reqwest::Client, api_key: &str) -> anyhow::Resu
Ok(max as i32)
}

#[tracing::instrument(skip(client, api_key))]
async fn load_submission(
client: &reqwest::Client,
api_key: &str,
id: i32,
) -> anyhow::Result<(Option<WeasylSubmission>, serde_json::Value)> {
println!("Loading submission {}", id);
tracing::debug!("Loading submission");

let body: serde_json::Value = client
.get(&format!(
Expand Down Expand Up @@ -114,14 +115,15 @@ async fn load_submission(
Ok((res, body))
}

#[tracing::instrument(skip(pool, client, faktory, body, sub), fields(id = sub.id))]
async fn process_submission(
pool: &sqlx::Pool<sqlx::Postgres>,
client: &reqwest::Client,
faktory: &FaktoryClient,
body: serde_json::Value,
sub: WeasylSubmission,
) -> anyhow::Result<()> {
println!("Processing submission {}", sub.id);
tracing::debug!("Processing submission");

let data = client
.get(&sub.media.submission.first().unwrap().url)
Expand All @@ -138,7 +140,7 @@ async fn process_submission(
let num = i64::from_be_bytes(bytes);
Some(num)
} else {
println!("Unable to decode image on submission {}", sub.id);
tracing::warn!("Unable to decode image");

None
};
Expand Down Expand Up @@ -172,12 +174,13 @@ async fn process_submission(
Ok(())
}

#[tracing::instrument(skip(pool, body))]
async fn insert_null(
pool: &sqlx::Pool<sqlx::Postgres>,
body: serde_json::Value,
id: i32,
) -> anyhow::Result<()> {
println!("Inserting null for submission {}", id);
tracing::debug!("Inserting null submission");

sqlx::query!("INSERT INTO WEASYL (id, data) VALUES ($1, $2)", id, body)
.execute(pool)
Expand All @@ -188,6 +191,15 @@ async fn insert_null(

#[tokio::main]
async fn main() {
if matches!(std::env::var("LOG_FMT").as_deref(), Ok("json")) {
tracing_subscriber::fmt::Subscriber::builder()
.json()
.with_timer(tracing_subscriber::fmt::time::ChronoUtc::rfc3339())
.init();
} else {
tracing_subscriber::fmt::init();
}

let api_key = std::env::var("WEASYL_APIKEY").unwrap();

let pool = sqlx::postgres::PgPoolOptions::new()
Expand Down
9 changes: 8 additions & 1 deletion fuzzysearch-webhook/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ pub enum WebhookError {
}

fn main() {
tracing_subscriber::fmt::init();
if matches!(std::env::var("LOG_FMT").as_deref(), Ok("json")) {
tracing_subscriber::fmt::Subscriber::builder()
.json()
.with_timer(tracing_subscriber::fmt::time::ChronoUtc::rfc3339())
.init();
} else {
tracing_subscriber::fmt::init();
}

tracing::info!("Starting...");

Expand Down

0 comments on commit 3974f85

Please sign in to comment.