UNPKG

@mastra/core

Version:
374 lines โ€ข 20.4 kB
/** * Storage contract for `aggregateSpans()`: grouped measures over individual current spans. * * Shares the `aggregateTraces()` envelope (window, intervals, `having`, `orderBy`, `limit`, * response rows) and measures. Differs in scope: `where` is the direct span field policy of * `querySpans()` (`SPAN_QUERY_FIELD_REGISTRY`), dimensions are span fields, and usage is * attributed per span rather than per trace. Trace-level questions ("traces containing a failing * tool call") remain `aggregateTraces()` with `spans.some`. */ import { z } from 'zod/v4'; import type { TraceAggregateInterval } from './trace-aggregate.js'; import type { TrustedAggregateMeasure, TrustedTraceAggregateHavingPredicate, TrustedTraceAggregateOrderBy } from './trace-aggregate-planner.js'; import type { TraceAggregateCanonicalMeasure, TraceAggregateDimensionDescriptor, TraceAggregateDimensionRule, TraceAggregateMeasureDescriptor } from './trace-aggregate-registry.js'; import type { TraceQueryPlanOptions, TraceQueryTenantScope, TrustedTraceQueryScalarPredicate } from './trace-query.js'; /** * Span-scope groupable dimensions for `aggregateSpans()`, in discovery order. * * The trace dimensions plus the span's own `name`/`spanType`, its `entityId`, the parent and root * entity context, and `model`/`provider`. `model` and `provider` read only their dedicated columns, * like their filters, so spans exported before the columns were filled group under `null`. Not * groupable: `metadata.<key>` (the span field policy has no metadata) and identity fields * (`traceId`, `spanId`, `parentSpanId`, `runId`, `requestId`). */ export declare const SPAN_AGGREGATE_DIMENSION_REGISTRY: { readonly name: TraceAggregateDimensionRule; readonly spanType: TraceAggregateDimensionRule; readonly entityType: TraceAggregateDimensionRule; readonly entityId: TraceAggregateDimensionRule; readonly entityName: TraceAggregateDimensionRule; readonly parentEntityType: TraceAggregateDimensionRule; readonly parentEntityName: TraceAggregateDimensionRule; readonly rootEntityType: TraceAggregateDimensionRule; readonly rootEntityName: TraceAggregateDimensionRule; readonly environment: TraceAggregateDimensionRule; readonly status: TraceAggregateDimensionRule; readonly outcome: TraceAggregateDimensionRule; readonly stopReason: TraceAggregateDimensionRule; readonly serviceName: TraceAggregateDimensionRule; readonly executionSource: TraceAggregateDimensionRule; readonly threadId: TraceAggregateDimensionRule; readonly resourceId: TraceAggregateDimensionRule; readonly userId: TraceAggregateDimensionRule; readonly sessionId: TraceAggregateDimensionRule; readonly organizationId: TraceAggregateDimensionRule; readonly experimentId: TraceAggregateDimensionRule; readonly model: TraceAggregateDimensionRule; readonly provider: TraceAggregateDimensionRule; }; export type SpanAggregateDimension = keyof typeof SPAN_AGGREGATE_DIMENSION_REGISTRY; export type SpanAggregateCountDistinctField = SpanAggregateDimension | 'traceId'; export declare function isSpanAggregateDimension(path: string): path is SpanAggregateDimension; export declare function isSpanAggregateCountDistinctField(path: string): path is SpanAggregateCountDistinctField; export type ParsedSpanAggregateMeasure = { type: 'canonical'; measure: TraceAggregateCanonicalMeasure; } | { type: 'countDistinct'; field: SpanAggregateCountDistinctField; }; /** Span aggregates use the trace measure registry; `countDistinct` targets are span dimensions or `traceId`. */ export declare function parseSpanAggregateMeasure(name: string): ParsedSpanAggregateMeasure | undefined; export declare function getSpanAggregateDimensionDescriptors(): Array<Omit<TraceAggregateDimensionDescriptor, 'path'> & { path: SpanAggregateDimension; }>; export declare function getSpanAggregateMeasureDescriptors(): TraceAggregateMeasureDescriptor[]; declare const spanAggregateRequestObjectSchema: z.ZodObject<{ timeRange: z.ZodObject<{ from: z.ZodString; to: z.ZodString; }, z.core.$strict>; where: z.ZodOptional<z.ZodType<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate, z.core.$ZodTypeInternals<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate>>>; groupBy: z.ZodDefault<z.ZodArray<z.ZodString>>; interval: z.ZodOptional<z.ZodEnum<{ "15m": "15m"; "1d": "1d"; "1h": "1h"; "1m": "1m"; "5m": "5m"; }>>; measures: z.ZodArray<z.ZodUnion<readonly [z.ZodEnum<{ "cost.avg": "cost.avg"; "cost.sum": "cost.sum"; count: "count"; "duration.avg": "duration.avg"; "duration.max": "duration.max"; "duration.min": "duration.min"; "duration.p50": "duration.p50"; "duration.p90": "duration.p90"; "duration.p95": "duration.p95"; "duration.p99": "duration.p99"; errorCount: "errorCount"; errorRate: "errorRate"; "timeToFirstChunk.avg": "timeToFirstChunk.avg"; "timeToFirstChunk.count": "timeToFirstChunk.count"; "timeToFirstChunk.max": "timeToFirstChunk.max"; "timeToFirstChunk.min": "timeToFirstChunk.min"; "timeToFirstChunk.p50": "timeToFirstChunk.p50"; "timeToFirstChunk.p90": "timeToFirstChunk.p90"; "timeToFirstChunk.p95": "timeToFirstChunk.p95"; "timeToFirstChunk.p99": "timeToFirstChunk.p99"; "timeToFirstToken.avg": "timeToFirstToken.avg"; "timeToFirstToken.count": "timeToFirstToken.count"; "timeToFirstToken.max": "timeToFirstToken.max"; "timeToFirstToken.min": "timeToFirstToken.min"; "timeToFirstToken.p50": "timeToFirstToken.p50"; "timeToFirstToken.p90": "timeToFirstToken.p90"; "timeToFirstToken.p95": "timeToFirstToken.p95"; "timeToFirstToken.p99": "timeToFirstToken.p99"; "tokens.cached.avg": "tokens.cached.avg"; "tokens.cached.sum": "tokens.cached.sum"; "tokens.input.avg": "tokens.input.avg"; "tokens.input.sum": "tokens.input.sum"; "tokens.output.avg": "tokens.output.avg"; "tokens.output.sum": "tokens.output.sum"; "tokens.reasoning.avg": "tokens.reasoning.avg"; "tokens.reasoning.sum": "tokens.reasoning.sum"; "tokens.total.avg": "tokens.total.avg"; "tokens.total.sum": "tokens.total.sum"; }>, z.ZodTemplateLiteral<`countDistinct.${string}`>]>>; having: z.ZodOptional<z.ZodType<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate, z.core.$ZodTypeInternals<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate>>>; orderBy: z.ZodDefault<z.ZodObject<{ field: z.ZodString; direction: z.ZodEnum<{ asc: "asc"; desc: "desc"; }>; }, z.core.$strict>>; limit: z.ZodDefault<z.ZodNumber>; }, z.core.$strict>; /** * A span aggregate request without its time range, for callers that store a query and supply the * range at run time (dashboard widgets). Apply `findPredicateComplexityIssue` before parsing. */ export declare const spanAggregateQueryObjectSchema: z.ZodObject<{ where: z.ZodOptional<z.ZodType<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate, z.core.$ZodTypeInternals<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate>>>; groupBy: z.ZodDefault<z.ZodArray<z.ZodString>>; interval: z.ZodOptional<z.ZodEnum<{ "15m": "15m"; "1d": "1d"; "1h": "1h"; "1m": "1m"; "5m": "5m"; }>>; measures: z.ZodArray<z.ZodUnion<readonly [z.ZodEnum<{ "cost.avg": "cost.avg"; "cost.sum": "cost.sum"; count: "count"; "duration.avg": "duration.avg"; "duration.max": "duration.max"; "duration.min": "duration.min"; "duration.p50": "duration.p50"; "duration.p90": "duration.p90"; "duration.p95": "duration.p95"; "duration.p99": "duration.p99"; errorCount: "errorCount"; errorRate: "errorRate"; "timeToFirstChunk.avg": "timeToFirstChunk.avg"; "timeToFirstChunk.count": "timeToFirstChunk.count"; "timeToFirstChunk.max": "timeToFirstChunk.max"; "timeToFirstChunk.min": "timeToFirstChunk.min"; "timeToFirstChunk.p50": "timeToFirstChunk.p50"; "timeToFirstChunk.p90": "timeToFirstChunk.p90"; "timeToFirstChunk.p95": "timeToFirstChunk.p95"; "timeToFirstChunk.p99": "timeToFirstChunk.p99"; "timeToFirstToken.avg": "timeToFirstToken.avg"; "timeToFirstToken.count": "timeToFirstToken.count"; "timeToFirstToken.max": "timeToFirstToken.max"; "timeToFirstToken.min": "timeToFirstToken.min"; "timeToFirstToken.p50": "timeToFirstToken.p50"; "timeToFirstToken.p90": "timeToFirstToken.p90"; "timeToFirstToken.p95": "timeToFirstToken.p95"; "timeToFirstToken.p99": "timeToFirstToken.p99"; "tokens.cached.avg": "tokens.cached.avg"; "tokens.cached.sum": "tokens.cached.sum"; "tokens.input.avg": "tokens.input.avg"; "tokens.input.sum": "tokens.input.sum"; "tokens.output.avg": "tokens.output.avg"; "tokens.output.sum": "tokens.output.sum"; "tokens.reasoning.avg": "tokens.reasoning.avg"; "tokens.reasoning.sum": "tokens.reasoning.sum"; "tokens.total.avg": "tokens.total.avg"; "tokens.total.sum": "tokens.total.sum"; }>, z.ZodTemplateLiteral<`countDistinct.${string}`>]>>; having: z.ZodOptional<z.ZodType<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate, z.core.$ZodTypeInternals<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate>>>; orderBy: z.ZodDefault<z.ZodObject<{ field: z.ZodString; direction: z.ZodEnum<{ asc: "asc"; desc: "desc"; }>; }, z.core.$strict>>; limit: z.ZodDefault<z.ZodNumber>; }, z.core.$strict>; export declare const spanAggregateRequestSchema: z.ZodPreprocess<z.ZodObject<{ timeRange: z.ZodObject<{ from: z.ZodString; to: z.ZodString; }, z.core.$strict>; where: z.ZodOptional<z.ZodType<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate, z.core.$ZodTypeInternals<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate>>>; groupBy: z.ZodDefault<z.ZodArray<z.ZodString>>; interval: z.ZodOptional<z.ZodEnum<{ "15m": "15m"; "1d": "1d"; "1h": "1h"; "1m": "1m"; "5m": "5m"; }>>; measures: z.ZodArray<z.ZodUnion<readonly [z.ZodEnum<{ "cost.avg": "cost.avg"; "cost.sum": "cost.sum"; count: "count"; "duration.avg": "duration.avg"; "duration.max": "duration.max"; "duration.min": "duration.min"; "duration.p50": "duration.p50"; "duration.p90": "duration.p90"; "duration.p95": "duration.p95"; "duration.p99": "duration.p99"; errorCount: "errorCount"; errorRate: "errorRate"; "timeToFirstChunk.avg": "timeToFirstChunk.avg"; "timeToFirstChunk.count": "timeToFirstChunk.count"; "timeToFirstChunk.max": "timeToFirstChunk.max"; "timeToFirstChunk.min": "timeToFirstChunk.min"; "timeToFirstChunk.p50": "timeToFirstChunk.p50"; "timeToFirstChunk.p90": "timeToFirstChunk.p90"; "timeToFirstChunk.p95": "timeToFirstChunk.p95"; "timeToFirstChunk.p99": "timeToFirstChunk.p99"; "timeToFirstToken.avg": "timeToFirstToken.avg"; "timeToFirstToken.count": "timeToFirstToken.count"; "timeToFirstToken.max": "timeToFirstToken.max"; "timeToFirstToken.min": "timeToFirstToken.min"; "timeToFirstToken.p50": "timeToFirstToken.p50"; "timeToFirstToken.p90": "timeToFirstToken.p90"; "timeToFirstToken.p95": "timeToFirstToken.p95"; "timeToFirstToken.p99": "timeToFirstToken.p99"; "tokens.cached.avg": "tokens.cached.avg"; "tokens.cached.sum": "tokens.cached.sum"; "tokens.input.avg": "tokens.input.avg"; "tokens.input.sum": "tokens.input.sum"; "tokens.output.avg": "tokens.output.avg"; "tokens.output.sum": "tokens.output.sum"; "tokens.reasoning.avg": "tokens.reasoning.avg"; "tokens.reasoning.sum": "tokens.reasoning.sum"; "tokens.total.avg": "tokens.total.avg"; "tokens.total.sum": "tokens.total.sum"; }>, z.ZodTemplateLiteral<`countDistinct.${string}`>]>>; having: z.ZodOptional<z.ZodType<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate, z.core.$ZodTypeInternals<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate>>>; orderBy: z.ZodDefault<z.ZodObject<{ field: z.ZodString; direction: z.ZodEnum<{ asc: "asc"; desc: "desc"; }>; }, z.core.$strict>>; limit: z.ZodDefault<z.ZodNumber>; }, z.core.$strict>, unknown>; /** Same row shape as trace aggregates; `cost.coverage` is per usage-bearing span. */ export declare const spanAggregateResponseSchema: z.ZodObject<{ rows: z.ZodArray<z.ZodObject<{ dimensions: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNullable<z.ZodString>>>; bucket: z.ZodOptional<z.ZodString>; measures: z.ZodRecord<z.ZodUnion<readonly [z.ZodEnum<{ "cost.avg": "cost.avg"; "cost.sum": "cost.sum"; count: "count"; "duration.avg": "duration.avg"; "duration.max": "duration.max"; "duration.min": "duration.min"; "duration.p50": "duration.p50"; "duration.p90": "duration.p90"; "duration.p95": "duration.p95"; "duration.p99": "duration.p99"; errorCount: "errorCount"; errorRate: "errorRate"; "timeToFirstChunk.avg": "timeToFirstChunk.avg"; "timeToFirstChunk.count": "timeToFirstChunk.count"; "timeToFirstChunk.max": "timeToFirstChunk.max"; "timeToFirstChunk.min": "timeToFirstChunk.min"; "timeToFirstChunk.p50": "timeToFirstChunk.p50"; "timeToFirstChunk.p90": "timeToFirstChunk.p90"; "timeToFirstChunk.p95": "timeToFirstChunk.p95"; "timeToFirstChunk.p99": "timeToFirstChunk.p99"; "timeToFirstToken.avg": "timeToFirstToken.avg"; "timeToFirstToken.count": "timeToFirstToken.count"; "timeToFirstToken.max": "timeToFirstToken.max"; "timeToFirstToken.min": "timeToFirstToken.min"; "timeToFirstToken.p50": "timeToFirstToken.p50"; "timeToFirstToken.p90": "timeToFirstToken.p90"; "timeToFirstToken.p95": "timeToFirstToken.p95"; "timeToFirstToken.p99": "timeToFirstToken.p99"; "tokens.cached.avg": "tokens.cached.avg"; "tokens.cached.sum": "tokens.cached.sum"; "tokens.input.avg": "tokens.input.avg"; "tokens.input.sum": "tokens.input.sum"; "tokens.output.avg": "tokens.output.avg"; "tokens.output.sum": "tokens.output.sum"; "tokens.reasoning.avg": "tokens.reasoning.avg"; "tokens.reasoning.sum": "tokens.reasoning.sum"; "tokens.total.avg": "tokens.total.avg"; "tokens.total.sum": "tokens.total.sum"; }>, z.ZodTemplateLiteral<`countDistinct.${string}`>]>, z.ZodNullable<z.ZodNumber>>; cost: z.ZodOptional<z.ZodObject<{ coverage: z.ZodNullable<z.ZodNumber>; unit: z.ZodNullable<z.ZodString>; }, z.core.$strict>>; }, z.core.$strict>>; truncated: z.ZodBoolean; }, z.core.$strict>; export type SpanAggregateRequest = z.input<typeof spanAggregateRequestObjectSchema>; export type NormalizedSpanAggregateRequest = z.output<typeof spanAggregateRequestObjectSchema>; export type SpanAggregateResponse = z.infer<typeof spanAggregateResponseSchema>; export declare function parseSpanAggregateRequest(input: unknown): NormalizedSpanAggregateRequest; export type TrustedSpanAggregateMeasure = TrustedAggregateMeasure<SpanAggregateCountDistinctField>; export type TrustedSpanAggregateMeasureName = TrustedSpanAggregateMeasure['name']; export type TrustedSpanAggregateHavingPredicate = TrustedTraceAggregateHavingPredicate<TrustedSpanAggregateMeasureName>; export type TrustedSpanAggregateOrderBy = TrustedTraceAggregateOrderBy<TrustedSpanAggregateMeasureName, SpanAggregateDimension>; /** * Trusted, backend-independent plan for `aggregateSpans()`. * * Group semantics (groups, `having`/`orderBy` over whole-window measures, null ordering, empty * populations, `limit`/`truncated`, buckets, caps enforced only by the planner, null measures) are * exactly those of `TrustedTraceAggregatePlan`. What differs: * * - **Window.** Up to `TRACE_AGGREGATE_MAX_TIME_RANGE_DAYS`, as for traces. Hosts accept the * span retention window they advertise, the same for every tenant scope (project or org), and * stores must serve that whole window: there is no narrower org-wide span window. * - **Population.** One row per current completed span: exactly the spans `querySpans()` pages * through for the same `timeRange`, `where`, and `scope`, so `count` with no `groupBy` equals * that row count. Retry copies of a span collapse to its current copy before `where`, * grouping, and measures. `where` uses the `querySpans()` field policy. Buckets use the span's * `startedAt`; `duration.*` is the span's `endedAt - startedAt`; `errorCount`/`errorRate` read * the span's own `error`. * - **Usage stage.** Usage is attributed per span, keyed by `(traceId, spanId)`, and aggregated per * span before grouping. A metric row belongs to selected span S when its `traceId` and `spanId` * are S's, its `name` is in `TRACE_AGGREGATE_USAGE_METRIC_NAMES`, its `timestamp >= * timeRange.from` (no upper bound), and it matches `scope`. Rows sharing a `metricId` collapse * to one, without relying on background merges. Usage is never rolled up to ancestor spans: a * model call's tokens belong to the span that emitted them (normally `model_generation`, or the * nearest exported span when model spans are hidden), so an `agent_run` span has no usage. * - **Tokens and cost.** The trace-aggregate rules with "trace" read as "span": a span is * usage-bearing when at least one metric row belongs to it; `tokens.*.avg` divides by the * group's usage-bearing spans, `cost.avg` by spans with a priced row, and `cost.coverage` is * covered spans รท usage-bearing spans. Pricing (priced / failed to price / neither, `'mixed'` * units) is the aggregate rule, not the per-span carrier rule `querySpans()` once used for its * removed `cost` field; stores must not reuse that path. */ export interface TrustedSpanAggregatePlan { result: 'spanAggregate'; timeRange: { from: string; to: string; }; where?: TrustedTraceQueryScalarPredicate; scope?: TraceQueryTenantScope; /** Normalized dimensions in request order (at most two). */ dimensions: SpanAggregateDimension[]; interval?: TraceAggregateInterval; /** Requested measures in request order; `countDistinct` names are normalized. */ measures: TrustedSpanAggregateMeasure[]; having?: TrustedSpanAggregateHavingPredicate; orderBy: TrustedSpanAggregateOrderBy; /** Maximum number of groups (not rows) in the response. */ limit: number; } /** * Converts a span aggregate request into the canonical plan consumed by observability storage * adapters. Accepts raw input; schema and planner failures throw `TraceQueryValidationError`. * * @internal This is a trusted server/storage boundary, not a client-side query builder. */ export declare function planSpanAggregate(input: unknown, options?: Pick<TraceQueryPlanOptions, 'scope' | 'allowTokenSearch'>): TrustedSpanAggregatePlan; export {}; //# sourceMappingURL=span-aggregate.d.ts.map