Unverified Commit eff3accf authored by Hong Minhee's avatar Hong Minhee
Browse files

Use Promise.allSettled() in #exportAsync for robust error handling

Process all store operations in parallel and ensure all records are
attempted even if some fail. This prevents partial data loss when
individual operations fail.

https://github.com/fedify-dev/fedify/pull/502#discussion_r2643077193
parent cb83e023
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -258,12 +258,20 @@ export class FedifySpanExporter implements SpanExporter {
  }

  async #exportAsync(spans: ReadableSpan[]): Promise<void> {
    const storeOperations: Promise<void>[] = [];
    for (const span of spans) {
      const records = this.#extractRecords(span);
      for (const record of records) {
        await this.#storeRecord(record);
        storeOperations.push(this.#storeRecord(record));
      }
    }
    const results = await Promise.allSettled(storeOperations);
    const rejected = results.filter(
      (r): r is PromiseRejectedResult => r.status === "rejected",
    );
    if (rejected.length > 0) {
      throw rejected[0].reason;
    }
  }

  #extractRecords(span: ReadableSpan): TraceActivityRecord[] {