Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Oct 21, 2023
1 parent 9a6dc9b commit a62e0b3
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 37 deletions.
7 changes: 5 additions & 2 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions crates/kitsune-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -179,6 +185,7 @@ pub struct Configuration {
pub instance: InstanceConfiguration,
pub job_queue: JobQueueConfiguration,
pub messaging: MessagingConfiguration,
pub opentelemetry: Option<OpenTelemetryConfiguration>,
pub server: ServerConfiguration,
pub search: SearchConfiguration,
pub storage: StorageConfiguration,
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-core/src/activitypub/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-core/src/resolve/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ mod test {
use super::PostResolver;
use crate::{
activitypub::Fetcher,
config::FederationFilterConfiguration,
job::KitsuneContextRepo,
service::{
account::AccountService, attachment::AttachmentService,
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion crates/kitsune-core/src/service/federation_filter.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 1 addition & 0 deletions crates/kitsune-observability/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
72 changes: 53 additions & 19 deletions crates/kitsune-observability/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<S, M>(config: &Configuration, meter_provider: M) -> impl Layer<S>
fn initialise_logging<T>(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"))
Expand All @@ -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(())
}
2 changes: 2 additions & 0 deletions kitsune-job-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion kitsune-job-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
5 changes: 4 additions & 1 deletion kitsune-job-runner/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions kitsune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ 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" }
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" }
Expand Down Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion kitsune/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions kitsune/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ 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;

#[cfg(feature = "oidc")]
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,
Expand Down
9 changes: 3 additions & 6 deletions kitsune/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a62e0b3

Please sign in to comment.