SLO Library

@contractspec/lib.slo keeps service level objectives front and center—declarative definitions, rolling snapshots, burn-rate math, and automated incidents.

Installation

bun add @contractspec/lib.slo

Define Targets

const definition: SLODefinition = {
  id: 'billing.createInvoice.availability',
  targetAvailability: 0.999,
  latencyP99TargetMs: 500,
  rollingWindowMs: 7 * 24 * 60 * 60 * 1000,
  alerts: { fastBurnThreshold: 14, slowBurnThreshold: 6 },
};

Monitor Burn Rate

const monitor = new SLOMonitor({ definition, incidentManager });
const { snapshot, burnRate } = monitor.recordWindow({
  good: 12500,
  bad: 3,
  latencyP99: 420,
  latencyP95: 210,
});

When burn rate exceeds the configured thresholds the monitor calls yourIncidentManager, providing the snapshot that triggered the alert.

History & Reporting

`SLOTracker.getHistory()` returns the latest snapshots so dashboards can show trends without hitting a warehouse. Prisma models persist everything for long-term audits.