refactor: extract telemetry init to telemetry.rs
This commit is contained in:
@@ -2,17 +2,14 @@
|
|||||||
//!
|
//!
|
||||||
//! Configures and starts the HTTP server with JWT-based authentication.
|
//! Configures and starts the HTTP server with JWT-based authentication.
|
||||||
|
|
||||||
use std::collections::VecDeque;
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::Arc;
|
||||||
use std::time::Duration as StdDuration;
|
use std::time::Duration as StdDuration;
|
||||||
|
|
||||||
use axum::Router;
|
use axum::Router;
|
||||||
use axum::http::{HeaderName, HeaderValue};
|
use axum::http::{HeaderName, HeaderValue};
|
||||||
use tokio::sync::broadcast;
|
|
||||||
use tower_http::cors::{AllowHeaders, AllowMethods, AllowOrigin, CorsLayer};
|
use tower_http::cors::{AllowHeaders, AllowMethods, AllowOrigin, CorsLayer};
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};
|
|
||||||
|
|
||||||
use domain::{ChannelService, IMediaProvider, IProviderRegistry, ProviderCapabilities, ScheduleEngineService, StreamingProtocol, UserService};
|
use domain::{ChannelService, IMediaProvider, IProviderRegistry, ProviderCapabilities, ScheduleEngineService, StreamingProtocol, UserService};
|
||||||
use infra::factory::{build_activity_log_repository, build_channel_repository, build_provider_config_repository, build_schedule_repository, build_user_repository};
|
use infra::factory::{build_activity_log_repository, build_channel_repository, build_provider_config_repository, build_schedule_repository, build_user_repository};
|
||||||
@@ -32,6 +29,7 @@ mod poller;
|
|||||||
mod routes;
|
mod routes;
|
||||||
mod scheduler;
|
mod scheduler;
|
||||||
mod state;
|
mod state;
|
||||||
|
mod telemetry;
|
||||||
mod webhook;
|
mod webhook;
|
||||||
|
|
||||||
use crate::config::{Config, ConfigSource};
|
use crate::config::{Config, ConfigSource};
|
||||||
@@ -39,16 +37,7 @@ use crate::state::AppState;
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
// Set up broadcast channel + ring buffer for SSE log streaming.
|
let handles = telemetry::init_tracing();
|
||||||
let (log_tx, _) = broadcast::channel::<log_layer::LogLine>(512);
|
|
||||||
let log_history = Arc::new(Mutex::new(VecDeque::<log_layer::LogLine>::new()));
|
|
||||||
|
|
||||||
// Initialize tracing with our custom layer in addition to the fmt layer.
|
|
||||||
tracing_subscriber::registry()
|
|
||||||
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")))
|
|
||||||
.with(fmt::layer())
|
|
||||||
.with(log_layer::AppLogLayer::new(log_tx.clone(), Arc::clone(&log_history)))
|
|
||||||
.init();
|
|
||||||
|
|
||||||
let config = Config::from_env();
|
let config = Config::from_env();
|
||||||
|
|
||||||
@@ -246,8 +235,8 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
provider_config_repo,
|
provider_config_repo,
|
||||||
config.clone(),
|
config.clone(),
|
||||||
event_tx.clone(),
|
event_tx.clone(),
|
||||||
log_tx,
|
handles.log_tx,
|
||||||
log_history,
|
handles.log_history,
|
||||||
activity_log_repo,
|
activity_log_repo,
|
||||||
db_pool,
|
db_pool,
|
||||||
#[cfg(feature = "local-files")]
|
#[cfg(feature = "local-files")]
|
||||||
|
|||||||
25
k-tv-backend/api/src/telemetry.rs
Normal file
25
k-tv-backend/api/src/telemetry.rs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
use std::collections::VecDeque;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
use tokio::sync::broadcast;
|
||||||
|
use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
|
||||||
|
use crate::log_layer::{AppLogLayer, LogLine};
|
||||||
|
|
||||||
|
pub struct LoggingHandles {
|
||||||
|
pub log_tx: broadcast::Sender<LogLine>,
|
||||||
|
pub log_history: Arc<Mutex<VecDeque<LogLine>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init_tracing() -> LoggingHandles {
|
||||||
|
let (log_tx, _) = broadcast::channel::<LogLine>(512);
|
||||||
|
let log_history = Arc::new(Mutex::new(VecDeque::<LogLine>::new()));
|
||||||
|
|
||||||
|
tracing_subscriber::registry()
|
||||||
|
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")))
|
||||||
|
.with(fmt::layer())
|
||||||
|
.with(AppLogLayer::new(log_tx.clone(), Arc::clone(&log_history)))
|
||||||
|
.init();
|
||||||
|
|
||||||
|
LoggingHandles { log_tx, log_history }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user