Skip to main content

Module events

Module events 

Source
Expand description

Structured events (Rails 8.1’s Rails.event): named facts about what the app did, with a payload, for analytics, audit trails or a data warehouse.

Every Ctx has an Events notifier, Ctx::events. An event is logged at once (an info line event <name> <payload> carrying the request’s log fields, so Workers Logs can query it) and handed to each Subscriber the app registered with subscribe.

use axum::extract::State;
use ocre::{Ctx, Result};
use serde_json::json;

async fn checkout(State(ctx): State<Ctx>) -> Result<&'static str> {
    ctx.events().set_context("tenant", "acme"); // on every event of this request
    ctx.events().notify("order.placed", json!({ "order_id": 42, "total": "19.99" }));
    // Tags group events of one part of the code (Rails' `Rails.event.tagged`).
    ctx.events().tagged("step", "payment").notify("payment.captured", json!({ "order_id": 42 }));
    Ok("OK")
}

§Subscribers

Register them once per Worker instance, in the start event of src/lib.rs. A subscriber turns an Event into the HTTP request to send (Delivery); Ocre sends it with fetch after the handler, like error reports.

§Free plan

Logging costs nothing. Each delivery is one subrequest (50 per request on the free plan): batch on the receiving side, or keep events in the logs only.

Re-exports§

pub use crate::errors::Delivery;

Structs§

Event
A structured event, from Events::notify.
Events
The event notifier of one request, job batch or cron run: Ctx::events.

Traits§

Subscriber
A destination for events (Rails’ event subscribers): turns an Event into the request to send.

Functions§

subscribe
Registers a subscriber for every event of this Worker instance (Rails’ Rails.event.subscribe).