use classy::extract::context::ConfigureContext;
use classy::extract::{Extract, FromContext};
use classy::{MetricType, MetricsHost};
use pdk_core::log::debug;
use pdk_core::policy_context::api::Metadata;
use std::convert::Infallible;
use std::rc::Rc;
pub trait Readiness {
fn ready(&self);
}
pub struct ReadinessInstance {
metrics: Rc<dyn MetricsHost>,
name: String,
}
impl Readiness for ReadinessInstance {
fn ready(&self) {
debug!("creating metric {}", self.name);
let id = self.metrics.define_metric(MetricType::Gauge, &self.name);
self.metrics.record_metric(id, 1);
}
}
impl FromContext<ConfigureContext> for ReadinessInstance {
type Error = Infallible;
fn from_context(context: &ConfigureContext) -> Result<Self, Self::Error> {
let metadata: Metadata = context.extract()?;
let metrics: Rc<dyn MetricsHost> = context.extract()?;
let name = format!(
"{}/{}/ready",
metadata.policy_metadata.policy_namespace, metadata.policy_metadata.policy_name
);
Ok(ReadinessInstance { metrics, name })
}
}