Write results to Kafka

This guide sends each value a hook computes to a Kafka topic, through a logger and a log collector, and says what the topic then receives.

No sink writes to Kafka in this version: a hook writes the stores serving reads. A logger on the statement the hook runs writes each call as a JSON line, and a log collector forwards the lines to the topic.

Before you start, have the hook running, and a log collector next to featuremeshd that reads files or a process's output and writes to Kafka: Vector, Fluent Bit or the OpenTelemetry Collector.

1. Put a logger on the hook's statement

Write JSON lines to stdout:// or file:// ( Where to write ):

FeatureQL
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.HOOK_REF AS
PERSIST
    pay_log := LOGGER(
        ON pay_ps
        TO SINK('file:///var/log/featuremeshd/payments' WITH (FORMAT = 'jsonl'))
    )
;
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.HOOK_REF.PAY_LOGCREATEDFeature created as not exists

Then RECOMPILE SERVING. The logger logs every call of the statement, the hook's calls included.

2. Forward the lines to the topic

Point the collector at the logger's files, <dir>/<logger>/<boot_id>-<seq>.jsonl with a new file every hour, or at featuremeshd's standard output, where each line carries "fm_logger" set to the logger's name. Have it write each line, unchanged, to the topic.

3. Read what each line holds

Each line is one call: its call_time is the call's REQUEST_TIME(), the record's Kafka timestamp for an event hook and the drain time for a scheduled one, not the time the line was written, so a replay logs lines dated in the past, and an object store's date=/hour= folders follow that time. Its other fields: the statement's inputs, its output and captures, and call_details , whose path names the hook and source holds partition and offset, the record behind the call, or drain, the drain's number.

A line written by the scoring hook of Scoring each event , for the record at offset 26 (one line in stdout, shown indented):

{
  "fm_logger": "FM.TUTORIALS.HOOK_SCORING.SCORE_LOG",
  "call_id": "019ab661-0d08-7a41-9b26-d5906d3b1d79",
  "call_index": 1,
  "call_time": "2025-11-24T15:00:05",
  "call_details": {
    "status": "OK", "error_code": null, "error_message": null,
    "latency_ms": 13.623208, "row_count": 1,
    "server_version": "featuremeshd 0.2.0", "boot_id": "e9d152b6-c4dd-4656-b368-6b577e215c41",
    "sources": [],
    "timings": {"total_ms": 13.623208, "transpile_ms": null, "plan_ms": 0.0, "compute_ms": 0.298625,
                "sources": [{"source": "sql", "calls": 1, "window_ms": 13.324583, "summed_ms": 13.324583}]},
    "path": "FM.TUTORIALS.HOOK_SCORING.ON_PAYMENT",
    "source": {"partition": 0, "offset": 26, "drain": null}
  },
  "fm.tutorials.hook_scoring.payment_id": 2,
  "fm.tutorials.hook_scoring.merchant_id": 9,
  "fm.tutorials.hook_scoring.amount": 4.0,
  "fm.tutorials.hook_scoring.score_ps": {"payment_id": 2, "score": 4.0, "tier": "low"}
}
json

4. Make the consumers tolerant

What the topic receives differs from what a hook writes. A logger is best effort ( Delivery ):

  • A hook's writes are at least once; a logger's rows are not. A row can be missing while the sink holds the value.
  • A row can be logged twice. A batch computed again (a source that did not answer) or a crash before the commit runs the records again, and logs them again. An event hook's duplicates carry the same path, partition and offset, one record of the hook's topic: deduplicate on them. A drain's do not: its number is shared by every reference it runs and changes when it is retried, so deduplicate a drain's rows on path and the call's inputs, keeping the latest.
  • No order or key on the topic beyond what the collector does: rows are logged as calls complete, and calls of one batch run concurrently.
  • Downstream, treat the topic as a feed of values, not a record of every event: idempotent on those identities, tolerant of a gap. Where every value must reach the topic exactly once, the pipeline that owns the topic runs FeatureQL itself and writes its outputs: embedded serving inside your consumer, or a call of the prepared statement over featuremeshd's HTTP API, POST /v1/featureql, which returns the value and writes no sink:
curl -X POST http://featuremeshd:10090/v1/featureql -H 'content-type: application/json' \
  -d '{"statement": {"id": "FM.SHOP.SCORE_PS", "inputs": {"input_1": [[1001, 7, 42.5]]}}}'    # payment 1001, merchant 7, amount
bash

Check the result

  • SHOW SERVING LOGS LIKE '<logger>' LIMIT 5 shows the last lines the logger kept, without opening the files ( A logged row ).
  • The topic receives one line per call, its call_details.path naming the hook.

Common failures

  • Lines missing on the topic while the sink holds the values: the logger dropped them. featuremeshd.logger.calls_dropped and featuremeshd.logger.rows with outcome dropped_write_failed count them ( Monitor and recover ).
  • Lines dated in the past: a replay logs its calls at the records' times, not at the time they are written.
  • Duplicates downstream: expected under at-least-once. Deduplicate on the identities above.

Related