Skip to content

Commit

Permalink
Add HTTP API request logs, through http_request_log=true configuratio…
Browse files Browse the repository at this point in the history
…n item control
  • Loading branch information
rmqtt committed Jul 22, 2023
1 parent 1eea2fe commit 36f575f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rmqtt-plugins/rmqtt-http-api.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ workers = 1
max_row_limit = 10_000
## HTTP Listener
http_laddr = "0.0.0.0:6060"
## Indicates whether to print HTTP request logs
http_request_log = false


37 changes: 37 additions & 0 deletions rmqtt-plugins/rmqtt-http-api/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::net::SocketAddr;

use salvo::affix;
use salvo::http::header::{HeaderValue, CONTENT_TYPE};
use salvo::http::mime;
use salvo::prelude::*;

use rmqtt::{
Expand Down Expand Up @@ -30,6 +31,7 @@ use super::{clients, plugin, subs};
fn route(cfg: PluginConfigType) -> Router {
Router::with_path("api/v1")
.hoop(affix::inject(cfg))
.hoop(api_logger)
.get(list_apis)
.push(Router::with_path("brokers").get(get_brokers).push(Router::with_path("<id>").get(get_brokers)))
.push(Router::with_path("nodes").get(get_nodes).push(Router::with_path("<id>").get(get_nodes)))
Expand Down Expand Up @@ -257,6 +259,41 @@ async fn list_apis(res: &mut Response) {
res.render(Json(data));
}

#[handler]
async fn api_logger(req: &mut Request, depot: &mut Depot) {
if let Some(cfg) = depot.obtain::<PluginConfigType>() {
if !cfg.read().http_request_log {
return;
}
}

let log_data = format!(
"Request {}, {:?}, {}, {}",
req.remote_addr().map(|addr| addr.to_string()).unwrap_or_else(|| "[Unknown]".into()),
req.version(),
req.method(),
req.uri()
);
let txt_body = if let Some(m) = req.content_type() {
if let mime::PLAIN | mime::JSON | mime::TEXT = m.subtype() {
if let Ok(body) = req.payload().await {
Some(String::from_utf8_lossy(body))
} else {
None
}
} else {
None
}
} else {
None
};
if let Some(txt_body) = txt_body {
log::info!("{}, body: {}", log_data, txt_body);
} else {
log::info!("{}", log_data);
}
}

#[handler]
async fn get_brokers(req: &mut Request, depot: &mut Depot, res: &mut Response) {
let cfg = depot.obtain::<PluginConfigType>().cloned().unwrap();
Expand Down
8 changes: 8 additions & 0 deletions rmqtt-plugins/rmqtt-http-api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub struct PluginConfig {

#[serde(default = "PluginConfig::message_type_default")]
pub message_type: MessageType,

#[serde(default = "PluginConfig::http_request_log_default")]
pub http_request_log: bool,
}

impl PluginConfig {
Expand All @@ -50,6 +53,10 @@ impl PluginConfig {
99
}

fn http_request_log_default() -> bool {
false
}

#[inline]
pub fn to_json(&self) -> Result<serde_json::Value> {
Ok(serde_json::to_value(self)?)
Expand All @@ -61,6 +68,7 @@ impl PluginConfig {
|| self.max_row_limit != other.max_row_limit
|| self.http_laddr != other.http_laddr
|| self.metrics_sample_interval != other.metrics_sample_interval
|| self.http_request_log != other.http_request_log
}

#[inline]
Expand Down

0 comments on commit 36f575f

Please sign in to comment.