Something went wrong. Try again.
Ephemeral DigitalOcean droplets for Forgejo Actions and Radicle CI, from one daemon
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! How long each part of a droplet's life takes, for Prometheus.//!//! A droplet's record holds a timestamp for each step: when its work was//! first seen waiting, when it was asked for, when DigitalOcean called it//! active, when its kernel started, when its agent first called and was//! given its work, when the work began and ended, and when the droplet was//! destroyed. When one is destroyed, the time between each pair of steps goes//! into a histogram for its tier and source, so that where the time goes --//! DigitalOcean's side, the guest's boot, the runner -- can be read off a//! graph, and the effect of changing any of it seen.//!//! The scheduler observes and the metrics listener reads, from different//! tasks, so every count is atomic. A restart starts them from zero, which//! Prometheus reads as a counter reset.
const std = @import("std");const Allocator = std.mem.Allocator;const Io = std.Io;
const RunStore = @import("RunStore.zig");
const Metrics = @This();
tiers: []const []const u8,/// Each forge's name, then `radicle`.sources: []const []const u8,/// One per phase, tier and source, in that order of nesting.histograms: []Histogram,
pub const Phase = enum { /// From the work being seen waiting to the droplet being asked for: /// polling, and waiting for room in the tier. queue, /// From asking DigitalOcean for the droplet to the first pass that sees /// it active. active, /// From asking for the droplet to its kernel starting: DigitalOcean's /// side of the wait. provision, /// From the kernel starting to the agent's first call: the guest's boot /// and cloud-init. boot, /// From the agent's first call to its being given its work. claim, /// From being given the work to its starting: fetching the job, or the /// runner taking it. start, /// The work itself. work, /// From the agent saying it is done to the droplet being destroyed. reap, /// From the work being seen waiting to its droplet claiming it: the /// whole wait as the job sees it, and the one a warm droplet shortens. wait,};
/// Seconds. Boots are tens of seconds and jobs are minutes, so the buckets/// are fine at the bottom and coarse at the top.pub const buckets = [_]i64{ 1, 2, 5, 10, 15, 20, 30, 45, 60, 90, 120, 180, 300, 600, 1200, 3600 };
const Histogram = struct { /// Not cumulative here; summed on the way out. buckets: [buckets.len + 1]std.atomic.Value(u64), count: std.atomic.Value(u64), sum: std.atomic.Value(u64),
const zero: Histogram = .{ .buckets = @splat(.init(0)), .count = .init(0), .sum = .init(0), };
fn observe(h: *Histogram, seconds: u64) void { const bucket = for (buckets, 0..) |bound, i| { if (seconds <= bound) break i; } else buckets.len; _ = h.buckets[bucket].fetchAdd(1, .monotonic); _ = h.count.fetchAdd(1, .monotonic); _ = h.sum.fetchAdd(seconds, .monotonic); }};
pub fn init(arena: Allocator, tiers: []const []const u8, sources: []const []const u8) !Metrics { const histograms = try arena.alloc(Histogram, std.enums.values(Phase).len * tiers.len * sources.len); @memset(histograms, .zero); return .{ .tiers = tiers, .sources = sources, .histograms = histograms };}
fn index(list: []const []const u8, name: []const u8) ?usize { for (list, 0..) |item, i| if (std.mem.eql(u8, item, name)) return i; return null;}
fn histogram(m: *Metrics, phase: Phase, tier: usize, source: usize) *Histogram { return &m.histograms[(@intFromEnum(phase) * m.tiers.len + tier) * m.sources.len + source];}
pub fn observe(m: *Metrics, phase: Phase, tier: []const u8, source: []const u8, seconds: i64) void { const t = index(m.tiers, tier) orelse return; const s = index(m.sources, source) orelse return; // A clock that stepped backwards is not a negative duration. m.histogram(phase, t, s).observe(@intCast(@max(seconds, 0)));}
/// Every phase of a droplet that has ended, as far as its record reaches:/// one that never booted has no boot to measure.pub fn observeDroplet(m: *Metrics, d: RunStore.Droplet, destroyed_at: i64) void { const source = if (d.binding) |b| switch (b) { .forgejo => |f| f.forge, .radicle => "radicle", } else "pool"; const spans = [_]struct { Phase, ?i64, ?i64 }{ .{ .queue, d.queued_at, d.created_at }, .{ .active, d.created_at, d.active_at }, .{ .provision, d.created_at, d.booted_at }, .{ .boot, d.booted_at, d.agent_at }, .{ .claim, d.agent_at, d.claimed_at }, .{ .start, d.claimed_at, d.work_started_at }, .{ .work, d.work_started_at, d.done_at }, .{ .reap, d.done_at, destroyed_at }, .{ .wait, d.queued_at, d.claimed_at }, }; for (spans) |span| { const from = span[1] orelse continue; const to = span[2] orelse continue; m.observe(span[0], d.tier, source, to - from); }}
/// The Prometheus text exposition: the histograms, and how many droplets/// stand in each tier and state right now.pub fn write(m: *Metrics, w: *Io.Writer, counts: []const RunStore.DropletCount) Io.Writer.Error!void { try w.writeAll( \\# HELP ci_autoscaler_droplet_phase_seconds How long each part of a droplet's life took, observed when it is destroyed. \\# TYPE ci_autoscaler_droplet_phase_seconds histogram \\ ); for (std.enums.values(Phase)) |phase| for (m.tiers, 0..) |tier, t| for (m.sources, 0..) |source, s| { const h = m.histogram(phase, t, s); const labels = .{ @tagName(phase), tier, source }; var cumulative: u64 = 0; for (buckets, 0..) |bound, i| { cumulative += h.buckets[i].load(.monotonic); try w.print("ci_autoscaler_droplet_phase_seconds_bucket{{phase=\"{s}\",tier=\"{s}\",source=\"{s}\",le=\"{d}\"}} {d}\n", labels ++ .{ bound, cumulative }); } cumulative += h.buckets[buckets.len].load(.monotonic); try w.print("ci_autoscaler_droplet_phase_seconds_bucket{{phase=\"{s}\",tier=\"{s}\",source=\"{s}\",le=\"+Inf\"}} {d}\n", labels ++ .{cumulative}); try w.print("ci_autoscaler_droplet_phase_seconds_sum{{phase=\"{s}\",tier=\"{s}\",source=\"{s}\"}} {d}\n", labels ++ .{h.sum.load(.monotonic)}); try w.print("ci_autoscaler_droplet_phase_seconds_count{{phase=\"{s}\",tier=\"{s}\",source=\"{s}\"}} {d}\n", labels ++ .{h.count.load(.monotonic)}); };
try w.writeAll( \\# HELP ci_autoscaler_droplets Droplets the daemon has a record of, by tier and state. \\# TYPE ci_autoscaler_droplets gauge \\ ); // Every tier and state, so that an empty one reads as a zero rather than // a missing series. for (m.tiers) |tier| for (std.enums.values(RunStore.DropletState)) |state| { if (state == .gone) continue; var n: u64 = 0; for (counts) |c| { if (c.state == state and std.mem.eql(u8, c.tier, tier)) n += c.count; } try w.print("ci_autoscaler_droplets{{tier=\"{s}\",state=\"{s}\"}} {d}\n", .{ tier, @tagName(state), n }); };}
const testing = std.testing;
test "a droplet's phases, and the text Prometheus reads" { var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); var m = try init(arena.allocator(), &.{ "s", "l" }, &.{ "ocjtech", "radicle" });
m.observeDroplet(.{ .id = 1, .do_id = 2, .tier = "l", .size = null, .image = null, .state = .done, .binding = .{ .radicle = 3 }, .exit_code = 0, .queued_at = 100, .created_at = 105, .active_at = 130, .booted_at = 128, .agent_at = 170, .claimed_at = 171, .work_started_at = 172, .done_at = 500, .destroyed_at = null, .heard_at = null, }, 503);
var out: Io.Writer.Allocating = .init(testing.allocator); defer out.deinit(); try m.write(&out.writer, &.{.{ .tier = "l", .state = .claimed, .count = 2 }}); const text = out.written();
// 23 seconds on DigitalOcean's side lands in the 30 bucket and above try testing.expect(std.mem.indexOf(u8, text, "ci_autoscaler_droplet_phase_seconds_bucket{phase=\"provision\",tier=\"l\",source=\"radicle\",le=\"20\"} 0\n") != null); try testing.expect(std.mem.indexOf(u8, text, "ci_autoscaler_droplet_phase_seconds_bucket{phase=\"provision\",tier=\"l\",source=\"radicle\",le=\"30\"} 1\n") != null); try testing.expect(std.mem.indexOf(u8, text, "ci_autoscaler_droplet_phase_seconds_sum{phase=\"boot\",tier=\"l\",source=\"radicle\"} 42\n") != null); try testing.expect(std.mem.indexOf(u8, text, "ci_autoscaler_droplet_phase_seconds_count{phase=\"work\",tier=\"l\",source=\"radicle\"} 1\n") != null); try testing.expect(std.mem.indexOf(u8, text, "ci_autoscaler_droplet_phase_seconds_bucket{phase=\"work\",tier=\"l\",source=\"radicle\",le=\"+Inf\"} 1\n") != null); try testing.expect(std.mem.indexOf(u8, text, "ci_autoscaler_droplet_phase_seconds_count{phase=\"work\",tier=\"s\",source=\"ocjtech\"} 0\n") != null); try testing.expect(std.mem.indexOf(u8, text, "ci_autoscaler_droplets{tier=\"l\",state=\"claimed\"} 2\n") != null); try testing.expect(std.mem.indexOf(u8, text, "ci_autoscaler_droplets{tier=\"s\",state=\"booting\"} 0\n") != null);}
test { std.testing.refAllDecls(@This());}