Skip to content

Commit

Permalink
[NO-ISSUE] fix/adapt logging tracing healthcheck (#39)
Browse files Browse the repository at this point in the history
* refactor: change url of healthcheck

* refactor(tracing): add global error handler and set healthcheck as DEBUG

* fix: url in JS client
  • Loading branch information
GuillaumeDecMeetsMore authored Aug 30, 2024
1 parent f95161e commit a7c00c9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion scheduler/clients/javascript/lib/healthClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NettuBaseClient } from './baseClient'

export class NettuHealthClient extends NettuBaseClient {
public async checkStatus(): Promise<number> {
const res = await this.get<void>('/')
const res = await this.get<void>('/healthcheck')
return res.status
}
}
4 changes: 3 additions & 1 deletion scheduler/clients/rust/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ impl StatusClient {
}

pub async fn check_health(&self) -> APIResponse<get_service_health::APIResponse> {
self.base.get("".into(), None, StatusCode::OK).await
self.base
.get("healthcheck".into(), None, StatusCode::OK)
.await
}
}
25 changes: 25 additions & 0 deletions scheduler/crates/api/src/http_logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use actix_web::{
body::MessageBody,
dev::{ServiceRequest, ServiceResponse},
Error,
};
use tracing::{Level, Span};
use tracing_actix_web::{DefaultRootSpanBuilder, RootSpanBuilder};

pub struct NitteiTracingRootSpanBuilder;

impl RootSpanBuilder for NitteiTracingRootSpanBuilder {
fn on_request_start(request: &ServiceRequest) -> Span {
// Ignore healthcheck endpoint
let level = if request.path() == "/api/v1/healthcheck" {
Level::DEBUG
} else {
Level::INFO
};
tracing_actix_web::root_span!(level = level, request)
}

fn on_request_end<B: MessageBody>(span: Span, outcome: &Result<ServiceResponse<B>, Error>) {
DefaultRootSpanBuilder::on_request_end(span, outcome);
}
}
4 changes: 3 additions & 1 deletion scheduler/crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod account;
mod calendar;
mod error;
mod event;
mod http_logger;
mod job_schedulers;
mod schedule;
mod service;
Expand All @@ -13,6 +14,7 @@ use std::net::TcpListener;

use actix_cors::Cors;
use actix_web::{dev::Server, middleware, web, web::Data, App, HttpServer};
use http_logger::NitteiTracingRootSpanBuilder;
use job_schedulers::{start_reminder_generation_job_scheduler, start_send_reminders_job};
use nettu_scheduler_domain::{
Account,
Expand Down Expand Up @@ -94,7 +96,7 @@ impl Application {
App::new()
.wrap(Cors::permissive())
.wrap(middleware::Compress::default())
.wrap(TracingLogger::default())
.wrap(TracingLogger::<NitteiTracingRootSpanBuilder>::new())
.app_data(Data::new(ctx))
.service(web::scope("/api/v1").configure(configure_server_api))
})
Expand Down
4 changes: 2 additions & 2 deletions scheduler/crates/api/src/status/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use nettu_scheduler_api_structs::get_service_health::*;

async fn status() -> HttpResponse {
HttpResponse::Ok().json(APIResponse {
message: "Yo! We are up!\r\n".into(),
message: "Ok!\r\n".into(),
})
}

pub fn configure_routes(cfg: &mut web::ServiceConfig) {
cfg.route("/", web::get().to(status));
cfg.route("/healthcheck", web::get().to(status));
}
11 changes: 8 additions & 3 deletions scheduler/src/telemetry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use opentelemetry::global;
use opentelemetry::global::{self, set_error_handler};
use opentelemetry_sdk::propagation::TraceContextPropagator;
use tracing::warn;
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};

Expand All @@ -10,8 +11,6 @@ pub fn init_subscriber() {
// Filter the spans that are shown based on the RUST_LOG env var or the default value ("info")
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));

// TODO: add the `env` on all logs

// If the binary is compiled in debug mode (aka for development)
// use the compact format for logs
if cfg!(debug_assertions) {
Expand Down Expand Up @@ -65,5 +64,11 @@ pub fn init_subscriber() {
// Set the global subscriber
tracing::subscriber::set_global_default(subscriber)
.expect("Unable to set global subscriber");

// Set a global error handler to log the tracing internal errors to the console
set_error_handler(|e| {
warn!("Error when exporting traces: {}", e);
})
.expect("Failed to set global error handler");
}
}

0 comments on commit a7c00c9

Please sign in to comment.