autotel-adapters 3.0.2 → 3.1.1

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/README.md CHANGED
@@ -112,9 +112,24 @@ export const POST = withAutotel(async (request: Request) => {
112
112
  });
113
113
  ```
114
114
 
115
+ `withAutotel()` is for route handlers: it reads its first argument as the
116
+ request. A server action has no request (its first argument is its form data
117
+ or its own argument), so wrap it with `withAutotelAction()` instead. The span
118
+ is `action.<name>`, the wide event carries no HTTP fields, and `useLogger()`
119
+ works inside:
120
+
121
+ ```ts
122
+ 'use server';
123
+ import { withAutotelAction, useLogger } from 'autotel-adapters/next';
124
+
125
+ export const save = withAutotelAction('save', async (formData: FormData) => {
126
+ useLogger().set({ 'item.id': formData.get('id') });
127
+ });
128
+ ```
129
+
115
130
  Navigation helpers such as `redirect()` and `notFound()` remain framework
116
- control flow: `withAutotel()` rethrows their signals without recording a
117
- phantom request error, including when a signal is wrapped in `cause`.
131
+ control flow: both wrappers rethrow their signals without recording a
132
+ phantom error, including when a signal is wrapped in `cause`.
118
133
 
119
134
  ### `next.config`: one `@opentelemetry/api`
120
135
 
package/dist/next.cjs CHANGED
@@ -63,13 +63,20 @@ const integration = require_integration.defineFrameworkIntegration({
63
63
  }),
64
64
  attachLogger: () => {}
65
65
  });
66
+ function asRequest(value) {
67
+ return typeof value === "object" && value !== null && "url" in value && "headers" in value ? value : void 0;
68
+ }
66
69
  function resolveSpanName(request, options) {
67
70
  if (typeof options?.spanName === "function") return options.spanName(request);
68
71
  return options?.spanName;
69
72
  }
73
+ /**
74
+ * Trace a route handler. Its first argument is read as the request. For a
75
+ * server action use {@link withAutotelAction}: it has no request.
76
+ */
70
77
  function withAutotel(handler, options) {
71
78
  return async (...args) => {
72
- const request = args[0];
79
+ const request = asRequest(args[0]);
73
80
  const tracedOptions = require_integration.buildTracedOptions(options, resolveSpanName(request, options));
74
81
  return integration.runTraced(request, tracedOptions, async (handle) => require_integration.runWithIntegratedHandle(handle, {
75
82
  autoEmit: tracedOptions.autoEmit,
@@ -84,6 +91,40 @@ function withAutotel(handler, options) {
84
91
  }), { isError: isRealNextError });
85
92
  };
86
93
  }
94
+ /**
95
+ * Trace a server action: span `action.<name>`, no HTTP attributes (an action
96
+ * has no request of its own), `useLogger()` inside it, and one wide event
97
+ * when it settles. `redirect()` and `notFound()` pass through as non-errors.
98
+ *
99
+ * @example
100
+ * ```ts
101
+ * 'use server';
102
+ * export const save = withAutotelAction('save', async (formData: FormData) => {
103
+ * useLogger().set({ 'item.id': formData.get('id') });
104
+ * });
105
+ * ```
106
+ */
107
+ function withAutotelAction(name, action, options) {
108
+ return (...args) => autotel.trace.run({
109
+ name: `action.${name}`,
110
+ isError: isRealNextError
111
+ }, async (ctx) => {
112
+ const log = (0, autotel.getRequestLogger)(ctx, options?.requestLoggerOptions);
113
+ const startedAt = Date.now();
114
+ try {
115
+ return await nextLoggerStorage.run(log, async () => action(...args));
116
+ } catch (error) {
117
+ const signal = findNextControlFlowSignal(error);
118
+ if (!signal) {
119
+ const err = error instanceof Error ? error : new Error(String(error));
120
+ log.error(err, { error_message: err.message });
121
+ }
122
+ throw signal ?? error;
123
+ } finally {
124
+ log.emitNow({ duration_ms: Date.now() - startedAt });
125
+ }
126
+ });
127
+ }
87
128
 
88
129
  //#endregion
89
130
  Object.defineProperty(exports, "createDrainPipeline", {
@@ -105,4 +146,5 @@ Object.defineProperty(exports, "parseError", {
105
146
  }
106
147
  });
107
148
  exports.useLogger = useLogger;
108
- exports.withAutotel = withAutotel;
149
+ exports.withAutotel = withAutotel;
150
+ exports.withAutotelAction = withAutotelAction;
package/dist/next.d.cts CHANGED
@@ -14,6 +14,26 @@ export interface NextWithAutotelOptions extends Omit<FrameworkHandlerOptions, 's
14
14
  enrichRequest?: (request?: NextRequestLike) => Record<string, unknown> | undefined;
15
15
  }
16
16
  export declare function useLogger(request?: NextRequestLike, requestLoggerOptions?: RequestLoggerOptions): RequestLogger;
17
+ /**
18
+ * Trace a route handler. Its first argument is read as the request. For a
19
+ * server action use {@link withAutotelAction}: it has no request.
20
+ */
17
21
  export declare function withAutotel<TArgs extends unknown[], TReturn>(handler: (...args: TArgs) => TReturn | Promise<TReturn>, options?: NextWithAutotelOptions): (...args: TArgs) => Promise<TReturn>;
22
+ /**
23
+ * Trace a server action: span `action.<name>`, no HTTP attributes (an action
24
+ * has no request of its own), `useLogger()` inside it, and one wide event
25
+ * when it settles. `redirect()` and `notFound()` pass through as non-errors.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * 'use server';
30
+ * export const save = withAutotelAction('save', async (formData: FormData) => {
31
+ * useLogger().set({ 'item.id': formData.get('id') });
32
+ * });
33
+ * ```
34
+ */
35
+ export declare function withAutotelAction<TArgs extends unknown[], TReturn>(name: string, action: (...args: TArgs) => TReturn | Promise<TReturn>, options?: {
36
+ requestLoggerOptions?: RequestLoggerOptions;
37
+ }): (...args: TArgs) => Promise<TReturn>;
18
38
  //#endregion
