Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add outbound index metrics to the policy controller #12429

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion policy-controller/k8s/index/src/outbound.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod index;

pub use index::{Index, ServiceRef, SharedIndex};
pub use index::{metrics, Index, ServiceRef, SharedIndex};
2 changes: 2 additions & 0 deletions policy-controller/k8s/index/src/outbound/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct Index {
service_info: HashMap<ServiceRef, ServiceInfo>,
}

pub mod metrics;

pub type SharedIndex = Arc<RwLock<Index>>;

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
Expand Down
67 changes: 67 additions & 0 deletions policy-controller/k8s/index/src/outbound/index/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use prometheus_client::{
collector::Collector,
encoding::{DescriptorEncoder, EncodeMetric},
metrics::{gauge::ConstGauge, MetricType},
registry::Registry,
};

use super::SharedIndex;

#[derive(Debug)]
struct Instrumented(SharedIndex);

pub fn register(reg: &mut Registry, index: SharedIndex) {
reg.register_collector(Box::new(Instrumented(index)));
}

impl Collector for Instrumented {
fn encode(&self, mut encoder: DescriptorEncoder<'_>) -> Result<(), std::fmt::Error> {
let this = self.0.read();

let service_encoder = encoder.encode_descriptor(
"service_index_size",
"The number of entires in service index",
None,
MetricType::Gauge,
)?;
let services = ConstGauge::new(this.services_by_ip.len() as u32);
services.encode(service_encoder)?;

let service_info_encoder = encoder.encode_descriptor(
"service_info_index_size",
"The number of entires in the service info index",
None,
MetricType::Gauge,
)?;
let service_infos = ConstGauge::new(this.service_info.len() as u32);
service_infos.encode(service_info_encoder)?;

let mut service_route_encoder = encoder.encode_descriptor(
"service_route_index_size",
"The number of entires in the service route index",
None,
MetricType::Gauge,
)?;
for (ns, index) in &this.namespaces.by_ns {
let labels = vec![("namespace", ns.as_str())];
let service_routes = ConstGauge::new(index.service_routes.len() as u32);
let service_route_encoder = service_route_encoder.encode_family(&labels)?;
service_routes.encode(service_route_encoder)?;
}

let mut service_port_route_encoder = encoder.encode_descriptor(
"service_port_route_index_size",
"The number of entires in the service port route index",
None,
MetricType::Gauge,
)?;
for (ns, index) in &this.namespaces.by_ns {
let labels = vec![("namespace", ns.as_str())];
let service_port_routes = ConstGauge::new(index.service_port_routes.len() as u32);
let service_port_route_encoder = service_port_route_encoder.encode_family(&labels)?;
service_port_routes.encode(service_port_route_encoder)?;
}

Ok(())
}
}
4 changes: 4 additions & 0 deletions policy-controller/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ async fn main() -> Result<()> {
let status_metrics = status::ControllerMetrics::register(resource_status);
let status_index_metrcs = status::IndexMetrics::register(resource_status);

outbound::metrics::register(
prom.sub_registry_with_prefix("outbound_index"),
outbound_index.clone(),
);
inbound::metrics::register(
prom.sub_registry_with_prefix("inbound_index"),
inbound_index.clone(),
Expand Down