From a62e0b3c85bd1b80298bed9365963cccb0257420 Mon Sep 17 00:00:00 2001 From: aumetra Date: Sun, 22 Oct 2023 00:25:59 +0200 Subject: [PATCH] finish --- Cargo.lock | 7 +- crates/kitsune-config/src/lib.rs | 7 ++ .../kitsune-core/src/activitypub/fetcher.rs | 2 +- crates/kitsune-core/src/resolve/post.rs | 2 +- .../src/service/federation_filter.rs | 3 +- crates/kitsune-observability/Cargo.toml | 1 + crates/kitsune-observability/src/lib.rs | 72 ++++++++++++++----- kitsune-job-runner/Cargo.toml | 2 + kitsune-job-runner/src/lib.rs | 2 +- kitsune-job-runner/src/main.rs | 5 +- kitsune/Cargo.toml | 4 +- kitsune/src/http/mod.rs | 2 +- kitsune/src/lib.rs | 6 +- kitsune/src/main.rs | 9 +-- 14 files changed, 87 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 502c465ee..a7032b747 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2765,6 +2765,7 @@ dependencies = [ "iso8601-timestamp", "kitsune-cache", "kitsune-captcha", + "kitsune-config", "kitsune-core", "kitsune-db", "kitsune-embed", @@ -2772,6 +2773,7 @@ dependencies = [ "kitsune-http-signatures", "kitsune-job-runner", "kitsune-language", + "kitsune-observability", "kitsune-search", "kitsune-storage", "kitsune-test", @@ -2801,8 +2803,6 @@ dependencies = [ "tower", "tower-http", "tracing", - "tracing-error", - "tracing-subscriber", "typed-builder", "url", "utoipa", @@ -3041,8 +3041,10 @@ dependencies = [ "clap", "color-eyre", "deadpool-redis", + "kitsune-config", "kitsune-core", "kitsune-db", + "kitsune-observability", "kitsune-retry-policies", "mimalloc", "tokio", @@ -3085,6 +3087,7 @@ version = "0.0.1-pre.3" dependencies = [ "eyre", "kitsune-config", + "kitsune-http-client", "metrics", "metrics-opentelemetry", "metrics-tracing-context", diff --git a/crates/kitsune-config/src/lib.rs b/crates/kitsune-config/src/lib.rs index b5f7eec36..79f438b4f 100644 --- a/crates/kitsune-config/src/lib.rs +++ b/crates/kitsune-config/src/lib.rs @@ -117,6 +117,12 @@ pub struct MeiliSearchConfiguration { pub api_key: SmolStr, } +#[derive(Clone, Deserialize, Serialize)] +#[serde(rename_all = "kebab-case")] +pub struct OpenTelemetryConfiguration { + pub http_endpoint: String, +} + #[derive(Clone, Deserialize, Serialize)] #[serde(rename_all = "kebab-case", tag = "type")] pub enum SearchConfiguration { @@ -179,6 +185,7 @@ pub struct Configuration { pub instance: InstanceConfiguration, pub job_queue: JobQueueConfiguration, pub messaging: MessagingConfiguration, + pub opentelemetry: Option, pub server: ServerConfiguration, pub search: SearchConfiguration, pub storage: StorageConfiguration, diff --git a/crates/kitsune-core/src/activitypub/fetcher.rs b/crates/kitsune-core/src/activitypub/fetcher.rs index 8e6ea10fe..b8f67f5da 100644 --- a/crates/kitsune-core/src/activitypub/fetcher.rs +++ b/crates/kitsune-core/src/activitypub/fetcher.rs @@ -324,7 +324,6 @@ mod test { use super::MAX_FETCH_DEPTH; use crate::{ activitypub::Fetcher, - config::FederationFilterConfiguration, error::{ApiError, Error}, service::federation_filter::FederationFilterService, webfinger::Webfinger, @@ -335,6 +334,7 @@ mod test { use hyper::{Body, Request, Response, StatusCode, Uri}; use iso8601_timestamp::Timestamp; use kitsune_cache::NoopCache; + use kitsune_config::FederationFilterConfiguration; use kitsune_db::{model::account::Account, schema::accounts}; use kitsune_http_client::Client; use kitsune_search::NoopSearchService; diff --git a/crates/kitsune-core/src/resolve/post.rs b/crates/kitsune-core/src/resolve/post.rs index 0d5c9a097..0978b830a 100644 --- a/crates/kitsune-core/src/resolve/post.rs +++ b/crates/kitsune-core/src/resolve/post.rs @@ -78,7 +78,6 @@ mod test { use super::PostResolver; use crate::{ activitypub::Fetcher, - config::FederationFilterConfiguration, job::KitsuneContextRepo, service::{ account::AccountService, attachment::AttachmentService, @@ -92,6 +91,7 @@ mod test { use diesel_async::RunQueryDsl; use hyper::{Body, Request, Response}; use kitsune_cache::NoopCache; + use kitsune_config::FederationFilterConfiguration; use kitsune_db::{model::account::Account, schema::accounts}; use kitsune_http_client::Client; use kitsune_search::NoopSearchService; diff --git a/crates/kitsune-core/src/service/federation_filter.rs b/crates/kitsune-core/src/service/federation_filter.rs index 19e43ee44..f879bba55 100644 --- a/crates/kitsune-core/src/service/federation_filter.rs +++ b/crates/kitsune-core/src/service/federation_filter.rs @@ -1,5 +1,6 @@ -use crate::{config::FederationFilterConfiguration, error::FederationFilterError}; +use crate::error::FederationFilterError; use globset::{Glob, GlobSet, GlobSetBuilder}; +use kitsune_config::FederationFilterConfiguration; use kitsune_type::ap::{actor::Actor, Activity, Object}; use std::sync::Arc; use url::Url; diff --git a/crates/kitsune-observability/Cargo.toml b/crates/kitsune-observability/Cargo.toml index dfb59b134..f41e58125 100644 --- a/crates/kitsune-observability/Cargo.toml +++ b/crates/kitsune-observability/Cargo.toml @@ -6,6 +6,7 @@ version.workspace = true [dependencies] eyre = "0.6.8" kitsune-config = { path = "../kitsune-config" } +kitsune-http-client = { path = "../kitsune-http-client" } metrics = "0.21.1" metrics-opentelemetry = { git = "https://github.com/aumetra/metrics-opentelemetry.git", rev = "7c3176266c215bb9a7cbc31b3c32f75a22824928" } metrics-tracing-context = "0.14.0" diff --git a/crates/kitsune-observability/src/lib.rs b/crates/kitsune-observability/src/lib.rs index de7c7c0de..595373875 100644 --- a/crates/kitsune-observability/src/lib.rs +++ b/crates/kitsune-observability/src/lib.rs @@ -1,32 +1,27 @@ use eyre::Context; use kitsune_config::Configuration; use metrics_opentelemetry::OpenTelemetryRecorder; -use opentelemetry::{metrics::MeterProvider, trace::Tracer}; +use metrics_tracing_context::{MetricsLayer, TracingContextLayer}; +use metrics_util::layers::Layer as _; +use opentelemetry::{ + metrics::{noop::NoopMeterProvider, Meter, MeterProvider}, + runtime::Tokio, + trace::{noop::NoopTracer, Tracer}, +}; +use opentelemetry_otlp::WithExportConfig; use std::env; use tracing_error::ErrorLayer; -use tracing_opentelemetry::OpenTelemetryLayer; +use tracing_opentelemetry::{OpenTelemetryLayer, PreSampledTracer}; use tracing_subscriber::{ filter::{LevelFilter, Targets}, layer::SubscriberExt, - Layer, Registry, + Layer as _, Registry, }; -fn initialise_metrics(config: &Configuration, meter_provider: M) -> impl Layer +fn initialise_logging(tracer: T) -> eyre::Result<()> where - S: for<'a> tracing_subscriber::registry::LookupSpan<'a> + tracing::Subscriber, - M: MeterProvider, + T: Tracer + PreSampledTracer + Send + Sync + 'static, { - use metrics_tracing_context::{MetricsLayer, TracingContextLayer}; - use metrics_util::layers::Layer as _; - - let recorder = TracingContextLayer::all() - .layer(OpenTelemetryRecorder::new(meter_provider.meter("kitsune"))); - metrics::set_boxed_recorder(Box::new(recorder)).unwrap(); - - MetricsLayer::new() -} - -fn initialise_logging(config: &Configuration) -> eyre::Result<()> { let env_filter = env::var("RUST_LOG") .map_err(eyre::Report::from) .and_then(|targets| targets.parse().context("Failed to parse RUST_LOG value")) @@ -35,12 +30,51 @@ fn initialise_logging(config: &Configuration) -> eyre::Result<()> { let subscriber = Registry::default() .with(tracing_subscriber::fmt::layer().with_filter(env_filter)) .with(ErrorLayer::default()) - .with(OpenTelemetryLayer::new()); + .with(OpenTelemetryLayer::new(tracer)); - let subscriber = subscriber.with(initialise_metrics(config)); + let subscriber = subscriber.with(MetricsLayer::new()); tracing::subscriber::set_global_default(subscriber) .context("Couldn't install the global tracing subscriber")?; Ok(()) } + +fn initialise_metrics(meter: Meter) -> eyre::Result<()> { + let recorder = TracingContextLayer::all().layer(OpenTelemetryRecorder::new(meter)); + metrics::set_boxed_recorder(Box::new(recorder)) + .context("Couldn't install the global metrics recorder")?; + + Ok(()) +} + +pub fn initialise(app_name: &'static str, config: &Configuration) -> eyre::Result<()> { + if let Some(ref opentelemetry_config) = config.opentelemetry { + let tracer = opentelemetry_otlp::new_pipeline() + .tracing() + .with_exporter( + opentelemetry_otlp::new_exporter() + .http() + .with_endpoint(opentelemetry_config.http_endpoint.as_str()), + ) + .install_batch(Tokio)?; + + initialise_logging(tracer)?; + + let meter_provider = opentelemetry_otlp::new_pipeline() + .metrics(Tokio) + .with_exporter( + opentelemetry_otlp::new_exporter() + .http() + .with_endpoint(opentelemetry_config.http_endpoint.as_str()), + ) + .build()?; + + initialise_metrics(meter_provider.meter(app_name))?; + } else { + initialise_logging(NoopTracer::new())?; + initialise_metrics(NoopMeterProvider::new().meter(app_name))?; + } + + Ok(()) +} diff --git a/kitsune-job-runner/Cargo.toml b/kitsune-job-runner/Cargo.toml index 4608f1f1c..3b60526b4 100644 --- a/kitsune-job-runner/Cargo.toml +++ b/kitsune-job-runner/Cargo.toml @@ -8,8 +8,10 @@ athena = { path = "../lib/athena" } clap = { version = "4.4.6", features = ["derive"] } color-eyre = "0.6.2" deadpool-redis = "0.13.0" +kitsune-config = { path = "../crates/kitsune-config" } kitsune-core = { path = "../crates/kitsune-core" } kitsune-db = { path = "../crates/kitsune-db" } +kitsune-observability = { path = "../crates/kitsune-observability" } kitsune-retry-policies = { path = "../crates/kitsune-retry-policies" } mimalloc = "0.1.39" tokio = { version = "1.33.0", features = ["full"] } diff --git a/kitsune-job-runner/src/lib.rs b/kitsune-job-runner/src/lib.rs index 355988f49..d0d4c156a 100644 --- a/kitsune-job-runner/src/lib.rs +++ b/kitsune-job-runner/src/lib.rs @@ -2,9 +2,9 @@ extern crate tracing; use athena::JobQueue; +use kitsune_config::JobQueueConfiguration; use kitsune_core::{ activitypub::Deliverer, - config::JobQueueConfiguration, job::{JobRunnerContext, KitsuneContextRepo}, state::State as CoreState, }; diff --git a/kitsune-job-runner/src/main.rs b/kitsune-job-runner/src/main.rs index bb0dc2acc..974afc0c5 100644 --- a/kitsune-job-runner/src/main.rs +++ b/kitsune-job-runner/src/main.rs @@ -1,6 +1,7 @@ use clap::Parser; use color_eyre::eyre; -use kitsune_core::{config::Configuration, consts::VERSION}; +use kitsune_config::Configuration; +use kitsune_core::consts::VERSION; use std::path::PathBuf; use tokio::fs; @@ -24,6 +25,8 @@ async fn main() -> eyre::Result<()> { let raw_config = fs::read_to_string(args.config).await?; let config: Configuration = toml::from_str(&raw_config)?; + kitsune_observability::initialise(env!("CARGO_PKG_NAME"), &config)?; + let db_pool = kitsune_db::connect( &config.database.url, config.database.max_connections as usize, diff --git a/kitsune/Cargo.toml b/kitsune/Cargo.toml index 57ef1a364..925a05e8b 100644 --- a/kitsune/Cargo.toml +++ b/kitsune/Cargo.toml @@ -38,6 +38,7 @@ hyper = { version = "0.14.27", features = ["deprecated"] } iso8601-timestamp = "0.2.12" kitsune-cache = { path = "../crates/kitsune-cache" } kitsune-captcha = { path = "../crates/kitsune-captcha" } +kitsune-config = { path = "../crates/kitsune-config" } kitsune-core = { path = "../crates/kitsune-core" } kitsune-db = { path = "../crates/kitsune-db" } kitsune-embed = { path = "../crates/kitsune-embed" } @@ -45,6 +46,7 @@ kitsune-http-client = { path = "../crates/kitsune-http-client" } kitsune-http-signatures = { path = "../crates/kitsune-http-signatures" } kitsune-job-runner = { path = "../kitsune-job-runner" } kitsune-language = { path = "../crates/kitsune-language" } +kitsune-observability = { path = "../crates/kitsune-observability" } kitsune-search = { path = "../crates/kitsune-search" } kitsune-storage = { path = "../crates/kitsune-storage" } kitsune-type = { path = "../crates/kitsune-type" } @@ -75,8 +77,6 @@ tower-http = { version = "0.4.4", features = [ "trace", ] } tracing = "0.1.40" -tracing-error = "0.2.0" -tracing-subscriber = "0.3.17" typed-builder = "0.18.0" url = "2.4.1" utoipa = { version = "4.0.0", features = ["axum_extras", "uuid"] } diff --git a/kitsune/src/http/mod.rs b/kitsune/src/http/mod.rs index 424eac47b..504e50819 100644 --- a/kitsune/src/http/mod.rs +++ b/kitsune/src/http/mod.rs @@ -4,7 +4,7 @@ use self::{ }; use crate::state::Zustand; use axum::{extract::DefaultBodyLimit, Router}; -use kitsune_core::config::ServerConfiguration; +use kitsune_config::ServerConfiguration; use std::time::Duration; use tower_http::{ catch_panic::CatchPanicLayer, diff --git a/kitsune/src/lib.rs b/kitsune/src/lib.rs index e7b21d895..7a72264ca 100644 --- a/kitsune/src/lib.rs +++ b/kitsune/src/lib.rs @@ -26,7 +26,8 @@ use self::{ state::{SessionConfig, Zustand}, }; use athena::JobQueue; -use kitsune_core::{config::Configuration, job::KitsuneContextRepo}; +use kitsune_config::Configuration; +use kitsune_core::job::KitsuneContextRepo; use kitsune_db::PgPool; use oauth2::OAuthEndpoint; @@ -34,7 +35,8 @@ use oauth2::OAuthEndpoint; use { self::oidc::{async_client, OidcService}, futures_util::future::OptionFuture, - kitsune_core::{config::OidcConfiguration, service::url::UrlService}, + kitsune_config::OidcConfiguration, + kitsune_core::service::url::UrlService, openidconnect::{ core::{CoreClient, CoreProviderMetadata}, ClientId, ClientSecret, IssuerUrl, RedirectUrl, diff --git a/kitsune/src/main.rs b/kitsune/src/main.rs index f58d32625..f37002f41 100644 --- a/kitsune/src/main.rs +++ b/kitsune/src/main.rs @@ -5,17 +5,14 @@ use clap::Parser; use color_eyre::{config::HookBuilder, Help}; use eyre::Context; use kitsune::consts::STARTUP_FIGLET; -use kitsune_core::{config::Configuration, consts::VERSION}; +use kitsune_config::Configuration; +use kitsune_core::consts::VERSION; use std::{ borrow::Cow, env, future, panic::{self, PanicInfo}, path::PathBuf, }; -use tracing::level_filters::LevelFilter; -use tracing_error::ErrorLayer; -use tracing_opentelemetry::OpenTelemetryLayer; -use tracing_subscriber::{filter::Targets, layer::SubscriberExt, Layer, Registry}; use url::Url; #[global_allocator] @@ -86,7 +83,7 @@ async fn boot() -> eyre::Result<()> { let args = Args::parse(); let config = Configuration::load(args.config).await?; - initialise_logging(&config)?; + kitsune_observability::initialise(env!("CARGO_PKG_NAME"), &config)?; let conn = kitsune_db::connect( &config.database.url,