ops_mongodb/
lib.rs

1use mongodb::Client;
2use ops_core::{async_trait, CheckResponse, Checker};
3
4const ACTION: &str = "Check state of mongo db cluster";
5
6/// Checks a Mongo database is healthy and responding to requests.
7pub struct MongoChecker {
8    client: Client,
9    db: String,
10    impact: String,
11}
12
13impl MongoChecker {
14    /// Creates a new Mongo health checker
15    pub fn new(client: Client, db: &str, impact: &str) -> Self {
16        Self {
17            client,
18            db: db.to_owned(),
19            impact: impact.to_owned(),
20        }
21    }
22}
23
24#[async_trait]
25impl Checker for MongoChecker {
26    async fn check(&self) -> CheckResponse {
27        match self
28            .client
29            .database(&self.db)
30            .run_command(mongodb::bson::doc! { "ping": 1 }, None)
31            .await
32        {
33            Ok(_) => CheckResponse::healthy("Connection to the mongo server is healthy"),
34            Err(err) => CheckResponse::unhealthy(
35                &format!("Cannot get in touch with server: {}", err),
36                ACTION,
37                &self.impact,
38            ),
39        }
40    }
41}