@sentry/server-utils 11.21.0 → 11.22.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.
Files changed (35) hide show
  1. package/build/cjs/chunks/config.js +2 -0
  2. package/build/cjs/chunks/config.js.map +1 -1
  3. package/build/cjs/chunks/exports.js +4 -1
  4. package/build/cjs/chunks/exports.js.map +1 -1
  5. package/build/cjs/index.js +53 -16
  6. package/build/cjs/index.js.map +1 -1
  7. package/build/esm/ai/workers-ai/utils.js +4 -1
  8. package/build/esm/ai/workers-ai/utils.js.map +1 -1
  9. package/build/esm/integrations/fastify/instrumentation.js +5 -1
  10. package/build/esm/integrations/fastify/instrumentation.js.map +1 -1
  11. package/build/esm/integrations/neon.js +10 -2
  12. package/build/esm/integrations/neon.js.map +1 -1
  13. package/build/esm/integrations/pg.js +41 -14
  14. package/build/esm/integrations/pg.js.map +1 -1
  15. package/build/esm/integrations/pino.js.map +1 -1
  16. package/build/esm/integrations/vercel-ai/extract.js +2 -3
  17. package/build/esm/integrations/vercel-ai/extract.js.map +1 -1
  18. package/build/esm/orchestrion/config/prisma.js +2 -0
  19. package/build/esm/orchestrion/config/prisma.js.map +1 -1
  20. package/build/esm/package.json +1 -1
  21. package/build/esm/tracing-channel.js.map +1 -1
  22. package/build/types/ai/workers-ai/utils.d.ts.map +1 -1
  23. package/build/types/integrations/fastify/instrumentation.d.ts.map +1 -1
  24. package/build/types/integrations/neon.d.ts.map +1 -1
  25. package/build/types/integrations/pg.d.ts +6 -1
  26. package/build/types/integrations/pg.d.ts.map +1 -1
  27. package/build/types/integrations/pino.d.ts.map +1 -1
  28. package/build/types/integrations/vercel-ai/extract.d.ts +2 -5
  29. package/build/types/integrations/vercel-ai/extract.d.ts.map +1 -1
  30. package/build/types/orchestrion/channels.d.ts +1 -0
  31. package/build/types/orchestrion/channels.d.ts.map +1 -1
  32. package/build/types/orchestrion/config/prisma.d.ts +2 -1
  33. package/build/types/orchestrion/config/prisma.d.ts.map +1 -1
  34. package/build/types/tracing-channel.d.ts.map +1 -1
  35. package/package.json +4 -4
