@mastra/clickhouse 1.28.0 → 1.29.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -6916,6 +6916,8 @@ const timingFeatures = [
6916
6916
  ...timeToFirstTokenSupported ? ["trace-query-time-to-first-token"] : [],
6917
6917
  ...timeToFirstTokenSupported && timeToFirstTokenSortSupported ? ["trace-query-time-to-first-token-sort"] : []
6918
6918
  ];
6919
+ /** Core before the fields rejects their trace and span predicates, so the feature is only declared when core plans them. */
6920
+ const contextFieldFeatures = "requestId" in (coreStorage.TRACE_QUERY_FIELD_REGISTRY?.trace ?? {}) ? ["trace-query-context-fields"] : [];
6919
6921
  /**
6920
6922
  * Outcome fields follow the same rule: core before them rejects their predicates and strict rows,
6921
6923
  * so they are only returned and declared when core knows them.
@@ -6961,6 +6963,18 @@ const TRACE_FIELDS = {
6961
6963
  sql: "r.organizationId",
6962
6964
  parameterType: "String"
6963
6965
  },
6966
+ requestId: {
6967
+ sql: "r.requestId",
6968
+ parameterType: "String"
6969
+ },
6970
+ serviceName: {
6971
+ sql: "r.serviceName",
6972
+ parameterType: "String"
6973
+ },
6974
+ experimentId: {
6975
+ sql: "r.experimentId",
6976
+ parameterType: "String"
6977
+ },
6964
6978
  startedAt: {
6965
6979
  sql: "r.startedAt",
6966
6980
  parameterType: "DateTime64(3, 'UTC')"
@@ -7114,6 +7128,18 @@ const SPAN_FIELDS = {
7114
7128
  organizationId: {
7115
7129
  sql: "s.organizationId",
7116
7130
  parameterType: "String"
7131
+ },
7132
+ requestId: {
7133
+ sql: "s.requestId",
7134
+ parameterType: "String"
7135
+ },
7136
+ serviceName: {
7137
+ sql: "s.serviceName",
7138
+ parameterType: "String"
7139
+ },
7140
+ experimentId: {
7141
+ sql: "s.experimentId",
7142
+ parameterType: "String"
7117
7143
  }
7118
7144
  };
7119
7145
  const SPAN_QUERY_FIELDS = {
@@ -7138,18 +7164,6 @@ const SPAN_QUERY_FIELDS = {
7138
7164
  sql: "s.resourceId",
7139
7165
  parameterType: "String"
7140
7166
  },
7141
- requestId: {
7142
- sql: "s.requestId",
7143
- parameterType: "String"
7144
- },
7145
- serviceName: {
7146
- sql: "s.serviceName",
7147
- parameterType: "String"
7148
- },
7149
- experimentId: {
7150
- sql: "s.experimentId",
7151
- parameterType: "String"
7152
- },
7153
7167
  environment: {
7154
7168
  sql: "s.environment",
7155
7169
  parameterType: "String"
@@ -7503,7 +7517,10 @@ function compileClickHouseTraceScope(selection, relationCollections, parameters,
7503
7517
  runId,
7504
7518
  sessionId,
7505
7519
  userId,
7506
- organizationId
7520
+ organizationId,
7521
+ requestId,
7522
+ serviceName,
7523
+ experimentId
7507
7524
  FROM ${TABLE_SPAN_EVENTS}
7508
7525
  WHERE isNotNull(traceId)
7509
7526
  AND traceId IN (${options.spanExistence && options.oneRootPerTrace ? windowTraces(bound) : "SELECT traceId FROM root_scope"})${tenant}${options.spanExistence ? "" : `
@@ -8821,6 +8838,27 @@ const PERCENTILES = {
8821
8838
  "duration.p95": .95,
8822
8839
  "duration.p99": .99
8823
8840
  };
8841
+ /** Quantile for each percentile statistic of a time-to-first measure. */
8842
+ const STATISTIC_PERCENTILES = {
8843
+ p50: .5,
8844
+ p90: .9,
8845
+ p95: .95,
8846
+ p99: .99
8847
+ };
8848
+ /**
8849
+ * Store features this compiler supports beyond `trace-aggregate` / `span-aggregate`. Core before
8850
+ * the time-to-first measures rejects them, so they are only declared when core plans them.
8851
+ */
8852
+ const aggregateTimeToFirstFeatures = "timeToFirstToken.p50" in coreStorage.TRACE_AGGREGATE_MEASURE_REGISTRY ? ["aggregate-time-to-first-measures"] : [];
8853
+ /** The nullable span columns read by the plan's time-to-first measures. */
8854
+ function timeToFirstFields(plan) {
8855
+ const fields = /* @__PURE__ */ new Set();
8856
+ for (const measure of plan.measures) {
8857
+ const field = measureRule(measure.name)?.field;
8858
+ if (field) fields.add(field);
8859
+ }
8860
+ return [...fields];
8861
+ }
8824
8862
  const COMPARISON_SQL = {
8825
8863
  eq: "=",
8826
8864
  ne: "!=",
@@ -8860,7 +8898,11 @@ function compileClickHouseTraceAggregate(plan) {
8860
8898
  function compileClickHouseSpanAggregate(plan) {
8861
8899
  const parameters = new ParameterBuilder();
8862
8900
  return compileClickHouseAggregate(plan, parameters, {
8863
- ctes: compileClickHouseSpanSelection(plan, [...plan.dimensions, ...plan.measures.flatMap((measure) => measure.type === "countDistinct" && measure.field !== "traceId" ? [measure.field] : [])], parameters),
8901
+ ctes: compileClickHouseSpanSelection(plan, [
8902
+ ...plan.dimensions,
8903
+ ...plan.measures.flatMap((measure) => measure.type === "countDistinct" && measure.field !== "traceId" ? [measure.field] : []),
8904
+ ...timeToFirstFields(plan)
8905
+ ], parameters),
8864
8906
  from: "selected",
8865
8907
  dimensionSql: (field) => lookupDimensionSql(SPAN_DIMENSION_SQL, field),
8866
8908
  isErrorSql: "r.status = 'error'",
@@ -8886,7 +8928,7 @@ function compileClickHouseAggregate(plan, parameters, source) {
8886
8928
  distinctColumns.set(measure.field, column);
8887
8929
  factColumns.push(`CAST(${distinctSql(measure.field)} AS Nullable(String)) AS ${column}`);
8888
8930
  }
8889
- factColumns.push(`toFloat64(${durationMsSql("r.startedAt", "r.endedAt")}) AS durationMs`, `${source.isErrorSql} AS isError`, `cityHash64(${source.usageKey.map((column) => `r.${column}`).join(", ")}) AS traceSeed`);
8931
+ factColumns.push(`toFloat64(${durationMsSql("r.startedAt", "r.endedAt")}) AS durationMs`, `${source.isErrorSql} AS isError`, `cityHash64(${source.usageKey.map((column) => `r.${column}`).join(", ")}) AS traceSeed`, ...timeToFirstFields(plan).map((field) => `toNullable(toFloat64(r.${field})) AS ${field}`));
8890
8932
  if (plan.interval !== void 0) {
8891
8933
  const intervalMs = coreStorage.TRACE_AGGREGATE_INTERVAL_MS[plan.interval];
8892
8934
  if (intervalMs === void 0) throw new Error(`Unsupported trusted trace-aggregate interval: ${plan.interval}`);
@@ -8917,6 +8959,14 @@ function compileClickHouseAggregate(plan, parameters, source) {
8917
8959
  const aggregate = rule.statistic === "avg" ? "avgOrNull" : "sumOrNull";
8918
8960
  return `if(${mixedUnitsSql}, NULL, toFloat64(${aggregate}(traceCost)))`;
8919
8961
  }
8962
+ if (rule?.field) {
8963
+ if (rule.statistic === "count") return `toFloat64(count(${rule.field}))`;
8964
+ if (rule.statistic === "avg") return `avgOrNull(${rule.field})`;
8965
+ if (rule.statistic === "min") return `minOrNull(${rule.field})`;
8966
+ if (rule.statistic === "max") return `maxOrNull(${rule.field})`;
8967
+ const quantile = rule.statistic && STATISTIC_PERCENTILES[rule.statistic];
8968
+ if (quantile !== void 0) return `quantileDeterministicOrNull(${quantile})(${rule.field}, traceSeed)`;
8969
+ }
8920
8970
  if (name === "errorCount") return "toFloat64(countIf(isError))";
8921
8971
  if (name === "errorRate") return "countIf(isError) / count()";
8922
8972
  if (name === "duration.avg") return "avg(durationMs)";
@@ -10419,11 +10469,13 @@ var ObservabilityStorageClickhouseVNext = class extends ObservabilityStorage {
10419
10469
  "trace-query-tenant-scope",
10420
10470
  "feedback",
10421
10471
  "trace-query-context-ids",
10472
+ ...contextFieldFeatures,
10422
10473
  ...timingFeatures,
10423
10474
  ...outcomeFeatures,
10424
10475
  "trace-query-root-span-fields",
10425
10476
  "trace-query-root-entity-id",
10426
10477
  "trace-aggregate",
10478
+ ...aggregateTimeToFirstFeatures,
10427
10479
  ...spanQueryFeatures,
10428
10480
  ...spanAggregateFeatures,
10429
10481
  ...threadQueryFeatures
@@ -10445,11 +10497,13 @@ var ObservabilityStorageClickhouseVNext = class extends ObservabilityStorage {
10445
10497
  "trace-query-tenant-scope",
10446
10498
  "feedback",
10447
10499
  "trace-query-context-ids",
10500
+ ...contextFieldFeatures,
10448
10501
  ...timingFeatures,
10449
10502
  ...outcomeFeatures,
10450
10503
  "trace-query-root-span-fields",
10451
10504
  "trace-query-root-entity-id",
10452
10505
  "trace-aggregate",
10506
+ ...aggregateTimeToFirstFeatures,
10453
10507
  ...spanQueryFeatures,
10454
10508
  ...spanAggregateFeatures,
10455
10509
  ...threadQueryFeatures