@tracepanel/js 0.5.0 → 0.7.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 (92) hide show
  1. package/README.md +265 -14
  2. package/dist/agent.d.ts +5 -1
  3. package/dist/agent.js +12 -3
  4. package/dist/agent.js.map +1 -1
  5. package/dist/aws.d.ts +27 -0
  6. package/dist/aws.js +368 -0
  7. package/dist/aws.js.map +1 -0
  8. package/dist/browser.d.ts +4 -1
  9. package/dist/browser.js +33 -3
  10. package/dist/browser.js.map +1 -1
  11. package/dist/bun.js +6 -82
  12. package/dist/bun.js.map +1 -1
  13. package/dist/calls.d.ts +5 -0
  14. package/dist/calls.js +7 -0
  15. package/dist/calls.js.map +1 -1
  16. package/dist/config.d.ts +7 -0
  17. package/dist/config.js +2 -0
  18. package/dist/config.js.map +1 -1
  19. package/dist/explain.d.ts +5 -0
  20. package/dist/explain.js +56 -7
  21. package/dist/explain.js.map +1 -1
  22. package/dist/express.js +43 -3
  23. package/dist/express.js.map +1 -1
  24. package/dist/fastify.d.ts +2 -0
  25. package/dist/fastify.js +100 -0
  26. package/dist/fastify.js.map +1 -0
  27. package/dist/frameworks.js +3 -0
  28. package/dist/frameworks.js.map +1 -1
  29. package/dist/graphql.d.ts +39 -0
  30. package/dist/graphql.js +337 -0
  31. package/dist/graphql.js.map +1 -0
  32. package/dist/hono.d.ts +2 -0
  33. package/dist/hono.js +63 -0
  34. package/dist/hono.js.map +1 -0
  35. package/dist/koa.d.ts +2 -0
  36. package/dist/koa.js +119 -0
  37. package/dist/koa.js.map +1 -0
  38. package/dist/llm.d.ts +20 -0
  39. package/dist/llm.js +632 -0
  40. package/dist/llm.js.map +1 -0
  41. package/dist/memory.d.ts +69 -0
  42. package/dist/memory.js +203 -0
  43. package/dist/memory.js.map +1 -0
  44. package/dist/model.d.ts +22 -1
  45. package/dist/model.js.map +1 -1
  46. package/dist/next.js +4 -4
  47. package/dist/next.js.map +1 -1
  48. package/dist/processor.d.ts +50 -5
  49. package/dist/processor.js +193 -30
  50. package/dist/processor.js.map +1 -1
  51. package/dist/profile.d.ts +34 -8
  52. package/dist/profile.js +98 -13
  53. package/dist/profile.js.map +1 -1
  54. package/dist/propagation.d.ts +12 -0
  55. package/dist/propagation.js +48 -0
  56. package/dist/propagation.js.map +1 -0
  57. package/dist/queries.d.ts +20 -0
  58. package/dist/queries.js +75 -0
  59. package/dist/queries.js.map +1 -0
  60. package/dist/respond.d.ts +12 -0
  61. package/dist/respond.js +85 -0
  62. package/dist/respond.js.map +1 -0
  63. package/dist/setup.js +27 -6
  64. package/dist/setup.js.map +1 -1
  65. package/dist/sql.d.ts +1 -0
  66. package/dist/sql.js +11 -2
  67. package/dist/sql.js.map +1 -1
  68. package/dist/sqlite.d.ts +13 -0
  69. package/dist/sqlite.js +291 -0
  70. package/dist/sqlite.js.map +1 -0
  71. package/dist/stack.d.ts +3 -1
  72. package/dist/stack.js +30 -6
  73. package/dist/stack.js.map +1 -1
  74. package/dist/testing.d.ts +128 -0
  75. package/dist/testing.js +362 -0
  76. package/dist/testing.js.map +1 -0
  77. package/dist/transactions.d.ts +11 -1
  78. package/dist/transactions.js +70 -34
  79. package/dist/transactions.js.map +1 -1
  80. package/dist/view.d.ts +17 -0
  81. package/dist/view.js +71 -0
  82. package/dist/view.js.map +1 -0
  83. package/dist/vitest-setup.d.ts +1 -0
  84. package/dist/vitest-setup.js +63 -0
  85. package/dist/vitest-setup.js.map +1 -0
  86. package/dist/vitest.d.ts +57 -0
  87. package/dist/vitest.js +130 -0
  88. package/dist/vitest.js.map +1 -0
  89. package/dist/widget.d.ts +16 -0
  90. package/dist/widget.js +42 -1
  91. package/dist/widget.js.map +1 -1
  92. package/package.json +81 -4
