@mastra/observability 1.16.5 → 1.16.6-alpha.0

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
@@ -8696,6 +8696,23 @@ function isInstance(obj) {
8696
8696
  return obj instanceof BaseObservabilityInstance;
8697
8697
  }
8698
8698
  /**
8699
+ * Delays (ms) between attempts to rehydrate a trace from storage when an
8700
+ * annotation (score/feedback) targets a span that has not been flushed by
8701
+ * the configured exporters yet. Exporters buffer and flush asynchronously:
8702
+ * by default they flush at 1000 spans or after a 5000ms wait
8703
+ * (`maxBatchWaitMs`), so an annotation emitted right after a span ends can
8704
+ * race the flush and be silently dropped. The schedule below sums to
8705
+ * ~5.85s, covering the default flush interval with margin.
8706
+ */
8707
+ const RECORDED_TRACE_LOOKUP_RETRY_DELAYS_MS = [
8708
+ 100,
8709
+ 250,
8710
+ 500,
8711
+ 1e3,
8712
+ 2e3,
8713
+ 2e3
8714
+ ];
8715
+ /**
8699
8716
  * Top-level observability entrypoint. Manages a registry of ObservabilityInstance
8700
8717
  * configurations and provides instance selection via config selectors.
8701
8718
  */
@@ -8852,14 +8869,15 @@ var Observability = class extends MastraBase {
8852
8869
  return;
8853
8870
  }
8854
8871
  if (!args.traceId) return;
8855
- const trace = await this.#getStoredTrace(args.traceId);
8856
- if (!trace) return;
8857
- const event = buildRecordedScoreEventFromTrace({
8872
+ const event = await this.#buildRecordedEventWithRetry(args.traceId, (trace) => buildRecordedScoreEventFromTrace({
8858
8873
  trace,
8859
8874
  spanId: args.spanId,
8860
8875
  score: args.score
8861
- });
8862
- if (!event) return;
8876
+ }));
8877
+ if (!event) {
8878
+ this.logger?.warn(`Score event was dropped because the target trace/span was not found in observability storage (traceId: ${args.traceId}, spanId: ${args.spanId})`);
8879
+ return;
8880
+ }
8863
8881
  await this.#emitRecordedEvent(event);
8864
8882
  }
8865
8883
  async addFeedback(args) {
@@ -8875,14 +8893,15 @@ var Observability = class extends MastraBase {
8875
8893
  return;
8876
8894
  }
8877
8895
  if (!args.traceId) return;
8878
- const trace = await this.#getStoredTrace(args.traceId);
8879
- if (!trace) return;
8880
- const event = buildRecordedFeedbackEventFromTrace({
8896
+ const event = await this.#buildRecordedEventWithRetry(args.traceId, (trace) => buildRecordedFeedbackEventFromTrace({
8881
8897
  trace,
8882
8898
  spanId: args.spanId,
8883
8899
  feedback: args.feedback
8884
- });
8885
- if (!event) return;
8900
+ }));
8901
+ if (!event) {
8902
+ this.logger?.warn(`Feedback event was dropped because the target trace/span was not found in observability storage (traceId: ${args.traceId}, spanId: ${args.spanId})`);
8903
+ return;
8904
+ }
8886
8905
  await this.#emitRecordedEvent(event);
8887
8906
  }
8888
8907
  /** Register a named observability instance, optionally marking it as default. */
@@ -8947,6 +8966,20 @@ var Observability = class extends MastraBase {
8947
8966
  if (!storage) return null;
8948
8967
  return await storage.getStore("observability") ?? null;
8949
8968
  }
8969
+ /**
8970
+ * Build a recorded score/feedback event from storage, retrying briefly to
8971
+ * ride out the async exporter flush: annotations emitted right after a
8972
+ * span ends can otherwise race the flush and be silently dropped.
8973
+ */
8974
+ async #buildRecordedEventWithRetry(traceId, build) {
8975
+ for (const delayMs of [0, ...RECORDED_TRACE_LOOKUP_RETRY_DELAYS_MS]) {
8976
+ if (delayMs > 0) await new Promise((resolve) => setTimeout(resolve, delayMs));
8977
+ const trace = await this.#getStoredTrace(traceId);
8978
+ const event = trace ? build(trace) : null;
8979
+ if (event) return event;
8980
+ }
8981
+ return null;
8982
+ }
8950
8983
  async #getStoredTrace(traceId) {
8951
8984
  const observabilityStorage = await this.#getObservabilityStorage();
8952
8985
  if (!observabilityStorage) return null;