ba18b44 had the JSON layer find a line's span context through the
dispatch it was registered with, and production's logging wrapped that
layer in an Option, whose Layer impl does not pass on_register_dispatch
on: the layer never got its dispatch and every line went out without
trace_id and span_id. The log test built a subscriber of its own,
without the Option, and passed.
Both services now box their stderr layer instead of an Option of each
kind (a Box passes registration on), the formatter falls back to the
default dispatcher when it was never registered, which a global
subscriber's lines find, and the log test builds the subscriber with
the function init uses, writing to its buffer, so a wrapper like this
one is under test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RqkrVDqdiQEMWAkNGQ2tYA
145 lines
6.2 KiB
Rust
145 lines
6.2 KiB
Rust
//! `ipx-directory`: serve the directory, make a token, or check that a running one answers.
|
|
|
|
use anyhow::{Context, Result};
|
|
use clap::{Parser, Subcommand};
|
|
use ipx_directory::store::Store;
|
|
use ipx_directory::{Directory, config};
|
|
use std::path::{Path, PathBuf};
|
|
use std::time::Duration;
|
|
use tokio::net::TcpListener;
|
|
use tokio::signal::unix::SignalKind;
|
|
use tracing_subscriber::EnvFilter;
|
|
use tracing_subscriber::prelude::*;
|
|
|
|
#[derive(Parser)]
|
|
#[command(version, about = "ipx's directory of public podcasts")]
|
|
struct Cli {
|
|
/// The config file: where to listen, the database, and the tokens it accepts. Otherwise
|
|
/// DIRECTORY_CONFIG, or else /config/directory.toml.
|
|
#[arg(long)]
|
|
config: Option<PathBuf>,
|
|
#[command(subcommand)]
|
|
cmd: Option<Cmd>,
|
|
}
|
|
|
|
#[derive(Subcommand)]
|
|
enum Cmd {
|
|
/// Serve the directory (the default).
|
|
Serve,
|
|
/// Make a token, and print it with the lines that let it in.
|
|
Token {
|
|
#[command(subcommand)]
|
|
cmd: TokenCmd,
|
|
},
|
|
/// Check that the directory on this machine answers and can read its database.
|
|
Health,
|
|
}
|
|
|
|
#[derive(Subcommand)]
|
|
enum TokenCmd {
|
|
New {
|
|
/// Who it is for, as the log and their snapshot will name them.
|
|
name: String,
|
|
#[arg(long, value_parser = ["ro", "rw", "rwa"])]
|
|
access: String,
|
|
},
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
let cli = Cli::parse();
|
|
let config_path = cli
|
|
.config
|
|
.or_else(|| std::env::var_os("DIRECTORY_CONFIG").map(Into::into))
|
|
.unwrap_or_else(|| "/config/directory.toml".into());
|
|
match cli.cmd.unwrap_or(Cmd::Serve) {
|
|
Cmd::Serve => serve(&config_path).await,
|
|
Cmd::Token { cmd: TokenCmd::New { name, access } } => {
|
|
let token = config::new_token();
|
|
println!("A token for {name}, to give them; it is not kept anywhere:\n\n {token}\n");
|
|
println!("To let it in, add this to {}:\n", config_path.display());
|
|
println!("[[tokens]]\nname = {name:?}\naccess = {access:?}\ntoken_sha256 = {:?}", config::hash(&token));
|
|
println!("\nThe directory reads the file again within a minute. Delete the entry to revoke it.");
|
|
Ok(())
|
|
}
|
|
Cmd::Health => {
|
|
let cfg = config::Config::load(&config_path)?;
|
|
let port = cfg.bind.rsplit(':').next().unwrap_or("8100");
|
|
let resp = reqwest::Client::new()
|
|
.get(format!("http://127.0.0.1:{port}/healthz"))
|
|
.timeout(Duration::from_secs(4))
|
|
.send()
|
|
.await
|
|
.context("the directory does not answer")?;
|
|
anyhow::ensure!(resp.status().is_success(), "the directory answers {}", resp.status());
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn serve(path: &Path) -> Result<()> {
|
|
// One JSON object a line for Loki when DIRECTORY_LOG_FORMAT=json, as ipx does; text otherwise.
|
|
let json = std::env::var("DIRECTORY_LOG_FORMAT").is_ok_and(|f| f.eq_ignore_ascii_case("json"));
|
|
let filter = || EnvFilter::try_from_env("DIRECTORY_LOG").unwrap_or_else(|_| "ipx_directory=info".into());
|
|
// Traces to Tempo when OTEL_EXPORTER_OTLP_ENDPOINT is set, joining ipx's (#213).
|
|
let otel = ipx_directory::otel::provider("ipx-directory")?;
|
|
// Each line's own fields say all its span did, and the span said it again (#363); the trace
|
|
// and span ids lead to the trace instead, as ipx's do (#375). Boxed rather than an Option of
|
|
// each: Option's Layer does not pass on_register_dispatch on, which the JSON layer needs (#408).
|
|
let stderr: Box<dyn tracing_subscriber::Layer<tracing_subscriber::Registry> + Send + Sync> = if json {
|
|
ipx_directory::otel::json_layer(std::io::stderr, false).with_filter(filter()).boxed()
|
|
} else {
|
|
tracing_subscriber::fmt::layer().with_writer(std::io::stderr).with_filter(filter()).boxed()
|
|
};
|
|
tracing_subscriber::registry()
|
|
.with(stderr)
|
|
.with(otel.as_ref().map(|p| {
|
|
use opentelemetry::trace::TracerProvider;
|
|
tracing_opentelemetry::layer()
|
|
.with_tracer(p.tracer("ipx-directory"))
|
|
.with_filter(EnvFilter::new("ipx_directory=info,ipx_feed=info"))
|
|
}))
|
|
.init();
|
|
|
|
let cfg = config::Config::load(path)?;
|
|
let tokens = cfg.tokens()?;
|
|
let url = std::env::var(&cfg.database_url_env)
|
|
.ok()
|
|
.filter(|u| !u.trim().is_empty())
|
|
.with_context(|| format!("{} is not set: the directory needs a database URL", cfg.database_url_env))?;
|
|
let store = Store::open(&url, /* keep_open */ true).await.context("opening the database")?;
|
|
let dir = Directory::new(store, vec![], ipx_feed::guard::Allowed::parse(&cfg.fetch_internal)?)?;
|
|
let dropped = dir.set_tokens(tokens).await?;
|
|
|
|
let listener = TcpListener::bind(&cfg.bind).await.with_context(|| format!("listening on {}", cfg.bind))?;
|
|
tracing::info!(version = env!("CARGO_PKG_VERSION"), bind = %cfg.bind, tokens = cfg.tokens.len(), dropped, "directory started");
|
|
if !cfg.metrics_bind.is_empty() {
|
|
let metrics = TcpListener::bind(&cfg.metrics_bind)
|
|
.await
|
|
.with_context(|| format!("listening for metrics on {}", cfg.metrics_bind))?;
|
|
tracing::info!(bind = %cfg.metrics_bind, "serving metrics for Prometheus at /metrics");
|
|
tokio::spawn(ipx_directory::metrics::serve(metrics));
|
|
}
|
|
tokio::spawn(ipx_directory::refresher(dir.clone()));
|
|
let country = cfg.apple_country.trim().to_ascii_lowercase();
|
|
if !country.is_empty() {
|
|
tracing::info!(country = %country, "Apple Podcasts' charts are synced weekly");
|
|
tokio::spawn(ipx_directory::apple::run(dir.clone(), country));
|
|
}
|
|
tokio::spawn(ipx_directory::reloader(dir.clone(), path.to_owned()));
|
|
axum::serve(listener, ipx_directory::router(dir))
|
|
.with_graceful_shutdown(async {
|
|
let mut term = tokio::signal::unix::signal(SignalKind::terminate()).expect("SIGTERM handler");
|
|
tokio::select! {
|
|
_ = term.recv() => {}
|
|
_ = tokio::signal::ctrl_c() => {}
|
|
}
|
|
})
|
|
.await?;
|
|
// The batch exporter holds the last few seconds of spans; without this they are lost.
|
|
if let Some(p) = otel {
|
|
let _ = p.shutdown();
|
|
}
|
|
Ok(())
|
|
}
|