Openstatus www.openstatus.dev
at main 99 lines 2.2 kB view raw view rendered
1## Motivation 2 3We want to track every change made for the `incident` and `monitor`. Therefore, 4it requires us to build some audit log / event sourcing foundation. 5 6The `Event` is what the data type stored within [Tinybird](https://tinybird.co). 7It has basic props that every event includes, as well as a `metadata` prop that 8can be used to store additional informations. 9 10```ts 11export type Event = { 12 /** 13 * Unique identifier for the event. 14 */ 15 id: string; 16 17 /** 18 * The actor that triggered the event. 19 * @default { id: "", name: "system" } 20 * @example { id: "1", name: "mxkaske" } 21 */ 22 actor?: { 23 id: string; 24 name: string; 25 }; 26 27 /** 28 * The ressources affected by the action taken. 29 * @example [{ id: "1", name: "monitor" }] 30 */ 31 targets?: { 32 id: string; 33 name: string; 34 }[]; 35 36 /** 37 * The action that was triggered. 38 * @example monitor.down | incident.create 39 */ 40 action: string; 41 42 /** 43 * The timestamp of the event in milliseconds since epoch UTC. 44 * @default Date.now() 45 */ 46 timestamp?: number; 47 48 /** 49 * The version of the event. Should be incremented on each update. 50 * @default 1 51 */ 52 version?: number; 53 54 /** 55 * Metadata for the event. Defined via zod schema. 56 */ 57 metadata?: unknown; 58}; 59``` 60 61The objects are parsed and stored as string via 62`schema.transform(val => JSON.stringify(val))` and transformed back into an 63object before parsing via `z.preprocess(val => JSON.parse(val), schema)`. 64 65## Example 66 67```ts 68const tb = new Tinybird({ token: process.env.TINY_BIRD_API_KEY || "" }); 69 70const auditLog = new AuditLog({ tb }); 71 72await auditLog.publishAuditLog({ 73 id: "monitor:1", 74 action: "monitor.down", 75 targets: [{ id: "1", type: "monitor" }], // not mandatory, but could be useful later on 76 metadata: { region: "gru", statusCode: 400, message: "timeout" }, 77}); 78 79await auditLog.getAuditLog({ event_id: "monitor:1" }); 80``` 81 82## Inspiration 83 84- WorkOS [Audit Logs](https://workos.com/docs/audit-logs) 85 86## Tinybird 87 88Push the pipe and datasource to tinybird: 89 90``` 91tb push datasources/audit_log.datasource 92tb push pipes/endpoint_audit_log.pipe 93``` 94 95--- 96 97### Possible extention 98 99> TODO: Remove `Nullable` from `targets` to better index and query it.