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

feat: add policy initialization error metric #713

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
24 changes: 17 additions & 7 deletions src/api/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ pub(crate) fn evaluate(
match evaluation_environment.validate(policy_id, validate_request) {
Ok(validation_response) => validation_response,
Err(EvaluationError::PolicyInitialization(error)) => {
let policy_initialization_error_metric = metrics::PolicyInitializationError {
policy_name: policy_id.to_string(),
initialization_error: error.to_string(),
};

metrics::add_policy_evaluation(&policy_initialization_error_metric);

return Ok(AdmissionResponse::reject(
validate_request.uid().to_owned(),
error.to_string(),
500,
))
));
}

Err(error) => return Err(error),
Expand Down Expand Up @@ -97,7 +104,7 @@ pub(crate) fn evaluate(
};
}
}
let policy_evaluation = metrics::PolicyEvaluation {
let policy_evaluation_metric = metrics::PolicyEvaluation {
policy_name,
policy_mode: policy_mode.into(),
resource_namespace: adm_req.clone().namespace,
Expand All @@ -108,21 +115,24 @@ pub(crate) fn evaluate(
request_origin: request_origin.to_string(),
error_code,
};
metrics::record_policy_latency(policy_evaluation_duration, &policy_evaluation);
metrics::add_policy_evaluation(&policy_evaluation);
metrics::record_policy_latency(policy_evaluation_duration, &policy_evaluation_metric);
metrics::add_policy_evaluation(&policy_evaluation_metric);
}
ValidateRequest::Raw(_) => {
let accepted = vanilla_validation_response.allowed;
let mutated = vanilla_validation_response.patch.is_some();
let policy_evaluation = metrics::RawPolicyEvaluation {
let raw_policy_evaluation_metric = metrics::RawPolicyEvaluation {
policy_name,
policy_mode: policy_mode.into(),
accepted,
mutated,
error_code,
};
metrics::record_policy_latency(policy_evaluation_duration, &policy_evaluation);
metrics::add_policy_evaluation(&policy_evaluation);
metrics::record_policy_latency(
policy_evaluation_duration,
&raw_policy_evaluation_metric,
);
metrics::add_policy_evaluation(&raw_policy_evaluation_metric);
}
};
Ok(validation_response)
Expand Down
18 changes: 18 additions & 0 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,21 @@ impl Into<Vec<KeyValue>> for &RawPolicyEvaluation {
baggage
}
}

#[derive(Clone)]
pub(crate) struct PolicyInitializationError {
pub(crate) policy_name: String,
pub(crate) initialization_error: String,
}

impl PolicyEvaluationMetric for &PolicyInitializationError {}

#[allow(clippy::from_over_into)]
impl Into<Vec<KeyValue>> for &PolicyInitializationError {
fn into(self) -> Vec<KeyValue> {
vec![
KeyValue::new("policy_name", self.policy_name.clone()),
KeyValue::new("initialization_error", self.initialization_error.clone()),
]
}
}
Loading