19
39
  export { createDrainPipeline, createStructuredError, parseError };
package/dist/next.d.ts CHANGED
@@ -14,6 +14,26 @@ export interface NextWithAutotelOptions extends Omit<FrameworkHandlerOptions, 's
14
14
  enrichRequest?: (request?: NextRequestLike) => Record<string, unknown> | undefined;
15
15
  }
16
16
  export declare function useLogger(request?: NextRequestLike, requestLoggerOptions?: RequestLoggerOptions): RequestLogger;
17
+ /**
18
+ * Trace a route handler. Its first argument is read as the request. For a
19
+ * server action use {@link withAutotelAction}: it has no request.
20
+ */
17
21
  export declare function withAutotel<TArgs extends unknown[], TReturn>(handler: (...args: TArgs) => TReturn | Promise<TReturn>, options?: NextWithAutotelOptions): (...args: TArgs) => Promise<TReturn>;
22
+ /**
23
+ * Trace a server action: span `action.<name>`, no HTTP attributes (an action
24
+ * has no request of its own), `useLogger()` inside it, and one wide event
25
+ * when it settles. `redirect()` and `notFound()` pass through as non-errors.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * 'use server';
30
+ * export const save = withAutotelAction('save', async (formData: FormData) => {
31
+ * useLogger().set({ 'item.id': formData.get('id') });
32
+ * });
33
+ * ```
34
+ */
35
+ export declare function withAutotelAction<TArgs extends unknown[], TReturn>(name: string, action: (...args: TArgs) => TReturn | Promise<TReturn>, options?: {
36
+ requestLoggerOptions?: RequestLoggerOptions;
37
+ }): (...args: TArgs) => Promise<TReturn>;
18
38
  //#endregion
19
39
  export { createDrainPipeline, createStructuredError, parseError };
