Skip to content

Commit

Permalink
refactor: adjust log for discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Dec 17, 2024
1 parent c602154 commit 5ffd70a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 10 deletions.
21 changes: 17 additions & 4 deletions src/acme/lets_encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use super::{get_token_path, Certificate, Error, Result};
use super::{get_token_path, Certificate, Error, Result, LOG_CATEGORY};
use crate::config::{
get_config_storage, get_current_config, load_config, save_config,
LoadConfigOptions, PingapConf, CATEGORY_CERTIFICATE,
Expand Down Expand Up @@ -104,7 +104,11 @@ impl ServiceTask for LetsEncryptService {
}
match update_certificate_lets_encrypt(&self.name, domains).await {
Ok(conf) => {
info!(domains = domains.join(","), "renew certificate success");
info!(
category = LOG_CATEGORY,
domains = domains.join(","),
"renew certificate success"
);
webhook::send(webhook::SendNotificationParams {
category: webhook::NotificationCategory::LetsEncrypt,
msg: "Generate new cert from lets encrypt".to_string(),
Expand Down Expand Up @@ -160,13 +164,17 @@ pub async fn handle_lets_encrypt(
let value =
storage.load(&get_token_path(token)).await.map_err(|e| {
error!(
category = LOG_CATEGORY,
token,
err = e.to_string(),
"let't encrypt http-01 fail"
);
util::new_internal_error(500, e.to_string())
})?;
info!(token, "let't encrypt http-01 success");
info!(
category = LOG_CATEGORY,
token, "let't encrypt http-01 success"
);
HttpResponse {
status: StatusCode::OK,
body: value.into(),
Expand All @@ -188,7 +196,11 @@ async fn new_lets_encrypt(
let mut domains: Vec<String> = domains.to_vec();
// sort domain for comparing later
domains.sort();
info!(domains = domains.join(","), "acme from let's encrypt");
info!(
category = LOG_CATEGORY,
domains = domains.join(","),
"acme from let's encrypt"
);
let url = if production {
LetsEncrypt::Production.url()
} else {
Expand Down Expand Up @@ -248,6 +260,7 @@ async fn new_lets_encrypt(

for authz in &authorizations {
info!(
category = LOG_CATEGORY,
status = format!("{:?}", authz.status),
"acme from let's encrypt"
);
Expand Down
1 change: 1 addition & 0 deletions src/acme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::util;
use serde::{Deserialize, Serialize};
use snafu::Snafu;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
pub static LOG_CATEGORY: &str = "acme";

#[derive(Debug, Snafu)]
pub enum Error {
Expand Down
4 changes: 2 additions & 2 deletions src/acme/validity_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use super::Certificate;
use super::{Certificate, LOG_CATEGORY};
use crate::proxy::get_certificate_info_list;
use crate::service::{CommonServiceTask, ServiceTask};
use crate::util;
Expand Down Expand Up @@ -65,7 +65,7 @@ impl ServiceTask for ValidityChecker {
validity_check(&certificate_info_list, self.time_offset)
{
// certificate will be expired
warn!(message);
warn!(category = LOG_CATEGORY, message);
webhook::send(webhook::SendNotificationParams {
level: webhook::NotificationLevel::Warn,
category: webhook::NotificationCategory::TlsValidity,
Expand Down
16 changes: 15 additions & 1 deletion src/discovery/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use super::COMMON_DISCOVERY;
use super::{format_addrs, Error, Result};
use super::{COMMON_DISCOVERY, LOG_CATEGORY};
use http::Extensions;
use pingora::lb::discovery;
use pingora::lb::{Backend, Backends};
use pingora::protocols::l4::socket::SocketAddr;
use std::collections::BTreeSet;
use std::net::ToSocketAddrs;
use std::time::SystemTime;
use tracing::info;

pub fn is_static_discovery(value: &str) -> bool {
value.is_empty() || value == COMMON_DISCOVERY
Expand All @@ -32,9 +34,12 @@ pub fn new_common_discover_backends(
tls: bool,
ipv4_only: bool,
) -> Result<Backends> {
let hosts = addrs.join(",");
let now = SystemTime::now();
let mut upstreams = BTreeSet::new();
let mut backends = vec![];
let addrs = format_addrs(addrs, tls);
let mut new_addrs = vec![];
for (ip, port, weight) in addrs.iter() {
let addr = format!("{ip}:{port}");
// resolve to socket addr
Expand All @@ -45,6 +50,7 @@ pub fn new_common_discover_backends(
if ipv4_only && !item.is_ipv4() {
continue;
}
new_addrs.push(item.to_string());
let backend = Backend {
addr: SocketAddr::Inet(item),
weight: weight.to_owned(),
Expand All @@ -53,6 +59,14 @@ pub fn new_common_discover_backends(
backends.push(backend)
}
}
info!(
category = LOG_CATEGORY,
hosts,
addrs = new_addrs.join(","),
elapsed =
format!("{}ms", now.elapsed().unwrap_or_default().as_millis()),
"common discover success"
);
upstreams.extend(backends);
let discovery = discovery::Static::new(upstreams);
let backends = Backends::new(discovery);
Expand Down
4 changes: 3 additions & 1 deletion src/discovery/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use super::DNS_DISCOVERY;
use super::{format_addrs, Addr, Error, Result};
use super::{DNS_DISCOVERY, LOG_CATEGORY};
use crate::webhook;
use async_trait::async_trait;
use hickory_resolver::config::{
Expand Down Expand Up @@ -131,6 +131,7 @@ impl ServiceDiscovery for Dns {
data.0.iter().map(|item| item.addr.to_string()).collect();

info!(
category = LOG_CATEGORY,
hosts = hosts.join(","),
addrs = addrs.join(","),
elapsed = format!(
Expand All @@ -143,6 +144,7 @@ impl ServiceDiscovery for Dns {
},
Err(e) => {
error!(
category = LOG_CATEGORY,
error = e.to_string(),
hosts = hosts.join(","),
elapsed = format!(
Expand Down
4 changes: 3 additions & 1 deletion src/discovery/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use super::DOCKER_DISCOVERY;
use super::{Error, Result};
use super::{DOCKER_DISCOVERY, LOG_CATEGORY};
use crate::webhook;
use async_trait::async_trait;
use bollard::container::ListContainersOptions;
Expand Down Expand Up @@ -196,6 +196,7 @@ impl ServiceDiscovery for Docker {
data.0.iter().map(|item| item.addr.to_string()).collect();

info!(
category = LOG_CATEGORY,
names = names.join(","),
addrs = addrs.join(","),
elapsed = format!(
Expand All @@ -208,6 +209,7 @@ impl ServiceDiscovery for Docker {
},
Err(e) => {
error!(
category = LOG_CATEGORY,
error = e.to_string(),
names = names.join(","),
elapsed = format!(
Expand Down
2 changes: 2 additions & 0 deletions src/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use hickory_resolver::error::ResolveError;
use snafu::Snafu;

pub static LOG_CATEGORY: &str = "discovery";

#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("Io error {source}, {content}"))]
Expand Down
9 changes: 8 additions & 1 deletion src/health/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,19 @@ fn new_http_health_check(
// 忽略append header fail
if let Err(e) = req.append_header("Host", &conf.host) {
error!(
category = "health",
error = e.to_string(),
host = conf.host,
"http health check append host fail"
);
}
check.req = req;
},
Err(e) => error!(error = e.to_string(), "http health check fail"),
Err(e) => error!(
category = "health",
error = e.to_string(),
"http health check fail"
),
}

check
Expand All @@ -115,6 +120,7 @@ pub fn new_health_check(
check.peer_template.options.connection_timeout =
Some(Duration::from_secs(3));
info!(
category = "health",
name,
options = format!("{:?}", check.peer_template.options),
"new health check"
Expand All @@ -124,6 +130,7 @@ pub fn new_health_check(
let health_check_conf: HealthCheckConf = health_check.try_into()?;
health_check_frequency = health_check_conf.check_frequency;
info!(
category = "health",
name,
schema = health_check_conf.schema,
health_check_conf = format!("{health_check_conf:?}"),
Expand Down

0 comments on commit 5ffd70a

Please sign in to comment.