@victor-studio/monitor 0.1.0 → 0.2.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/dist/index.cjs +181 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +104 -39
- package/dist/index.d.ts +104 -39
- package/dist/index.js +181 -36
- package/dist/index.js.map +1 -1
- package/dist/next/index.cjs +31 -12
- package/dist/next/index.cjs.map +1 -1
- package/dist/next/index.d.cts +110 -45
- package/dist/next/index.d.ts +110 -45
- package/dist/next/index.js +31 -12
- package/dist/next/index.js.map +1 -1
- package/dist/react/index.cjs +20 -1
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +99 -38
- package/dist/react/index.d.ts +99 -38
- package/dist/react/index.js +20 -1
- package/dist/react/index.js.map +1 -1
- package/package.json +12 -7
package/dist/index.cjs
CHANGED
|
@@ -1,23 +1,73 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
// src/core/transport.ts
|
|
4
|
-
|
|
4
|
+
var DEFAULT_TIMEOUT = 1e4;
|
|
5
|
+
var DEFAULT_MAX_RETRIES = 3;
|
|
6
|
+
var MAX_PAYLOAD_BYTES = 6e4;
|
|
7
|
+
async function sendBatch(config, events, logger, options) {
|
|
5
8
|
if (events.length === 0) return true;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
9
|
+
const payload = JSON.stringify({ events });
|
|
10
|
+
if (options?.useBeacon && typeof navigator !== "undefined" && navigator.sendBeacon) {
|
|
11
|
+
const beaconPayload = JSON.stringify({ apiKey: config.apiKey, events });
|
|
12
|
+
const beaconBlob = new Blob([beaconPayload], { type: "application/json" });
|
|
13
|
+
const sent = navigator.sendBeacon(config.endpoint, beaconBlob);
|
|
14
|
+
logger.debug("sendBeacon", sent ? "queued" : "failed", `${events.length} events`);
|
|
15
|
+
return sent;
|
|
16
|
+
}
|
|
17
|
+
const payloadSize = new Blob([payload]).size;
|
|
18
|
+
if (payloadSize > MAX_PAYLOAD_BYTES && events.length > 1) {
|
|
19
|
+
const mid = Math.floor(events.length / 2);
|
|
20
|
+
const [first, second] = await Promise.all([
|
|
21
|
+
sendBatch(config, events.slice(0, mid), logger),
|
|
22
|
+
sendBatch(config, events.slice(mid), logger)
|
|
23
|
+
]);
|
|
24
|
+
return first && second;
|
|
25
|
+
}
|
|
26
|
+
return sendWithRetry(config, payload, logger);
|
|
27
|
+
}
|
|
28
|
+
async function sendWithRetry(config, payload, logger) {
|
|
29
|
+
const maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
30
|
+
const timeout = config.timeout ?? DEFAULT_TIMEOUT;
|
|
31
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
32
|
+
try {
|
|
33
|
+
const controller = new AbortController();
|
|
34
|
+
const timer = setTimeout(() => controller.abort(), timeout);
|
|
35
|
+
const response = await fetch(config.endpoint, {
|
|
36
|
+
method: "POST",
|
|
37
|
+
headers: {
|
|
38
|
+
"Content-Type": "application/json",
|
|
39
|
+
"Authorization": `Bearer ${config.apiKey}`
|
|
40
|
+
},
|
|
41
|
+
body: payload,
|
|
42
|
+
signal: controller.signal,
|
|
43
|
+
keepalive: attempt === 0
|
|
44
|
+
// keepalive só na primeira tentativa
|
|
45
|
+
});
|
|
46
|
+
clearTimeout(timer);
|
|
47
|
+
if (response.ok) {
|
|
48
|
+
logger.debug("sent", `attempt ${attempt + 1}`, response.status);
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
if (response.status >= 400 && response.status < 500) {
|
|
52
|
+
logger.warn("client error", response.status, "\u2014 no retry");
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
logger.warn("server error", response.status, `attempt ${attempt + 1}/${maxRetries + 1}`);
|
|
56
|
+
} catch (err) {
|
|
57
|
+
const message = err instanceof Error ? err.message : "unknown error";
|
|
58
|
+
logger.warn("network error", message, `attempt ${attempt + 1}/${maxRetries + 1}`);
|
|
59
|
+
}
|
|
60
|
+
if (attempt < maxRetries) {
|
|
61
|
+
const delay = Math.min(1e3 * Math.pow(2, attempt), 16e3);
|
|
62
|
+
const jitter = Math.random() * 1e3;
|
|
63
|
+
await sleep(delay + jitter);
|
|
64
|
+
}
|
|
20
65
|
}
|
|
66
|
+
logger.error("all retries exhausted, dropping batch");
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
function sleep(ms) {
|
|
70
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
21
71
|
}
|
|
22
72
|
|
|
23
73
|
// src/core/collector.ts
|
|
@@ -28,9 +78,16 @@ var Collector = class {
|
|
|
28
78
|
timer = null;
|
|
29
79
|
config;
|
|
30
80
|
flushInterval;
|
|
31
|
-
|
|
81
|
+
logger;
|
|
82
|
+
beforeSend;
|
|
83
|
+
sampleRate;
|
|
84
|
+
visibilityHandler = null;
|
|
85
|
+
constructor(config, logger, options) {
|
|
32
86
|
this.config = config;
|
|
33
|
-
this.
|
|
87
|
+
this.logger = logger;
|
|
88
|
+
this.flushInterval = options?.flushInterval ?? DEFAULT_FLUSH_INTERVAL;
|
|
89
|
+
this.beforeSend = options?.beforeSend;
|
|
90
|
+
this.sampleRate = options?.sampleRate ?? 1;
|
|
34
91
|
}
|
|
35
92
|
start() {
|
|
36
93
|
if (this.timer) return;
|
|
@@ -38,11 +95,12 @@ var Collector = class {
|
|
|
38
95
|
this.flush();
|
|
39
96
|
}, this.flushInterval);
|
|
40
97
|
if (typeof window !== "undefined") {
|
|
41
|
-
|
|
98
|
+
this.visibilityHandler = () => {
|
|
42
99
|
if (document.visibilityState === "hidden") {
|
|
43
|
-
this.flush();
|
|
100
|
+
this.flush(true);
|
|
44
101
|
}
|
|
45
|
-
}
|
|
102
|
+
};
|
|
103
|
+
window.addEventListener("visibilitychange", this.visibilityHandler);
|
|
46
104
|
}
|
|
47
105
|
}
|
|
48
106
|
stop() {
|
|
@@ -50,55 +108,132 @@ var Collector = class {
|
|
|
50
108
|
clearInterval(this.timer);
|
|
51
109
|
this.timer = null;
|
|
52
110
|
}
|
|
111
|
+
if (this.visibilityHandler && typeof window !== "undefined") {
|
|
112
|
+
window.removeEventListener("visibilitychange", this.visibilityHandler);
|
|
113
|
+
this.visibilityHandler = null;
|
|
114
|
+
}
|
|
53
115
|
this.flush();
|
|
54
116
|
}
|
|
55
|
-
push(
|
|
56
|
-
this.
|
|
57
|
-
|
|
117
|
+
push(input) {
|
|
118
|
+
if (input.type !== "heartbeat" && this.sampleRate < 1) {
|
|
119
|
+
if (Math.random() >= this.sampleRate) {
|
|
120
|
+
this.logger.debug("sampled out", input.type);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const event = {
|
|
125
|
+
...input,
|
|
58
126
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
59
|
-
}
|
|
127
|
+
};
|
|
128
|
+
if (this.beforeSend) {
|
|
129
|
+
const result = this.beforeSend(event);
|
|
130
|
+
if (result === null) {
|
|
131
|
+
this.logger.debug("dropped by beforeSend", event.type);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
this.buffer.push(result);
|
|
135
|
+
} else {
|
|
136
|
+
this.buffer.push(event);
|
|
137
|
+
}
|
|
138
|
+
this.logger.debug("buffered", event.type, `(${this.buffer.length}/${MAX_BUFFER_SIZE})`);
|
|
60
139
|
if (this.buffer.length >= MAX_BUFFER_SIZE) {
|
|
61
140
|
this.flush();
|
|
62
141
|
}
|
|
63
142
|
}
|
|
64
|
-
flush()
|
|
143
|
+
/** Flush manual — exposto via MonitorClient.flush() */
|
|
144
|
+
flush(useBeacon = false) {
|
|
65
145
|
if (this.buffer.length === 0) return;
|
|
66
146
|
const events = [...this.buffer];
|
|
67
147
|
this.buffer = [];
|
|
68
|
-
|
|
148
|
+
this.logger.debug("flushing", events.length, "events", useBeacon ? "(beacon)" : "");
|
|
149
|
+
sendBatch(this.config, events, this.logger, { useBeacon }).catch(() => {
|
|
69
150
|
});
|
|
70
151
|
}
|
|
152
|
+
/** Número de eventos no buffer (para debug/testes) */
|
|
153
|
+
get pending() {
|
|
154
|
+
return this.buffer.length;
|
|
155
|
+
}
|
|
71
156
|
};
|
|
72
157
|
|
|
158
|
+
// src/core/env.ts
|
|
159
|
+
var LOCAL_HOSTNAMES = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "0.0.0.0", "[::1]"]);
|
|
160
|
+
function isDev() {
|
|
161
|
+
if (typeof process !== "undefined" && process.env?.NODE_ENV === "development") return true;
|
|
162
|
+
if (typeof process !== "undefined" && process.env?.NODE_ENV === "test") return true;
|
|
163
|
+
if (typeof window !== "undefined" && LOCAL_HOSTNAMES.has(window.location?.hostname)) return true;
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// src/core/logger.ts
|
|
168
|
+
var PREFIX = "[monitor]";
|
|
169
|
+
function createLogger(debug) {
|
|
170
|
+
if (!debug) {
|
|
171
|
+
return { debug: noop, warn: noop, error: noop };
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
debug: (...args) => console.debug(PREFIX, ...args),
|
|
175
|
+
warn: (...args) => console.warn(PREFIX, ...args),
|
|
176
|
+
error: (...args) => console.error(PREFIX, ...args)
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
function noop() {
|
|
180
|
+
}
|
|
181
|
+
|
|
73
182
|
// src/core/client.ts
|
|
74
183
|
var MonitorClient = class {
|
|
75
184
|
config;
|
|
76
185
|
collector;
|
|
186
|
+
logger;
|
|
77
187
|
heartbeatTimer = null;
|
|
78
188
|
active = false;
|
|
79
189
|
constructor(config) {
|
|
80
190
|
this.config = config;
|
|
191
|
+
this.logger = createLogger(config.debug ?? false);
|
|
81
192
|
this.collector = new Collector(
|
|
82
|
-
{
|
|
83
|
-
|
|
193
|
+
{
|
|
194
|
+
endpoint: config.endpoint,
|
|
195
|
+
apiKey: config.apiKey,
|
|
196
|
+
timeout: config.timeout,
|
|
197
|
+
maxRetries: config.maxRetries
|
|
198
|
+
},
|
|
199
|
+
this.logger,
|
|
200
|
+
{
|
|
201
|
+
flushInterval: config.flushInterval,
|
|
202
|
+
beforeSend: config.beforeSend,
|
|
203
|
+
sampleRate: config.sampleRate
|
|
204
|
+
}
|
|
84
205
|
);
|
|
85
206
|
}
|
|
86
207
|
/** Inicia o monitoring (heartbeat + collector) */
|
|
87
208
|
start() {
|
|
88
|
-
if (this.config.disableInDev !== false &&
|
|
209
|
+
if (this.config.disableInDev !== false && isDev()) {
|
|
210
|
+
this.logger.debug("disabled in dev \u2014 call start() with disableInDev: false to override");
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
89
213
|
if (this.active) return;
|
|
90
214
|
this.active = true;
|
|
91
215
|
this.collector.start();
|
|
92
216
|
this.startHeartbeat();
|
|
217
|
+
this.logger.debug("started", `endpoint=${this.config.endpoint}`);
|
|
93
218
|
}
|
|
94
219
|
/** Para o monitoring */
|
|
95
220
|
stop() {
|
|
221
|
+
if (!this.active) return;
|
|
96
222
|
this.active = false;
|
|
97
223
|
this.collector.stop();
|
|
98
224
|
if (this.heartbeatTimer) {
|
|
99
225
|
clearInterval(this.heartbeatTimer);
|
|
100
226
|
this.heartbeatTimer = null;
|
|
101
227
|
}
|
|
228
|
+
this.logger.debug("stopped");
|
|
229
|
+
}
|
|
230
|
+
/** Força um flush imediato do buffer */
|
|
231
|
+
flush() {
|
|
232
|
+
this.collector.flush();
|
|
233
|
+
}
|
|
234
|
+
/** Verifica se o monitoring está ativo */
|
|
235
|
+
get isActive() {
|
|
236
|
+
return this.active;
|
|
102
237
|
}
|
|
103
238
|
/** Registra um evento de request HTTP (usado pelo middleware) */
|
|
104
239
|
trackRequest(data) {
|
|
@@ -110,6 +245,22 @@ var MonitorClient = class {
|
|
|
110
245
|
if (!this.active) return;
|
|
111
246
|
this.collector.push({ type: "vital", data });
|
|
112
247
|
}
|
|
248
|
+
/** Registra um evento de adapter (DB, cache, AI, queue, email) */
|
|
249
|
+
trackAdapter(data) {
|
|
250
|
+
if (!this.active) return;
|
|
251
|
+
this.collector.push({ type: "adapter", data });
|
|
252
|
+
}
|
|
253
|
+
/** Registra um erro capturado */
|
|
254
|
+
captureError(data) {
|
|
255
|
+
if (!this.active) return;
|
|
256
|
+
this.collector.push({ type: "error", data });
|
|
257
|
+
}
|
|
258
|
+
/** Registra um evento de deploy */
|
|
259
|
+
trackDeploy(data) {
|
|
260
|
+
if (!this.active) return;
|
|
261
|
+
this.collector.push({ type: "deploy", data });
|
|
262
|
+
}
|
|
263
|
+
// ─── Private ────────────────────────────────────────
|
|
113
264
|
startHeartbeat() {
|
|
114
265
|
const interval = this.config.heartbeatInterval ?? 6e4;
|
|
115
266
|
this.sendHeartbeat();
|
|
@@ -136,18 +287,12 @@ var MonitorClient = class {
|
|
|
136
287
|
this.collector.push({
|
|
137
288
|
type: "heartbeat",
|
|
138
289
|
data: {
|
|
139
|
-
status: "
|
|
140
|
-
// O app está rodando, mesmo que o endpoint falhe
|
|
290
|
+
status: "offline",
|
|
141
291
|
latencyMs: Math.round(performance.now() - start)
|
|
142
292
|
}
|
|
143
293
|
});
|
|
144
294
|
}
|
|
145
295
|
}
|
|
146
|
-
isDev() {
|
|
147
|
-
if (typeof process !== "undefined" && process.env?.NODE_ENV === "development") return true;
|
|
148
|
-
if (typeof window !== "undefined" && window.location?.hostname === "localhost") return true;
|
|
149
|
-
return false;
|
|
150
|
-
}
|
|
151
296
|
};
|
|
152
297
|
|
|
153
298
|
// src/index.ts
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/core/transport.ts","../src/core/collector.ts","../src/core/client.ts","../src/index.ts"],"names":[],"mappings":";;;AAaA,eAAsB,SAAA,CACpB,QACA,MAAA,EACkB;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orEvent {\n type: 'heartbeat' | 'request' | 'vital'\n data: Record<string, unknown>\n timestamp: string\n}\n\nexport interface TransportConfig {\n endpoint: string\n apiKey: string\n}\n\nexport async function sendBatch(\n config: TransportConfig,\n events: MonitorEvent[],\n): Promise<boolean> {\n if (events.length === 0) return true\n\n try {\n const response = await fetch(config.endpoint, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n apiKey: config.apiKey,\n events,\n }),\n // Usar keepalive pra garantir envio mesmo quando a página fecha\n keepalive: true,\n })\n\n return response.ok\n } catch {\n // Falha silenciosa — monitoring não pode quebrar o app do cliente\n return false\n }\n}\n","// Collector — acumula eventos em buffer e envia em batch\n\nimport { sendBatch, type MonitorEvent, type TransportConfig } from './transport'\n\nconst DEFAULT_FLUSH_INTERVAL = 30_000 // 30 segundos\nconst MAX_BUFFER_SIZE = 50\n\nexport class Collector {\n private buffer: MonitorEvent[] = []\n private timer: ReturnType<typeof setInterval> | null = null\n private config: TransportConfig\n private flushInterval: number\n\n constructor(config: TransportConfig, flushInterval = DEFAULT_FLUSH_INTERVAL) {\n this.config = config\n this.flushInterval = flushInterval\n }\n\n start() {\n if (this.timer) return\n\n this.timer = setInterval(() => {\n this.flush()\n }, this.flushInterval)\n\n // Flush quando a página for fechada (browser)\n if (typeof window !== 'undefined') {\n window.addEventListener('visibilitychange', () => {\n if (document.visibilityState === 'hidden') {\n this.flush()\n }\n })\n }\n }\n\n stop() {\n if (this.timer) {\n clearInterval(this.timer)\n this.timer = null\n }\n // Flush final\n this.flush()\n }\n\n push(event: Omit<MonitorEvent, 'timestamp'>) {\n this.buffer.push({\n ...event,\n timestamp: new Date().toISOString(),\n })\n\n // Se buffer cheio, flush imediato\n if (this.buffer.length >= MAX_BUFFER_SIZE) {\n this.flush()\n }\n }\n\n private flush() {\n if (this.buffer.length === 0) return\n\n const events = [...this.buffer]\n this.buffer = []\n\n // Fire-and-forget — não bloqueia o app\n sendBatch(this.config, events).catch(() => {\n // Silencioso — eventos perdidos são aceitáveis\n })\n }\n}\n","// MonitorClient — instância principal do SDK\n\nimport { Collector } from './collector'\n\nexport interface MonitorConfig {\n /** ID do projeto no Nuvio */\n projectId: string\n /** API key gerada no Nuvio */\n apiKey: string\n /** URL do endpoint de ingest (ex: https://app.nuvio.com/api/monitor/ingest) */\n endpoint: string\n /** Intervalo do heartbeat em ms (default: 60000) */\n heartbeatInterval?: number\n /** Intervalo do flush do buffer em ms (default: 30000) */\n flushInterval?: number\n /** Desabilitar em desenvolvimento (default: true) */\n disableInDev?: boolean\n}\n\nexport class MonitorClient {\n readonly config: MonitorConfig\n readonly collector: Collector\n private heartbeatTimer: ReturnType<typeof setInterval> | null = null\n private active = false\n\n constructor(config: MonitorConfig) {\n this.config = config\n this.collector = new Collector(\n { endpoint: config.endpoint, apiKey: config.apiKey },\n config.flushInterval,\n )\n }\n\n /** Inicia o monitoring (heartbeat + collector) */\n start() {\n // Não roda em dev por padrão\n if (this.config.disableInDev !== false && this.isDev()) return\n if (this.active) return\n\n this.active = true\n this.collector.start()\n this.startHeartbeat()\n }\n\n /** Para o monitoring */\n stop() {\n this.active = false\n this.collector.stop()\n\n if (this.heartbeatTimer) {\n clearInterval(this.heartbeatTimer)\n this.heartbeatTimer = null\n }\n }\n\n /** Registra um evento de request HTTP (usado pelo middleware) */\n trackRequest(data: {\n method: string\n route: string\n statusCode: number\n responseTimeMs: number\n userAgent?: string\n region?: string\n }) {\n if (!this.active) return\n this.collector.push({ type: 'request', data })\n }\n\n /** Registra um Web Vital (usado pelo MonitorScript) */\n trackVital(data: {\n name: 'LCP' | 'INP' | 'CLS' | 'FCP' | 'TTFB'\n value: number\n rating: 'good' | 'needs-improvement' | 'poor'\n page?: string\n deviceType?: string\n browser?: string\n }) {\n if (!this.active) return\n this.collector.push({ type: 'vital', data })\n }\n\n private startHeartbeat() {\n const interval = this.config.heartbeatInterval ?? 60_000\n\n // Primeiro heartbeat imediato\n this.sendHeartbeat()\n\n this.heartbeatTimer = setInterval(() => {\n this.sendHeartbeat()\n }, interval)\n }\n\n private async sendHeartbeat() {\n const start = performance.now()\n\n try {\n // Ping pro endpoint pra medir latência\n const response = await fetch(this.config.endpoint, {\n method: 'HEAD',\n cache: 'no-store',\n })\n\n const latencyMs = Math.round(performance.now() - start)\n\n this.collector.push({\n type: 'heartbeat',\n data: {\n status: response.ok ? 'online' : 'offline',\n latencyMs,\n },\n })\n } catch {\n this.collector.push({\n type: 'heartbeat',\n data: {\n status: 'online', // O app está rodando, mesmo que o endpoint falhe\n latencyMs: Math.round(performance.now() - start),\n },\n })\n }\n }\n\n private isDev(): boolean {\n if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development') return true\n if (typeof window !== 'undefined' && window.location?.hostname === 'localhost') return true\n return false\n }\n}\n","// @victor-studio/monitor — SDK de monitoramento\n\nimport { MonitorClient, type MonitorConfig } from './core/client'\n\nexport { MonitorClient, type MonitorConfig } from './core/client'\n\n/**\n * Cria e inicia uma instância do monitor.\n *\n * @example\n * ```ts\n * import { createMonitor } from '@victor-studio/monitor'\n *\n * export const monitor = createMonitor({\n * projectId: 'uuid-do-projeto',\n * apiKey: 'nuvio_mon_xxxxxxxx',\n * endpoint: 'https://app.nuvio.com/api/monitor/ingest',\n * })\n * ```\n */\nexport function createMonitor(config: MonitorConfig): MonitorClient {\n const client = new MonitorClient(config)\n client.start()\n return client\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/core/transport.ts","../src/core/collector.ts","../src/core/env.ts","../src/core/logger.ts","../src/core/client.ts","../src/index.ts"],"names":[],"mappings":";;;AAmBA,IAAM,eAAA,GAAkB,GAAA;AACxB,IAAM,mBAAA,GAAsB,CAAA;AAC5B,IAAM,iBAAA,GAAoB,GAAA;AAM1B,eAAsB,SAAA,CACpB,MAAA,EACA,MAAA,EACA,MAAA,EACA,OAAA,EACkB;AAClB,EAAA,IAAI,MAAA,CAAO,MAAA,KAAW,CAAA,EAAG,OAAO,IAAA;AAEhC,EAAA,MAAM,OAAA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,UACJ,MAAA,EAAQ,SAAA;AAAA,UACR,WAAW,IAAA,CAAK,KAAA,CAAM,WAAA,CAAY,GAAA,KAAQ,KAAK;AAAA;AACjD,OACD,CAAA;AAAA,IACH;AAAA,EACF;AACF;;;ACnJO,SAAS,cAAc,MAAA,EAAsC;AAClE,EAAA,MAAM,MAAA,GAAS,IAAI,aAAA,CAAc,MAAM,CAAA;AACvC,EAAA,MAAA,CAAO,KAAA,EAAM;AACb,EAAA,OAAO,MAAA;AACT","file":"index.cjs","sourcesContent":["// Transport — envia batch de eventos pro backend Nuvio\n\nimport type { MonitorEvent } from '../types'\nimport type { Logger } from './logger'\n\nexport interface TransportConfig {\n endpoint: string\n apiKey: string\n /** Timeout do fetch em ms (default: 10000) */\n timeout?: number\n /** Max tentativas de retry (default: 3) */\n maxRetries?: number\n}\n\ninterface SendOptions {\n /** Usar sendBeacon em vez de fetch (para page unload) */\n useBeacon?: boolean\n}\n\nconst DEFAULT_TIMEOUT = 10_000\nconst DEFAULT_MAX_RETRIES = 3\nconst MAX_PAYLOAD_BYTES = 60_000 // 60KB — abaixo do limite de 64KB do keepalive\n\n/**\n * Envia um batch de eventos para o endpoint.\n * Usa Authorization header, retry com backoff, e sendBeacon como fallback.\n */\nexport async function sendBatch(\n config: TransportConfig,\n events: MonitorEvent[],\n logger: Logger,\n options?: SendOptions,\n): Promise<boolean> {\n if (events.length === 0) return true\n\n const payload = JSON.stringify({ events })\n\n // Se é unload, usa sendBeacon (mais confiável que fetch no page close)\n if (options?.useBeacon && typeof navigator !== 'undefined' && navigator.sendBeacon) {\n // sendBeacon não suporta headers customizados — inclui apiKey no body\n const beaconPayload = JSON.stringify({ apiKey: config.apiKey, events })\n const beaconBlob = new Blob([beaconPayload], { type: 'application/json' })\n const sent = navigator.sendBeacon(config.endpoint, beaconBlob)\n logger.debug('sendBeacon', sent ? 'queued' : 'failed', `${events.length} events`)\n return sent\n }\n\n // Dividir em batches menores se payload é muito grande\n const payloadSize = new Blob([payload]).size\n if (payloadSize > MAX_PAYLOAD_BYTES && events.length > 1) {\n const mid = Math.floor(events.length / 2)\n const [first, second] = await Promise.all([\n sendBatch(config, events.slice(0, mid), logger),\n sendBatch(config, events.slice(mid), logger),\n ])\n return first && second\n }\n\n return sendWithRetry(config, payload, logger)\n}\n\nasync function sendWithRetry(\n config: TransportConfig,\n payload: string,\n logger: Logger,\n): Promise<boolean> {\n const maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES\n const timeout = config.timeout ?? DEFAULT_TIMEOUT\n\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n try {\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), timeout)\n\n const response = await fetch(config.endpoint, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n body: payload,\n signal: controller.signal,\n keepalive: attempt === 0, // keepalive só na primeira tentativa\n })\n\n clearTimeout(timer)\n\n if (response.ok) {\n logger.debug('sent', `attempt ${attempt + 1}`, response.status)\n return true\n }\n\n // 4xx — erro do cliente, não faz retry (apiKey inválida, payload mal formado, etc.)\n if (response.status >= 400 && response.status < 500) {\n logger.warn('client error', response.status, '— no retry')\n return false\n }\n\n // 5xx — erro do servidor, faz retry\n logger.warn('server error', response.status, `attempt ${attempt + 1}/${maxRetries + 1}`)\n } catch (err) {\n // Network error ou timeout — faz retry\n const message = err instanceof Error ? err.message : 'unknown error'\n logger.warn('network error', message, `attempt ${attempt + 1}/${maxRetries + 1}`)\n }\n\n // Esperar antes do retry (exponential backoff com jitter)\n if (attempt < maxRetries) {\n const delay = Math.min(1000 * Math.pow(2, attempt), 16_000)\n const jitter = Math.random() * 1000\n await sleep(delay + jitter)\n }\n }\n\n logger.error('all retries exhausted, dropping batch')\n return false\n}\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n","// Collector — acumula eventos em buffer e envia em batch\n\nimport type { MonitorEvent, MonitorEventInput, BeforeSendHook } from '../types'\nimport { sendBatch, type TransportConfig } from './transport'\nimport type { Logger } from './logger'\n\nconst DEFAULT_FLUSH_INTERVAL = 30_000 // 30 segundos\nconst MAX_BUFFER_SIZE = 50\n\nexport class Collector {\n private buffer: MonitorEvent[] = []\n private timer: ReturnType<typeof setInterval> | null = null\n private config: TransportConfig\n private flushInterval: number\n private logger: Logger\n private beforeSend: BeforeSendHook | undefined\n private sampleRate: number\n private visibilityHandler: (() => void) | null = null\n\n constructor(\n config: TransportConfig,\n logger: Logger,\n options?: {\n flushInterval?: number\n beforeSend?: BeforeSendHook\n sampleRate?: number\n },\n ) {\n this.config = config\n this.logger = logger\n this.flushInterval = options?.flushInterval ?? DEFAULT_FLUSH_INTERVAL\n this.beforeSend = options?.beforeSend\n this.sampleRate = options?.sampleRate ?? 1.0\n }\n\n start() {\n if (this.timer) return\n\n this.timer = setInterval(() => {\n this.flush()\n }, this.flushInterval)\n\n // Flush quando a página for fechada (browser) — usa sendBeacon\n if (typeof window !== 'undefined') {\n this.visibilityHandler = () => {\n if (document.visibilityState === 'hidden') {\n this.flush(true)\n }\n }\n window.addEventListener('visibilitychange', this.visibilityHandler)\n }\n }\n\n stop() {\n if (this.timer) {\n clearInterval(this.timer)\n this.timer = null\n }\n\n // Remover listener para evitar memory leak\n if (this.visibilityHandler && typeof window !== 'undefined') {\n window.removeEventListener('visibilitychange', this.visibilityHandler)\n this.visibilityHandler = null\n }\n\n // Flush final\n this.flush()\n }\n\n push(input: MonitorEventInput) {\n // Sampling — heartbeats nunca são amostrados\n if (input.type !== 'heartbeat' && this.sampleRate < 1.0) {\n if (Math.random() >= this.sampleRate) {\n this.logger.debug('sampled out', input.type)\n return\n }\n }\n\n const event: MonitorEvent = {\n ...input,\n timestamp: new Date().toISOString(),\n } as MonitorEvent\n\n // beforeSend hook — permite filtrar ou modificar eventos\n if (this.beforeSend) {\n const result = this.beforeSend(event)\n if (result === null) {\n this.logger.debug('dropped by beforeSend', event.type)\n return\n }\n // Usa o evento possivelmente modificado\n this.buffer.push(result)\n } else {\n this.buffer.push(event)\n }\n\n this.logger.debug('buffered', event.type, `(${this.buffer.length}/${MAX_BUFFER_SIZE})`)\n\n // Se buffer cheio, flush imediato\n if (this.buffer.length >= MAX_BUFFER_SIZE) {\n this.flush()\n }\n }\n\n /** Flush manual — exposto via MonitorClient.flush() */\n flush(useBeacon = false) {\n if (this.buffer.length === 0) return\n\n const events = [...this.buffer]\n this.buffer = []\n\n this.logger.debug('flushing', events.length, 'events', useBeacon ? '(beacon)' : '')\n\n // Fire-and-forget — não bloqueia o app\n sendBatch(this.config, events, this.logger, { useBeacon }).catch(() => {\n // Silencioso — transport já loga erros via logger\n })\n }\n\n /** Número de eventos no buffer (para debug/testes) */\n get pending(): number {\n return this.buffer.length\n }\n}\n","// Detecção de ambiente — centraliza lógica de dev/server\n\nconst LOCAL_HOSTNAMES = new Set(['localhost', '127.0.0.1', '0.0.0.0', '[::1]'])\n\n/** Detecta se estamos em ambiente de desenvolvimento */\nexport function isDev(): boolean {\n // Node.js / bundler env\n if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development') return true\n if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'test') return true\n\n // Browser\n if (typeof window !== 'undefined' && LOCAL_HOSTNAMES.has(window.location?.hostname)) return true\n\n return false\n}\n\n/** Detecta se estamos rodando no servidor (Node.js) */\nexport function isServer(): boolean {\n return typeof window === 'undefined'\n}\n\n/** Detecta se estamos no browser */\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined'\n}\n","// Logger — debug mode com prefixo [monitor]\n\nconst PREFIX = '[monitor]'\n\nexport interface Logger {\n debug(...args: unknown[]): void\n warn(...args: unknown[]): void\n error(...args: unknown[]): void\n}\n\n/** Cria logger que loga no console quando debug=true, noop caso contrário */\nexport function createLogger(debug: boolean): Logger {\n if (!debug) {\n return { debug: noop, warn: noop, error: noop }\n }\n\n return {\n debug: (...args: unknown[]) => console.debug(PREFIX, ...args),\n warn: (...args: unknown[]) => console.warn(PREFIX, ...args),\n error: (...args: unknown[]) => console.error(PREFIX, ...args),\n }\n}\n\nfunction noop() {\n // silencioso\n}\n","// MonitorClient — instância principal do SDK\n\nimport type {\n HeartbeatData,\n RequestData,\n VitalData,\n AdapterData,\n ErrorData,\n DeployData,\n BeforeSendHook,\n} from '../types'\nimport { Collector } from './collector'\nimport { isDev } from './env'\nimport { createLogger, type Logger } from './logger'\n\nexport interface MonitorConfig {\n /** ID do projeto no Nuvio */\n projectId: string\n /** API key gerada no Nuvio */\n apiKey: string\n /** URL do endpoint de ingest */\n endpoint: string\n /** Intervalo do heartbeat em ms (default: 60000) */\n heartbeatInterval?: number\n /** Intervalo do flush do buffer em ms (default: 30000) */\n flushInterval?: number\n /** Timeout do fetch em ms (default: 10000) */\n timeout?: number\n /** Max tentativas de retry (default: 3) */\n maxRetries?: number\n /** Desabilitar em desenvolvimento (default: true) */\n disableInDev?: boolean\n /** Taxa de amostragem 0.0-1.0 (default: 1.0). Heartbeats nunca são amostrados */\n sampleRate?: number\n /** Hook chamado antes de enfileirar cada evento. Retorna null para dropar */\n beforeSend?: BeforeSendHook\n /** Habilitar logs de debug no console (default: false) */\n debug?: boolean\n}\n\nexport class MonitorClient {\n readonly config: MonitorConfig\n private readonly collector: Collector\n private readonly logger: Logger\n private heartbeatTimer: ReturnType<typeof setInterval> | null = null\n private active = false\n\n constructor(config: MonitorConfig) {\n this.config = config\n this.logger = createLogger(config.debug ?? false)\n this.collector = new Collector(\n {\n endpoint: config.endpoint,\n apiKey: config.apiKey,\n timeout: config.timeout,\n maxRetries: config.maxRetries,\n },\n this.logger,\n {\n flushInterval: config.flushInterval,\n beforeSend: config.beforeSend,\n sampleRate: config.sampleRate,\n },\n )\n }\n\n /** Inicia o monitoring (heartbeat + collector) */\n start() {\n // Não roda em dev por padrão\n if (this.config.disableInDev !== false && isDev()) {\n this.logger.debug('disabled in dev — call start() with disableInDev: false to override')\n return\n }\n\n if (this.active) return\n\n this.active = true\n this.collector.start()\n this.startHeartbeat()\n this.logger.debug('started', `endpoint=${this.config.endpoint}`)\n }\n\n /** Para o monitoring */\n stop() {\n if (!this.active) return\n\n this.active = false\n this.collector.stop()\n\n if (this.heartbeatTimer) {\n clearInterval(this.heartbeatTimer)\n this.heartbeatTimer = null\n }\n\n this.logger.debug('stopped')\n }\n\n /** Força um flush imediato do buffer */\n flush() {\n this.collector.flush()\n }\n\n /** Verifica se o monitoring está ativo */\n get isActive(): boolean {\n return this.active\n }\n\n /** Registra um evento de request HTTP (usado pelo middleware) */\n trackRequest(data: RequestData) {\n if (!this.active) return\n this.collector.push({ type: 'request', data })\n }\n\n /** Registra um Web Vital (usado pelo MonitorScript) */\n trackVital(data: VitalData) {\n if (!this.active) return\n this.collector.push({ type: 'vital', data })\n }\n\n /** Registra um evento de adapter (DB, cache, AI, queue, email) */\n trackAdapter(data: AdapterData) {\n if (!this.active) return\n this.collector.push({ type: 'adapter', data })\n }\n\n /** Registra um erro capturado */\n captureError(data: ErrorData) {\n if (!this.active) return\n this.collector.push({ type: 'error', data })\n }\n\n /** Registra um evento de deploy */\n trackDeploy(data: DeployData) {\n if (!this.active) return\n this.collector.push({ type: 'deploy', data })\n }\n\n // ─── Private ────────────────────────────────────────\n\n private startHeartbeat() {\n const interval = this.config.heartbeatInterval ?? 60_000\n\n // Primeiro heartbeat imediato\n this.sendHeartbeat()\n\n this.heartbeatTimer = setInterval(() => {\n this.sendHeartbeat()\n }, interval)\n }\n\n private async sendHeartbeat() {\n const start = performance.now()\n\n try {\n const response = await fetch(this.config.endpoint, {\n method: 'HEAD',\n cache: 'no-store',\n })\n\n const latencyMs = Math.round(performance.now() - start)\n\n this.collector.push({\n type: 'heartbeat',\n data: {\n status: response.ok ? 'online' : 'offline',\n latencyMs,\n } satisfies HeartbeatData,\n })\n } catch {\n // Network error — o endpoint não respondeu, reporta offline\n this.collector.push({\n type: 'heartbeat',\n data: {\n status: 'offline',\n latencyMs: Math.round(performance.now() - start),\n } satisfies HeartbeatData,\n })\n }\n }\n}\n","// @victor-studio/monitor — SDK de monitoramento\n\nimport { MonitorClient, type MonitorConfig } from './core/client'\n\nexport { MonitorClient, type MonitorConfig } from './core/client'\nexport type {\n MonitorEvent,\n MonitorEventInput,\n MonitorEventType,\n BeforeSendHook,\n HeartbeatData,\n RequestData,\n VitalData,\n ErrorData,\n DeployData,\n AdapterData,\n} from './types'\n\n/**\n * Cria e inicia uma instância do monitor.\n *\n * @example\n * ```ts\n * import { createMonitor } from '@victor-studio/monitor'\n *\n * export const monitor = createMonitor({\n * projectId: 'uuid-do-projeto',\n * apiKey: 'nuvio_mon_xxxxxxxx',\n * endpoint: 'https://nuvio.app/api/monitor/ingest',\n * })\n * ```\n */\nexport function createMonitor(config: MonitorConfig): MonitorClient {\n const client = new MonitorClient(config)\n client.start()\n return client\n}\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,42 +1,112 @@
|
|
|
1
|
-
interface
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
timestamp: string;
|
|
1
|
+
interface HeartbeatData {
|
|
2
|
+
status: 'online' | 'offline';
|
|
3
|
+
latencyMs: number;
|
|
5
4
|
}
|
|
6
|
-
interface
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
interface RequestData {
|
|
6
|
+
method: string;
|
|
7
|
+
route: string;
|
|
8
|
+
statusCode: number;
|
|
9
|
+
responseTimeMs: number;
|
|
10
|
+
userAgent?: string;
|
|
11
|
+
region?: string;
|
|
9
12
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
interface VitalData {
|
|
14
|
+
name: 'LCP' | 'INP' | 'CLS' | 'FCP' | 'TTFB';
|
|
15
|
+
value: number;
|
|
16
|
+
rating: 'good' | 'needs-improvement' | 'poor';
|
|
17
|
+
page?: string;
|
|
18
|
+
deviceType?: string;
|
|
19
|
+
browser?: string;
|
|
20
|
+
}
|
|
21
|
+
interface ErrorData {
|
|
22
|
+
type: 'unhandled' | 'caught' | 'promise';
|
|
23
|
+
message: string;
|
|
24
|
+
stack?: string;
|
|
25
|
+
groupingKey: string;
|
|
26
|
+
route?: string;
|
|
27
|
+
method?: string;
|
|
28
|
+
statusCode?: number;
|
|
29
|
+
environment?: string;
|
|
30
|
+
commitHash?: string;
|
|
31
|
+
extra?: Record<string, string>;
|
|
32
|
+
}
|
|
33
|
+
interface DeployData {
|
|
34
|
+
commitHash: string;
|
|
35
|
+
branch?: string;
|
|
36
|
+
author?: string;
|
|
37
|
+
status: 'started' | 'succeeded' | 'failed';
|
|
38
|
+
buildDurationMs?: number;
|
|
39
|
+
url?: string;
|
|
40
|
+
environment?: string;
|
|
41
|
+
provider?: string;
|
|
21
42
|
}
|
|
43
|
+
interface AdapterData {
|
|
44
|
+
adapter: string;
|
|
45
|
+
operation: string;
|
|
46
|
+
durationMs: number;
|
|
47
|
+
success: boolean;
|
|
48
|
+
error?: string;
|
|
49
|
+
meta?: Record<string, string>;
|
|
50
|
+
}
|
|
51
|
+
type MonitorEvent = {
|
|
52
|
+
type: 'heartbeat';
|
|
53
|
+
data: HeartbeatData;
|
|
54
|
+
timestamp: string;
|
|
55
|
+
} | {
|
|
56
|
+
type: 'request';
|
|
57
|
+
data: RequestData;
|
|
58
|
+
timestamp: string;
|
|
59
|
+
} | {
|
|
60
|
+
type: 'vital';
|
|
61
|
+
data: VitalData;
|
|
62
|
+
timestamp: string;
|
|
63
|
+
} | {
|
|
64
|
+
type: 'error';
|
|
65
|
+
data: ErrorData;
|
|
66
|
+
timestamp: string;
|
|
67
|
+
} | {
|
|
68
|
+
type: 'deploy';
|
|
69
|
+
data: DeployData;
|
|
70
|
+
timestamp: string;
|
|
71
|
+
} | {
|
|
72
|
+
type: 'adapter';
|
|
73
|
+
data: AdapterData;
|
|
74
|
+
timestamp: string;
|
|
75
|
+
};
|
|
76
|
+
type MonitorEventType = MonitorEvent['type'];
|
|
77
|
+
/** Evento sem timestamp — usado pelo collector ao receber do client */
|
|
78
|
+
type MonitorEventInput = Omit<MonitorEvent, 'timestamp'>;
|
|
79
|
+
/** Hook para filtrar/modificar eventos antes do envio */
|
|
80
|
+
type BeforeSendHook = (event: MonitorEvent) => MonitorEvent | null;
|
|
22
81
|
|
|
23
82
|
interface MonitorConfig {
|
|
24
83
|
/** ID do projeto no Nuvio */
|
|
25
84
|
projectId: string;
|
|
26
85
|
/** API key gerada no Nuvio */
|
|
27
86
|
apiKey: string;
|
|
28
|
-
/** URL do endpoint de ingest
|
|
87
|
+
/** URL do endpoint de ingest */
|
|
29
88
|
endpoint: string;
|
|
30
89
|
/** Intervalo do heartbeat em ms (default: 60000) */
|
|
31
90
|
heartbeatInterval?: number;
|
|
32
91
|
/** Intervalo do flush do buffer em ms (default: 30000) */
|
|
33
92
|
flushInterval?: number;
|
|
93
|
+
/** Timeout do fetch em ms (default: 10000) */
|
|
94
|
+
timeout?: number;
|
|
95
|
+
/** Max tentativas de retry (default: 3) */
|
|
96
|
+
maxRetries?: number;
|
|
34
97
|
/** Desabilitar em desenvolvimento (default: true) */
|
|
35
98
|
disableInDev?: boolean;
|
|
99
|
+
/** Taxa de amostragem 0.0-1.0 (default: 1.0). Heartbeats nunca são amostrados */
|
|
100
|
+
sampleRate?: number;
|
|
101
|
+
/** Hook chamado antes de enfileirar cada evento. Retorna null para dropar */
|
|
102
|
+
beforeSend?: BeforeSendHook;
|
|
103
|
+
/** Habilitar logs de debug no console (default: false) */
|
|
104
|
+
debug?: boolean;
|
|
36
105
|
}
|
|
37
106
|
declare class MonitorClient {
|
|
38
107
|
readonly config: MonitorConfig;
|
|
39
|
-
readonly collector
|
|
108
|
+
private readonly collector;
|
|
109
|
+
private readonly logger;
|
|
40
110
|
private heartbeatTimer;
|
|
41
111
|
private active;
|
|
42
112
|
constructor(config: MonitorConfig);
|
|
@@ -44,27 +114,22 @@ declare class MonitorClient {
|
|
|
44
114
|
start(): void;
|
|
45
115
|
/** Para o monitoring */
|
|
46
116
|
stop(): void;
|
|
117
|
+
/** Força um flush imediato do buffer */
|
|
118
|
+
flush(): void;
|
|
119
|
+
/** Verifica se o monitoring está ativo */
|
|
120
|
+
get isActive(): boolean;
|
|
47
121
|
/** Registra um evento de request HTTP (usado pelo middleware) */
|
|
48
|
-
trackRequest(data:
|
|
49
|
-
method: string;
|
|
50
|
-
route: string;
|
|
51
|
-
statusCode: number;
|
|
52
|
-
responseTimeMs: number;
|
|
53
|
-
userAgent?: string;
|
|
54
|
-
region?: string;
|
|
55
|
-
}): void;
|
|
122
|
+
trackRequest(data: RequestData): void;
|
|
56
123
|
/** Registra um Web Vital (usado pelo MonitorScript) */
|
|
57
|
-
trackVital(data:
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}): void;
|
|
124
|
+
trackVital(data: VitalData): void;
|
|
125
|
+
/** Registra um evento de adapter (DB, cache, AI, queue, email) */
|
|
126
|
+
trackAdapter(data: AdapterData): void;
|
|
127
|
+
/** Registra um erro capturado */
|
|
128
|
+
captureError(data: ErrorData): void;
|
|
129
|
+
/** Registra um evento de deploy */
|
|
130
|
+
trackDeploy(data: DeployData): void;
|
|
65
131
|
private startHeartbeat;
|
|
66
132
|
private sendHeartbeat;
|
|
67
|
-
private isDev;
|
|
68
133
|
}
|
|
69
134
|
|
|
70
135
|
/**
|
|
@@ -77,10 +142,10 @@ declare class MonitorClient {
|
|
|
77
142
|
* export const monitor = createMonitor({
|
|
78
143
|
* projectId: 'uuid-do-projeto',
|
|
79
144
|
* apiKey: 'nuvio_mon_xxxxxxxx',
|
|
80
|
-
* endpoint: 'https://
|
|
145
|
+
* endpoint: 'https://nuvio.app/api/monitor/ingest',
|
|
81
146
|
* })
|
|
82
147
|
* ```
|
|
83
148
|
*/
|
|
84
149
|
declare function createMonitor(config: MonitorConfig): MonitorClient;
|
|
85
150
|
|
|
86
|
-
export { MonitorClient, type MonitorConfig, createMonitor };
|
|
151
|
+
export { type AdapterData, type BeforeSendHook, type DeployData, type ErrorData, type HeartbeatData, MonitorClient, type MonitorConfig, type MonitorEvent, type MonitorEventInput, type MonitorEventType, type RequestData, type VitalData, createMonitor };
|