traza-log 0.1.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.
- package/README.md +135 -0
- package/dist/index.cjs +735 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +724 -0
- package/dist/index.js.map +1 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# traza
|
|
2
|
+
|
|
3
|
+
SDK ligero de tracing y logging para Node, navegador y edge runtimes. Envía eventos y spans al dashboard de **Traza** sin bloquear el flujo principal de tu aplicación.
|
|
4
|
+
|
|
5
|
+
- `traza.log / info / warn / error / debug` — drop-in replacement de `console`.
|
|
6
|
+
- `traza.startSpan`, `traza.time`, `traza.instrument` — mide latencia de queries, peticiones HTTP, o cualquier bloque async.
|
|
7
|
+
- `traza.middleware()` — instrumenta cada request HTTP automáticamente.
|
|
8
|
+
- Cola en memoria + batching HTTP + reintentos con backoff: **nunca bloquea**, descarta antes que retrasar.
|
|
9
|
+
- Compatible con Node 18+, Bun, Deno, Cloudflare Workers, Vercel Edge, navegadores modernos, React Native / Capacitor / Ionic.
|
|
10
|
+
|
|
11
|
+
## Instalación
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add traza # o npm i traza / yarn add traza / bun add traza
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Configuración
|
|
18
|
+
|
|
19
|
+
Solo necesitas el token. El endpoint por defecto es **`https://traza.gd.pe`** — pásalo explícitamente solo si self-hosteas.
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { traza } from 'traza'
|
|
23
|
+
|
|
24
|
+
traza.configure({
|
|
25
|
+
token: process.env.TRAZA_TOKEN!, // generado en el dashboard
|
|
26
|
+
service: 'api', // opcional
|
|
27
|
+
environment: process.env.NODE_ENV, // opcional
|
|
28
|
+
release: process.env.GIT_SHA, // opcional
|
|
29
|
+
defaultAttributes: { region: 'lima' }, // se adjunta a todo evento
|
|
30
|
+
// endpoint: 'https://tu-traza.miempresa.com', // solo si self-hosteas
|
|
31
|
+
flushIntervalMs: 3000, // default
|
|
32
|
+
maxBatchSize: 100, // default
|
|
33
|
+
maxQueueSize: 10000, // default
|
|
34
|
+
sampleRate: 1, // 0..1
|
|
35
|
+
forwardToConsole: true, // imprime también en consola local
|
|
36
|
+
})
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Logs
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
traza.log('Hola mundo')
|
|
43
|
+
traza.info('usuario autenticado', { userId: 42 })
|
|
44
|
+
traza.warn('consulta lenta', { ms: 1200 })
|
|
45
|
+
traza.error('fallo en checkout', { err })
|
|
46
|
+
traza.debug('cache miss', { key: 'user:42' })
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Medir rendimiento
|
|
50
|
+
|
|
51
|
+
Tres formas, elige la que te quede más natural:
|
|
52
|
+
|
|
53
|
+
### 1. Span manual
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
const span = traza.startSpan('db.query', { attributes: { table: 'users' } })
|
|
57
|
+
try {
|
|
58
|
+
const rows = await db.select().from(users)
|
|
59
|
+
span.setAttribute('rowCount', rows.length)
|
|
60
|
+
span.end({ status: 'ok' })
|
|
61
|
+
} catch (err) {
|
|
62
|
+
span.end({ status: 'error', error: err })
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 2. `time()` — wrap automático
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
const users = await traza.time('db.users', () => db.select().from(usersTable))
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 3. `instrument()` — para funciones reutilizables
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
const getUser = traza.instrument(
|
|
76
|
+
'db.getUser',
|
|
77
|
+
async (id: string) => db.query.users.findFirst({ where: eq(users.id, id) }),
|
|
78
|
+
(id) => ({ userId: id }),
|
|
79
|
+
)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Middleware HTTP
|
|
83
|
+
|
|
84
|
+
### Express / Connect / Polka
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import express from 'express'
|
|
88
|
+
import { traza } from 'traza'
|
|
89
|
+
|
|
90
|
+
const app = express()
|
|
91
|
+
app.use(traza.middleware())
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Nuxt 3 / Nitro / H3
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
// server/middleware/traza.ts
|
|
98
|
+
import { traza } from 'traza'
|
|
99
|
+
|
|
100
|
+
export default defineEventHandler((event) => {
|
|
101
|
+
return traza.h3((e) => e)(event)
|
|
102
|
+
})
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Cada request HTTP genera una span con `http.method`, `http.url`, `http.status_code` y propaga el `traceId` a todos los logs y spans hijos.
|
|
106
|
+
|
|
107
|
+
## Contexto
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
traza.runWithContext({ attributes: { requestId } }, async () => {
|
|
111
|
+
traza.info('start') // incluye requestId
|
|
112
|
+
await traza.time('work', work)
|
|
113
|
+
})
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Comportamiento no bloqueante
|
|
117
|
+
|
|
118
|
+
- Los eventos se acumulan en una cola en memoria (max 10 000 por defecto).
|
|
119
|
+
- Se envían al dashboard cada `flushIntervalMs` o cuando la cola alcanza `maxBatchSize`.
|
|
120
|
+
- Si la red falla: reintentos con backoff exponencial (200 ms → 5 s, 3 intentos).
|
|
121
|
+
- Si la cola se llena: se descartan los eventos **más antiguos**, el código de aplicación nunca espera.
|
|
122
|
+
- Hooks de shutdown (`beforeExit` en Node, `pagehide` en navegador) hacen flush final.
|
|
123
|
+
|
|
124
|
+
## Apagar limpiamente
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
process.on('SIGTERM', async () => {
|
|
128
|
+
await traza.shutdown()
|
|
129
|
+
process.exit(0)
|
|
130
|
+
})
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Licencia
|
|
134
|
+
|
|
135
|
+
MIT.
|