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

A meta endpoint serving hostname and other basic info #68

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dotenvy = "0.15.6"
engine = { path = "../engine" }
env_logger = { workspace = true }
futures = "0.3.28"
gethostname = "0.4.2"
http = "0.2.8"
hyper = "0.14.23"
log = "0.4.17"
Expand Down
16 changes: 16 additions & 0 deletions agent/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use anyhow::Result;
use axum::extract::State;
use gethostname::gethostname;
use axum::http::{Request, StatusCode};
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use http::header::HeaderValue;
use serde::Serialize;

use crate::structures::AppState;

Expand Down Expand Up @@ -52,3 +54,17 @@ pub async fn monitor_status(_state: State<AppState>) -> impl IntoResponse {
metrics::increment_counter!("monitor:status");
StatusCode::NOT_IMPLEMENTED
}

#[derive(Serialize)]
pub struct AgentMetadata {
hostname: String,
}

/// Provide agent metadata.
pub async fn meta() -> String {
metrics::increment_counter!("meta");
let meta = AgentMetadata {
hostname: gethostname().into_string().expect("Failed to get hostname"),
};
serde_json::to_string(&meta).expect("Failed to serialize agent metadata")
jkbecker marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 2 additions & 1 deletion agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ fn init_router(state: &Arc<RunnerState>) -> Router {

let mut router: Router<Arc<RunnerState>, Body> = Router::new()
.route("/monitor/ping", get(ping))
.route("/monitor/status", get(monitor_status));
.route("/monitor/status", get(monitor_status))
.route("/meta", get(meta));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we stick this under something like /meta/hostinfo? I want the /meta/ namespace to be open to future expansion.

Copy link
Contributor

@ceejbot ceejbot May 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/status is the name I chose because it should include a lot more than host info. Here's an example response blob that contains some of the information I consider essential for lightweight runtime inspection of a process.

{
   "git": "b2277c7",
    "hostname": "unobtanium.local",
    "node": "name-of-agent",
    "stats": {
        "requestCount": 0,
        "statuses": {}
    },
    "uptime": 22.108189125
}

Plus whatever else is cheap to extract from a running process: peer counts? etc. Uptime is particularly useful to me. PID if we have it. (these two together can signal crash loops.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Getting the git hash involves a little build-time Rust fun that will show you how all that works.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx, great input! Will add the very easy stats details to my PR and create separate issues for those that are more complex.

router = v1::mesh::mount(router);

// NOTE: We have two of these now. If we develop a third, generalize this pattern.
Expand Down