swarm-controller: move the observability concern into its own module
Refs #6897
This commit is contained in:
parent
9b2ba275d8
commit
cd8feedf73
11 changed files with 32 additions and 27 deletions
|
|
@ -1085,7 +1085,7 @@ in
|
|||
# separated: a matrix user id cannot contain a space. Consumed by
|
||||
# swarm-controller/src/accounts/matrix_account/operator_admin.rs.
|
||||
SWARM_CONTROLLER_MATRIX_ADMIN_SYNC_IGNORE_USERS = lib.concatStringsSep " " deployCfg.swarm-controller.matrixAdminSync.ignoreUsers;
|
||||
# Where `GET /api/alerts` reads alerts (swarm-controller/src/alerts.rs).
|
||||
# Where `GET /api/alerts` reads alerts (swarm-controller/src/observability/alerts.rs).
|
||||
# Every swarm has one Grafana, reached by name wherever it runs. The
|
||||
# token is in the store, written by `swarm-bao-grafana-controller-token`
|
||||
# on Grafana's host.
|
||||
|
|
|
|||
|
|
@ -1844,7 +1844,7 @@ in
|
|||
# the renewal has been stuck for days. `sender` and `agent`
|
||||
# stay in the grouping so the alert names the holder.
|
||||
# Agents whose deploy the build gate holds back, from the gauge
|
||||
# swarm-controller exports (`held_deploys.rs`): a build that
|
||||
# swarm-controller exports (`observability/held_deploys.rs`): a build that
|
||||
# fails, or that keeps getting no answer from the builder. A deploy
|
||||
# whose facts copy to its hive fails is held too.
|
||||
# Thirty minutes covers a builder restart and the controller's
|
||||
|
|
|
|||
|
|
@ -457,7 +457,7 @@ let
|
|||
e: map lib.head (lib.filter lib.isList (builtins.split ''"(hyperhive\.vcs\.[a-z_.]+)"'' e))
|
||||
) exprs
|
||||
);
|
||||
recordedVcs = builtins.readFile ../../swarm-controller/src/vcs_metrics.rs;
|
||||
recordedVcs = builtins.readFile ../../swarm-controller/src/observability/vcs_metrics.rs;
|
||||
in
|
||||
lib.elem "forge.json" shipped
|
||||
&& lib.elem "bao.json" shipped
|
||||
|
|
@ -1143,17 +1143,17 @@ let
|
|||
}
|
||||
{
|
||||
# The held-deploys alert fires on any agent held for 30 minutes and
|
||||
# selects the gauge name `held_deploys.rs` spells in Rust. The disk rule
|
||||
# selects the gauge name `observability/held_deploys.rs` spells in Rust. The disk rule
|
||||
# is the presence control: it goes through the same lookup.
|
||||
# 🔬 Invert-proof: change `for = "30m"` or the selector on
|
||||
# `hh-deploys-held` in swarm-grafana.nix, or `METRIC` in held_deploys.rs.
|
||||
# `hh-deploys-held` in swarm-grafana.nix, or `METRIC` in observability/held_deploys.rs.
|
||||
name = "the held-deploys alert fires after 30 minutes on the gauge swarm-controller exports";
|
||||
ok =
|
||||
let
|
||||
groups =
|
||||
grafanaHere.containers.swarm-grafana.config.services.grafana.provision.alerting.rules.settings.groups;
|
||||
rules = lib.concatMap (g: g.rules) groups;
|
||||
source = builtins.readFile ../../swarm-controller/src/held_deploys.rs;
|
||||
source = builtins.readFile ../../swarm-controller/src/observability/held_deploys.rs;
|
||||
rule = uid: lib.findFirst (r: r.uid == uid) null rules;
|
||||
held = rule "hh-deploys-held";
|
||||
threshold =
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ dashboards_dir="nix/host-modules/swarm-grafana/dashboards"
|
|||
known='bao|nix/host-modules/swarm-bao.nix|collectorServiceName = "bao"
|
||||
authelia|nix/host-modules/swarm-authelia.nix|serviceName = "authelia"
|
||||
forgejo|nix/host-modules/hive-forge/default.nix|serviceName = "forgejo"
|
||||
swarm-controller|swarm-controller/src/vcs_metrics.rs|with_service_name("swarm-controller")
|
||||
swarm-controller|swarm-controller/src/observability/vcs_metrics.rs|with_service_name("swarm-controller")
|
||||
otelcol-contrib|nix/host-modules/otel.nix|package = pkgs.opentelemetry-collector-contrib
|
||||
grafana|nix/host-modules/swarm-otel-service.nix|grafana = "https://${hyperhiveCfg.swarm.grafana.domain}/metrics"'
|
||||
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ hive-meta-flake.workspace = true
|
|||
# (e.g. hive-c0re, for its own per-hive job graph) doesn't have to depend on
|
||||
# this whole binary to reuse it.
|
||||
hive-jobq-metrics.workspace = true
|
||||
# Direct OTEL SDK use in `vcs_metrics.rs` — sync counters recorded off
|
||||
# Direct OTEL SDK use in `observability/vcs_metrics.rs` — sync counters recorded off
|
||||
# webhook deliveries, a different shape from `hive-jobq-metrics`'s
|
||||
# observable-gauge rollup, so it isn't a fit for that crate's API and lives
|
||||
# here instead. Same three crates that pairing already pulls in transitively,
|
||||
|
|
@ -135,7 +135,7 @@ swarm-wire = { workspace = true, features = ["schema"] }
|
|||
# The appservice calls `matrix_account::agent_token` mints agents' accounts
|
||||
# with — shared with `swarm-matrix-ctl`, which pins the same device id.
|
||||
swarm-matrix-client.workspace = true
|
||||
# `otel_http_client.rs`'s `AuthenticatedHttpClient` — an
|
||||
# `observability/otel_http_client.rs`'s `AuthenticatedHttpClient` — an
|
||||
# `opentelemetry_http::HttpClient` impl authenticated with this crate's own
|
||||
# `swarm-queue-client` identity. Lives in this crate rather than
|
||||
# `swarm-queue-client` (mara's own call during review) since this daemon is
|
||||
|
|
|
|||
|
|
@ -45,7 +45,6 @@ mod agent_status;
|
|||
mod agent_todos;
|
||||
mod agents_md;
|
||||
mod agents_md_initial;
|
||||
mod alerts;
|
||||
mod archetypes;
|
||||
mod archive_access;
|
||||
mod attic_copy;
|
||||
|
|
@ -63,7 +62,6 @@ mod egress_allow;
|
|||
mod egress_deny;
|
||||
mod forge;
|
||||
mod forge_events;
|
||||
mod held_deploys;
|
||||
mod inference;
|
||||
mod issue_report;
|
||||
mod jobq;
|
||||
|
|
@ -71,8 +69,8 @@ mod message_stream;
|
|||
mod meta_build;
|
||||
mod meta_fanout;
|
||||
mod meta_flake;
|
||||
mod observability;
|
||||
mod observability_grant;
|
||||
mod otel_http_client;
|
||||
mod pause_hold;
|
||||
mod pr_conflicts;
|
||||
mod pr_report;
|
||||
|
|
@ -92,15 +90,15 @@ mod tcp_tls;
|
|||
mod term_stream;
|
||||
#[cfg(test)]
|
||||
mod test_nats;
|
||||
mod vcs_metrics;
|
||||
mod wanted;
|
||||
mod webhook;
|
||||
|
||||
// The accounts and review modules keep their crate-root paths (`crate::matrix_account::…`).
|
||||
// The accounts, observability and review modules keep their crate-root paths (`crate::matrix_account::…`).
|
||||
use accounts::{
|
||||
forge_account, github_account, gitlab_account, link_egress, linked_accounts, matrix_account,
|
||||
};
|
||||
use jobq::{Late, WorkerDeps, spawn_jobq_worker};
|
||||
use observability::{alerts, held_deploys, otel_http_client, vcs_metrics};
|
||||
use review::{notify_flows, notify_router_api, request_reviewer, review_classifier};
|
||||
use routes::AppState;
|
||||
use startup::{
|
||||
|
|
|
|||
7
swarm-controller/src/observability/mod.rs
Normal file
7
swarm-controller/src/observability/mod.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
//! The observability concern: the Grafana alerts route, the held-deploy
|
||||
//! gauge, the OTLP HTTP client and the forge metrics helpers.
|
||||
|
||||
pub(crate) mod alerts;
|
||||
pub(crate) mod held_deploys;
|
||||
pub(crate) mod otel_http_client;
|
||||
pub(crate) mod vcs_metrics;
|
||||
|
|
@ -42,30 +42,30 @@ pub(crate) const EXPORT_INTERVAL: Duration = Duration::from_mins(1);
|
|||
/// this module needs, not a full mirror of Forgejo's push-event schema.
|
||||
/// Kept separate from the wire parsing so a test can construct one without
|
||||
/// a JSON literal.
|
||||
pub(super) struct PushActivity {
|
||||
pub(crate) struct PushActivity {
|
||||
/// The pushed ref, e.g. `refs/heads/main`.
|
||||
pub(super) git_ref: String,
|
||||
pub(crate) git_ref: String,
|
||||
/// `repository.default_branch`; empty when the payload names none, in
|
||||
/// which case no push counts as a default-branch push.
|
||||
pub(super) default_branch: String,
|
||||
pub(crate) default_branch: String,
|
||||
/// The ref's tip before and after the push. All zeros means the ref did
|
||||
/// not exist before (`before`) or no longer exists (`after`).
|
||||
pub(super) before: String,
|
||||
pub(super) after: String,
|
||||
pub(crate) before: String,
|
||||
pub(crate) after: String,
|
||||
/// `repository.full_name` (`org/repo`) — the attribute every series
|
||||
/// this module emits carries, so a dashboard can break activity down
|
||||
/// per repo without a second join.
|
||||
pub(super) repo: String,
|
||||
pub(crate) repo: String,
|
||||
/// `commits.len()` from the payload. A push with zero commits is real
|
||||
/// (a branch delete, a force-push moving the ref backward) and still
|
||||
/// counted as a push — just with a commit delta of zero, not skipped.
|
||||
pub(super) commit_count: u64,
|
||||
pub(crate) commit_count: u64,
|
||||
}
|
||||
|
||||
/// Parse a Forgejo `push` webhook body into a [`PushActivity`]. `None` on
|
||||
/// anything unparseable — a malformed body is logged and dropped by the
|
||||
/// caller, not a reason to fail the delivery Forgejo already got a 200 for.
|
||||
pub(super) fn parse_push(body: &[u8]) -> Option<PushActivity> {
|
||||
pub(crate) fn parse_push(body: &[u8]) -> Option<PushActivity> {
|
||||
let v: serde_json::Value = serde_json::from_slice(body).ok()?;
|
||||
let repo = v.get("repository")?.get("full_name")?.as_str()?.to_owned();
|
||||
let commit_count = v.get("commits")?.as_array()?.len();
|
||||
|
|
@ -99,7 +99,7 @@ static INSTRUMENTS: OnceLock<Option<Instruments>> = OnceLock::new();
|
|||
/// (`OTEL_EXPORTER_OTLP_ENDPOINT` unset) — the same graceful-absence shape
|
||||
/// every other exporter in this tree uses, so a deployment with no
|
||||
/// collector pays nothing beyond the one-time `OnceLock` check.
|
||||
pub(super) fn record_push(activity: &PushActivity) {
|
||||
pub(crate) fn record_push(activity: &PushActivity) {
|
||||
let Some(inst) = INSTRUMENTS.get_or_init(build).as_ref() else {
|
||||
return;
|
||||
};
|
||||
|
|
@ -112,10 +112,10 @@ pub(super) fn record_push(activity: &PushActivity) {
|
|||
|
||||
/// What one default-branch push adds to the counters.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub(super) struct DefaultBranchTally {
|
||||
pub(crate) struct DefaultBranchTally {
|
||||
/// `None` when the lines are unknown: the push created the branch, or
|
||||
/// the compare failed. Nothing is added to the line counters then.
|
||||
pub(super) lines: Option<LineStats>,
|
||||
pub(crate) lines: Option<LineStats>,
|
||||
}
|
||||
|
||||
fn is_null_sha(sha: &str) -> bool {
|
||||
|
|
@ -134,7 +134,7 @@ fn is_null_sha(sha: &str) -> bool {
|
|||
/// `after` and not from `before`, with their stats. The commits the push
|
||||
/// discarded are not subtracted, so a rewrite of already-counted history
|
||||
/// counts those lines a second time.
|
||||
pub(super) async fn default_branch_tally<F, Fut>(
|
||||
pub(crate) async fn default_branch_tally<F, Fut>(
|
||||
activity: &PushActivity,
|
||||
compare: F,
|
||||
) -> Option<DefaultBranchTally>
|
||||
|
|
@ -164,7 +164,7 @@ where
|
|||
|
||||
/// Count `activity` on the default-branch counters, comparing through
|
||||
/// `forge`. Runs after the webhook has answered; nothing here can fail it.
|
||||
pub(super) async fn count_default_branch_push(
|
||||
pub(crate) async fn count_default_branch_push(
|
||||
activity: PushActivity,
|
||||
forge: Option<Arc<crate::forge::Client>>,
|
||||
) {
|
||||
Loading…
Reference in a new issue