package/dist/next.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { a as defineFrameworkIntegration, n as buildTracedOptions, o as runWithIntegratedHandle, t as applyLoggerEnrichment } from "./integration-D_ABolGu.js";
2
2
  import { createLoggerStorage } from "./toolkit/storage.js";
3
3
  import { createUseLogger, getHeader } from "./core.js";
4
- import { createDrainPipeline, createStructuredError, parseError } from "autotel";
4
+ import { createDrainPipeline, createStructuredError, getRequestLogger, parseError, trace } from "autotel";
5
5
  import { unstable_rethrow } from "next/navigation.js";
6
6
 
7
7
  //#region src/next.ts
@@ -62,13 +62,20 @@ const integration = defineFrameworkIntegration({
62
62
  }),
63
63
  attachLogger: () => {}
64
64
  });
65
+ function asRequest(value) {
66
+ return typeof value === "object" && value !== null && "url" in value && "headers" in value ? value : void 0;
67
+ }
65
68
  function resolveSpanName(request, options) {
66
69
  if (typeof options?.spanName === "function") return options.spanName(request);
67
70
  return options?.spanName;
68
71
  }
72
+ /**
73
+ * Trace a route handler. Its first argument is read as the request. For a
74
+ * server action use {@link withAutotelAction}: it has no request.
75
+ */
69
76
  function withAutotel(handler, options) {
70
77
  return async (...args) => {
71
- const request = args[0];
78
+ const request = asRequest(args[0]);
72
79
  const tracedOptions = buildTracedOptions(options, resolveSpanName(request, options));
73
80
  return integration.runTraced(request, tracedOptions, async (handle) => runWithIntegratedHandle(handle, {
74
81
  autoEmit: tracedOptions.autoEmit,
@@ -83,6 +90,40 @@ function withAutotel(handler, options) {
83
90
  }), { isError: isRealNextError });
84
91
  };
85
92
  }
93
+ /**
94
+ * Trace a server action: span `action.<name>`, no HTTP attributes (an action
95
+ * has no request of its own), `useLogger()` inside it, and one wide event
96
+ * when it settles. `redirect()` and `notFound()` pass through as non-errors.
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * 'use server';
101
+ * export const save = withAutotelAction('save', async (formData: FormData) => {
102
+ * useLogger().set({ 'item.id': formData.get('id') });
103
+ * });
104
+ * ```
105
+ */
106
+ function withAutotelAction(name, action, options) {
107
+ return (...args) => trace.run({
108
+ name: `action.${name}`,
109
+ isError: isRealNextError
110
+ }, async (ctx) => {
111
+ const log = getRequestLogger(ctx, options?.requestLoggerOptions);
112
+ const startedAt = Date.now();
113
+ try {
114
+ return await nextLoggerStorage.run(log, async () => action(...args));
115
+ } catch (error) {
116
+ const signal = findNextControlFlowSignal(error);
117
+ if (!signal) {
118
+ const err = error instanceof Error ? error : new Error(String(error));
119
+ log.error(err, { error_message: err.message });
120
+ }
121
+ throw signal ?? error;
122
+ } finally {
123
+ log.emitNow({ duration_ms: Date.now() - startedAt });
124
+ }
125
+ });
126
+ }
86
127
 
87
128
  //#endregion
88
- export { createDrainPipeline, createStructuredError, parseError, useLogger, withAutotel };
129
+ export { createDrainPipeline, createStructuredError, parseError, useLogger, withAutotel, withAutotelAction };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autotel-adapters",
3
- "version": "3.0.2",
3
+ "version": "3.1.1",
4
4
  "description": "Framework adapters and composable DX helpers for autotel",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -91,8 +91,8 @@
91
91
  "NOTICE"
92
92
  ],
93
93
  "dependencies": {
94
- "autotel": "8.6.1",
95
- "autotel-edge": "6.1.3"
94
+ "autotel": "8.7.0",
95
+ "autotel-edge": "6.1.4"
96
96
  },
97
97
  "peerDependencies": {
98
98
  "hono": ">=4.13.13",