@mastra/observability 1.24.0-alpha.7 → 1.24.1-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
@@ -4126,7 +4126,8 @@ var DefaultSpan = class extends BaseSpan {
4126
4126
  if (options) this.applyOutcome(options);
4127
4127
  if (this.outcome === null) this.outcome = "unknown";
4128
4128
  const endTime = options?.endTime;
4129
- this.endTime = endTime instanceof Date && endTime >= this.startTime ? endTime : /* @__PURE__ */ new Date();
4129
+ const candidate = endTime instanceof Date ? endTime : /* @__PURE__ */ new Date();
4130
+ this.endTime = candidate < this.startTime ? this.startTime : candidate;
4130
4131
  this.detachFromParent();
4131
4132
  if (options?.metadata) this.metadata = {
4132
4133
  ...this.metadata,
@@ -6993,6 +6994,14 @@ function generateClientSignalId() {
6993
6994
  * Using `@opentelemetry/otlp-transformer` would pull in the full proto
6994
6995
  * dependency tree (~hundreds of KB) for what amounts to a 100-line walker.
6995
6996
  */
6997
+ function hasReversedTimestamps(start, end) {
6998
+ if (typeof start !== "string" && typeof start !== "number" || typeof end !== "string" && typeof end !== "number") return false;
6999
+ try {
7000
+ return BigInt(end) < BigInt(start);
7001
+ } catch {
7002
+ return false;
7003
+ }
7004
+ }
6996
7005
  /**
6997
7006
  * Convert an OTLP nanosecond timestamp (string or number) to a JS Date.
6998
7007
  *
@@ -7001,7 +7010,7 @@ function generateClientSignalId() {
7001
7010
  * uint64 to avoid JS number precision loss) or numbers.
7002
7011
  */
7003
7012
  function nanosToDate(value) {
7004
- if (typeof value === "number") return new Date(Math.round(value / 1e6));
7013
+ if (typeof value === "number") return new Date(Math.trunc(value / 1e6));
7005
7014
  if (typeof value === "string") {
7006
7015
  if (value.length <= 6) return /* @__PURE__ */ new Date(0);
7007
7016
  const ms = Number(value.slice(0, -6));
@@ -7059,6 +7068,7 @@ function decodeResourceSpans(payload) {
7059
7068
  spanId,
7060
7069
  parentSpanId: typeof s.parentSpanId === "string" && s.parentSpanId ? s.parentSpanId : void 0,
7061
7070
  name,
7071
+ ...hasReversedTimestamps(s.startTimeUnixNano, s.endTimeUnixNano) ? { hasReversedTimestamps: true } : {},
7062
7072
  startTime: nanosToDate(s.startTimeUnixNano),
7063
7073
  endTime: s.endTimeUnixNano !== void 0 ? nanosToDate(s.endTimeUnixNano) : void 0,
7064
7074
  attributes: flattenAttributes(s.attributes)
@@ -7305,6 +7315,14 @@ var ClientObservabilityProxyImpl = class {
7305
7315
  this.#warn("Client observability payload contains spans with orphan parents; dropping.");
7306
7316
  return;
7307
7317
  }
7318
+ if (decodedSpans.some((span) => span.hasReversedTimestamps || span.endTime && span.endTime < span.startTime)) {
7319
+ this.#warn("Client observability payload contains a span whose end precedes its start; dropping.");
7320
+ return;
7321
+ }
7322
+ if (typeof payload.executionDurationMs === "number" && payload.executionDurationMs < 0) {
7323
+ this.#warn("Client observability payload contains a negative execution duration; dropping.");
7324
+ return;
7325
+ }
7308
7326
  for (const decoded of decodedSpans) {
7309
7327
  const exported = buildExportedSpan(decoded);
7310
7328
  const startedEvent = {