lemmy/src/prometheus_metrics.rs
Nutomic ad90cd77f9
Implement private communities (#5076)
* add private visibility

* filter private communities in post_view.rs

* also filter in comment_view

* community follower state

* remove unused method

* sql fmt

* add CommunityFollower.approved_by

* implement api endpoints

* api changes

* only admins can create private community for now

* add local api tests

* fix api tests

* follow remote private community

* use authorized fetch for content in private community

* federate community visibility

* dont mark content in private community as public

* expose ApprovalRequired in api

* also check content fetchable for outbox/featured

* address private community content to followers

* implement reject activity

* fix tests

* add files

* remove local api tests

* dont use delay

* is_new_instance

* single query for is_new_instance

* return subscribed type for pending follow

* working

* need to catch errors in waitUntil

* clippy

* fix query

* lint for unused async

* diesel.toml comment

* add comment

* avoid db reads

* rename approved_by to approver_id

* add helper

* form init

* list pending follows should return items for all communities

* clippy

* ci

* fix down migration

* fix api tests

* references

* rename

* run git diff

* ci

* fix schema check

* fix joins

* ci

* ci

* skip_serializing_none

* fix test

---------

Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
2024-11-07 05:49:05 -05:00

99 lines
2.9 KiB
Rust

use actix_web::{rt::System, web, App, HttpServer};
use lemmy_api_common::context::LemmyContext;
use lemmy_utils::{error::LemmyResult, settings::structs::PrometheusConfig};
use prometheus::{default_registry, Encoder, Gauge, Opts, TextEncoder};
use std::{sync::Arc, thread};
use tracing::error;
struct PromContext {
lemmy: LemmyContext,
db_pool_metrics: DbPoolMetrics,
}
struct DbPoolMetrics {
max_size: Gauge,
size: Gauge,
available: Gauge,
}
pub fn serve_prometheus(config: PrometheusConfig, lemmy_context: LemmyContext) -> LemmyResult<()> {
let context = Arc::new(PromContext {
lemmy: lemmy_context,
db_pool_metrics: create_db_pool_metrics()?,
});
// spawn thread that blocks on handling requests
// only mapping /metrics to a handler
thread::spawn(move || {
let sys = System::new();
sys.block_on(async {
let server = HttpServer::new(move || {
App::new()
.app_data(web::Data::new(Arc::clone(&context)))
.route("/metrics", web::get().to(metrics))
})
.bind((config.bind, config.port as u16))
.unwrap_or_else(|e| panic!("Cannot bind to {}:{}: {e}", config.bind, config.port))
.run();
if let Err(err) = server.await {
error!("Prometheus server error: {err}");
}
})
});
Ok(())
}
// handler for the /metrics path
async fn metrics(context: web::Data<Arc<PromContext>>) -> LemmyResult<String> {
// collect metrics
collect_db_pool_metrics(&context);
let mut buffer = Vec::new();
let encoder = TextEncoder::new();
// gather metrics from registry and encode in prometheus format
let metric_families = prometheus::gather();
encoder.encode(&metric_families, &mut buffer)?;
let output = String::from_utf8(buffer)?;
Ok(output)
}
// create lemmy_db_pool_* metrics and register them with the default registry
fn create_db_pool_metrics() -> LemmyResult<DbPoolMetrics> {
let metrics = DbPoolMetrics {
max_size: Gauge::with_opts(Opts::new(
"lemmy_db_pool_max_connections",
"Maximum number of connections in the pool",
))?,
size: Gauge::with_opts(Opts::new(
"lemmy_db_pool_connections",
"Current number of connections in the pool",
))?,
available: Gauge::with_opts(Opts::new(
"lemmy_db_pool_available_connections",
"Number of available connections in the pool",
))?,
};
default_registry().register(Box::new(metrics.max_size.clone()))?;
default_registry().register(Box::new(metrics.size.clone()))?;
default_registry().register(Box::new(metrics.available.clone()))?;
Ok(metrics)
}
fn collect_db_pool_metrics(context: &PromContext) {
let pool_status = context.lemmy.inner_pool().status();
context
.db_pool_metrics
.max_size
.set(pool_status.max_size as f64);
context.db_pool_metrics.size.set(pool_status.size as f64);
context
.db_pool_metrics
.available
.set(pool_status.available as f64);
}