package/dist/koa.js ADDED
@@ -0,0 +1,119 @@
1
+ // `app.use(tracepanel())`, first: Koa middleware. The handler of each @koa/router route is a span of its own, named
2
+ // by its route (`/books/:id`), and so is the request's trace. Every response gets what the badge reads
3
+ // (Tracepanel-Id, Server-Timing), an error's answer too, and an HTML page the widget's script: a string or a Buffer
4
+ // has it put into its body, a stream as it passes. What the request carried goes into its trace as the response goes
5
+ // out: its headers, query, cookies, the body a body parser read (`ctx.request.body`, its files), the session
6
+ // (`ctx.session`), and the response's headers. An error is the request's unless Koa answers it below 500 (`ctx.throw(404)`).
7
+ //
8
+ // The handlers are found from the app's middleware, as the request comes: the routers it mounted (`router.routes()`,
9
+ // and the routers nested in them), each route's last function its handler. OpenTelemetry's Koa instrumentation does
10
+ // the same from Koa's `use()`, once, with a span for each middleware besides; here the routes added later count too.
11
+ import { hasBody, parsedBody, requestAttrs, responseHeaders, searchOf } from "./request.js";
12
+ import { runtime } from "./runtime.js";
13
+ import { expressSession } from "./session.js";
14
+ import { inView, statusOf, wasRecorded } from "./view.js";
15
+ import { inject, responseMarks, streamablePage, streamWith } from "./widget.js";
16
+ const FRAMEWORK = "Koa";
17
+ const TRACED = Symbol.for("tracepanel.koa.handler");
18
+ // How many layers each router had when its handlers were traced: a route added since is traced as it comes.
19
+ const seen = new WeakMap();
20
+ /** Each route's handler in a span, in every router among these middleware, and in the routers nested in them. */
21
+ function traceRoutes(middleware) {
22
+ for (const fn of middleware) {
23
+ const router = fn.router;
24
+ if (!router || !Array.isArray(router.stack) || seen.get(router) === router.stack.length)
25
+ continue;
26
+ seen.set(router, router.stack.length);
27
+ for (const layer of router.stack) {
28
+ if (!Array.isArray(layer.stack))
29
+ continue;
30
+ traceRoutes(layer.stack);
31
+ const last = layer.stack.length - 1;
32
+ const handler = layer.stack[last];
33
+ // A route's own layer has its methods; router.use()'s middleware has none.
34
+ if (!handler || handler.router || handler[TRACED] || layer.methods.length === 0)
35
+ continue;
36
+ const route = String(layer.path);
37
+ const traced = (ctx, next) => inView(route, null, () => handler(ctx, next));
38
+ traced[TRACED] = true;
39
+ layer.stack[last] = traced;
40
+ }
41
+ }
42
+ }
43
+ function isStream(body) {
44
+ return body !== null && typeof body === "object" && typeof body.pipe === "function";
45
+ }
46
+ /** What the request carried, the body a parser read, the session as the handler left it, the response's headers. */
47
+ function carried(ctx) {
48
+ const { request, session } = ctx;
49
+ const body = hasBody(ctx.headers)
50
+ ? parsedBody({ contentType: ctx.get("content-type"), body: request.body, files: request.files })
51
+ : {};
52
+ return {
53
+ ...requestAttrs(ctx.headers, searchOf(ctx.originalUrl ?? ctx.url)),
54
+ ...body,
55
+ ...expressSession(session),
56
+ ...responseHeaders(Object.entries(ctx.response.headers)),
57
+ };
58
+ }
59
+ /** The route @koa/router matched, the request's name. */
60
+ function named(tp, ctx) {
61
+ const route = ctx._matchedRoute;
62
+ if (route !== undefined)
63
+ tp.processor.setRoute(String(route));
64
+ }
65
+ export function tracepanel() {
66
+ const tp = runtime();
67
+ return async function tracepanelMiddleware(ctx, next) {
68
+ if (!tp) {
69
+ await next();
70
+ return;
71
+ }
72
+ tp.processor.setFramework(FRAMEWORK);
73
+ const startedMs = performance.now();
74
+ traceRoutes(ctx.app.middleware);
75
+ const badge = (status, page) => responseMarks(tp, { method: ctx.method, status, startedMs, page, framework: tp.processor.framework() ?? FRAMEWORK }, ctx.headers, (name) => ctx.response.get(name) || undefined);
76
+ try {
77
+ await next();
78
+ }
79
+ catch (error) {
80
+ named(tp, ctx);
81
+ if (statusOf(error) >= 500 && !wasRecorded(error))
82
+ tp.processor.addError(error);
83
+ if (tp.config.captureRequest)
84
+ tp.processor.setTraceAttrs(carried(ctx));
85
+ // Koa answers it once it is out of here, its headers cleared but for the error's own: the badge's go with them.
86
+ const marks = badge(statusOf(error), false);
87
+ if (marks && error !== null && typeof error === "object") {
88
+ const own = error.headers;
89
+ const headers = { ...own, ...marks.headers };
90
+ if (marks.cookie)
91
+ headers["Set-Cookie"] = marks.cookie;
92
+ Object.assign(error, { headers });
93
+ }
94
+ throw error;
95
+ }
96
+ named(tp, ctx);
97
+ if (tp.config.captureRequest)
98
+ tp.processor.setTraceAttrs(carried(ctx));
99
+ const { body } = ctx;
100
+ const header = (name) => ctx.response.get(name) || undefined;
101
+ const html = Boolean(ctx.response.is("html")) && !header("content-encoding");
102
+ const whole = typeof body === "string" || Buffer.isBuffer(body);
103
+ const page = html && (whole || (isStream(body) && streamablePage(header)));
104
+ const marks = badge(ctx.status, page);
105
+ if (!marks)
106
+ return;
107
+ ctx.set(marks.headers);
108
+ if (marks.cookie)
109
+ ctx.append("Set-Cookie", marks.cookie);
110
+ if (!page || !marks.script)
111
+ return;
112
+ // Koa sets the new body's Content-Length, and leaves a stream's out.
113
+ if (isStream(body))
114
+ ctx.body = streamWith(body, marks.script);
115
+ else
116
+ ctx.body = inject(Buffer.isBuffer(body) ? body.toString("utf8") : body, marks.script);
117
+ };
118
+ }
119
+ //# sourceMappingURL=koa.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"koa.js","sourceRoot":"","sources":["../src/koa.ts"],"names":[],"mappings":"AAAA,oHAAoH;AACpH,uGAAuG;AACvG,oHAAoH;AACpH,qHAAqH;AACrH,6GAA6G;AAC7G,6HAA6H;AAC7H,EAAE;AACF,qHAAqH;AACrH,oHAAoH;AACpH,qHAAqH;AAKrH,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAC5F,OAAO,EAAgB,OAAO,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAC1D,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEhF,MAAM,SAAS,GAAG,KAAK,CAAC;AACxB,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC;AAYpD,4GAA4G;AAC5G,MAAM,IAAI,GAAG,IAAI,OAAO,EAAkB,CAAC;AAE3C,iHAAiH;AACjH,SAAS,WAAW,CAA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AM,CAAC,CAAC;QACzD,IAAI,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM;YAAE,OAAO;QACnC,qEAAqE;QACrE,IAAI,QAAQ,CAAC,IAAI,CAAC;YAAE,GAAG,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;;YACzD,GAAG,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAE,IAAe,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACzG,CAAC,CAAC;AACJ,CAAC"}
package/dist/llm.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { InstrumentationBase, InstrumentationNodeModuleDefinition } from "@opentelemetry/instrumentation";
2
+ /** The span kind of an LLM call, in `tracepanel.span_kind`. */
3
+ export declare const KIND = "llm";
4
+ /** The instrumentation scope of Anthropic's SDK's own spans: the processor leaves them out. */
5
+ export declare const ANTHROPIC_SCOPE = "com.anthropic.sdk";
6
+ /** OpenAI's and Anthropic's clients, their API calls as LLM call spans. */
7
+ export declare class LlmInstrumentation extends InstrumentationBase {
8
+ constructor();
9
+ protected init(): InstrumentationNodeModuleDefinition[];
10
+ private patch;
11
+ }
12
+ type Fetch = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
13
+ /**
14
+ * fetch(), its calls to a model's API (a POST of JSON with a model to a known endpoint) as LLM call spans: what the
15
+ * AI SDK's providers and other clients of these APIs send. One an SDK's client makes is that call's already.
16
+ */
17
+ export declare function llmFetch<F extends Fetch>(fetch: F): F;
18
+ /** globalThis.fetch, its LLM calls traced, once per process. */
19
+ export declare function traceLlmFetch(): void;
20
+ export {};