@mastra/core
Version:
1 lines • 7.44 kB
Source Map (JSON)
{"version":3,"file":"metrics-BlJ_NV8I.cjs","names":[],"sources":["../src/observability/types/metrics.ts"],"sourcesContent":["import type { CorrelationContext } from './core';\n\n// ============================================================================\n// Metric Type\n// ============================================================================\n\n/**\n * @deprecated MetricType is no longer stored. All metrics are raw events\n * with aggregation determined at query time.\n */\nexport type MetricType = 'counter' | 'gauge' | 'histogram';\n\n// ============================================================================\n// Token Metric Names\n// ============================================================================\n\n/**\n * Names of the token usage metrics auto-extracted from model generations.\n * Emitted by `@mastra/observability` and read by storage aggregation.\n */\nexport const TokenMetrics = {\n TOTAL_INPUT: 'mastra_model_total_input_tokens',\n TOTAL_OUTPUT: 'mastra_model_total_output_tokens',\n INPUT_TEXT: 'mastra_model_input_text_tokens',\n INPUT_CACHE_READ: 'mastra_model_input_cache_read_tokens',\n INPUT_CACHE_WRITE: 'mastra_model_input_cache_write_tokens',\n INPUT_CACHE_WRITE_5M: 'mastra_model_input_cache_write_5m_tokens',\n INPUT_CACHE_WRITE_1H: 'mastra_model_input_cache_write_1h_tokens',\n INPUT_AUDIO: 'mastra_model_input_audio_tokens',\n INPUT_IMAGE: 'mastra_model_input_image_tokens',\n OUTPUT_TEXT: 'mastra_model_output_text_tokens',\n OUTPUT_REASONING: 'mastra_model_output_reasoning_tokens',\n OUTPUT_AUDIO: 'mastra_model_output_audio_tokens',\n OUTPUT_IMAGE: 'mastra_model_output_image_tokens',\n} as const;\n\nexport type TokenMetrics = (typeof TokenMetrics)[keyof typeof TokenMetrics];\n\n// ============================================================================\n// MetricsContext (API Interface)\n// ============================================================================\n\n/**\n * MetricsContext - API for emitting metrics.\n * Use `emit()` to record a metric observation.\n */\nexport interface MetricEmitOptions {\n /** Canonical model/cost context for this specific metric row */\n costContext?: CostContext;\n /** Shared by every token/cost row emitted from the same model usage observation */\n usageId?: string;\n}\n\nexport interface MetricsContext {\n /** Emit a metric observation. */\n emit(name: string, value: number, labels?: Record<string, string>, options?: MetricEmitOptions): void;\n\n /** @deprecated Use `emit()` instead. */\n counter(name: string): Counter;\n /** @deprecated Use `emit()` instead. */\n gauge(name: string): Gauge;\n /** @deprecated Use `emit()` instead. */\n histogram(name: string): Histogram;\n}\n\n/** @deprecated Use MetricsContext.emit() instead. */\nexport interface Counter {\n add(value: number, additionalLabels?: Record<string, string>): void;\n}\n\n/** @deprecated Use MetricsContext.emit() instead. */\nexport interface Gauge {\n set(value: number, additionalLabels?: Record<string, string>): void;\n}\n\n/** @deprecated Use MetricsContext.emit() instead. */\nexport interface Histogram {\n record(value: number, additionalLabels?: Record<string, string>): void;\n}\n\n// ============================================================================\n// ExportedMetric (Event Bus Transport)\n// ============================================================================\n\n/**\n * Typed context used for cost estimations.\n */\nexport interface CostContext {\n provider?: string;\n model?: string;\n estimatedCost?: number;\n costUnit?: string;\n costMetadata?: Record<string, unknown>;\n}\n\n/**\n * Metric data transported via the event bus.\n * Represents a single metric observation.\n * Must be JSON-serializable (Date serializes via toJSON()).\n *\n * Descriptive correlation metadata travels in `correlationContext`.\n * Signal identity stays on the top-level `traceId` / `spanId` fields.\n * pricing/model fields travel in `costContext`.\n */\nexport interface ExportedMetric {\n /** Unique identifier for this metric event, generated at emission time */\n metricId: string;\n\n /** When the metric was recorded */\n timestamp: Date;\n\n /** Trace associated with this metric (undefined = not tied to a trace) */\n traceId?: string;\n\n /** Specific span associated with this metric */\n spanId?: string;\n\n /** Metric name (e.g., mastra_agent_duration_ms) */\n name: string;\n\n /** Metric value (single observation) */\n value: number;\n\n /** Metric labels for dimensional filtering */\n labels: Record<string, string>;\n\n /** Context for correlation to traces */\n correlationContext?: CorrelationContext;\n\n /** Context for cost estimation */\n costContext?: CostContext;\n\n /**\n * Identifies one model usage observation. Every token/cost metric row emitted\n * from the same usage (one model call, or one hidden call rolled up onto a\n * visible ancestor) shares this id; separate observations get distinct ids,\n * even when they share a span. Retries resend the same id. Undefined for\n * non-usage metrics.\n */\n usageId?: string;\n\n /**\n * User-defined metadata.\n * This is reserved for non-canonical metadata that does not belong\n * in record context or cost context.\n */\n metadata?: Record<string, unknown>;\n}\n\n// ============================================================================\n// MetricEvent (Event Bus Event)\n// ============================================================================\n\n/** Metric event emitted to the ObservabilityBus */\nexport interface MetricEvent {\n type: 'metric';\n metric: ExportedMetric;\n}\n\n// ============================================================================\n// Cardinality Protection\n// ============================================================================\n\n/**\n * Default labels to block from metrics to prevent cardinality explosion.\n * These are high-cardinality fields that should not be used as metric labels.\n */\nexport const DEFAULT_BLOCKED_LABELS = [\n 'trace_id',\n 'span_id',\n 'run_id',\n 'request_id',\n 'user_id',\n 'resource_id',\n 'session_id',\n 'thread_id',\n] as const;\n\n/** Cardinality protection configuration */\nexport interface CardinalityConfig {\n /**\n * Labels to block from metrics. **Replaces** the default list entirely —\n * DEFAULT_BLOCKED_LABELS are NOT merged in when this is set.\n *\n * - `undefined` (default) → uses DEFAULT_BLOCKED_LABELS\n * - `[]` → disables label blocking (allows all labels through)\n * - `['x', 'y']` → blocks only x and y; defaults like trace_id are allowed\n *\n * To extend the defaults, spread them into your list:\n * ```ts\n * blockedLabels: [...DEFAULT_BLOCKED_LABELS, 'my_custom_label']\n * ```\n */\n blockedLabels?: string[];\n\n /**\n * Whether to block UUID-like values in labels.\n * @default true\n */\n blockUUIDs?: boolean;\n}\n\n/** Metrics-specific configuration */\nexport interface MetricsConfig {\n /** Whether metrics are enabled */\n enabled?: boolean;\n /** Cardinality protection settings */\n cardinality?: CardinalityConfig;\n}\n"],"mappings":";;;;;AAoBA,MAAa,eAAe;CAC1B,aAAa;CACb,cAAc;CACd,YAAY;CACZ,kBAAkB;CAClB,mBAAmB;CACnB,sBAAsB;CACtB,sBAAsB;CACtB,aAAa;CACb,aAAa;CACb,aAAa;CACb,kBAAkB;CAClB,cAAc;CACd,cAAc;AAChB;;;;;AAqIA,MAAa,yBAAyB;CACpC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF"}