@@ -766,6 +766,10 @@ function onRequest(request, _reply, hookDone) {
766
766
  const routeName = getRequestRouteUrl(request);
767
767
  const method = request.method || "GET";
768
768
  (0, _sentry_core.getIsolationScope)().setTransactionName(`${method} ${routeName}`);
769
+ if (!(0, _sentry_core.getActiveSpan)()) {
770
+ hookDone();
771
+ return;
772
+ }
769
773
  const attributes = {
770
774
  [_sentry_conventions_attributes.SENTRY_ORIGIN]: ORIGIN$23,
771
775
  [_sentry_conventions_attributes.SENTRY_OP]: _sentry_conventions_op.HANDLER,
@@ -864,7 +868,7 @@ function handlerWrapper(handler, hookName, spanAttributes = {}) {
864
868
  if (!request) return handler.call(this, ...args);
865
869
  const parentSpan = request[kRequestSpan] ?? void 0;
866
870
  const effectiveParentSpan = parentSpan ?? (0, _sentry_core.getActiveSpan)();
867
- if (effectiveParentSpan && !(0, _sentry_core.spanIsSampled)(effectiveParentSpan)) return handler.call(this, ...args);
871
+ if (!effectiveParentSpan || !(0, _sentry_core.spanIsSampled)(effectiveParentSpan)) return handler.call(this, ...args);
868
872
  const handlerName = handler.name?.length > 0 ? handler.name : this.pluginName ?? ANONYMOUS_FUNCTION_NAME;
869
873
  const hookType = spanAttributes[ATTRIBUTE_FASTIFY_TYPE];
870
874
  const op = hookType === HOOK_TYPE_INSTANCE ? _sentry_conventions_op.MIDDLEWARE : hookType === HOOK_TYPE_HANDLER ? _sentry_conventions_op.HANDLER : void 0;
@@ -8848,15 +8852,16 @@ function instrumentPostgres(options) {
8848
8852
  * streamable `Submittable` result, so it's the only one that defers span-ending to the emitter.
8849
8853
  */
8850
8854
  function bindPgQueryChannel(channelName, options) {
8851
- subscribeQueryLikeChannel(channelName, (ctx) => querySpanOptions(ctx, options), { deferStreamedResult: true });
8855
+ subscribeQueryLikeChannel(channelName, (ctx) => querySpanOptions(ctx, options), { isQuery: true });
8852
8856
  }
8853
8857
  /**
8854
8858
  * Subscribe to a pg tracing-channel and manage a span across its lifecycle.
8855
8859
  * Shared by the query/connect/pool-connect channels. They differ only in how
8856
8860
  * the span's name + attributes are built (`getSpanOptions`), and whether the
8857
- * result can be a streamable emitter (`deferStreamedResult`, query-only).
8861
+ * result is a query result that can be a streamable emitter and carries a row
8862
+ * count (`isQuery`).
8858
8863
  */
8859
- function subscribeQueryLikeChannel(channelName, getSpanOptions, { deferStreamedResult = false } = {}) {
8864
+ function subscribeQueryLikeChannel(channelName, getSpanOptions, { isQuery = false } = {}) {
8860
8865
  bindTracingChannelToSpan(tracingChannel(channelName), (data) => {
8861
8866
  data._sentryCallerScope = (0, _sentry_core.getCurrentScope)();
8862
8867
  const spanOptions = getSpanOptions(data);
@@ -8868,15 +8873,24 @@ function subscribeQueryLikeChannel(channelName, getSpanOptions, { deferStreamedR
8868
8873
  [_sentry_conventions_attributes.SENTRY_KIND]: "client"
8869
8874
  }
8870
8875
  });
8871
- }, deferStreamedResult ? { deferSpanEnd({ data, end }) {
8872
- const result = data.result;
8873
- if (!result || typeof result !== "object" || !hasOnMethod(result)) return false;
8874
- const callerScope = data._sentryCallerScope;
8875
- if (callerScope) (0, _sentry_core.bindScopeToEmitter)(result, callerScope);
8876
- result.on("error", (err) => end(err));
8877
- result.on("end", () => end());
8878
- return true;
8879
- } } : void 0);
8876
+ }, isQuery ? {
8877
+ beforeSpanEnd(span, data) {
8878
+ if ("error" in data || (0, _sentry_core.isObjectLike)(data.result) && hasOnMethod(data.result)) return;
8879
+ setReturnedRows(span, data.result);
8880
+ },
8881
+ deferSpanEnd({ span, data, end }) {
8882
+ const result = data.result;
8883
+ if (!result || typeof result !== "object" || !hasOnMethod(result)) return false;
8884
+ const callerScope = data._sentryCallerScope;
8885
+ if (callerScope) (0, _sentry_core.bindScopeToEmitter)(result, callerScope);
8886
+ result.on("error", (err) => end(err));
8887
+ result.on("end", (endResult) => {
8888
+ if (!hasReadMethod(result)) setReturnedRows(span, endResult);
8889
+ end();
8890
+ });
8891
+ return true;
8892
+ }
8893
+ } : void 0);
8880
8894
  }
8881
8895
  function querySpanOptions(ctx, { origin, connectionString }) {
8882
8896
  const params = ctx.self?.connectionParameters ?? {};
@@ -8916,6 +8930,23 @@ function poolConnectSpanOptions(ctx) {
8916
8930
  function hasOnMethod(obj) {
8917
8931
  return "on" in obj && typeof obj.on === "function";
8918
8932
  }
8933
+ function hasReadMethod(obj) {
8934
+ return "read" in obj && typeof obj.read === "function";
8935
+ }
8936
+ /**
8937
+ * Sets `db.response.returned_rows` from a pg-style result (`{ rowCount, rows }`), or an array of them
8938
+ * (one per statement of a multi-statement query), summed. Sets nothing when no count is known.
8939
+ */
8940
+ function setReturnedRows(span, results) {
8941
+ const counts = (Array.isArray(results) ? results : [results]).map(getRowCount).filter((count) => count !== void 0);
8942
+ if (counts.length) span.setAttribute(_sentry_conventions_attributes.DB_RESPONSE_RETURNED_ROWS, counts.reduce((sum, count) => sum + count, 0));
8943
+ }
8944
+ function getRowCount(result) {
8945
+ if (!(0, _sentry_core.isObjectLike)(result)) return;
8946
+ const { rowCount, rows } = result;
8947
+ if (typeof rowCount === "number") return rowCount;
8948
+ return Array.isArray(rows) ? rows.length : void 0;
8949
+ }
8919
8950
  function extractQueryConfig(args) {
8920
8951
  const arg0 = args[0];
8921
8952
  if (typeof arg0 === "string") return { text: arg0 };
@@ -9209,7 +9240,11 @@ function instrumentNeon() {
9209
9240
  const span = (0, _sentry_core.startInactiveSpan)(httpQuerySpanOptions(data.arguments));
9210
9241
  httpQuerySpans.add(span);
9211
9242
  return span;
9212
- });
9243
+ }, { beforeSpanEnd(span, data) {
9244
+ if ("error" in data) return;
9245
+ const { result } = data;
9246
+ setReturnedRows(span, Array.isArray(data.arguments[0]) && Array.isArray(result) ? result.map(toPgResult) : toPgResult(result));
9247
+ } });
9213
9248
  tracingChannel(CHANNELS.NEON_RESOLVE_CONNECTION).subscribe({
9214
9249
  start: NOOP$2,
9215
9250
  end: NOOP$2,
@@ -9251,6 +9286,9 @@ function httpQuerySpanOptions(args) {
9251
9286
  attributes
9252
9287
  };
9253
9288
  }
9289
+ function toPgResult(result) {
9290
+ return Array.isArray(result) ? { rows: result } : result;
9291
+ }
9254
9292
  function getBatchOperationName(statements) {
9255
9293
  const operations = new Set(statements.map((s) => s[_sentry_conventions_attributes.DB_OPERATION_NAME]?.toUpperCase()));
9256
9294
  const [operation] = operations;
@@ -9672,7 +9710,6 @@ function mapObjectValues(record, fn) {
9672
9710
  return Object.fromEntries(Object.entries(record).map(([key, value]) => [key, (0, _sentry_core.isObjectLike)(value) ? fn(value, key) : value]));
9673
9711
  }
9674
9712
  // src/integrations/vercel-ai/extract.ts
9675
- /** Maps the AI SDK `experimental_extract` input and output to the Clairic shape of the `gen_ai.extract` schemas. */
9676
9713
  /**
9677
9714
  * Joins the text of the AI SDK state parts into one Clairic `state` string. The gateway does this join on
9678
9715
  * the server and does not show its separator, so this one is our choice. It only makes the recorded text
@@ -9733,7 +9770,7 @@ function toExtractAnswers(answers, parts) {
9733
9770
  return mapObjectValues(answers, ({ pNull, source, items, ...rest }) => {
9734
9771
  const mapped = {
9735
9772
  ...rest,
9736
- p_null: pNull
9773
+ p_null: asNumber(pNull)
9737
9774
  };
9738
9775
  const span = toExtractSpan(source, parts);
9739
9776
  if (span) {