Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(billing): count dual-write observations in the billable units ledger (#18954)

Steffen Reuther1432e566a7ae
diff --git a/packages/shared/clickhouse/scripts/dev-tables.sh b/packages/shared/clickhouse/scripts/dev-tables.sh
index c2344d270b..eeb02eca2d 100644
--- a/packages/shared/clickhouse/scripts/dev-tables.sh
+++ b/packages/shared/clickhouse/scripts/dev-tables.sh
@@ -148,31 +148,32 @@ ORDER BY (project_id, toStartOfFifteenMinutes(ingested_at), event_type, event_id
-- of historic rows that keep their created_at (v4 backfills, updates) are not
-- new usage. Rows with is_deleted = 1 are tombstones, not usage.
--- MV: events_full -> billable_events (observations). Dual-write trace spans
--- (span_id 't-<trace_id>') only count as a trace. Span ids are only unique
--- within their trace, and ingestion API clients choose them, so event_id is
--- '<length(trace_id)>:<trace_id><span_id>'. The length prefix keeps distinct
--- (trace_id, span_id) pairs from concatenating to the same id.
+-- MV: events_full -> billable_events (observations). A trace span that an
+-- internal writer derived from a trace only
+-- counts as a trace. Span ids are only unique within their trace, and ingestion API
+-- clients choose them, so event_id is '<length(trace_id)>:<trace_id>:<span_id>'.
+-- The length prefix keeps distinct (trace_id, span_id) pairs from
+-- concatenating to the same id.
CREATE MATERIALIZED VIEW IF NOT EXISTS billable_events_observations_mv
TO billable_events AS
SELECT
now() AS ingested_at,
project_id,
'observation' AS event_type,
- concat(toString(length(trace_id)), ':', trace_id, span_id) AS event_id,
+ concat(toString(length(trace_id)), ':', trace_id, ':', span_id) AS event_id,
start_time AS event_time,
event_bytes
FROM events_full
WHERE is_deleted = 0
- AND (
- span_id != concat('t-', trace_id)
- AND source NOT LIKE '%-dual-write'
+ AND NOT (
+ span_id = concat('t-', trace_id)
+ AND (source LIKE '%-dual-write%' OR source LIKE '%-backfill')
)
AND created_at >= now() - INTERVAL 48 HOUR;
-- MV: events_full -> billable_events (traces), one row per trace per insert
--- block. event_bytes carries the dual-write trace span, which has no
--- observation row, so event_bytes over all rows adds up to events_full.
+-- block. event_bytes carries the derived trace span, which has no observation
+-- row, so event_bytes over all rows adds up to events_full.
CREATE MATERIALIZED VIEW IF NOT EXISTS billable_events_traces_mv
TO billable_events AS
SELECT
@@ -181,7 +182,11 @@ SELECT
'trace' AS event_type,
trace_id AS event_id,
min(start_time) AS event_time,
- sumIf(event_bytes, span_id = concat('t-', trace_id)) AS event_bytes
+ sumIf(
+ event_bytes,
+ span_id = concat('t-', trace_id)
+ AND (source LIKE '%-dual-write%' OR source LIKE '%-backfill')
+ ) AS event_bytes
FROM events_full
WHERE is_deleted = 0
AND trace_id != ''
diff --git a/worker/src/__tests__/billableEvents.test.ts b/worker/src/__tests__/billableEvents.test.ts
index f94950e7ee..138d267ff4 100644
--- a/worker/src/__tests__/billableEvents.test.ts
+++ b/worker/src/__tests__/billableEvents.test.ts
@@ -30,7 +30,7 @@ const THREE_DAYS_AGO = Date.now() - 72 * HOUR;
/** Ledger id of an observation: span ids are only unique within a trace. */
const observationEventId = (traceId: string, spanId: string) =>
- `${traceId.length}:${traceId}${spanId}`;
+ `${traceId.length}:${traceId}:${spanId}`;
const getBillableRows = (projectId: string) =>
queryClickhouse<{
@@ -91,6 +91,8 @@ describe("billable_events materialized views", () => {
await skipUnlessBillableEvents(ctx);
const projectId = randomUUID();
const traceId = randomUUID();
+ // Sorts right after traceId, which keeps the expected row order fixed.
+ const experimentTraceId = `${traceId}-experiment`;
const backfilledTraceId = randomUUID();
await createEventsCh([
@@ -111,8 +113,34 @@ describe("billable_events materialized views", () => {
project_id: projectId,
trace_id: traceId,
span_id: `t-${traceId}`,
+ source: "ingestion-api-dual-write",
event_bytes: 7,
}),
+ // The experiment backfill derives trace spans the same way.
+ createEvent({
+ project_id: projectId,
+ trace_id: experimentTraceId,
+ span_id: `t-${experimentTraceId}`,
+ source: "ingestion-api-dual-write-experiments",
+ event_bytes: 5,
+ }),
+ // Event propagation writes real observations with a dual-write source.
+ createEvent({
+ project_id: projectId,
+ trace_id: traceId,
+ span_id: "span-dual-write",
+ source: "ingestion-api-dual-write",
+ event_bytes: 30,
+ }),
+ // A client may choose a 't-' span id itself; outside dual-write it is
+ // an observation.
+ createEvent({
+ project_id: projectId,
+ trace_id: traceId,
+ span_id: `t-${traceId}`,
+ source: "otel",
+ event_bytes: 40,
+ }),
// Tombstone: not usage.
createEvent({
project_id: projectId,
@@ -169,9 +197,20 @@ describe("billable_events materialized views", () => {
id: observationEventId(traceId, "span-2"),
bytes: 20,
},
+ {
+ type: "observation",
+ id: observationEventId(traceId, "span-dual-write"),
+ bytes: 30,
+ },
+ {
+ type: "observation",
+ id: observationEventId(traceId, `t-${traceId}`),
+ bytes: 40,
+ },
{ type: "score", id: "score-correction", bytes: expect.any(Number) },
{ type: "score", id: "score-numeric", bytes: expect.any(Number) },
{ type: "trace", id: traceId, bytes: 7 },
+ { type: "trace", id: experimentTraceId, bytes: 5 },
]);
});