Skip to content

Commit

Permalink
refactor: adjust performance metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Jan 7, 2025
1 parent 83632a5 commit bbd1faf
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 32 deletions.
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ use pingora::server::configuration::Opt;
use pingora::services::background::background_service;
use proxy::{new_upstream_health_check_task, Server, ServerConf};
use service::new_simple_service_task;
use service::{new_auto_restart_service, new_observer_service};
use state::{
get_admin_addr, get_start_time, new_performance_metrics_log_service,
set_admin_addr,
use service::{
new_auto_restart_service, new_observer_service,
new_performance_metrics_log_service,
};
use state::{get_admin_addr, get_start_time, set_admin_addr};
use std::collections::HashMap;
use std::error::Error;
use std::ffi::OsString;
Expand All @@ -51,6 +51,7 @@ mod health;
mod http_extra;
mod limit;
mod logger;

#[cfg(feature = "full")]
mod otel;
mod plugin;
Expand Down
8 changes: 8 additions & 0 deletions src/proxy/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,14 @@ pub fn get_location(name: &str) -> Option<Arc<Location>> {
LOCATION_MAP.load().get(name).cloned()
}

pub fn get_locations_processing() -> HashMap<String, i32> {
let mut processing = HashMap::new();
LOCATION_MAP.load().iter().for_each(|(k, v)| {
processing.insert(k.to_string(), v.processing.load(Ordering::Relaxed));
});
processing
}

pub fn try_init_locations(
confs: &HashMap<String, LocationConf>,
) -> Result<Vec<String>> {
Expand Down
5 changes: 3 additions & 2 deletions src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ pub use location::Location;
pub use dynamic_certificate::{
get_certificate_info_list, try_update_certificates,
};
pub use location::try_init_locations;
pub use location::{get_locations_processing, try_init_locations};
pub use logger::Parser;
pub use server::*;
pub use server_conf::ServerConf;
pub use upstream::{
new_upstream_health_check_task, try_init_upstreams, try_update_upstreams,
get_upstreams_processing, new_upstream_health_check_task,
try_init_upstreams, try_update_upstreams,
};
8 changes: 8 additions & 0 deletions src/proxy/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,14 @@ pub fn get_upstream(name: &str) -> Option<Arc<Upstream>> {
UPSTREAM_MAP.load().get(name).cloned()
}

pub fn get_upstreams_processing() -> HashMap<String, i32> {
let mut processing = HashMap::new();
UPSTREAM_MAP.load().iter().for_each(|(k, v)| {
processing.insert(k.to_string(), v.processing.load(Ordering::Relaxed));
});
processing
}

fn new_ahash_upstreams(
confs: &HashMap<String, UpstreamConf>,
) -> Result<(Upstreams, Vec<String>)> {
Expand Down
2 changes: 2 additions & 0 deletions src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,7 @@ impl BackgroundService for CommonServiceTask {
}

mod auto_restart;
mod performance_metrics;

pub use auto_restart::{new_auto_restart_service, new_observer_service};
pub use performance_metrics::new_performance_metrics_log_service;
57 changes: 57 additions & 0 deletions src/service/performance_metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2025 Tree xie.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::proxy::{get_locations_processing, get_upstreams_processing};
use crate::service::SimpleServiceTaskFuture;
use crate::state::{get_process_system_info, get_processing_accepted};
use tracing::info;

static PERFORMANCE_METRICS_LOG_SERVICE: &str = "performanceMetricsLog";

pub fn new_performance_metrics_log_service() -> (String, SimpleServiceTaskFuture)
{
let task: SimpleServiceTaskFuture = Box::new(move |_count: u32| {
Box::pin({
async move {
let locations_processing = get_locations_processing()
.iter()
.map(|(name, count)| format!("{name}:{count}"))
.collect::<Vec<String>>()
.join(",");
let upstreams_processing = get_upstreams_processing()
.iter()
.map(|(name, count)| format!("{name}:{count}"))
.collect::<Vec<String>>()
.join(",");
let system_info = get_process_system_info();
let (processing, accepted) = get_processing_accepted();
info!(
category = PERFORMANCE_METRICS_LOG_SERVICE,
threads = system_info.threads,
locations_processing,
upstreams_processing,
accepted,
processing,
used_memory = system_info.memory,
fd_count = system_info.fd_count,
tcp_count = system_info.tcp_count,
tcp6_count = system_info.tcp6_count,
"performance metrics"
);
Ok(true)
}
})
});
(PERFORMANCE_METRICS_LOG_SERVICE.to_string(), task)
}
26 changes: 0 additions & 26 deletions src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::service::SimpleServiceTaskFuture;
#[cfg(feature = "full")]
use snafu::Snafu;
use tracing::info;

mod ctx;
mod process;
Expand All @@ -39,27 +37,3 @@ pub enum Error {
}
#[cfg(feature = "full")]
pub type Result<T, E = Error> = std::result::Result<T, E>;

pub fn new_performance_metrics_log_service() -> (String, SimpleServiceTaskFuture)
{
let task: SimpleServiceTaskFuture = Box::new(move |_count: u32| {
Box::pin({
async move {
let system_info = get_process_system_info();
let (processing, accepted) = get_processing_accepted();
info!(
threads = system_info.threads,
accepted,
processing,
used_memory = system_info.memory,
fd_count = system_info.fd_count,
tcp_count = system_info.tcp_count,
tcp6_count = system_info.tcp6_count,
"performance metrics"
);
Ok(true)
}
})
});
("performanceMetricsLog".to_string(), task)
}

0 comments on commit bbd1faf

Please sign in to comment.