Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Records

A record groups related fields into a named, reusable type. Event attributes can use a record name wherever they can use a scalar type.

YAML model

quent: alpha
model: records

records:
  # Reused by events on both Task and Batch.
  WorkResult:
    fields:
      success: bool
      items_processed: u64

entities:
  Task:
    events:
      started: {}
      ended:
        attributes:
          result: WorkResult
  Batch:
    events:
      started: {}
      ended:
        attributes:
          result: WorkResult

Instrumentation API

The generated API represents WorkResult as a target-language record type. Both Task and Batch accept that type when emitting ended.

use instrumentation::{Batch, Context, Noop, Records, Task, WorkResult};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let context = Context::<Records>::try_new(Noop)?;
    let mut task = context.observer::<Task>().handle();
    let mut batch = context.observer::<Batch>().handle();

    task.started()?;
    task.ended(WorkResult {
        success: true,
        items_processed: 128,
    })?;

    batch.started()?;
    batch.ended(WorkResult {
        success: true,
        items_processed: 512,
    })?;

    Ok(())
}
#include "quent-tutorial-records-cpp-bridge/gen/quent.hpp"

int main() {
  auto context = quent::Context::none();
  auto task = context.task_observer()->handle();
  auto batch = context.batch_observer()->handle();

  task.started();
  task.ended(quent::task::Ended{
      .result = quent::records::WorkResult{
          .success = true,
          .items_processed = 128,
      },
  });

  batch.started();
  batch.ended(quent::batch::Ended{
      .result = quent::records::WorkResult{
          .success = true,
          .items_processed = 512,
      },
  });
  return 0;
}
import quent_tutorial_records as quent


def main() -> None:
    with quent.Context() as context:
        task = context.task_observer().handle()
        batch = context.batch_observer().handle()

        task.started()
        task.ended(result={"success": True, "items_processed": 128})

        batch.started()
        batch.ended(result={"success": True, "items_processed": 512})


if __name__ == "__main__":
    main()
Key point

A named record provides one reusable structure for event attributes.

Check yourself

What type do the Task and Batch ended events expect for result?

Why declare a record instead of repeating its fields?

Full code