Sending contextual events
Contextual events are events-in-events. Think of them as specific group of events that chained together. You can use this to visualized cron jobs, complex functions, workflows, etc.
Here’s a simple example:
// first event
let e = {
name: "cron job started",
avatar: "🤖",
contextId : "1_cron_job", // contextId needs to be common for all contextual events
contextStart : true // context start is only true for the first contextual event
};
await ops.events.ingest(e);
// later on
e = {
name: "processing",
contextId : "1_cron_job",
content : "343543 job failed"
};
await ops.events.ingest(e);
// some time later
e = {
name: "processing",
contextId : "1_cron_job",
contextId : "183542 job passed"
};
await ops.events.ingest(e);
CopyTo start with contextual events, you need to make contextStart : true
for the first event in the context and supply a contextId that is common for all contextual events in the chain.
Note that you only need to mark the first event in the chain with contextStart:true.
What is the contextId?
Imagine contextual events as part of a chain. To join events together in this chain, you need to have a common id. contextId is exactly that.
However, you need a id that is also sufficiently unique for a chain of events that happen over various time periods and in seperate parts of your codebase.
For instance, if you’re tracking user signup flow, you might want to have "user_signup_" + userId
as the contextId. This will generate a context of user_signup events for that specific user only.
Here’s an example.
const e = {
name : 'Cron job started',
contextId : 'cronjob_1',
contextStart : true,
avatar: "🤖",
}
CopyThen further down your code base, add this in:
const e = {
name : 'Cron job: processing job',
contextId : 'cronjob_1',
}
CopyAnd finally:
const e = {
name : 'Cron job: processing finished',
contextId : 'cronjob_1',
type : "rows",
content : [
{
label : 'Successful jobs',
content : 10
},
{
label : 'Failed jobs',
content : 3
}
]
}
CopyPutting it all together, these events will look like this: