hobots 1.7.2 → 1.7.4

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.cjs CHANGED
@@ -163,7 +163,7 @@ function scrubSecrets(text) {
163
163
 
164
164
  // src/utils.ts
165
165
  var SDK_NAME = "hobots";
166
- var SDK_VERSION = true ? "1.7.1" : "0.0.0-dev";
166
+ var SDK_VERSION = true ? "1.7.2" : "0.0.0-dev";
167
167
  function isNode() {
168
168
  return typeof process !== "undefined" && typeof process.versions?.node === "string";
169
169
  }
@@ -722,12 +722,6 @@ var Scope = class _Scope {
722
722
  function spanId() {
723
723
  return uuid4().slice(0, 16);
724
724
  }
725
- function printSpanLog(kind, phase, label, startTs, endTs) {
726
- const durationMs = startTs && endTs ? new Date(endTs).getTime() - new Date(startTs).getTime() : void 0;
727
- const tag = colorize(`[hobots:${kind}] ${phase.toUpperCase()}`, phase === "finish" ? "green" : "cyan");
728
- const suffix = durationMs != null ? ` (${durationMs}ms)` : "";
729
- console.log(`${tag} ${label}${suffix}`);
730
- }
731
725
  function spanLabel(op, description) {
732
726
  return [op, description].filter(Boolean).join(" \u2014 ") || "(sem nome)";
733
727
  }
@@ -758,7 +752,7 @@ var Span = class {
758
752
  finish() {
759
753
  if (this.endTimestamp) return;
760
754
  this.endTimestamp = nowISO();
761
- printSpanLog("span", "finish", spanLabel(this.op, this.description), this.startTimestamp, this.endTimestamp);
755
+ this.transaction.emit("span", "finish", spanLabel(this.op, this.description), this.startTimestamp, this.endTimestamp);
762
756
  }
763
757
  toJSON() {
764
758
  return {
@@ -780,7 +774,7 @@ var Transaction = class {
780
774
  this.op = op;
781
775
  this.sampled = sampled;
782
776
  this.runId = runId;
783
- printSpanLog("transaction", "start", op ? `${name} (${op})` : name);
777
+ this.emit("transaction", "start", op ? `${name} (${op})` : name);
784
778
  }
785
779
  eventId = uuid4();
786
780
  spanId = spanId();
@@ -789,10 +783,36 @@ var Transaction = class {
789
783
  spans = [];
790
784
  tags = {};
791
785
  finished = false;
786
+ lifecycleSink;
787
+ // eventos emitidos antes do sink existir (o START da transaction sai no construtor,
788
+ // antes do RunReporter conectar) — replay no setLifecycleSink
789
+ lifecycleBacklog = [];
790
+ /**
791
+ * Ecoa no terminal o início/fim de um span ou transaction (sempre, independente de
792
+ * sampling ou do debug) e espelha a linha no sink de lifecycle, se conectado — é
793
+ * assim que os eventos de performance também aparecem nos logs da run no app.
794
+ */
795
+ emit(kind, phase, label, startTs, endTs) {
796
+ const durationMs = startTs && endTs ? new Date(endTs).getTime() - new Date(startTs).getTime() : void 0;
797
+ const tag = colorize(`[hobots:${kind}] ${phase.toUpperCase()}`, phase === "finish" ? "green" : "cyan");
798
+ const suffix = durationMs != null ? ` (${durationMs}ms)` : "";
799
+ console.log(`${tag} ${label}${suffix}`);
800
+ const level = phase === "finish" ? "success" : "info";
801
+ const message = `[${kind}] ${phase.toUpperCase()} ${label}${suffix}`;
802
+ if (this.lifecycleSink) this.lifecycleSink(level, message);
803
+ else if (this.lifecycleBacklog.length < 100) this.lifecycleBacklog.push([level, message]);
804
+ }
805
+ /** Conecta o espelho de lifecycle (RunReporter) e faz replay do backlog em ordem. */
806
+ setLifecycleSink(sink) {
807
+ this.lifecycleSink = sink;
808
+ const backlog = this.lifecycleBacklog;
809
+ this.lifecycleBacklog = [];
810
+ for (const [level, message] of backlog) sink(level, message);
811
+ }
792
812
  createSpan(parentSpanId, op, description) {
793
813
  const span = new Span(this, parentSpanId, op, description);
794
814
  this.spans.push(span);
795
- printSpanLog("span", "start", spanLabel(op, description));
815
+ this.emit("span", "start", spanLabel(op, description));
796
816
  return span;
797
817
  }
798
818
  /** Span direto da transaction. */
@@ -816,7 +836,7 @@ var Transaction = class {
816
836
  this.finished = true;
817
837
  for (const span of this.spans) span.finish();
818
838
  const endTimestamp = nowISO();
819
- printSpanLog(
839
+ this.emit(
820
840
  "transaction",
821
841
  "finish",
822
842
  this.op ? `${this.name} (${this.op})` : this.name,
@@ -1024,6 +1044,7 @@ var RunReporter = class {
1024
1044
  error: (message) => this.log("error", message),
1025
1045
  success: (message) => this.log("success", message)
1026
1046
  };
1047
+ tx.setLifecycleSink((level, message) => this.push(level, message));
1027
1048
  const signal = this.abortController.signal;
1028
1049
  this.context = {
1029
1050
  runId,
@@ -1081,6 +1102,12 @@ var RunReporter = class {
1081
1102
  if (this.dead) return;
1082
1103
  const scrubbed = scrubSecrets(message);
1083
1104
  printRunLog(this.context.task, level, scrubbed);
1105
+ this.push(level, scrubbed, true);
1106
+ }
1107
+ /** Bufferiza uma entrada de log sem eco no terminal (cap, truncate, flush). */
1108
+ push(level, message, alreadyScrubbed = false) {
1109
+ if (this.dead) return;
1110
+ const scrubbed = alreadyScrubbed ? message : scrubSecrets(message);
1084
1111
  if (this.loggedCount >= MAX_LOGS_PER_RUN) {
1085
1112
  this.droppedCount++;
1086
1113
  return;
@@ -1477,15 +1504,16 @@ var TaskAgent = class {
1477
1504
  this.logger.debug(`executando run ${run.id} (${run.task})`);
1478
1505
  try {
1479
1506
  await runWithRunContext({ runId: run.id, tx }, () => this.invokeWithTimeout(handler, run, reporter));
1480
- reporter.reportSuccess();
1481
1507
  tx.setStatus("ok");
1508
+ tx.finish();
1509
+ reporter.reportSuccess();
1482
1510
  this.logger.debug(`run ${run.id} conclu\xEDda`);
1483
1511
  } catch (error) {
1484
- reporter.reportFailure(toRunError(error));
1485
1512
  tx.setStatus("error");
1513
+ tx.finish();
1514
+ reporter.reportFailure(toRunError(error));
1486
1515
  this.logger.debug(`run ${run.id} falhou: ${String(error)}`);
1487
1516
  }
1488
- tx.finish();
1489
1517
  await reporter.finalize();
1490
1518
  } finally {
1491
1519
  this.reporters.delete(reporter);