lopata 0.5.1 → 0.5.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lopata",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/plugin.ts CHANGED
@@ -15,7 +15,21 @@ import { globalEnv } from './env'
15
15
  import { getActiveExecutionContext } from './execution-context'
16
16
  import { getActiveContext } from './tracing/context'
17
17
  import { instrumentBinding } from './tracing/instrument'
18
- import { addSpanEvent, setSpanAttribute, startSpan } from './tracing/span'
18
+ import { addSpanEvent, persistError, setSpanAttribute, startSpan } from './tracing/span' // ─── Userland tracing API ────────────────────────────────────────────
19
+ // Exposes a lightweight global that user code can call to create custom
20
+ // spans visible in the Lopata dashboard. In production (without Lopata)
21
+ // the global is simply absent, so the user's thin wrapper becomes a no-op.
22
+ ;(globalThis as any).__lopata = {
23
+ trace<T>(name: string, attrsOrFn: Record<string, unknown> | (() => T | Promise<T>), maybeFn?: () => T | Promise<T>): Promise<T> {
24
+ const fn = typeof attrsOrFn === 'function' ? attrsOrFn : maybeFn!
25
+ const attributes = typeof attrsOrFn === 'function' ? undefined : attrsOrFn
26
+ return startSpan({ name, attributes }, fn)
27
+ },
28
+ setAttribute: setSpanAttribute,
29
+ addEvent(name: string, message?: string, attrs?: Record<string, unknown>): void {
30
+ addSpanEvent(name, 'info', message ?? '', attrs)
31
+ },
32
+ }
19
33
 
20
34
  // Register global `caches` object (CacheStorage) with tracing
21
35
  const rawCacheStorage = new SqliteCacheStorage(getDatabase())
@@ -151,6 +165,10 @@ for (const method of consoleMethods) {
151
165
  if (!ctx) return
152
166
  const message = args.map(formatConsoleArg).join(' ')
153
167
  addSpanEvent(`console.${method}`, method, message)
168
+ if (method === 'error') {
169
+ const errorArg = args.find((a) => a instanceof Error)
170
+ persistError(errorArg ?? new Error(message), 'console.error')
171
+ }
154
172
  }
155
173
  }
156
174
 
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Lopata userland tracing API.
3
+ *
4
+ * Available on `globalThis.__lopata` when running under Lopata dev server.
5
+ * In production (Cloudflare Workers) the global is `undefined` — wrap calls
6
+ * in a thin helper that falls back to a no-op:
7
+ *
8
+ * ```ts
9
+ * // app/lib/trace.ts
10
+ * type TraceFn = <T>(name: string, fn: () => T | Promise<T>) => Promise<T>
11
+ *
12
+ * export const trace: TraceFn = (name, fn) =>
13
+ * globalThis.__lopata?.trace(name, fn) ?? fn()
14
+ *
15
+ * export const setAttribute = (key: string, value: unknown) =>
16
+ * globalThis.__lopata?.setAttribute(key, value)
17
+ *
18
+ * export const addEvent = (name: string, message?: string) =>
19
+ * globalThis.__lopata?.addEvent(name, message)
20
+ * ```
21
+ *
22
+ * Add this file to your tsconfig types to get autocomplete:
23
+ * ```json
24
+ * { "compilerOptions": { "types": ["lopata/src/tracing/global"] } }
25
+ * ```
26
+ */
27
+
28
+ interface LopataTracing {
29
+ /**
30
+ * Create a traced span around `fn`. The span is visible in the Lopata
31
+ * dashboard and becomes a child of the currently active span (if any).
32
+ */
33
+ trace<T>(name: string, fn: () => T | Promise<T>): Promise<T>
34
+ /**
35
+ * Create a traced span with custom attributes.
36
+ */
37
+ trace<T>(name: string, attrs: Record<string, unknown>, fn: () => T | Promise<T>): Promise<T>
38
+
39
+ /**
40
+ * Set an attribute on the currently active span.
41
+ */
42
+ setAttribute(key: string, value: unknown): void
43
+
44
+ /**
45
+ * Add an event (log entry) to the currently active span.
46
+ */
47
+ addEvent(name: string, message?: string, attrs?: Record<string, unknown>): void
48
+ }
49
+
50
+ declare var __lopata: LopataTracing | undefined