@senzops/apm-node 1.0.0 → 1.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/dist/index.d.mts +19 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.global.js +2 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +17 -14
- package/src/core/client.ts +80 -27
- package/src/core/context.ts +26 -0
- package/src/core/transport.ts +12 -23
- package/src/core/types.ts +37 -0
- package/src/index.ts +2 -2
- package/src/instrumentation/http.ts +94 -0
- package/src/instrumentation/mongo.ts +77 -0
- package/src/instrumentation/pg.ts +41 -0
- package/src/middleware/express.ts +22 -32
- package/src/wrappers/fastify.ts +1 -1
- package/src/wrappers/h3.ts +26 -38
- package/src/wrappers/next.ts +50 -59
- package/wiki.md +24 -1
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface SenzorOptions {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
endpoint?: string;
|
|
4
|
+
batchSize?: number;
|
|
5
|
+
flushInterval?: number;
|
|
6
|
+
debug?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare const Senzor: {
|
|
10
|
+
init: (options: SenzorOptions) => void;
|
|
11
|
+
flush: () => Promise<void>;
|
|
12
|
+
requestHandler: () => (req: any, res: any, next: () => void) => void;
|
|
13
|
+
wrapNextRoute: (handler: Function) => (req: Request | any, context?: any) => Promise<any>;
|
|
14
|
+
wrapNextPages: (handler: Function) => (req: any, res: any) => Promise<any>;
|
|
15
|
+
wrapH3: (handler: (event: any) => any) => (event: any) => Promise<any>;
|
|
16
|
+
fastifyPlugin: (fastify: any, options: SenzorOptions, done: Function) => void;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { Senzor, Senzor as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface SenzorOptions {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
endpoint?: string;
|
|
4
|
+
batchSize?: number;
|
|
5
|
+
flushInterval?: number;
|
|
6
|
+
debug?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare const Senzor: {
|
|
10
|
+
init: (options: SenzorOptions) => void;
|
|
11
|
+
flush: () => Promise<void>;
|
|
12
|
+
requestHandler: () => (req: any, res: any, next: () => void) => void;
|
|
13
|
+
wrapNextRoute: (handler: Function) => (req: Request | any, context?: any) => Promise<any>;
|
|
14
|
+
wrapNextPages: (handler: Function) => (req: any, res: any) => Promise<any>;
|
|
15
|
+
wrapH3: (handler: (event: any) => any) => (event: any) => Promise<any>;
|
|
16
|
+
fastifyPlugin: (fastify: any, options: SenzorOptions, done: Function) => void;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { Senzor, Senzor as default };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";(()=>{var K=Object.create;var v=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var W=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var l=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,r)=>(typeof require<"u"?require:t)[r]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var j=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of G(t))!L.call(e,o)&&o!==r&&v(e,o,{get:()=>t[o],enumerable:!(n=D(t,o))||n.enumerable});return e};var F=(e,t,r)=>(r=e!=null?K(W(e)):{},j(t||!e||!e.__esModule?v(r,"default",{value:e,enumerable:!0}):r,e));var w=class{constructor(t){this.config=t;this.queue=[];this.timer=null;typeof setInterval<"u"&&(this.timer=setInterval(()=>this.flush(),t.flushInterval||1e4),this.timer&&typeof this.timer.unref=="function"&&this.timer.unref())}add(t){this.queue.push(t),this.queue.length>=(this.config.batchSize||100)&&this.flush()}async flush(){if(this.queue.length===0)return;let t=[...this.queue];this.queue=[];try{await fetch(this.config.endpoint||"https://api.senzor.dev/api/ingest/apm",{method:"POST",headers:{"Content-Type":"application/json","x-service-api-key":this.config.apiKey},body:JSON.stringify(t),keepalive:!0}),this.config.debug&&console.log(`[Senzor] Flushed ${t.length} traces`)}catch(r){this.config.debug&&console.error("[Senzor] Ingestion Error:",r)}}};var P=l("async_hooks"),x=new P.AsyncLocalStorage,u={run:(e,t)=>x.run(e,t),current:()=>x.getStore(),addSpan:e=>{let t=x.getStore();t&&t.spans.push(e)}};var z=l("crypto");var b=F(l("http")),A=F(l("https")),S=l("url");var T=(e,t,r)=>{if(!e[t])return;let n=e[t];e[t]=r(n)},I=e=>{let t=new S.URL(e).hostname,r=n=>function(...o){let a={},c="";if(typeof o[0]=="string"||o[0]instanceof S.URL)c=o[0].toString(),a=o[1]||{};else{a=o[0]||{};let d=a.protocol||"http:",y=a.hostname||a.host||"localhost",M=a.path||"/";c=`${d}//${y}${M}`}if(c.includes(t)||a.hostname&&a.hostname.includes(t))return n.apply(this,o);let i=u.current();if(!i)return n.apply(this,o);let m=(a.method||"GET").toUpperCase(),h=performance.now()-i.startTime,f=performance.now(),p=n.apply(this,o);return p.on("response",d=>{d.on("end",()=>{let y=performance.now()-f;u.addSpan({name:`${m} ${new S.URL(c).hostname}`,type:"http",startTime:h,duration:y,status:d.statusCode,meta:{url:c,method:m}})})}),p.on("error",d=>{let y=performance.now()-f;u.addSpan({name:`${m} ${c}`,type:"http",startTime:h,duration:y,status:500,meta:{error:d.message}})}),p};T(b.default,"request",r),T(b.default,"get",r),T(A.default,"request",r),T(A.default,"get",r)};var N=()=>{try{let t=l("mongodb").Collection;["find","findOne","insertOne","insertMany","updateOne","updateMany","deleteOne","deleteMany","aggregate","countDocuments"].forEach(n=>{if(!t.prototype[n])return;let o=t.prototype[n];t.prototype[n]=function(...a){let c=u.current();if(!c)return o.apply(this,a);let i=performance.now()-c.startTime,m=performance.now(),h=this.collectionName,f=p=>{let d=performance.now()-m;u.addSpan({name:`MongoDB ${n} (${h})`,type:"db",startTime:i,duration:d,status:p?500:0,meta:{collection:h,operation:n,error:p?p.message:void 0}})};try{let p=o.apply(this,a);return p&&typeof p.then=="function"?p.then(d=>(f(),d),d=>{throw f(d),d}):(f(),p)}catch(p){throw f(p),p}}})}catch{}};var R=()=>{try{let e=l("pg"),t=e.Client.prototype.query;e.Client.prototype.query=function(...r){let n=u.current();if(!n)return t.apply(this,r);let o=performance.now()-n.startTime,a=performance.now(),c=typeof r[0]=="string"?r[0]:r[0].text,i=t.apply(this,r);return i&&typeof i.then=="function"?i.then(m=>{let h=performance.now()-a;return u.addSpan({name:"Postgres Query",type:"db",startTime:o,duration:h,meta:{query:c}}),m}):i}}catch{}};var C=class{constructor(){this.transport=null;this.options=null;this.isInstrumented=!1}init(t){if(!t.apiKey){console.warn("[Senzor] API Key missing. SDK disabled.");return}this.options=t;let r=t.endpoint||"https://api.senzor.dev/api/ingest/apm";if(this.transport=new w({...t,endpoint:r}),!this.isInstrumented){try{I(r)}catch{}try{N()}catch{}try{R()}catch{}this.isInstrumented=!0,t.debug&&console.log("[Senzor] Auto-instrumentation enabled")}t.debug&&console.log("[Senzor] Initialized")}startTrace(t,r){if(!this.transport)return r();let n={id:(0,z.randomUUID)(),startTime:performance.now(),data:t,spans:[]};return u.run(n,r)}endTrace(t,r={}){let n=u.current();if(!n||!this.transport)return;let o=performance.now()-n.startTime,a={traceId:n.id,...n.data,...r,status:t,duration:o,spans:n.spans,timestamp:new Date().toISOString()};this.transport.add(a)}track(t){if(!this.transport)return;let r={traceId:(0,z.randomUUID)(),...t,spans:[],timestamp:new Date().toISOString()};this.transport.add(r)}startSpan(t,r="custom"){let n=u.current();if(!n)return{end:()=>{}};let o=performance.now()-n.startTime,a=performance.now();return{end:(c,i)=>{let m=performance.now()-a;u.addSpan({name:t,type:r,startTime:o,duration:m,status:i,meta:c})}}}async flush(){this.transport&&await this.transport.flush()}},s=new C;var U=()=>(e,t,r)=>{s.startTrace({method:e.method,path:e.originalUrl||e.url,ip:e.ip||e.socket?.remoteAddress,userAgent:e.headers["user-agent"]},()=>{t.once("finish",()=>{try{let n="UNKNOWN";e.route&&e.route.path?n=(e.baseUrl||"")+e.route.path:t.statusCode===404?n="Not Found":n=e.path||"Wildcard",s.endTrace(t.statusCode,{route:n})}catch{}}),r()})};var g=e=>!e||e==="/"?"/":e.replace(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/g,":uuid").replace(/[0-9a-fA-F]{24}/g,":objectId").replace(/\/(\d+)(?=\/|$)/g,"/:id").split("?")[0],O=(e,t)=>e.route&&e.route.path?(e.baseUrl||"")+e.route.path:e.context&&e.context.matchedRoute?e.context.matchedRoute.path:e.routerPath?e.routerPath:g(t);var $=e=>t=>{let r=t.node.req,n=r.originalUrl||r.url||"/";return s.startTrace({method:r.method||"GET",path:n,ip:r.headers["x-forwarded-for"]||r.socket?.remoteAddress,userAgent:r.headers["user-agent"]},async()=>{try{let o=await e(t),a=200;return t.node.res.statusCode&&(a=t.node.res.statusCode),o&&o.statusCode&&(a=o.statusCode),s.endTrace(a,{route:O(t,n)}),o}catch(o){let a=o.statusCode||o.status||500;throw s.endTrace(a,{route:O(t,n)}),o}})};var E=e=>async(t,r)=>{let n=t.url?new URL(t.url):{pathname:"/"},o=t.method||"GET",a=t.headers.get?t.headers.get("user-agent"):void 0,c=t.headers.get?t.headers.get("x-forwarded-for"):void 0;return s.startTrace({method:o,path:n.pathname,userAgent:a,ip:c},async()=>{try{let i=await e(t,r),m=i?.status||200;return s.endTrace(m,{route:g(n.pathname)}),i}catch(i){throw s.endTrace(500,{route:g(n.pathname)}),i}})},H=e=>async(t,r)=>{let n=t.url?t.url.split("?")[0]:"/";return s.startTrace({method:t.method||"GET",path:n,userAgent:t.headers["user-agent"],ip:t.headers["x-forwarded-for"]||t.socket?.remoteAddress},async()=>{let o=()=>{s.endTrace(r.statusCode||200,{route:g(n)})};r.once("finish",o),r.once("close",o);try{return await e(t,r)}catch(a){throw a}})};var k=(e,t,r)=>{t&&t.apiKey&&s.init(t),e.addHook("onRequest",(n,o,a)=>{n.senzorStart=performance.now(),a()}),e.addHook("onResponse",(n,o,a)=>{let c=performance.now()-(n.senzorStart||performance.now()),i=n.routeOptions?.url||n.routerPath;s.track({method:n.method,route:i||"UNKNOWN",path:n.raw.url||n.url,status:o.statusCode,duration:c,ip:n.ip,userAgent:n.headers["user-agent"]}),a()}),r()};var J={init:e=>s.init(e),flush:()=>s.flush(),requestHandler:U,wrapNextRoute:E,wrapNextPages:H,wrapH3:$,fastifyPlugin:k},At=J;})();
|
|
2
|
+
//# sourceMappingURL=index.global.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/transport.ts","../src/core/context.ts","../src/core/client.ts","../src/instrumentation/http.ts","../src/instrumentation/mongo.ts","../src/instrumentation/pg.ts","../src/middleware/express.ts","../src/core/normalizer.ts","../src/wrappers/h3.ts","../src/wrappers/next.ts","../src/wrappers/fastify.ts","../src/index.ts"],"sourcesContent":["import { SenzorOptions } from './types';\r\n\r\nexport class Transport {\r\n private queue: any[] = [];\r\n private timer: NodeJS.Timeout | null = null;\r\n\r\n constructor(private config: SenzorOptions) {\r\n if (typeof setInterval !== 'undefined') {\r\n this.timer = setInterval(() => this.flush(), config.flushInterval || 10000);\r\n if (this.timer && typeof this.timer.unref === 'function') {\r\n this.timer.unref(); // Don't block process exit\r\n }\r\n }\r\n }\r\n\r\n public add(trace: any) {\r\n this.queue.push(trace);\r\n if (this.queue.length >= (this.config.batchSize || 100)) {\r\n this.flush();\r\n }\r\n }\r\n\r\n public async flush() {\r\n if (this.queue.length === 0) return;\r\n\r\n const batch = [...this.queue];\r\n this.queue = [];\r\n\r\n try {\r\n // Use global fetch (Node 18+)\r\n await fetch(this.config.endpoint || 'https://api.senzor.dev/api/ingest/apm', {\r\n method: 'POST',\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n 'x-service-api-key': this.config.apiKey,\r\n },\r\n body: JSON.stringify(batch),\r\n keepalive: true,\r\n });\r\n \r\n if (this.config.debug) console.log(`[Senzor] Flushed ${batch.length} traces`);\r\n } catch (err) {\r\n if (this.config.debug) console.error('[Senzor] Ingestion Error:', err);\r\n // Dropping data to prevent memory leaks is preferred in APM\r\n }\r\n }\r\n}","import { AsyncLocalStorage } from 'async_hooks';\r\nimport { ActiveTrace } from './types';\r\n\r\n// Storage to hold the current Trace for any async operation\r\nexport const storage = new AsyncLocalStorage<ActiveTrace>();\r\n\r\nexport const Context = {\r\n // Run a function within a trace context\r\n // Updated to be Generic <T> to allow returning values (Promises, etc)\r\n run: <T>(trace: ActiveTrace, fn: () => T): T => {\r\n return storage.run(trace, fn);\r\n },\r\n\r\n // Get current trace (safe)\r\n current: (): ActiveTrace | undefined => {\r\n return storage.getStore();\r\n },\r\n\r\n // Add a span to the current trace\r\n addSpan: (span: any) => {\r\n const store = storage.getStore();\r\n if (store) {\r\n store.spans.push(span);\r\n }\r\n }\r\n};","import { Transport } from './transport';\r\nimport { Context } from './context';\r\nimport { SenzorOptions, ActiveTrace } from './types';\r\nimport { randomUUID } from 'crypto';\r\nimport { instrumentHttp } from '../instrumentation/http';\r\nimport { instrumentMongo } from '../instrumentation/mongo';\r\nimport { instrumentPg } from '../instrumentation/pg';\r\n\r\nexport class SenzorClient {\r\n private transport: Transport | null = null;\r\n private options: SenzorOptions | null = null;\r\n private isInstrumented = false;\r\n\r\n public init(options: SenzorOptions) {\r\n if (!options.apiKey) {\r\n console.warn('[Senzor] API Key missing. SDK disabled.');\r\n return;\r\n }\r\n this.options = options;\r\n const endpoint = options.endpoint || 'https://api.senzor.dev/api/ingest/apm';\r\n\r\n this.transport = new Transport({\r\n ...options,\r\n endpoint\r\n });\r\n\r\n if (!this.isInstrumented) {\r\n try { instrumentHttp(endpoint); } catch (e) { }\r\n try { instrumentMongo(); } catch (e) { }\r\n try { instrumentPg(); } catch (e) { }\r\n\r\n this.isInstrumented = true;\r\n if (options.debug) console.log('[Senzor] Auto-instrumentation enabled');\r\n }\r\n\r\n if (options.debug) console.log('[Senzor] Initialized');\r\n }\r\n\r\n public startTrace<T>(data: Partial<ActiveTrace['data']>, next: () => T): T {\r\n if (!this.transport) return next();\r\n\r\n const trace: ActiveTrace = {\r\n id: randomUUID(),\r\n startTime: performance.now(),\r\n data: data,\r\n spans: []\r\n };\r\n\r\n return Context.run(trace, next);\r\n }\r\n\r\n public endTrace(status: number, extraData: any = {}) {\r\n const trace = Context.current();\r\n if (!trace || !this.transport) return;\r\n\r\n const duration = performance.now() - trace.startTime;\r\n\r\n const payload = {\r\n traceId: trace.id,\r\n ...trace.data,\r\n ...extraData,\r\n status,\r\n duration,\r\n spans: trace.spans,\r\n timestamp: new Date().toISOString()\r\n };\r\n\r\n this.transport.add(payload);\r\n }\r\n\r\n public track(data: {\r\n method: string;\r\n route: string;\r\n path: string;\r\n status: number;\r\n duration: number;\r\n ip?: string;\r\n userAgent?: string;\r\n }) {\r\n if (!this.transport) return;\r\n const payload = {\r\n traceId: randomUUID(),\r\n ...data,\r\n spans: [],\r\n timestamp: new Date().toISOString()\r\n };\r\n this.transport.add(payload);\r\n }\r\n\r\n public startSpan(name: string, type: 'db' | 'http' | 'function' | 'custom' = 'custom') {\r\n const trace = Context.current();\r\n if (!trace) return { end: () => { } };\r\n\r\n const startTime = performance.now() - trace.startTime;\r\n const spanStartAbs = performance.now();\r\n\r\n return {\r\n end: (meta?: any, status?: number) => {\r\n const duration = performance.now() - spanStartAbs;\r\n Context.addSpan({\r\n name,\r\n type,\r\n startTime,\r\n duration,\r\n status,\r\n meta\r\n });\r\n }\r\n };\r\n }\r\n\r\n public async flush() {\r\n if (this.transport) await this.transport.flush();\r\n }\r\n}\r\n\r\nexport const client = new SenzorClient();","import http from 'http';\r\nimport https from 'https';\r\nimport { URL } from 'url';\r\nimport { Context } from '../core/context';\r\n\r\n// Helper to safely wrap modules\r\nconst shimmer = (module: any, methodName: string, wrapper: (original: Function) => Function) => {\r\n if (!module[methodName]) return;\r\n const original = module[methodName];\r\n module[methodName] = wrapper(original);\r\n};\r\n\r\nexport const instrumentHttp = (ingestUrl: string) => {\r\n const ingestHost = new URL(ingestUrl).hostname;\r\n\r\n const requestWrapper = (original: Function) => {\r\n return function (this: any, ...args: any[]) {\r\n // 1. Parse Arguments to get URL\r\n let options: any = {};\r\n let urlStr = '';\r\n\r\n if (typeof args[0] === 'string' || args[0] instanceof URL) {\r\n urlStr = args[0].toString();\r\n options = args[1] || {};\r\n } else {\r\n options = args[0] || {};\r\n const protocol = options.protocol || 'http:';\r\n const host = options.hostname || options.host || 'localhost';\r\n const path = options.path || '/';\r\n urlStr = `${protocol}//${host}${path}`;\r\n }\r\n\r\n // 2. SAFETY GUARD: Ignore calls to Senzor Ingest API (Prevent Infinite Loop)\r\n if (urlStr.includes(ingestHost) || (options.hostname && options.hostname.includes(ingestHost))) {\r\n return original.apply(this, args);\r\n }\r\n\r\n // 3. Check if we are inside an Active Trace\r\n // If we are not handling a user request, don't trace background http calls\r\n const trace = Context.current();\r\n if (!trace) {\r\n return original.apply(this, args);\r\n }\r\n\r\n // 4. Start Span\r\n const method = (options.method || 'GET').toUpperCase();\r\n const startTime = performance.now() - trace.startTime; // Relative to trace start\r\n const spanStartAbs = performance.now();\r\n\r\n // 5. Execute Request\r\n const req = original.apply(this, args);\r\n\r\n // 6. Hook into Response/Error\r\n req.on('response', (res: any) => {\r\n // Wait for end of stream to calculate full duration (TTFB + Download)\r\n res.on('end', () => {\r\n const duration = performance.now() - spanStartAbs;\r\n \r\n Context.addSpan({\r\n name: `${method} ${new URL(urlStr).hostname}`, // e.g. \"GET api.stripe.com\"\r\n type: 'http',\r\n startTime,\r\n duration,\r\n status: res.statusCode,\r\n meta: {\r\n url: urlStr,\r\n method: method,\r\n }\r\n });\r\n });\r\n });\r\n\r\n req.on('error', (err: Error) => {\r\n const duration = performance.now() - spanStartAbs;\r\n Context.addSpan({\r\n name: `${method} ${urlStr}`,\r\n type: 'http',\r\n startTime,\r\n duration,\r\n status: 500, // Client Error\r\n meta: { error: err.message }\r\n });\r\n });\r\n\r\n return req;\r\n };\r\n };\r\n\r\n // Apply patches\r\n shimmer(http, 'request', requestWrapper);\r\n shimmer(http, 'get', requestWrapper);\r\n shimmer(https, 'request', requestWrapper);\r\n shimmer(https, 'get', requestWrapper);\r\n};","import { Context } from '../core/context';\r\n\r\nexport const instrumentMongo = () => {\r\n try {\r\n const mongodb = require('mongodb');\r\n const Collection = mongodb.Collection;\r\n\r\n const methods = [\r\n 'find',\r\n 'findOne',\r\n 'insertOne',\r\n 'insertMany',\r\n 'updateOne',\r\n 'updateMany',\r\n 'deleteOne',\r\n 'deleteMany',\r\n 'aggregate',\r\n 'countDocuments'\r\n ];\r\n\r\n methods.forEach((method) => {\r\n if (!Collection.prototype[method]) return;\r\n\r\n const original = Collection.prototype[method];\r\n\r\n Collection.prototype[method] = function (...args: any[]) {\r\n const trace = Context.current();\r\n // If not inside a tracked request, just run normally\r\n if (!trace) return original.apply(this, args);\r\n\r\n const startTime = performance.now() - trace.startTime;\r\n const spanStartAbs = performance.now();\r\n const collectionName = this.collectionName;\r\n\r\n // Helper to finish span\r\n const endSpan = (err?: Error) => {\r\n const duration = performance.now() - spanStartAbs;\r\n Context.addSpan({\r\n name: `MongoDB ${method} (${collectionName})`,\r\n type: 'db',\r\n startTime,\r\n duration,\r\n status: err ? 500 : 0,\r\n meta: {\r\n collection: collectionName,\r\n operation: method,\r\n error: err ? err.message : undefined\r\n }\r\n });\r\n };\r\n\r\n try {\r\n const result = original.apply(this, args);\r\n\r\n // Handle Promise/Cursor\r\n if (result && typeof result.then === 'function') {\r\n return result.then(\r\n (res: any) => { endSpan(); return res; },\r\n (err: any) => { endSpan(err); throw err; }\r\n );\r\n }\r\n // Handle FindCursor (it doesn't execute immediately, but for APM visuals we mark start/return)\r\n // Ideally we'd wrap toArray() but that's complex. For 'find', we just mark the creation.\r\n endSpan();\r\n return result;\r\n\r\n } catch (err: any) {\r\n endSpan(err);\r\n throw err;\r\n }\r\n };\r\n });\r\n\r\n } catch (e) {\r\n // User doesn't use mongodb, ignore\r\n }\r\n};","import { Context } from '../core/context';\r\n\r\n// Simple shim for 'pg' library\r\nexport const instrumentPg = () => {\r\n try {\r\n // Try to require pg (it might not be installed by user)\r\n const pg = require('pg');\r\n const originalQuery = pg.Client.prototype.query;\r\n\r\n pg.Client.prototype.query = function (...args: any[]) {\r\n const trace = Context.current();\r\n if (!trace) return originalQuery.apply(this, args);\r\n\r\n const startTime = performance.now() - trace.startTime;\r\n const spanStartAbs = performance.now();\r\n\r\n // Extract SQL (first arg usually string or config object)\r\n const sql = typeof args[0] === 'string' ? args[0] : args[0].text;\r\n\r\n // Wrap callback if present, or handle Promise\r\n const result = originalQuery.apply(this, args);\r\n\r\n if (result && typeof result.then === 'function') {\r\n return result.then((res: any) => {\r\n const duration = performance.now() - spanStartAbs;\r\n Context.addSpan({\r\n name: 'Postgres Query',\r\n type: 'db',\r\n startTime,\r\n duration,\r\n meta: { query: sql }\r\n });\r\n return res;\r\n });\r\n }\r\n return result;\r\n };\r\n } catch (e) {\r\n // User doesn't use pg, ignore\r\n }\r\n};","import { client } from '../core/client';\r\n\r\nexport const expressMiddleware = () => {\r\n return (req: any, res: any, next: () => void) => {\r\n // We MUST use startTrace to enable Auto-Instrumentation for this request\r\n client.startTrace({\r\n method: req.method,\r\n path: req.originalUrl || req.url,\r\n ip: req.ip || req.socket?.remoteAddress,\r\n userAgent: req.headers['user-agent'],\r\n }, () => {\r\n\r\n res.once('finish', () => {\r\n try {\r\n let route = 'UNKNOWN';\r\n if (req.route && req.route.path) {\r\n route = (req.baseUrl || '') + req.route.path;\r\n } else if (res.statusCode === 404) {\r\n route = 'Not Found';\r\n } else {\r\n route = req.path || 'Wildcard';\r\n }\r\n\r\n client.endTrace(res.statusCode, { route });\r\n } catch (e) {\r\n // Fail open\r\n }\r\n });\r\n\r\n next();\r\n });\r\n };\r\n};","/**\r\n * Heuristic URL Normalizer\r\n * Converts raw paths with IDs into generic patterns to prevent high cardinality.\r\n * Example: /users/123/orders/abc-def -> /users/:id/orders/:uuid\r\n */\r\nexport const normalizePath = (path: string): string => {\r\n if (!path || path === '/') return '/';\r\n\r\n return path\r\n // Replace UUIDs (long alphanumeric strings)\r\n .replace(\r\n /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/g,\r\n ':uuid'\r\n )\r\n // Replace MongoDB ObjectIds (24 hex chars)\r\n .replace(/[0-9a-fA-F]{24}/g, ':objectId')\r\n // Replace pure numeric IDs (e.g., /123)\r\n .replace(/\\/(\\d+)(?=\\/|$)/g, '/:id')\r\n // Remove query strings\r\n .split('?')[0];\r\n};\r\n\r\n/**\r\n * Tries to extract route from Framework internals, falls back to heuristic\r\n */\r\nexport const getRoute = (req: any, fallbackPath: string): string => {\r\n // Express / Connect\r\n if (req.route && req.route.path) {\r\n return (req.baseUrl || '') + req.route.path;\r\n }\r\n\r\n // H3 / Nitro (Nuxt)\r\n if (req.context && req.context.matchedRoute) {\r\n return req.context.matchedRoute.path;\r\n }\r\n\r\n // Fastify\r\n if (req.routerPath) {\r\n return req.routerPath;\r\n }\r\n\r\n // Fallback: Heuristic Normalization\r\n return normalizePath(fallbackPath);\r\n};","import { client } from '../core/client';\r\nimport { getRoute } from '../core/normalizer';\r\n\r\n// Minimal types for H3 to avoid peer-deps\r\ntype EventHandler = (event: any) => any;\r\n\r\nexport const wrapH3 = (handler: EventHandler) => {\r\n return (event: any) => {\r\n const req = event.node.req;\r\n const path = req.originalUrl || req.url || '/';\r\n\r\n // Start Trace Context\r\n return client.startTrace({\r\n method: req.method || 'GET',\r\n path: path,\r\n ip: req.headers['x-forwarded-for'] || req.socket?.remoteAddress,\r\n userAgent: req.headers['user-agent'],\r\n }, async () => {\r\n try {\r\n const response = await handler(event);\r\n\r\n // H3/Nitro response status\r\n let status = 200;\r\n if (event.node.res.statusCode) status = event.node.res.statusCode;\r\n // Check if response is an error object\r\n if (response && response.statusCode) status = response.statusCode;\r\n\r\n client.endTrace(status, { route: getRoute(event, path) });\r\n return response;\r\n } catch (err: any) {\r\n const status = err.statusCode || err.status || 500;\r\n client.endTrace(status, { route: getRoute(event, path) });\r\n throw err;\r\n }\r\n });\r\n };\r\n};","import { client } from '../core/client';\r\nimport { normalizePath } from '../core/normalizer';\r\n\r\n// --- App Router Wrapper (Route Handlers) ---\r\nexport const wrapNextRoute = (handler: Function) => {\r\n return async (req: Request | any, context?: any) => {\r\n // 1. Extract Info\r\n const url = req.url ? new URL(req.url) : { pathname: '/' };\r\n const method = req.method || 'GET';\r\n const ua = req.headers.get ? req.headers.get('user-agent') : undefined;\r\n const ip = req.headers.get ? req.headers.get('x-forwarded-for') : undefined;\r\n\r\n // 2. Run in Context\r\n return client.startTrace({\r\n method,\r\n path: url.pathname,\r\n userAgent: ua,\r\n ip: ip\r\n }, async () => {\r\n try {\r\n const response = await handler(req, context);\r\n const status = response?.status || 200;\r\n \r\n client.endTrace(status, { route: normalizePath(url.pathname) });\r\n return response;\r\n } catch (err: any) {\r\n client.endTrace(500, { route: normalizePath(url.pathname) });\r\n throw err;\r\n }\r\n });\r\n };\r\n};\r\n\r\n// --- Pages Router Wrapper (API Routes) ---\r\nexport const wrapNextPages = (handler: Function) => {\r\n return async (req: any, res: any) => {\r\n const path = req.url ? req.url.split('?')[0] : '/';\r\n \r\n // 1. Run in Context\r\n return client.startTrace({\r\n method: req.method || 'GET',\r\n path: path,\r\n userAgent: req.headers['user-agent'],\r\n ip: req.headers['x-forwarded-for'] || req.socket?.remoteAddress,\r\n }, async () => {\r\n \r\n // 2. Hook Response\r\n const done = () => {\r\n client.endTrace(res.statusCode || 200, { route: normalizePath(path) });\r\n };\r\n \r\n res.once('finish', done);\r\n res.once('close', done); // Fallback if finish doesn't fire\r\n\r\n // 3. Execute\r\n try {\r\n return await handler(req, res);\r\n } catch (e) {\r\n // Next.js Pages router usually handles errors internally, \r\n // but we ensure we catch sync errors here\r\n throw e;\r\n }\r\n });\r\n };\r\n};","import { client } from '../core/client';\r\nimport { SenzorOptions } from '../core/types';\r\n\r\n// We don't import Fastify types to keep zero-deps, but structure matches\r\nexport const senzorPlugin = (fastify: any, options: SenzorOptions, done: Function) => {\r\n\r\n // Init if options provided inline, otherwise assume global init\r\n if (options && options.apiKey) {\r\n client.init(options);\r\n }\r\n\r\n // Hook: On Request (Start Timer)\r\n fastify.addHook('onRequest', (request: any, reply: any, next: Function) => {\r\n request.senzorStart = performance.now();\r\n next();\r\n });\r\n\r\n // Hook: On Response (End Timer & Track)\r\n fastify.addHook('onResponse', (request: any, reply: any, next: Function) => {\r\n const duration = performance.now() - (request.senzorStart || performance.now());\r\n\r\n // Fastify provides 'routerPath' (e.g. /user/:id)\r\n const route = request.routeOptions?.url || request.routerPath;\r\n\r\n client.track({\r\n method: request.method,\r\n route: route || 'UNKNOWN',\r\n path: request.raw.url || request.url,\r\n status: reply.statusCode,\r\n duration: duration,\r\n ip: request.ip,\r\n userAgent: request.headers['user-agent']\r\n });\r\n\r\n next();\r\n });\r\n\r\n done();\r\n};","import { client } from './core/client';\r\nimport { expressMiddleware } from './middleware/express';\r\nimport { wrapH3 } from './wrappers/h3';\r\nimport { wrapNextRoute, wrapNextPages } from './wrappers/next';\r\nimport { senzorPlugin } from './wrappers/fastify';\r\nimport { SenzorOptions } from './core/types';\r\n\r\nconst Senzor = {\r\n // Core\r\n init: (options: SenzorOptions) => client.init(options),\r\n flush: () => client.flush(),\r\n\r\n // Express / Connect\r\n requestHandler: expressMiddleware,\r\n\r\n // Next.js\r\n wrapNextRoute, // For App Router (Route Handlers)\r\n wrapNextPages, // For Pages Router (API Routes)\r\n\r\n // H3 / Nuxt / Nitro\r\n wrapH3,\r\n\r\n // Fastify\r\n fast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AI,GAGvEI,EAAQJ,EAAQ,cAAc,KAAOA,EAAQ,WAEnDD,EAAO,MAAM,CACX,OAAQC,EAAQ,OAChB,MAAOI,GAAS,UAChB,KAAMJ,EAAQ,IAAI,KAAOA,EAAQ,IACjC,OAAQC,EAAM,WACd,SAAUE,EACV,GAAIH,EAAQ,GACZ,UAAWA,EAAQ,QAAQ,YAAY,CACzC,CAAC,EAEDE,EAAK,CACP,CAAC,EAEDJ,EAAK,CACP,EC/BA,IAAMO,EAAS,CAEb,KAAOC,GAA2BC,EAAO,KAAKD,CAAO,EACrD,MAAO,IAAMC,EAAO,MAAM,EAG1B,eAAgBC,EAGhB,cAAAC,EACA,cAAAC,EAGA,OAAAC,EAGA,cAAeC,CACjB,EAEOC,GAAQR","names":["Transport","config","trace","batch","err","import_async_hooks","storage","Context","trace","fn","span","store","import_crypto","import_http","import_https","import_url","shimmer","module","methodName","wrapper","original","instrumentHttp","ingestUrl","ingestHost","requestWrapper","args","options","urlStr","protocol","host","path","trace","Context","method","startTime","spanStartAbs","req","res","duration","err","http","https","instrumentMongo","Collection","method","original","args","trace","Context","startTime","spanStartAbs","collectionName","endSpan","err","duration","result","res","instrumentPg","pg","originalQuery","args","trace","Context","startTime","spanStartAbs","sql","result","res","duration","SenzorClient","options","endpoint","Transport","instrumentHttp","instrumentMongo","instrumentPg","data","next","trace","Context","status","extraData","duration","payload","name","type","startTime","spanStartAbs","meta","client","expressMiddleware","req","res","next","client","route","normalizePath","path","getRoute","req","fallbackPath","wrapH3","handler","event","req","path","client","response","status","getRoute","err","wrapNextRoute","handler","req","context","url","method","ua","ip","client","response","status","normalizePath","err","wrapNextPages","res","path","done","e","senzorPlugin","fastify","options","done","client","request","reply","next","duration","route","Senzor","options","client","expressMiddleware","wrapNextRoute","wrapNextPages","wrapH3","senzorPlugin","index_default"]}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var D=Object.create;var g=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var L=Object.getPrototypeOf,j=Object.prototype.hasOwnProperty;var J=(e,t)=>{for(var r in t)g(e,r,{get:t[r],enumerable:!0})},v=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of W(t))!j.call(e,o)&&o!==r&&g(e,o,{get:()=>t[o],enumerable:!(n=G(t,o))||n.enumerable});return e};var F=(e,t,r)=>(r=e!=null?D(L(e)):{},v(t||!e||!e.__esModule?g(r,"default",{value:e,enumerable:!0}):r,e)),Q=e=>v(g({},"__esModule",{value:!0}),e);var V={};J(V,{Senzor:()=>M,default:()=>B});module.exports=Q(V);var w=class{constructor(t){this.config=t;this.queue=[];this.timer=null;typeof setInterval<"u"&&(this.timer=setInterval(()=>this.flush(),t.flushInterval||1e4),this.timer&&typeof this.timer.unref=="function"&&this.timer.unref())}add(t){this.queue.push(t),this.queue.length>=(this.config.batchSize||100)&&this.flush()}async flush(){if(this.queue.length===0)return;let t=[...this.queue];this.queue=[];try{await fetch(this.config.endpoint||"https://api.senzor.dev/api/ingest/apm",{method:"POST",headers:{"Content-Type":"application/json","x-service-api-key":this.config.apiKey},body:JSON.stringify(t),keepalive:!0}),this.config.debug&&console.log(`[Senzor] Flushed ${t.length} traces`)}catch(r){this.config.debug&&console.error("[Senzor] Ingestion Error:",r)}}};var P=require("async_hooks"),x=new P.AsyncLocalStorage,u={run:(e,t)=>x.run(e,t),current:()=>x.getStore(),addSpan:e=>{let t=x.getStore();t&&t.spans.push(e)}};var z=require("crypto");var b=F(require("http")),A=F(require("https")),S=require("url");var T=(e,t,r)=>{if(!e[t])return;let n=e[t];e[t]=r(n)},I=e=>{let t=new S.URL(e).hostname,r=n=>function(...o){let a={},c="";if(typeof o[0]=="string"||o[0]instanceof S.URL)c=o[0].toString(),a=o[1]||{};else{a=o[0]||{};let d=a.protocol||"http:",l=a.hostname||a.host||"localhost",K=a.path||"/";c=`${d}//${l}${K}`}if(c.includes(t)||a.hostname&&a.hostname.includes(t))return n.apply(this,o);let i=u.current();if(!i)return n.apply(this,o);let m=(a.method||"GET").toUpperCase(),h=performance.now()-i.startTime,f=performance.now(),p=n.apply(this,o);return p.on("response",d=>{d.on("end",()=>{let l=performance.now()-f;u.addSpan({name:`${m} ${new S.URL(c).hostname}`,type:"http",startTime:h,duration:l,status:d.statusCode,meta:{url:c,method:m}})})}),p.on("error",d=>{let l=performance.now()-f;u.addSpan({name:`${m} ${c}`,type:"http",startTime:h,duration:l,status:500,meta:{error:d.message}})}),p};T(b.default,"request",r),T(b.default,"get",r),T(A.default,"request",r),T(A.default,"get",r)};var N=()=>{try{let t=require("mongodb").Collection;["find","findOne","insertOne","insertMany","updateOne","updateMany","deleteOne","deleteMany","aggregate","countDocuments"].forEach(n=>{if(!t.prototype[n])return;let o=t.prototype[n];t.prototype[n]=function(...a){let c=u.current();if(!c)return o.apply(this,a);let i=performance.now()-c.startTime,m=performance.now(),h=this.collectionName,f=p=>{let d=performance.now()-m;u.addSpan({name:`MongoDB ${n} (${h})`,type:"db",startTime:i,duration:d,status:p?500:0,meta:{collection:h,operation:n,error:p?p.message:void 0}})};try{let p=o.apply(this,a);return p&&typeof p.then=="function"?p.then(d=>(f(),d),d=>{throw f(d),d}):(f(),p)}catch(p){throw f(p),p}}})}catch{}};var R=()=>{try{let e=require("pg"),t=e.Client.prototype.query;e.Client.prototype.query=function(...r){let n=u.current();if(!n)return t.apply(this,r);let o=performance.now()-n.startTime,a=performance.now(),c=typeof r[0]=="string"?r[0]:r[0].text,i=t.apply(this,r);return i&&typeof i.then=="function"?i.then(m=>{let h=performance.now()-a;return u.addSpan({name:"Postgres Query",type:"db",startTime:o,duration:h,meta:{query:c}}),m}):i}}catch{}};var C=class{constructor(){this.transport=null;this.options=null;this.isInstrumented=!1}init(t){if(!t.apiKey){console.warn("[Senzor] API Key missing. SDK disabled.");return}this.options=t;let r=t.endpoint||"https://api.senzor.dev/api/ingest/apm";if(this.transport=new w({...t,endpoint:r}),!this.isInstrumented){try{I(r)}catch{}try{N()}catch{}try{R()}catch{}this.isInstrumented=!0,t.debug&&console.log("[Senzor] Auto-instrumentation enabled")}t.debug&&console.log("[Senzor] Initialized")}startTrace(t,r){if(!this.transport)return r();let n={id:(0,z.randomUUID)(),startTime:performance.now(),data:t,spans:[]};return u.run(n,r)}endTrace(t,r={}){let n=u.current();if(!n||!this.transport)return;let o=performance.now()-n.startTime,a={traceId:n.id,...n.data,...r,status:t,duration:o,spans:n.spans,timestamp:new Date().toISOString()};this.transport.add(a)}track(t){if(!this.transport)return;let r={traceId:(0,z.randomUUID)(),...t,spans:[],timestamp:new Date().toISOString()};this.transport.add(r)}startSpan(t,r="custom"){let n=u.current();if(!n)return{end:()=>{}};let o=performance.now()-n.startTime,a=performance.now();return{end:(c,i)=>{let m=performance.now()-a;u.addSpan({name:t,type:r,startTime:o,duration:m,status:i,meta:c})}}}async flush(){this.transport&&await this.transport.flush()}},s=new C;var U=()=>(e,t,r)=>{s.startTrace({method:e.method,path:e.originalUrl||e.url,ip:e.ip||e.socket?.remoteAddress,userAgent:e.headers["user-agent"]},()=>{t.once("finish",()=>{try{let n="UNKNOWN";e.route&&e.route.path?n=(e.baseUrl||"")+e.route.path:t.statusCode===404?n="Not Found":n=e.path||"Wildcard",s.endTrace(t.statusCode,{route:n})}catch{}}),r()})};var y=e=>!e||e==="/"?"/":e.replace(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/g,":uuid").replace(/[0-9a-fA-F]{24}/g,":objectId").replace(/\/(\d+)(?=\/|$)/g,"/:id").split("?")[0],O=(e,t)=>e.route&&e.route.path?(e.baseUrl||"")+e.route.path:e.context&&e.context.matchedRoute?e.context.matchedRoute.path:e.routerPath?e.routerPath:y(t);var $=e=>t=>{let r=t.node.req,n=r.originalUrl||r.url||"/";return s.startTrace({method:r.method||"GET",path:n,ip:r.headers["x-forwarded-for"]||r.socket?.remoteAddress,userAgent:r.headers["user-agent"]},async()=>{try{let o=await e(t),a=200;return t.node.res.statusCode&&(a=t.node.res.statusCode),o&&o.statusCode&&(a=o.statusCode),s.endTrace(a,{route:O(t,n)}),o}catch(o){let a=o.statusCode||o.status||500;throw s.endTrace(a,{route:O(t,n)}),o}})};var E=e=>async(t,r)=>{let n=t.url?new URL(t.url):{pathname:"/"},o=t.method||"GET",a=t.headers.get?t.headers.get("user-agent"):void 0,c=t.headers.get?t.headers.get("x-forwarded-for"):void 0;return s.startTrace({method:o,path:n.pathname,userAgent:a,ip:c},async()=>{try{let i=await e(t,r),m=i?.status||200;return s.endTrace(m,{route:y(n.pathname)}),i}catch(i){throw s.endTrace(500,{route:y(n.pathname)}),i}})},H=e=>async(t,r)=>{let n=t.url?t.url.split("?")[0]:"/";return s.startTrace({method:t.method||"GET",path:n,userAgent:t.headers["user-agent"],ip:t.headers["x-forwarded-for"]||t.socket?.remoteAddress},async()=>{let o=()=>{s.endTrace(r.statusCode||200,{route:y(n)})};r.once("finish",o),r.once("close",o);try{return await e(t,r)}catch(a){throw a}})};var k=(e,t,r)=>{t&&t.apiKey&&s.init(t),e.addHook("onRequest",(n,o,a)=>{n.senzorStart=performance.now(),a()}),e.addHook("onResponse",(n,o,a)=>{let c=performance.now()-(n.senzorStart||performance.now()),i=n.routeOptions?.url||n.routerPath;s.track({method:n.method,route:i||"UNKNOWN",path:n.raw.url||n.url,status:o.statusCode,duration:c,ip:n.ip,userAgent:n.headers["user-agent"]}),a()}),r()};var M={init:e=>s.init(e),flush:()=>s.flush(),requestHandler:U,wrapNextRoute:E,wrapNextPages:H,wrapH3:$,fastifyPlugin:k},B=M;0&&(module.exports={Senzor});
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/core/transport.ts","../src/core/context.ts","../src/core/client.ts","../src/instrumentation/http.ts","../src/instrumentation/mongo.ts","../src/instrumentation/pg.ts","../src/middleware/express.ts","../src/core/normalizer.ts","../src/wrappers/h3.ts","../src/wrappers/next.ts","../src/wrappers/fastify.ts"],"sourcesContent":["import { client } from './core/client';\r\nimport { expressMiddleware } from './middleware/express';\r\nimport { wrapH3 } from './wrappers/h3';\r\nimport { wrapNextRoute, wrapNextPages } from './wrappers/next';\r\nimport { senzorPlugin } from './wrappers/fastify';\r\nimport { SenzorOptions } from './core/types';\r\n\r\nconst Senzor = {\r\n // Core\r\n init: (options: SenzorOptions) => client.init(options),\r\n flush: () => client.flush(),\r\n\r\n // Express / Connect\r\n requestHandler: expressMiddleware,\r\n\r\n // Next.js\r\n wrapNextRoute, // For App Router (Route Handlers)\r\n wrapNextPages, // For Pages Router (API Routes)\r\n\r\n // H3 / Nuxt / Nitro\r\n wrapH3,\r\n\r\n // Fastify\r\n fastifyPlugin: senzorPlugin\r\n};\r\n\r\nexport default Senzor;\r\nexport { Senzor };","import { SenzorOptions } from './types';\r\n\r\nexport class Transport {\r\n private queue: any[] = [];\r\n private timer: NodeJS.Timeout | null = null;\r\n\r\n constructor(private config: SenzorOptions) {\r\n if (typeof setInterval !== 'undefined') {\r\n this.timer = setInterval(() => this.flush(), config.flushInterval || 10000);\r\n if (this.timer && typeof this.timer.unref === 'function') {\r\n this.timer.unref(); // Don't block process exit\r\n }\r\n }\r\n }\r\n\r\n public add(trace: any) {\r\n this.queue.push(trace);\r\n if (this.queue.length >= (this.config.batchSize || 100)) {\r\n this.flush();\r\n }\r\n }\r\n\r\n public async flush() {\r\n if (this.queue.length === 0) return;\r\n\r\n const batch = [...this.queue];\r\n this.queue = [];\r\n\r\n try {\r\n // Use global fetch (Node 18+)\r\n await fetch(this.config.endpoint || 'https://api.senzor.dev/api/ingest/apm', {\r\n method: 'POST',\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n 'x-service-api-key': this.config.apiKey,\r\n },\r\n body: JSON.stringify(batch),\r\n keepalive: true,\r\n });\r\n \r\n if (this.config.debug) console.log(`[Senzor] Flushed ${batch.length} traces`);\r\n } catch (err) {\r\n if (this.config.debug) console.error('[Senzor] Ingestion Error:', err);\r\n // Dropping data to prevent memory leaks is preferred in APM\r\n }\r\n }\r\n}","import { AsyncLocalStorage } from 'async_hooks';\r\nimport { ActiveTrace } from './types';\r\n\r\n// Storage to hold the current Trace for any async operation\r\nexport const storage = new AsyncLocalStorage<ActiveTrace>();\r\n\r\nexport const Context = {\r\n // Run a function within a trace context\r\n // Updated to be Generic <T> to allow returning values (Promises, etc)\r\n run: <T>(trace: ActiveTrace, fn: () => T): T => {\r\n return storage.run(trace, fn);\r\n },\r\n\r\n // Get current trace (safe)\r\n current: (): ActiveTrace | undefined => {\r\n return storage.getStore();\r\n },\r\n\r\n // Add a span to the current trace\r\n addSpan: (span: any) => {\r\n const store = storage.getStore();\r\n if (store) {\r\n store.spans.push(span);\r\n }\r\n }\r\n};","import { Transport } from './transport';\r\nimport { Context } from './context';\r\nimport { SenzorOptions, ActiveTrace } from './types';\r\nimport { randomUUID } from 'crypto';\r\nimport { instrumentHttp } from '../instrumentation/http';\r\nimport { instrumentMongo } from '../instrumentation/mongo';\r\nimport { instrumentPg } from '../instrumentation/pg';\r\n\r\nexport class SenzorClient {\r\n private transport: Transport | null = null;\r\n private options: SenzorOptions | null = null;\r\n private isInstrumented = false;\r\n\r\n public init(options: SenzorOptions) {\r\n if (!options.apiKey) {\r\n console.warn('[Senzor] API Key missing. SDK disabled.');\r\n return;\r\n }\r\n this.options = options;\r\n const endpoint = options.endpoint || 'https://api.senzor.dev/api/ingest/apm';\r\n\r\n this.transport = new Transport({\r\n ...options,\r\n endpoint\r\n });\r\n\r\n if (!this.isInstrumented) {\r\n try { instrumentHttp(endpoint); } catch (e) { }\r\n try { instrumentMongo(); } catch (e) { }\r\n try { instrumentPg(); } catch (e) { }\r\n\r\n this.isInstrumented = true;\r\n if (options.debug) console.log('[Senzor] Auto-instrumentation enabled');\r\n }\r\n\r\n if (options.debug) console.log('[Senzor] Initialized');\r\n }\r\n\r\n public startTrace<T>(data: Partial<ActiveTrace['data']>, next: () => T): T {\r\n if (!this.transport) return next();\r\n\r\n const trace: ActiveTrace = {\r\n id: randomUUID(),\r\n startTime: performance.now(),\r\n data: data,\r\n spans: []\r\n };\r\n\r\n return Context.run(trace, next);\r\n }\r\n\r\n public endTrace(status: number, extraData: any = {}) {\r\n const trace = Context.current();\r\n if (!trace || !this.transport) return;\r\n\r\n const duration = performance.now() - trace.startTime;\r\n\r\n const payload = {\r\n traceId: trace.id,\r\n ...trace.data,\r\n ...extraData,\r\n status,\r\n duration,\r\n spans: trace.spans,\r\n timestamp: new Date().toISOString()\r\n };\r\n\r\n this.transport.add(payload);\r\n }\r\n\r\n public track(data: {\r\n method: string;\r\n route: string;\r\n path: string;\r\n status: number;\r\n duration: number;\r\n ip?: string;\r\n userAgent?: string;\r\n }) {\r\n if (!this.transport) return;\r\n const payload = {\r\n traceId: randomUUID(),\r\n ...data,\r\n spans: [],\r\n timestamp: new Date().toISOString()\r\n };\r\n this.transport.add(payload);\r\n }\r\n\r\n public startSpan(name: string, type: 'db' | 'http' | 'function' | 'custom' = 'custom') {\r\n const trace = Context.current();\r\n if (!trace) return { end: () => { } };\r\n\r\n const startTime = performance.now() - trace.startTime;\r\n const spanStartAbs = performance.now();\r\n\r\n return {\r\n end: (meta?: any, status?: number) => {\r\n const duration = performance.now() - spanStartAbs;\r\n Context.addSpan({\r\n name,\r\n type,\r\n startTime,\r\n duration,\r\n status,\r\n meta\r\n });\r\n }\r\n };\r\n }\r\n\r\n public async flush() {\r\n if (this.transport) await this.transport.flush();\r\n }\r\n}\r\n\r\nexport const client = new SenzorClient();","import http from 'http';\r\nimport https from 'https';\r\nimport { URL } from 'url';\r\nimport { Context } from '../core/context';\r\n\r\n// Helper to safely wrap modules\r\nconst shimmer = (module: any, methodName: string, wrapper: (original: Function) => Function) => {\r\n if (!module[methodName]) return;\r\n const original = module[methodName];\r\n module[methodName] = wrapper(original);\r\n};\r\n\r\nexport const instrumentHttp = (ingestUrl: string) => {\r\n const ingestHost = new URL(ingestUrl).hostname;\r\n\r\n const requestWrapper = (original: Function) => {\r\n return function (this: any, ...args: any[]) {\r\n // 1. Parse Arguments to get URL\r\n let options: any = {};\r\n let urlStr = '';\r\n\r\n if (typeof args[0] === 'string' || args[0] instanceof URL) {\r\n urlStr = args[0].toString();\r\n options = args[1] || {};\r\n } else {\r\n options = args[0] || {};\r\n const protocol = options.protocol || 'http:';\r\n const host = options.hostname || options.host || 'localhost';\r\n const path = options.path || '/';\r\n urlStr = `${protocol}//${host}${path}`;\r\n }\r\n\r\n // 2. SAFETY GUARD: Ignore calls to Senzor Ingest API (Prevent Infinite Loop)\r\n if (urlStr.includes(ingestHost) || (options.hostname && options.hostname.includes(ingestHost))) {\r\n return original.apply(this, args);\r\n }\r\n\r\n // 3. Check if we are inside an Active Trace\r\n // If we are not handling a user request, don't trace background http calls\r\n const trace = Context.current();\r\n if (!trace) {\r\n return original.apply(this, args);\r\n }\r\n\r\n // 4. Start Span\r\n const method = (options.method || 'GET').toUpperCase();\r\n const startTime = performance.now() - trace.startTime; // Relative to trace start\r\n const spanStartAbs = performance.now();\r\n\r\n // 5. Execute Request\r\n const req = original.apply(this, args);\r\n\r\n // 6. Hook into Response/Error\r\n req.on('response', (res: any) => {\r\n // Wait for end of stream to calculate full duration (TTFB + Download)\r\n res.on('end', () => {\r\n const duration = performance.now() - spanStartAbs;\r\n \r\n Context.addSpan({\r\n name: `${method} ${new URL(urlStr).hostname}`, // e.g. \"GET api.stripe.com\"\r\n type: 'http',\r\n startTime,\r\n duration,\r\n status: res.statusCode,\r\n meta: {\r\n url: urlStr,\r\n method: method,\r\n }\r\n });\r\n });\r\n });\r\n\r\n req.on('error', (err: Error) => {\r\n const duration = performance.now() - spanStartAbs;\r\n Context.addSpan({\r\n name: `${method} ${urlStr}`,\r\n type: 'http',\r\n startTime,\r\n duration,\r\n status: 500, // Client Error\r\n meta: { error: err.message }\r\n });\r\n });\r\n\r\n return req;\r\n };\r\n };\r\n\r\n // Apply patches\r\n shimmer(http, 'request', requestWrapper);\r\n shimmer(http, 'get', requestWrapper);\r\n shimmer(https, 'request', requestWrapper);\r\n shimmer(https, 'get', requestWrapper);\r\n};","import { Context } from '../core/context';\r\n\r\nexport const instrumentMongo = () => {\r\n try {\r\n const mongodb = require('mongodb');\r\n const Collection = mongodb.Collection;\r\n\r\n const methods = [\r\n 'find',\r\n 'findOne',\r\n 'insertOne',\r\n 'insertMany',\r\n 'updateOne',\r\n 'updateMany',\r\n 'deleteOne',\r\n 'deleteMany',\r\n 'aggregate',\r\n 'countDocuments'\r\n ];\r\n\r\n methods.forEach((method) => {\r\n if (!Collection.prototype[method]) return;\r\n\r\n const original = Collection.prototype[method];\r\n\r\n Collection.prototype[method] = function (...args: any[]) {\r\n const trace = Context.current();\r\n // If not inside a tracked request, just run normally\r\n if (!trace) return original.apply(this, args);\r\n\r\n const startTime = performance.now() - trace.startTime;\r\n const spanStartAbs = performance.now();\r\n const collectionName = this.collectionName;\r\n\r\n // Helper to finish span\r\n const endSpan = (err?: Error) => {\r\n const duration = performance.now() - spanStartAbs;\r\n Context.addSpan({\r\n name: `MongoDB ${method} (${collectionName})`,\r\n type: 'db',\r\n startTime,\r\n duration,\r\n status: err ? 500 : 0,\r\n meta: {\r\n collection: collectionName,\r\n operation: method,\r\n error: err ? err.message : undefined\r\n }\r\n });\r\n };\r\n\r\n try {\r\n const result = original.apply(this, args);\r\n\r\n // Handle Promise/Cursor\r\n if (result && typeof result.then === 'function') {\r\n return result.then(\r\n (res: any) => { endSpan(); return res; },\r\n (err: any) => { endSpan(err); throw err; }\r\n );\r\n }\r\n // Handle FindCursor (it doesn't execute immediately, but for APM visuals we mark start/return)\r\n // Ideally we'd wrap toArray() but that's complex. For 'find', we just mark the creation.\r\n endSpan();\r\n return result;\r\n\r\n } catch (err: any) {\r\n endSpan(err);\r\n throw err;\r\n }\r\n };\r\n });\r\n\r\n } catch (e) {\r\n // User doesn't use mongodb, ignore\r\n }\r\n};","import { Context } from '../core/context';\r\n\r\n// Simple shim for 'pg' library\r\nexport const instrumentPg = () => {\r\n try {\r\n // Try to require pg (it might not be installed by user)\r\n const pg = require('pg');\r\n const originalQuery = pg.Client.prototype.query;\r\n\r\n pg.Client.prototype.query = function (...args: any[]) {\r\n const trace = Context.current();\r\n if (!trace) return originalQuery.apply(this, args);\r\n\r\n const startTime = performance.now() - trace.startTime;\r\n const spanStartAbs = performance.now();\r\n\r\n // Extract SQL (first arg usually string or config object)\r\n const sql = typeof args[0] === 'string' ? args[0] : args[0].text;\r\n\r\n // Wrap callback if present, or handle Promise\r\n const result = originalQuery.apply(this, args);\r\n\r\n if (result && typeof result.then === 'function') {\r\n return result.then((res: any) => {\r\n const duration = performance.now() - spanStartAbs;\r\n Context.addSpan({\r\n name: 'Postgres Query',\r\n type: 'db',\r\n startTime,\r\n duration,\r\n meta: { query: sql }\r\n });\r\n return res;\r\n });\r\n }\r\n return result;\r\n };\r\n } catch (e) {\r\n // User doesn't use pg, ignore\r\n }\r\n};","import { client } from '../core/client';\r\n\r\nexport const expressMiddleware = () => {\r\n return (req: any, res: any, next: () => void) => {\r\n // We MUST use startTrace to enable Auto-Instrumentation for this request\r\n client.startTrace({\r\n method: req.method,\r\n path: req.originalUrl || req.url,\r\n ip: req.ip || req.socket?.remoteAddress,\r\n userAgent: req.headers['user-agent'],\r\n }, () => {\r\n\r\n res.once('finish', () => {\r\n try {\r\n let route = 'UNKNOWN';\r\n if (req.route && req.route.path) {\r\n route = (req.baseUrl || '') + req.route.path;\r\n } else if (res.statusCode === 404) {\r\n route = 'Not Found';\r\n } else {\r\n route = req.path || 'Wildcard';\r\n }\r\n\r\n client.endTrace(res.statusCode, { route });\r\n } catch (e) {\r\n // Fail open\r\n }\r\n });\r\n\r\n next();\r\n });\r\n };\r\n};","/**\r\n * Heuristic URL Normalizer\r\n * Converts raw paths with IDs into generic patterns to prevent high cardinality.\r\n * Example: /users/123/orders/abc-def -> /users/:id/orders/:uuid\r\n */\r\nexport const normalizePath = (path: string): string => {\r\n if (!path || path === '/') return '/';\r\n\r\n return path\r\n // Replace UUIDs (long alphanumeric strings)\r\n .replace(\r\n /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/g,\r\n ':uuid'\r\n )\r\n // Replace MongoDB ObjectIds (24 hex chars)\r\n .replace(/[0-9a-fA-F]{24}/g, ':objectId')\r\n // Replace pure numeric IDs (e.g., /123)\r\n .replace(/\\/(\\d+)(?=\\/|$)/g, '/:id')\r\n // Remove query strings\r\n .split('?')[0];\r\n};\r\n\r\n/**\r\n * Tries to extract route from Framework internals, falls back to heuristic\r\n */\r\nexport const getRoute = (req: any, fallbackPath: string): string => {\r\n // Express / Connect\r\n if (req.route && req.route.path) {\r\n return (req.baseUrl || '') + req.route.path;\r\n }\r\n\r\n // H3 / Nitro (Nuxt)\r\n if (req.context && req.context.matchedRoute) {\r\n return req.context.matchedRoute.path;\r\n }\r\n\r\n // Fastify\r\n if (req.routerPath) {\r\n return req.routerPath;\r\n }\r\n\r\n // Fallback: Heuristic Normalization\r\n return normalizePath(fallbackPath);\r\n};","import { client } from '../core/client';\r\nimport { getRoute } from '../core/normalizer';\r\n\r\n// Minimal types for H3 to avoid peer-deps\r\ntype EventHandler = (event: any) => any;\r\n\r\nexport const wrapH3 = (handler: EventHandler) => {\r\n return (event: any) => {\r\n const req = event.node.req;\r\n const path = req.originalUrl || req.url || '/';\r\n\r\n // Start Trace Context\r\n return client.startTrace({\r\n method: req.method || 'GET',\r\n path: path,\r\n ip: req.headers['x-forwarded-for'] || req.socket?.remoteAddress,\r\n userAgent: req.headers['user-agent'],\r\n }, async () => {\r\n try {\r\n const response = await handler(event);\r\n\r\n // H3/Nitro response status\r\n let status = 200;\r\n if (event.node.res.statusCode) status = event.node.res.statusCode;\r\n // Check if response is an error object\r\n if (response && response.statusCode) status = response.statusCode;\r\n\r\n client.endTrace(status, { route: getRoute(event, path) });\r\n return response;\r\n } catch (err: any) {\r\n const status = err.statusCode || err.status || 500;\r\n client.endTrace(status, { route: getRoute(event, path) });\r\n throw err;\r\n }\r\n });\r\n };\r\n};","import { client } from '../core/client';\r\nimport { normalizePath } from '../core/normalizer';\r\n\r\n// --- App Router Wrapper (Route Handlers) ---\r\nexport const wrapNextRoute = (handler: Function) => {\r\n return async (req: Request | any, context?: any) => {\r\n // 1. Extract Info\r\n const url = req.url ? new URL(req.url) : { pathname: '/' };\r\n const method = req.method || 'GET';\r\n const ua = req.headers.get ? req.headers.get('user-agent') : undefined;\r\n const ip = req.headers.get ? req.headers.get('x-forwarded-for') : undefined;\r\n\r\n // 2. Run in Context\r\n return client.startTrace({\r\n method,\r\n path: url.pathname,\r\n userAgent: ua,\r\n ip: ip\r\n }, async () => {\r\n try {\r\n const response = await handler(req, context);\r\n const status = response?.status || 200;\r\n \r\n client.endTrace(status, { route: normalizePath(url.pathname) });\r\n return response;\r\n } catch (err: any) {\r\n client.endTrace(500, { route: normalizePath(url.pathname) });\r\n throw err;\r\n }\r\n });\r\n };\r\n};\r\n\r\n// --- Pages Router Wrapper (API Routes) ---\r\nexport const wrapNextPages = (handler: Function) => {\r\n return async (req: any, res: any) => {\r\n const path = req.url ? req.url.split('?')[0] : '/';\r\n \r\n // 1. Run in Context\r\n return client.startTrace({\r\n method: req.method || 'GET',\r\n path: path,\r\n userAgent: req.headers['user-agent'],\r\n ip: req.headers['x-forwarded-for'] || req.socket?.remoteAddress,\r\n }, async () => {\r\n \r\n // 2. Hook Response\r\n const done = () => {\r\n client.endTrace(res.statusCode || 200, { route: normalizePath(path) });\r\n };\r\n \r\n res.once('finish', done);\r\n res.once('close', done); // Fallback if finish doesn't fire\r\n\r\n // 3. Execute\r\n try {\r\n return await handler(req, res);\r\n } catch (e) {\r\n // Next.js Pages router usually handles errors internally, \r\n // but we ensure we catch sync errors here\r\n throw e;\r\n }\r\n });\r\n };\r\n};","import { client } from '../core/client';\r\nimport { SenzorOptions } from '../core/types';\r\n\r\n// We don't import Fastify types to keep zero-deps, but structure matches\r\nexport const senzorPlugin = (fastify: any, options: SenzorOptions, done: Function) => {\r\n\r\n // Init if options provided inline, otherwise assume global init\r\n if (options && options.apiKey) {\r\n client.init(options);\r\n }\r\n\r\n // Hook: On Request (Start Timer)\r\n fastify.addHook('onRequest', (request: any, reply: any, next: Function) => {\r\n request.senzorStart = performance.now();\r\n next();\r\n });\r\n\r\n // Hook: On Response (End Timer & Track)\r\n fastify.addHook('onResponse', (request: any, reply: any, next: Function) => {\r\n const duration = performance.now() - (request.senzorStart || performance.now());\r\n\r\n // Fastify provides 'routerPath' (e.g. /user/:id)\r\n const route = request.routeOptions?.url || request.routerPath;\r\n\r\n client.track({\r\n method: request.method,\r\n route: route || 'UNKNOWN',\r\n path: request.raw.url || request.url,\r\n status: reply.statusCode,\r\n duration: duration,\r\n ip: request.ip,\r\n userAgent: request.headers['user-agent']\r\n });\r\n\r\n next();\r\n });\r\n\r\n done();\r\n};"],"mappings":"0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,YAAAE,EAAA,YAAAC,IAAA,eAAAC,EAAAJ,GCEO,IAAMK,EAAN,KAAgB,CAIrB,YAAoBC,EAAuB,CAAvB,YAAAA,EAHpB,KAAQ,MAAe,CAAC,EACxB,KAAQ,MAA+B,KAGjC,OAAO,YAAgB,MACzB,KAAK,MAAQ,YAAY,IAAM,KAAK,MAAM,EAAGA,EAAO,eAAiB,GAAK,EACtE,KAAK,OAAS,OAAO,KAAK,MAAM,OAAU,YAC5C,KAAK,MAAM,MAAM,EAGvB,CAEO,IAAIC,EAAY,CACrB,KAAK,MAAM,KAAKA,CAAK,EACjB,KAAK,MAAM,SAAW,KAAK,OAAO,WAAa,MACjD,KAAK,MAAM,CAEf,CAEA,MAAa,OAAQ,CACnB,GAAI,KAAK,MAAM,SAAW,EAAG,OAE7B,IAAMC,EAAQ,CAAC,GAAG,KAAK,KAAK,EAC5B,KAAK,MAAQ,CAAC,EAEd,GAAI,CAEF,M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QAAQ,aACpD,EAAG,SAAY,CAGb,IAAMc,EAAO,IAAM,CACjBR,EAAO,SAASM,EAAI,YAAc,IAAK,CAAE,MAAOH,EAAcI,CAAI,CAAE,CAAC,CACvE,EAEAD,EAAI,KAAK,SAAUE,CAAI,EACvBF,EAAI,KAAK,QAASE,CAAI,EAGtB,GAAI,CACF,OAAO,MAAMf,EAAQC,EAAKY,CAAG,CAC/B,OAASG,EAAG,CAGV,MAAMA,CACR,CACF,CAAC,CACH,EC3DK,IAAMC,EAAe,CAACC,EAAcC,EAAwBC,IAAmB,CAGhFD,GAAWA,EAAQ,QACrBE,EAAO,KAAKF,CAAO,EAIrBD,EAAQ,QAAQ,YAAa,CAACI,EAAcC,EAAYC,IAAmB,CACzEF,EAAQ,YAAc,YAAY,IAAI,EACtCE,EAAK,CACP,CAAC,EAGDN,EAAQ,QAAQ,aAAc,CAACI,EAAcC,EAAYC,IAAmB,CAC1E,IAAMC,EAAW,YAAY,IAAI,GAAKH,EAAQ,aAAe,YAAY,IAAI,GAGvEI,EAAQJ,EAAQ,cAAc,KAAOA,EAAQ,WAEnDD,EAAO,MAAM,CACX,OAAQC,EAAQ,OAChB,MAAOI,GAAS,UAChB,KAAMJ,EAAQ,IAAI,KAAOA,EAAQ,IACjC,OAAQC,EAAM,WACd,SAAUE,EACV,GAAIH,EAAQ,GACZ,UAAWA,EAAQ,QAAQ,YAAY,CACzC,CAAC,EAEDE,EAAK,CACP,CAAC,EAEDJ,EAAK,CACP,EX/BA,IAAMO,EAAS,CAEb,KAAOC,GAA2BC,EAAO,KAAKD,CAAO,EACrD,MAAO,IAAMC,EAAO,MAAM,EAG1B,eAAgBC,EAGhB,cAAAC,EACA,cAAAC,EAGA,OAAAC,EAGA,cAAeC,CACjB,EAEOC,EAAQR","names":["index_exports","__export","Senzor","index_default","__toCommonJS","Transport","config","trace","batch","err","import_async_hooks","storage","Context","trace","fn","span","store","import_crypto","import_http","import_https","import_url","shimmer","module","methodName","wrapper","original","instrumentHttp","ingestUrl","ingestHost","requestWrapper","args","options","urlStr","protocol","host","path","trace","Context","method","startTime","spanStartAbs","req","res","duration","err","http","https","instrumentMongo","Collection","method","original","args","trace","Context","startTime","spanStartAbs","collectionName","endSpan","err","duration","result","res","instrumentPg","pg","originalQuery","args","trace","Context","startTime","spanStartAbs","sql","result","res","duration","SenzorClient","options","endpoint","Transport","instrumentHttp","instrumentMongo","instrumentPg","data","next","trace","Context","status","extraData","duration","payload","name","type","startTime","spanStartAbs","meta","client","expressMiddleware","req","res","next","client","route","normalizePath","path","getRoute","req","fallbackPath","wrapH3","handler","event","req","path","client","response","status","getRoute","err","wrapNextRoute","handler","req","context","url","method","ua","ip","client","response","status","normalizePath","err","wrapNextPages","res","path","done","e","senzorPlugin","fastify","options","done","client","request","reply","next","duration","route","Senzor","options","client","expressMiddleware","wrapNextRoute","wrapNextPages","wrapH3","senzorPlugin","index_default"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var A=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,r)=>(typeof require<"u"?require:t)[r]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var g=class{constructor(t){this.config=t;this.queue=[];this.timer=null;typeof setInterval<"u"&&(this.timer=setInterval(()=>this.flush(),t.flushInterval||1e4),this.timer&&typeof this.timer.unref=="function"&&this.timer.unref())}add(t){this.queue.push(t),this.queue.length>=(this.config.batchSize||100)&&this.flush()}async flush(){if(this.queue.length===0)return;let t=[...this.queue];this.queue=[];try{await fetch(this.config.endpoint||"https://api.senzor.dev/api/ingest/apm",{method:"POST",headers:{"Content-Type":"application/json","x-service-api-key":this.config.apiKey},body:JSON.stringify(t),keepalive:!0}),this.config.debug&&console.log(`[Senzor] Flushed ${t.length} traces`)}catch(r){this.config.debug&&console.error("[Senzor] Ingestion Error:",r)}}};import{AsyncLocalStorage as H}from"async_hooks";var T=new H,u={run:(e,t)=>T.run(e,t),current:()=>T.getStore(),addSpan:e=>{let t=T.getStore();t&&t.spans.push(e)}};import{randomUUID as P}from"crypto";import z from"http";import C from"https";import{URL as S}from"url";var w=(e,t,r)=>{if(!e[t])return;let n=e[t];e[t]=r(n)},O=e=>{let t=new S(e).hostname,r=n=>function(...o){let a={},c="";if(typeof o[0]=="string"||o[0]instanceof S)c=o[0].toString(),a=o[1]||{};else{a=o[0]||{};let d=a.protocol||"http:",l=a.hostname||a.host||"localhost",E=a.path||"/";c=`${d}//${l}${E}`}if(c.includes(t)||a.hostname&&a.hostname.includes(t))return n.apply(this,o);let i=u.current();if(!i)return n.apply(this,o);let m=(a.method||"GET").toUpperCase(),h=performance.now()-i.startTime,f=performance.now(),p=n.apply(this,o);return p.on("response",d=>{d.on("end",()=>{let l=performance.now()-f;u.addSpan({name:`${m} ${new S(c).hostname}`,type:"http",startTime:h,duration:l,status:d.statusCode,meta:{url:c,method:m}})})}),p.on("error",d=>{let l=performance.now()-f;u.addSpan({name:`${m} ${c}`,type:"http",startTime:h,duration:l,status:500,meta:{error:d.message}})}),p};w(z,"request",r),w(z,"get",r),w(C,"request",r),w(C,"get",r)};var v=()=>{try{let t=A("mongodb").Collection;["find","findOne","insertOne","insertMany","updateOne","updateMany","deleteOne","deleteMany","aggregate","countDocuments"].forEach(n=>{if(!t.prototype[n])return;let o=t.prototype[n];t.prototype[n]=function(...a){let c=u.current();if(!c)return o.apply(this,a);let i=performance.now()-c.startTime,m=performance.now(),h=this.collectionName,f=p=>{let d=performance.now()-m;u.addSpan({name:`MongoDB ${n} (${h})`,type:"db",startTime:i,duration:d,status:p?500:0,meta:{collection:h,operation:n,error:p?p.message:void 0}})};try{let p=o.apply(this,a);return p&&typeof p.then=="function"?p.then(d=>(f(),d),d=>{throw f(d),d}):(f(),p)}catch(p){throw f(p),p}}})}catch{}};var F=()=>{try{let e=A("pg"),t=e.Client.prototype.query;e.Client.prototype.query=function(...r){let n=u.current();if(!n)return t.apply(this,r);let o=performance.now()-n.startTime,a=performance.now(),c=typeof r[0]=="string"?r[0]:r[0].text,i=t.apply(this,r);return i&&typeof i.then=="function"?i.then(m=>{let h=performance.now()-a;return u.addSpan({name:"Postgres Query",type:"db",startTime:o,duration:h,meta:{query:c}}),m}):i}}catch{}};var x=class{constructor(){this.transport=null;this.options=null;this.isInstrumented=!1}init(t){if(!t.apiKey){console.warn("[Senzor] API Key missing. SDK disabled.");return}this.options=t;let r=t.endpoint||"https://api.senzor.dev/api/ingest/apm";if(this.transport=new g({...t,endpoint:r}),!this.isInstrumented){try{O(r)}catch{}try{v()}catch{}try{F()}catch{}this.isInstrumented=!0,t.debug&&console.log("[Senzor] Auto-instrumentation enabled")}t.debug&&console.log("[Senzor] Initialized")}startTrace(t,r){if(!this.transport)return r();let n={id:P(),startTime:performance.now(),data:t,spans:[]};return u.run(n,r)}endTrace(t,r={}){let n=u.current();if(!n||!this.transport)return;let o=performance.now()-n.startTime,a={traceId:n.id,...n.data,...r,status:t,duration:o,spans:n.spans,timestamp:new Date().toISOString()};this.transport.add(a)}track(t){if(!this.transport)return;let r={traceId:P(),...t,spans:[],timestamp:new Date().toISOString()};this.transport.add(r)}startSpan(t,r="custom"){let n=u.current();if(!n)return{end:()=>{}};let o=performance.now()-n.startTime,a=performance.now();return{end:(c,i)=>{let m=performance.now()-a;u.addSpan({name:t,type:r,startTime:o,duration:m,status:i,meta:c})}}}async flush(){this.transport&&await this.transport.flush()}},s=new x;var I=()=>(e,t,r)=>{s.startTrace({method:e.method,path:e.originalUrl||e.url,ip:e.ip||e.socket?.remoteAddress,userAgent:e.headers["user-agent"]},()=>{t.once("finish",()=>{try{let n="UNKNOWN";e.route&&e.route.path?n=(e.baseUrl||"")+e.route.path:t.statusCode===404?n="Not Found":n=e.path||"Wildcard",s.endTrace(t.statusCode,{route:n})}catch{}}),r()})};var y=e=>!e||e==="/"?"/":e.replace(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/g,":uuid").replace(/[0-9a-fA-F]{24}/g,":objectId").replace(/\/(\d+)(?=\/|$)/g,"/:id").split("?")[0],b=(e,t)=>e.route&&e.route.path?(e.baseUrl||"")+e.route.path:e.context&&e.context.matchedRoute?e.context.matchedRoute.path:e.routerPath?e.routerPath:y(t);var N=e=>t=>{let r=t.node.req,n=r.originalUrl||r.url||"/";return s.startTrace({method:r.method||"GET",path:n,ip:r.headers["x-forwarded-for"]||r.socket?.remoteAddress,userAgent:r.headers["user-agent"]},async()=>{try{let o=await e(t),a=200;return t.node.res.statusCode&&(a=t.node.res.statusCode),o&&o.statusCode&&(a=o.statusCode),s.endTrace(a,{route:b(t,n)}),o}catch(o){let a=o.statusCode||o.status||500;throw s.endTrace(a,{route:b(t,n)}),o}})};var R=e=>async(t,r)=>{let n=t.url?new URL(t.url):{pathname:"/"},o=t.method||"GET",a=t.headers.get?t.headers.get("user-agent"):void 0,c=t.headers.get?t.headers.get("x-forwarded-for"):void 0;return s.startTrace({method:o,path:n.pathname,userAgent:a,ip:c},async()=>{try{let i=await e(t,r),m=i?.status||200;return s.endTrace(m,{route:y(n.pathname)}),i}catch(i){throw s.endTrace(500,{route:y(n.pathname)}),i}})},U=e=>async(t,r)=>{let n=t.url?t.url.split("?")[0]:"/";return s.startTrace({method:t.method||"GET",path:n,userAgent:t.headers["user-agent"],ip:t.headers["x-forwarded-for"]||t.socket?.remoteAddress},async()=>{let o=()=>{s.endTrace(r.statusCode||200,{route:y(n)})};r.once("finish",o),r.once("close",o);try{return await e(t,r)}catch(a){throw a}})};var $=(e,t,r)=>{t&&t.apiKey&&s.init(t),e.addHook("onRequest",(n,o,a)=>{n.senzorStart=performance.now(),a()}),e.addHook("onResponse",(n,o,a)=>{let c=performance.now()-(n.senzorStart||performance.now()),i=n.routeOptions?.url||n.routerPath;s.track({method:n.method,route:i||"UNKNOWN",path:n.raw.url||n.url,status:o.statusCode,duration:c,ip:n.ip,userAgent:n.headers["user-agent"]}),a()}),r()};var k={init:e=>s.init(e),flush:()=>s.flush(),requestHandler:I,wrapNextRoute:R,wrapNextPages:U,wrapH3:N,fastifyPlugin:$},St=k;export{k as Senzor,St as default};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/transport.ts","../src/core/context.ts","../src/core/client.ts","../src/instrumentation/http.ts","../src/instrumentation/mongo.ts","../src/instrumentation/pg.ts","../src/middleware/express.ts","../src/core/normalizer.ts","../src/wrappers/h3.ts","../src/wrappers/next.ts","../src/wrappers/fastify.ts","../src/index.ts"],"sourcesContent":["import { SenzorOptions } from './types';\r\n\r\nexport class Transport {\r\n private queue: any[] = [];\r\n private timer: NodeJS.Timeout | null = null;\r\n\r\n constructor(private config: SenzorOptions) {\r\n if (typeof setInterval !== 'undefined') {\r\n this.timer = setInterval(() => this.flush(), config.flushInterval || 10000);\r\n if (this.timer && typeof this.timer.unref === 'function') {\r\n this.timer.unref(); // Don't block process exit\r\n }\r\n }\r\n }\r\n\r\n public add(trace: any) {\r\n this.queue.push(trace);\r\n if (this.queue.length >= (this.config.batchSize || 100)) {\r\n this.flush();\r\n }\r\n }\r\n\r\n public async flush() {\r\n if (this.queue.length === 0) return;\r\n\r\n const batch = [...this.queue];\r\n this.queue = [];\r\n\r\n try {\r\n // Use global fetch (Node 18+)\r\n await fetch(this.config.endpoint || 'https://api.senzor.dev/api/ingest/apm', {\r\n method: 'POST',\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n 'x-service-api-key': this.config.apiKey,\r\n },\r\n body: JSON.stringify(batch),\r\n keepalive: true,\r\n });\r\n \r\n if (this.config.debug) console.log(`[Senzor] Flushed ${batch.length} traces`);\r\n } catch (err) {\r\n if (this.config.debug) console.error('[Senzor] Ingestion Error:', err);\r\n // Dropping data to prevent memory leaks is preferred in APM\r\n }\r\n }\r\n}","import { AsyncLocalStorage } from 'async_hooks';\r\nimport { ActiveTrace } from './types';\r\n\r\n// Storage to hold the current Trace for any async operation\r\nexport const storage = new AsyncLocalStorage<ActiveTrace>();\r\n\r\nexport const Context = {\r\n // Run a function within a trace context\r\n // Updated to be Generic <T> to allow returning values (Promises, etc)\r\n run: <T>(trace: ActiveTrace, fn: () => T): T => {\r\n return storage.run(trace, fn);\r\n },\r\n\r\n // Get current trace (safe)\r\n current: (): ActiveTrace | undefined => {\r\n return storage.getStore();\r\n },\r\n\r\n // Add a span to the current trace\r\n addSpan: (span: any) => {\r\n const store = storage.getStore();\r\n if (store) {\r\n store.spans.push(span);\r\n }\r\n }\r\n};","import { Transport } from './transport';\r\nimport { Context } from './context';\r\nimport { SenzorOptions, ActiveTrace } from './types';\r\nimport { randomUUID } from 'crypto';\r\nimport { instrumentHttp } from '../instrumentation/http';\r\nimport { instrumentMongo } from '../instrumentation/mongo';\r\nimport { instrumentPg } from '../instrumentation/pg';\r\n\r\nexport class SenzorClient {\r\n private transport: Transport | null = null;\r\n private options: SenzorOptions | null = null;\r\n private isInstrumented = false;\r\n\r\n public init(options: SenzorOptions) {\r\n if (!options.apiKey) {\r\n console.warn('[Senzor] API Key missing. SDK disabled.');\r\n return;\r\n }\r\n this.options = options;\r\n const endpoint = options.endpoint || 'https://api.senzor.dev/api/ingest/apm';\r\n\r\n this.transport = new Transport({\r\n ...options,\r\n endpoint\r\n });\r\n\r\n if (!this.isInstrumented) {\r\n try { instrumentHttp(endpoint); } catch (e) { }\r\n try { instrumentMongo(); } catch (e) { }\r\n try { instrumentPg(); } catch (e) { }\r\n\r\n this.isInstrumented = true;\r\n if (options.debug) console.log('[Senzor] Auto-instrumentation enabled');\r\n }\r\n\r\n if (options.debug) console.log('[Senzor] Initialized');\r\n }\r\n\r\n public startTrace<T>(data: Partial<ActiveTrace['data']>, next: () => T): T {\r\n if (!this.transport) return next();\r\n\r\n const trace: ActiveTrace = {\r\n id: randomUUID(),\r\n startTime: performance.now(),\r\n data: data,\r\n spans: []\r\n };\r\n\r\n return Context.run(trace, next);\r\n }\r\n\r\n public endTrace(status: number, extraData: any = {}) {\r\n const trace = Context.current();\r\n if (!trace || !this.transport) return;\r\n\r\n const duration = performance.now() - trace.startTime;\r\n\r\n const payload = {\r\n traceId: trace.id,\r\n ...trace.data,\r\n ...extraData,\r\n status,\r\n duration,\r\n spans: trace.spans,\r\n timestamp: new Date().toISOString()\r\n };\r\n\r\n this.transport.add(payload);\r\n }\r\n\r\n public track(data: {\r\n method: string;\r\n route: string;\r\n path: string;\r\n status: number;\r\n duration: number;\r\n ip?: string;\r\n userAgent?: string;\r\n }) {\r\n if (!this.transport) return;\r\n const payload = {\r\n traceId: randomUUID(),\r\n ...data,\r\n spans: [],\r\n timestamp: new Date().toISOString()\r\n };\r\n this.transport.add(payload);\r\n }\r\n\r\n public startSpan(name: string, type: 'db' | 'http' | 'function' | 'custom' = 'custom') {\r\n const trace = Context.current();\r\n if (!trace) return { end: () => { } };\r\n\r\n const startTime = performance.now() - trace.startTime;\r\n const spanStartAbs = performance.now();\r\n\r\n return {\r\n end: (meta?: any, status?: number) => {\r\n const duration = performance.now() - spanStartAbs;\r\n Context.addSpan({\r\n name,\r\n type,\r\n startTime,\r\n duration,\r\n status,\r\n meta\r\n });\r\n }\r\n };\r\n }\r\n\r\n public async flush() {\r\n if (this.transport) await this.transport.flush();\r\n }\r\n}\r\n\r\nexport const client = new SenzorClient();","import http from 'http';\r\nimport https from 'https';\r\nimport { URL } from 'url';\r\nimport { Context } from '../core/context';\r\n\r\n// Helper to safely wrap modules\r\nconst shimmer = (module: any, methodName: string, wrapper: (original: Function) => Function) => {\r\n if (!module[methodName]) return;\r\n const original = module[methodName];\r\n module[methodName] = wrapper(original);\r\n};\r\n\r\nexport const instrumentHttp = (ingestUrl: string) => {\r\n const ingestHost = new URL(ingestUrl).hostname;\r\n\r\n const requestWrapper = (original: Function) => {\r\n return function (this: any, ...args: any[]) {\r\n // 1. Parse Arguments to get URL\r\n let options: any = {};\r\n let urlStr = '';\r\n\r\n if (typeof args[0] === 'string' || args[0] instanceof URL) {\r\n urlStr = args[0].toString();\r\n options = args[1] || {};\r\n } else {\r\n options = args[0] || {};\r\n const protocol = options.protocol || 'http:';\r\n const host = options.hostname || options.host || 'localhost';\r\n const path = options.path || '/';\r\n urlStr = `${protocol}//${host}${path}`;\r\n }\r\n\r\n // 2. SAFETY GUARD: Ignore calls to Senzor Ingest API (Prevent Infinite Loop)\r\n if (urlStr.includes(ingestHost) || (options.hostname && options.hostname.includes(ingestHost))) {\r\n return original.apply(this, args);\r\n }\r\n\r\n // 3. Check if we are inside an Active Trace\r\n // If we are not handling a user request, don't trace background http calls\r\n const trace = Context.current();\r\n if (!trace) {\r\n return original.apply(this, args);\r\n }\r\n\r\n // 4. Start Span\r\n const method = (options.method || 'GET').toUpperCase();\r\n const startTime = performance.now() - trace.startTime; // Relative to trace start\r\n const spanStartAbs = performance.now();\r\n\r\n // 5. Execute Request\r\n const req = original.apply(this, args);\r\n\r\n // 6. Hook into Response/Error\r\n req.on('response', (res: any) => {\r\n // Wait for end of stream to calculate full duration (TTFB + Download)\r\n res.on('end', () => {\r\n const duration = performance.now() - spanStartAbs;\r\n \r\n Context.addSpan({\r\n name: `${method} ${new URL(urlStr).hostname}`, // e.g. \"GET api.stripe.com\"\r\n type: 'http',\r\n startTime,\r\n duration,\r\n status: res.statusCode,\r\n meta: {\r\n url: urlStr,\r\n method: method,\r\n }\r\n });\r\n });\r\n });\r\n\r\n req.on('error', (err: Error) => {\r\n const duration = performance.now() - spanStartAbs;\r\n Context.addSpan({\r\n name: `${method} ${urlStr}`,\r\n type: 'http',\r\n startTime,\r\n duration,\r\n status: 500, // Client Error\r\n meta: { error: err.message }\r\n });\r\n });\r\n\r\n return req;\r\n };\r\n };\r\n\r\n // Apply patches\r\n shimmer(http, 'request', requestWrapper);\r\n shimmer(http, 'get', requestWrapper);\r\n shimmer(https, 'request', requestWrapper);\r\n shimmer(https, 'get', requestWrapper);\r\n};","import { Context } from '../core/context';\r\n\r\nexport const instrumentMongo = () => {\r\n try {\r\n const mongodb = require('mongodb');\r\n const Collection = mongodb.Collection;\r\n\r\n const methods = [\r\n 'find',\r\n 'findOne',\r\n 'insertOne',\r\n 'insertMany',\r\n 'updateOne',\r\n 'updateMany',\r\n 'deleteOne',\r\n 'deleteMany',\r\n 'aggregate',\r\n 'countDocuments'\r\n ];\r\n\r\n methods.forEach((method) => {\r\n if (!Collection.prototype[method]) return;\r\n\r\n const original = Collection.prototype[method];\r\n\r\n Collection.prototype[method] = function (...args: any[]) {\r\n const trace = Context.current();\r\n // If not inside a tracked request, just run normally\r\n if (!trace) return original.apply(this, args);\r\n\r\n const startTime = performance.now() - trace.startTime;\r\n const spanStartAbs = performance.now();\r\n const collectionName = this.collectionName;\r\n\r\n // Helper to finish span\r\n const endSpan = (err?: Error) => {\r\n const duration = performance.now() - spanStartAbs;\r\n Context.addSpan({\r\n name: `MongoDB ${method} (${collectionName})`,\r\n type: 'db',\r\n startTime,\r\n duration,\r\n status: err ? 500 : 0,\r\n meta: {\r\n collection: collectionName,\r\n operation: method,\r\n error: err ? err.message : undefined\r\n }\r\n });\r\n };\r\n\r\n try {\r\n const result = original.apply(this, args);\r\n\r\n // Handle Promise/Cursor\r\n if (result && typeof result.then === 'function') {\r\n return result.then(\r\n (res: any) => { endSpan(); return res; },\r\n (err: any) => { endSpan(err); throw err; }\r\n );\r\n }\r\n // Handle FindCursor (it doesn't execute immediately, but for APM visuals we mark start/return)\r\n // Ideally we'd wrap toArray() but that's complex. For 'find', we just mark the creation.\r\n endSpan();\r\n return result;\r\n\r\n } catch (err: any) {\r\n endSpan(err);\r\n throw err;\r\n }\r\n };\r\n });\r\n\r\n } catch (e) {\r\n // User doesn't use mongodb, ignore\r\n }\r\n};","import { Context } from '../core/context';\r\n\r\n// Simple shim for 'pg' library\r\nexport const instrumentPg = () => {\r\n try {\r\n // Try to require pg (it might not be installed by user)\r\n const pg = require('pg');\r\n const originalQuery = pg.Client.prototype.query;\r\n\r\n pg.Client.prototype.query = function (...args: any[]) {\r\n const trace = Context.current();\r\n if (!trace) return originalQuery.apply(this, args);\r\n\r\n const startTime = performance.now() - trace.startTime;\r\n const spanStartAbs = performance.now();\r\n\r\n // Extract SQL (first arg usually string or config object)\r\n const sql = typeof args[0] === 'string' ? args[0] : args[0].text;\r\n\r\n // Wrap callback if present, or handle Promise\r\n const result = originalQuery.apply(this, args);\r\n\r\n if (result && typeof result.then === 'function') {\r\n return result.then((res: any) => {\r\n const duration = performance.now() - spanStartAbs;\r\n Context.addSpan({\r\n name: 'Postgres Query',\r\n type: 'db',\r\n startTime,\r\n duration,\r\n meta: { query: sql }\r\n });\r\n return res;\r\n });\r\n }\r\n return result;\r\n };\r\n } catch (e) {\r\n // User doesn't use pg, ignore\r\n }\r\n};","import { client } from '../core/client';\r\n\r\nexport const expressMiddleware = () => {\r\n return (req: any, res: any, next: () => void) => {\r\n // We MUST use startTrace to enable Auto-Instrumentation for this request\r\n client.startTrace({\r\n method: req.method,\r\n path: req.originalUrl || req.url,\r\n ip: req.ip || req.socket?.remoteAddress,\r\n userAgent: req.headers['user-agent'],\r\n }, () => {\r\n\r\n res.once('finish', () => {\r\n try {\r\n let route = 'UNKNOWN';\r\n if (req.route && req.route.path) {\r\n route = (req.baseUrl || '') + req.route.path;\r\n } else if (res.statusCode === 404) {\r\n route = 'Not Found';\r\n } else {\r\n route = req.path || 'Wildcard';\r\n }\r\n\r\n client.endTrace(res.statusCode, { route });\r\n } catch (e) {\r\n // Fail open\r\n }\r\n });\r\n\r\n next();\r\n });\r\n };\r\n};","/**\r\n * Heuristic URL Normalizer\r\n * Converts raw paths with IDs into generic patterns to prevent high cardinality.\r\n * Example: /users/123/orders/abc-def -> /users/:id/orders/:uuid\r\n */\r\nexport const normalizePath = (path: string): string => {\r\n if (!path || path === '/') return '/';\r\n\r\n return path\r\n // Replace UUIDs (long alphanumeric strings)\r\n .replace(\r\n /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/g,\r\n ':uuid'\r\n )\r\n // Replace MongoDB ObjectIds (24 hex chars)\r\n .replace(/[0-9a-fA-F]{24}/g, ':objectId')\r\n // Replace pure numeric IDs (e.g., /123)\r\n .replace(/\\/(\\d+)(?=\\/|$)/g, '/:id')\r\n // Remove query strings\r\n .split('?')[0];\r\n};\r\n\r\n/**\r\n * Tries to extract route from Framework internals, falls back to heuristic\r\n */\r\nexport const getRoute = (req: any, fallbackPath: string): string => {\r\n // Express / Connect\r\n if (req.route && req.route.path) {\r\n return (req.baseUrl || '') + req.route.path;\r\n }\r\n\r\n // H3 / Nitro (Nuxt)\r\n if (req.context && req.context.matchedRoute) {\r\n return req.context.matchedRoute.path;\r\n }\r\n\r\n // Fastify\r\n if (req.routerPath) {\r\n return req.routerPath;\r\n }\r\n\r\n // Fallback: Heuristic Normalization\r\n return normalizePath(fallbackPath);\r\n};","import { client } from '../core/client';\r\nimport { getRoute } from '../core/normalizer';\r\n\r\n// Minimal types for H3 to avoid peer-deps\r\ntype EventHandler = (event: any) => any;\r\n\r\nexport const wrapH3 = (handler: EventHandler) => {\r\n return (event: any) => {\r\n const req = event.node.req;\r\n const path = req.originalUrl || req.url || '/';\r\n\r\n // Start Trace Context\r\n return client.startTrace({\r\n method: req.method || 'GET',\r\n path: path,\r\n ip: req.headers['x-forwarded-for'] || req.socket?.remoteAddress,\r\n userAgent: req.headers['user-agent'],\r\n }, async () => {\r\n try {\r\n const response = await handler(event);\r\n\r\n // H3/Nitro response status\r\n let status = 200;\r\n if (event.node.res.statusCode) status = event.node.res.statusCode;\r\n // Check if response is an error object\r\n if (response && response.statusCode) status = response.statusCode;\r\n\r\n client.endTrace(status, { route: getRoute(event, path) });\r\n return response;\r\n } catch (err: any) {\r\n const status = err.statusCode || err.status || 500;\r\n client.endTrace(status, { route: getRoute(event, path) });\r\n throw err;\r\n }\r\n });\r\n };\r\n};","import { client } from '../core/client';\r\nimport { normalizePath } from '../core/normalizer';\r\n\r\n// --- App Router Wrapper (Route Handlers) ---\r\nexport const wrapNextRoute = (handler: Function) => {\r\n return async (req: Request | any, context?: any) => {\r\n // 1. Extract Info\r\n const url = req.url ? new URL(req.url) : { pathname: '/' };\r\n const method = req.method || 'GET';\r\n const ua = req.headers.get ? req.headers.get('user-agent') : undefined;\r\n const ip = req.headers.get ? req.headers.get('x-forwarded-for') : undefined;\r\n\r\n // 2. Run in Context\r\n return client.startTrace({\r\n method,\r\n path: url.pathname,\r\n userAgent: ua,\r\n ip: ip\r\n }, async () => {\r\n try {\r\n const response = await handler(req, context);\r\n const status = response?.status || 200;\r\n \r\n client.endTrace(status, { route: normalizePath(url.pathname) });\r\n return response;\r\n } catch (err: any) {\r\n client.endTrace(500, { route: normalizePath(url.pathname) });\r\n throw err;\r\n }\r\n });\r\n };\r\n};\r\n\r\n// --- Pages Router Wrapper (API Routes) ---\r\nexport const wrapNextPages = (handler: Function) => {\r\n return async (req: any, res: any) => {\r\n const path = req.url ? req.url.split('?')[0] : '/';\r\n \r\n // 1. Run in Context\r\n return client.startTrace({\r\n method: req.method || 'GET',\r\n path: path,\r\n userAgent: req.headers['user-agent'],\r\n ip: req.headers['x-forwarded-for'] || req.socket?.remoteAddress,\r\n }, async () => {\r\n \r\n // 2. Hook Response\r\n const done = () => {\r\n client.endTrace(res.statusCode || 200, { route: normalizePath(path) });\r\n };\r\n \r\n res.once('finish', done);\r\n res.once('close', done); // Fallback if finish doesn't fire\r\n\r\n // 3. Execute\r\n try {\r\n return await handler(req, res);\r\n } catch (e) {\r\n // Next.js Pages router usually handles errors internally, \r\n // but we ensure we catch sync errors here\r\n throw e;\r\n }\r\n });\r\n };\r\n};","import { client } from '../core/client';\r\nimport { SenzorOptions } from '../core/types';\r\n\r\n// We don't import Fastify types to keep zero-deps, but structure matches\r\nexport const senzorPlugin = (fastify: any, options: SenzorOptions, done: Function) => {\r\n\r\n // Init if options provided inline, otherwise assume global init\r\n if (options && options.apiKey) {\r\n client.init(options);\r\n }\r\n\r\n // Hook: On Request (Start Timer)\r\n fastify.addHook('onRequest', (request: any, reply: any, next: Function) => {\r\n request.senzorStart = performance.now();\r\n next();\r\n });\r\n\r\n // Hook: On Response (End Timer & Track)\r\n fastify.addHook('onResponse', (request: any, reply: any, next: Function) => {\r\n const duration = performance.now() - (request.senzorStart || performance.now());\r\n\r\n // Fastify provides 'routerPath' (e.g. /user/:id)\r\n const route = request.routeOptions?.url || request.routerPath;\r\n\r\n client.track({\r\n method: request.method,\r\n route: route || 'UNKNOWN',\r\n path: request.raw.url || request.url,\r\n status: reply.statusCode,\r\n duration: duration,\r\n ip: request.ip,\r\n userAgent: request.headers['user-agent']\r\n });\r\n\r\n next();\r\n });\r\n\r\n done();\r\n};","import { client } from './core/client';\r\nimport { expressMiddleware } from './middleware/express';\r\nimport { wrapH3 } from './wrappers/h3';\r\nimport { wrapNextRoute, wrapNextPages } from './wrappers/next';\r\nimport { senzorPlugin } from './wrappers/fastify';\r\nimport { SenzorOptions } from './core/types';\r\n\r\nconst Senzor = {\r\n // Core\r\n init: (options: SenzorOptions) => client.init(options),\r\n flush: () => client.flush(),\r\n\r\n // Express / Connect\r\n requestHandler: expressMiddleware,\r\n\r\n // Next.js\r\n wrapNextRoute, // For App Router (Route Handlers)\r\n wrapNextPages, // For Pages Router (API Routes)\r\n\r\n // H3 / Nuxt / Nitro\r\n wrapH3,\r\n\r\n // Fastify\r\n fastifyPlugin: senzorPlugin\r\n};\r\n\r\nexport default Senzor;\r\nexport { Senzor };"],"mappings":"yPAEO,IAAMA,EAAN,KAAgB,CAIrB,YAAoBC,EAAuB,CAAvB,YAAAA,EAHpB,KAAQ,MAAe,CAAC,EACxB,KAAQ,MAA+B,KAGjC,OAAO,YAAgB,MACzB,KAAK,MAAQ,YAAY,IAAM,KAAK,MAAM,EAAGA,EAAO,eAAiB,GAAK,EACtE,KAAK,OAAS,OAAO,KAAK,MAAM,OAAU,YAC5C,KAAK,MAAM,MAAM,EAGvB,CAEO,IAAIC,EAAY,CAC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D,MAAO,IAAMC,EAAO,MAAM,EAG1B,eAAgBC,EAGhB,cAAAC,EACA,cAAAC,EAGA,OAAAC,EAGA,cAAeC,CACjB,EAEOC,GAAQR","names":["Transport","config","trace","batch","err","AsyncLocalStorage","storage","Context","trace","fn","span","store","randomUUID","http","https","URL","shimmer","module","methodName","wrapper","original","instrumentHttp","ingestUrl","ingestHost","URL","requestWrapper","args","options","urlStr","protocol","host","path","trace","Context","method","startTime","spanStartAbs","req","res","duration","err","http","https","instrumentMongo","Collection","method","original","args","trace","Context","startTime","spanStartAbs","collectionName","endSpan","err","duration","result","res","instrumentPg","pg","originalQuery","args","trace","Context","startTime","spanStartAbs","sql","result","res","duration","SenzorClient","options","endpoint","Transport","instrumentHttp","instrumentMongo","instrumentPg","data","next","trace","randomUUID","Context","status","extraData","duration","payload","name","type","startTime","spanStartAbs","meta","client","expressMiddleware","req","res","next","client","route","normalizePath","path","getRoute","req","fallbackPath","wrapH3","handler","event","req","path","client","response","status","getRoute","err","wrapNextRoute","handler","req","context","url","method","ua","ip","client","response","status","normalizePath","err","wrapNextPages","res","path","done","e","senzorPlugin","fastify","options","done","client","request","reply","next","duration","route","Senzor","options","client","expressMiddleware","wrapNextRoute","wrapNextPages","wrapH3","senzorPlugin","index_default"]}
|
package/package.json
CHANGED
|
@@ -1,19 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@senzops/apm-node",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Universal APM SDK for Senzor",
|
|
5
|
-
"main": "
|
|
6
|
-
"
|
|
7
|
-
"types": "./dist/index.d.ts",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"require": "./dist/index.js",
|
|
11
|
-
"import": "./dist/index.mjs",
|
|
12
|
-
"types": "./dist/index.d.ts"
|
|
13
|
-
}
|
|
14
|
-
},
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
15
7
|
"scripts": {
|
|
16
|
-
"build": "tsup",
|
|
8
|
+
"build": "tsup src/index.ts --format cjs,esm,iife --dts --clean --minify",
|
|
17
9
|
"prepublishOnly": "npm run build"
|
|
18
10
|
},
|
|
19
11
|
"dependencies": {},
|
|
@@ -24,5 +16,16 @@
|
|
|
24
16
|
},
|
|
25
17
|
"engines": {
|
|
26
18
|
"node": ">=18.0.0"
|
|
27
|
-
}
|
|
28
|
-
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"apm",
|
|
22
|
+
"monitoring",
|
|
23
|
+
"senzor",
|
|
24
|
+
"node",
|
|
25
|
+
"javascript",
|
|
26
|
+
"api",
|
|
27
|
+
"observability"
|
|
28
|
+
],
|
|
29
|
+
"author": "Senzops",
|
|
30
|
+
"license": "MIT"
|
|
31
|
+
}
|
package/src/core/client.ts
CHANGED
|
@@ -1,43 +1,73 @@
|
|
|
1
1
|
import { Transport } from './transport';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
debug?: boolean;
|
|
9
|
-
}
|
|
2
|
+
import { Context } from './context';
|
|
3
|
+
import { SenzorOptions, ActiveTrace } from './types';
|
|
4
|
+
import { randomUUID } from 'crypto';
|
|
5
|
+
import { instrumentHttp } from '../instrumentation/http';
|
|
6
|
+
import { instrumentMongo } from '../instrumentation/mongo';
|
|
7
|
+
import { instrumentPg } from '../instrumentation/pg';
|
|
10
8
|
|
|
11
9
|
export class SenzorClient {
|
|
12
10
|
private transport: Transport | null = null;
|
|
13
11
|
private options: SenzorOptions | null = null;
|
|
12
|
+
private isInstrumented = false;
|
|
14
13
|
|
|
15
14
|
public init(options: SenzorOptions) {
|
|
16
15
|
if (!options.apiKey) {
|
|
17
16
|
console.warn('[Senzor] API Key missing. SDK disabled.');
|
|
18
17
|
return;
|
|
19
18
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
endpoint: 'https://api.senzor.dev/api/ingest/apm',
|
|
23
|
-
batchSize: 100,
|
|
24
|
-
flushInterval: 10000,
|
|
25
|
-
debug: false,
|
|
26
|
-
...options
|
|
27
|
-
};
|
|
19
|
+
this.options = options;
|
|
20
|
+
const endpoint = options.endpoint || 'https://api.senzor.dev/api/ingest/apm';
|
|
28
21
|
|
|
29
22
|
this.transport = new Transport({
|
|
30
|
-
|
|
31
|
-
endpoint
|
|
32
|
-
batchSize: this.options.batchSize!,
|
|
33
|
-
flushInterval: this.options.flushInterval!,
|
|
34
|
-
debug: this.options.debug || false
|
|
23
|
+
...options,
|
|
24
|
+
endpoint
|
|
35
25
|
});
|
|
36
26
|
|
|
37
|
-
if (this.
|
|
27
|
+
if (!this.isInstrumented) {
|
|
28
|
+
try { instrumentHttp(endpoint); } catch (e) { }
|
|
29
|
+
try { instrumentMongo(); } catch (e) { }
|
|
30
|
+
try { instrumentPg(); } catch (e) { }
|
|
31
|
+
|
|
32
|
+
this.isInstrumented = true;
|
|
33
|
+
if (options.debug) console.log('[Senzor] Auto-instrumentation enabled');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (options.debug) console.log('[Senzor] Initialized');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public startTrace<T>(data: Partial<ActiveTrace['data']>, next: () => T): T {
|
|
40
|
+
if (!this.transport) return next();
|
|
41
|
+
|
|
42
|
+
const trace: ActiveTrace = {
|
|
43
|
+
id: randomUUID(),
|
|
44
|
+
startTime: performance.now(),
|
|
45
|
+
data: data,
|
|
46
|
+
spans: []
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
return Context.run(trace, next);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public endTrace(status: number, extraData: any = {}) {
|
|
53
|
+
const trace = Context.current();
|
|
54
|
+
if (!trace || !this.transport) return;
|
|
55
|
+
|
|
56
|
+
const duration = performance.now() - trace.startTime;
|
|
57
|
+
|
|
58
|
+
const payload = {
|
|
59
|
+
traceId: trace.id,
|
|
60
|
+
...trace.data,
|
|
61
|
+
...extraData,
|
|
62
|
+
status,
|
|
63
|
+
duration,
|
|
64
|
+
spans: trace.spans,
|
|
65
|
+
timestamp: new Date().toISOString()
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
this.transport.add(payload);
|
|
38
69
|
}
|
|
39
70
|
|
|
40
|
-
// --- Manual Tracking (For any framework) ---
|
|
41
71
|
public track(data: {
|
|
42
72
|
method: string;
|
|
43
73
|
route: string;
|
|
@@ -48,14 +78,37 @@ export class SenzorClient {
|
|
|
48
78
|
userAgent?: string;
|
|
49
79
|
}) {
|
|
50
80
|
if (!this.transport) return;
|
|
51
|
-
|
|
52
|
-
|
|
81
|
+
const payload = {
|
|
82
|
+
traceId: randomUUID(),
|
|
53
83
|
...data,
|
|
84
|
+
spans: [],
|
|
54
85
|
timestamp: new Date().toISOString()
|
|
55
|
-
}
|
|
86
|
+
};
|
|
87
|
+
this.transport.add(payload);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public startSpan(name: string, type: 'db' | 'http' | 'function' | 'custom' = 'custom') {
|
|
91
|
+
const trace = Context.current();
|
|
92
|
+
if (!trace) return { end: () => { } };
|
|
93
|
+
|
|
94
|
+
const startTime = performance.now() - trace.startTime;
|
|
95
|
+
const spanStartAbs = performance.now();
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
end: (meta?: any, status?: number) => {
|
|
99
|
+
const duration = performance.now() - spanStartAbs;
|
|
100
|
+
Context.addSpan({
|
|
101
|
+
name,
|
|
102
|
+
type,
|
|
103
|
+
startTime,
|
|
104
|
+
duration,
|
|
105
|
+
status,
|
|
106
|
+
meta
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
};
|
|
56
110
|
}
|
|
57
111
|
|
|
58
|
-
// --- Force Flush (For Serverless/Lambda) ---
|
|
59
112
|
public async flush() {
|
|
60
113
|
if (this.transport) await this.transport.flush();
|
|
61
114
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
2
|
+
import { ActiveTrace } from './types';
|
|
3
|
+
|
|
4
|
+
// Storage to hold the current Trace for any async operation
|
|
5
|
+
export const storage = new AsyncLocalStorage<ActiveTrace>();
|
|
6
|
+
|
|
7
|
+
export const Context = {
|
|
8
|
+
// Run a function within a trace context
|
|
9
|
+
// Updated to be Generic <T> to allow returning values (Promises, etc)
|
|
10
|
+
run: <T>(trace: ActiveTrace, fn: () => T): T => {
|
|
11
|
+
return storage.run(trace, fn);
|
|
12
|
+
},
|
|
13
|
+
|
|
14
|
+
// Get current trace (safe)
|
|
15
|
+
current: (): ActiveTrace | undefined => {
|
|
16
|
+
return storage.getStore();
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
// Add a span to the current trace
|
|
20
|
+
addSpan: (span: any) => {
|
|
21
|
+
const store = storage.getStore();
|
|
22
|
+
if (store) {
|
|
23
|
+
store.spans.push(span);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
package/src/core/transport.ts
CHANGED
|
@@ -1,31 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
apiKey: string;
|
|
3
|
-
endpoint: string;
|
|
4
|
-
batchSize: number;
|
|
5
|
-
flushInterval: number;
|
|
6
|
-
debug: boolean;
|
|
7
|
-
}
|
|
1
|
+
import { SenzorOptions } from './types';
|
|
8
2
|
|
|
9
3
|
export class Transport {
|
|
10
4
|
private queue: any[] = [];
|
|
11
|
-
private
|
|
12
|
-
private timer: any = null;
|
|
5
|
+
private timer: NodeJS.Timeout | null = null;
|
|
13
6
|
|
|
14
|
-
constructor(config:
|
|
15
|
-
this.config = config;
|
|
16
|
-
// Only start timer in non-serverless environments (long running processes)
|
|
7
|
+
constructor(private config: SenzorOptions) {
|
|
17
8
|
if (typeof setInterval !== 'undefined') {
|
|
18
|
-
this.timer = setInterval(() => this.flush(),
|
|
19
|
-
// Unref if in Node.js to allow process exit
|
|
9
|
+
this.timer = setInterval(() => this.flush(), config.flushInterval || 10000);
|
|
20
10
|
if (this.timer && typeof this.timer.unref === 'function') {
|
|
21
|
-
this.timer.unref();
|
|
11
|
+
this.timer.unref(); // Don't block process exit
|
|
22
12
|
}
|
|
23
13
|
}
|
|
24
14
|
}
|
|
25
15
|
|
|
26
|
-
public add(
|
|
27
|
-
this.queue.push(
|
|
28
|
-
if (this.queue.length >= this.config.batchSize) {
|
|
16
|
+
public add(trace: any) {
|
|
17
|
+
this.queue.push(trace);
|
|
18
|
+
if (this.queue.length >= (this.config.batchSize || 100)) {
|
|
29
19
|
this.flush();
|
|
30
20
|
}
|
|
31
21
|
}
|
|
@@ -37,22 +27,21 @@ export class Transport {
|
|
|
37
27
|
this.queue = [];
|
|
38
28
|
|
|
39
29
|
try {
|
|
40
|
-
// Use
|
|
41
|
-
await fetch(this.config.endpoint, {
|
|
30
|
+
// Use global fetch (Node 18+)
|
|
31
|
+
await fetch(this.config.endpoint || 'https://api.senzor.dev/api/ingest/apm', {
|
|
42
32
|
method: 'POST',
|
|
43
33
|
headers: {
|
|
44
34
|
'Content-Type': 'application/json',
|
|
45
35
|
'x-service-api-key': this.config.apiKey,
|
|
46
36
|
},
|
|
47
37
|
body: JSON.stringify(batch),
|
|
48
|
-
// keepalive ensures connection stays open even if function ends (vital for APM)
|
|
49
38
|
keepalive: true,
|
|
50
39
|
});
|
|
51
|
-
|
|
40
|
+
|
|
52
41
|
if (this.config.debug) console.log(`[Senzor] Flushed ${batch.length} traces`);
|
|
53
42
|
} catch (err) {
|
|
54
43
|
if (this.config.debug) console.error('[Senzor] Ingestion Error:', err);
|
|
55
|
-
//
|
|
44
|
+
// Dropping data to prevent memory leaks is preferred in APM
|
|
56
45
|
}
|
|
57
46
|
}
|
|
58
47
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface SenzorOptions {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
endpoint?: string;
|
|
4
|
+
batchSize?: number;
|
|
5
|
+
flushInterval?: number; // ms
|
|
6
|
+
debug?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface Span {
|
|
10
|
+
name: string;
|
|
11
|
+
type: 'db' | 'http' | 'function' | 'custom';
|
|
12
|
+
startTime: number; // Relative to trace start
|
|
13
|
+
duration: number;
|
|
14
|
+
status?: number;
|
|
15
|
+
meta?: Record<string, any>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Trace {
|
|
19
|
+
traceId: string;
|
|
20
|
+
method: string;
|
|
21
|
+
route: string; // Normalized
|
|
22
|
+
path: string; // Raw
|
|
23
|
+
status: number;
|
|
24
|
+
duration: number;
|
|
25
|
+
ip?: string;
|
|
26
|
+
userAgent?: string;
|
|
27
|
+
timestamp: string;
|
|
28
|
+
spans: Span[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Internal interface for an active trace object
|
|
32
|
+
export interface ActiveTrace {
|
|
33
|
+
id: string;
|
|
34
|
+
startTime: number;
|
|
35
|
+
data: Partial<Trace>;
|
|
36
|
+
spans: Span[];
|
|
37
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { client
|
|
1
|
+
import { client } from './core/client';
|
|
2
2
|
import { expressMiddleware } from './middleware/express';
|
|
3
3
|
import { wrapH3 } from './wrappers/h3';
|
|
4
4
|
import { wrapNextRoute, wrapNextPages } from './wrappers/next';
|
|
5
5
|
import { senzorPlugin } from './wrappers/fastify';
|
|
6
|
+
import { SenzorOptions } from './core/types';
|
|
6
7
|
|
|
7
8
|
const Senzor = {
|
|
8
9
|
// Core
|
|
9
10
|
init: (options: SenzorOptions) => client.init(options),
|
|
10
11
|
flush: () => client.flush(),
|
|
11
|
-
track: client.track.bind(client),
|
|
12
12
|
|
|
13
13
|
// Express / Connect
|
|
14
14
|
requestHandler: expressMiddleware,
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import https from 'https';
|
|
3
|
+
import { URL } from 'url';
|
|
4
|
+
import { Context } from '../core/context';
|
|
5
|
+
|
|
6
|
+
// Helper to safely wrap modules
|
|
7
|
+
const shimmer = (module: any, methodName: string, wrapper: (original: Function) => Function) => {
|
|
8
|
+
if (!module[methodName]) return;
|
|
9
|
+
const original = module[methodName];
|
|
10
|
+
module[methodName] = wrapper(original);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const instrumentHttp = (ingestUrl: string) => {
|
|
14
|
+
const ingestHost = new URL(ingestUrl).hostname;
|
|
15
|
+
|
|
16
|
+
const requestWrapper = (original: Function) => {
|
|
17
|
+
return function (this: any, ...args: any[]) {
|
|
18
|
+
// 1. Parse Arguments to get URL
|
|
19
|
+
let options: any = {};
|
|
20
|
+
let urlStr = '';
|
|
21
|
+
|
|
22
|
+
if (typeof args[0] === 'string' || args[0] instanceof URL) {
|
|
23
|
+
urlStr = args[0].toString();
|
|
24
|
+
options = args[1] || {};
|
|
25
|
+
} else {
|
|
26
|
+
options = args[0] || {};
|
|
27
|
+
const protocol = options.protocol || 'http:';
|
|
28
|
+
const host = options.hostname || options.host || 'localhost';
|
|
29
|
+
const path = options.path || '/';
|
|
30
|
+
urlStr = `${protocol}//${host}${path}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 2. SAFETY GUARD: Ignore calls to Senzor Ingest API (Prevent Infinite Loop)
|
|
34
|
+
if (urlStr.includes(ingestHost) || (options.hostname && options.hostname.includes(ingestHost))) {
|
|
35
|
+
return original.apply(this, args);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 3. Check if we are inside an Active Trace
|
|
39
|
+
// If we are not handling a user request, don't trace background http calls
|
|
40
|
+
const trace = Context.current();
|
|
41
|
+
if (!trace) {
|
|
42
|
+
return original.apply(this, args);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 4. Start Span
|
|
46
|
+
const method = (options.method || 'GET').toUpperCase();
|
|
47
|
+
const startTime = performance.now() - trace.startTime; // Relative to trace start
|
|
48
|
+
const spanStartAbs = performance.now();
|
|
49
|
+
|
|
50
|
+
// 5. Execute Request
|
|
51
|
+
const req = original.apply(this, args);
|
|
52
|
+
|
|
53
|
+
// 6. Hook into Response/Error
|
|
54
|
+
req.on('response', (res: any) => {
|
|
55
|
+
// Wait for end of stream to calculate full duration (TTFB + Download)
|
|
56
|
+
res.on('end', () => {
|
|
57
|
+
const duration = performance.now() - spanStartAbs;
|
|
58
|
+
|
|
59
|
+
Context.addSpan({
|
|
60
|
+
name: `${method} ${new URL(urlStr).hostname}`, // e.g. "GET api.stripe.com"
|
|
61
|
+
type: 'http',
|
|
62
|
+
startTime,
|
|
63
|
+
duration,
|
|
64
|
+
status: res.statusCode,
|
|
65
|
+
meta: {
|
|
66
|
+
url: urlStr,
|
|
67
|
+
method: method,
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
req.on('error', (err: Error) => {
|
|
74
|
+
const duration = performance.now() - spanStartAbs;
|
|
75
|
+
Context.addSpan({
|
|
76
|
+
name: `${method} ${urlStr}`,
|
|
77
|
+
type: 'http',
|
|
78
|
+
startTime,
|
|
79
|
+
duration,
|
|
80
|
+
status: 500, // Client Error
|
|
81
|
+
meta: { error: err.message }
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return req;
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// Apply patches
|
|
90
|
+
shimmer(http, 'request', requestWrapper);
|
|
91
|
+
shimmer(http, 'get', requestWrapper);
|
|
92
|
+
shimmer(https, 'request', requestWrapper);
|
|
93
|
+
shimmer(https, 'get', requestWrapper);
|
|
94
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Context } from '../core/context';
|
|
2
|
+
|
|
3
|
+
export const instrumentMongo = () => {
|
|
4
|
+
try {
|
|
5
|
+
const mongodb = require('mongodb');
|
|
6
|
+
const Collection = mongodb.Collection;
|
|
7
|
+
|
|
8
|
+
const methods = [
|
|
9
|
+
'find',
|
|
10
|
+
'findOne',
|
|
11
|
+
'insertOne',
|
|
12
|
+
'insertMany',
|
|
13
|
+
'updateOne',
|
|
14
|
+
'updateMany',
|
|
15
|
+
'deleteOne',
|
|
16
|
+
'deleteMany',
|
|
17
|
+
'aggregate',
|
|
18
|
+
'countDocuments'
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
methods.forEach((method) => {
|
|
22
|
+
if (!Collection.prototype[method]) return;
|
|
23
|
+
|
|
24
|
+
const original = Collection.prototype[method];
|
|
25
|
+
|
|
26
|
+
Collection.prototype[method] = function (...args: any[]) {
|
|
27
|
+
const trace = Context.current();
|
|
28
|
+
// If not inside a tracked request, just run normally
|
|
29
|
+
if (!trace) return original.apply(this, args);
|
|
30
|
+
|
|
31
|
+
const startTime = performance.now() - trace.startTime;
|
|
32
|
+
const spanStartAbs = performance.now();
|
|
33
|
+
const collectionName = this.collectionName;
|
|
34
|
+
|
|
35
|
+
// Helper to finish span
|
|
36
|
+
const endSpan = (err?: Error) => {
|
|
37
|
+
const duration = performance.now() - spanStartAbs;
|
|
38
|
+
Context.addSpan({
|
|
39
|
+
name: `MongoDB ${method} (${collectionName})`,
|
|
40
|
+
type: 'db',
|
|
41
|
+
startTime,
|
|
42
|
+
duration,
|
|
43
|
+
status: err ? 500 : 0,
|
|
44
|
+
meta: {
|
|
45
|
+
collection: collectionName,
|
|
46
|
+
operation: method,
|
|
47
|
+
error: err ? err.message : undefined
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
const result = original.apply(this, args);
|
|
54
|
+
|
|
55
|
+
// Handle Promise/Cursor
|
|
56
|
+
if (result && typeof result.then === 'function') {
|
|
57
|
+
return result.then(
|
|
58
|
+
(res: any) => { endSpan(); return res; },
|
|
59
|
+
(err: any) => { endSpan(err); throw err; }
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
// Handle FindCursor (it doesn't execute immediately, but for APM visuals we mark start/return)
|
|
63
|
+
// Ideally we'd wrap toArray() but that's complex. For 'find', we just mark the creation.
|
|
64
|
+
endSpan();
|
|
65
|
+
return result;
|
|
66
|
+
|
|
67
|
+
} catch (err: any) {
|
|
68
|
+
endSpan(err);
|
|
69
|
+
throw err;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
} catch (e) {
|
|
75
|
+
// User doesn't use mongodb, ignore
|
|
76
|
+
}
|
|
77
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Context } from '../core/context';
|
|
2
|
+
|
|
3
|
+
// Simple shim for 'pg' library
|
|
4
|
+
export const instrumentPg = () => {
|
|
5
|
+
try {
|
|
6
|
+
// Try to require pg (it might not be installed by user)
|
|
7
|
+
const pg = require('pg');
|
|
8
|
+
const originalQuery = pg.Client.prototype.query;
|
|
9
|
+
|
|
10
|
+
pg.Client.prototype.query = function (...args: any[]) {
|
|
11
|
+
const trace = Context.current();
|
|
12
|
+
if (!trace) return originalQuery.apply(this, args);
|
|
13
|
+
|
|
14
|
+
const startTime = performance.now() - trace.startTime;
|
|
15
|
+
const spanStartAbs = performance.now();
|
|
16
|
+
|
|
17
|
+
// Extract SQL (first arg usually string or config object)
|
|
18
|
+
const sql = typeof args[0] === 'string' ? args[0] : args[0].text;
|
|
19
|
+
|
|
20
|
+
// Wrap callback if present, or handle Promise
|
|
21
|
+
const result = originalQuery.apply(this, args);
|
|
22
|
+
|
|
23
|
+
if (result && typeof result.then === 'function') {
|
|
24
|
+
return result.then((res: any) => {
|
|
25
|
+
const duration = performance.now() - spanStartAbs;
|
|
26
|
+
Context.addSpan({
|
|
27
|
+
name: 'Postgres Query',
|
|
28
|
+
type: 'db',
|
|
29
|
+
startTime,
|
|
30
|
+
duration,
|
|
31
|
+
meta: { query: sql }
|
|
32
|
+
});
|
|
33
|
+
return res;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
};
|
|
38
|
+
} catch (e) {
|
|
39
|
+
// User doesn't use pg, ignore
|
|
40
|
+
}
|
|
41
|
+
};
|
|
@@ -2,42 +2,32 @@ import { client } from '../core/client';
|
|
|
2
2
|
|
|
3
3
|
export const expressMiddleware = () => {
|
|
4
4
|
return (req: any, res: any, next: () => void) => {
|
|
5
|
-
//
|
|
6
|
-
|
|
5
|
+
// We MUST use startTrace to enable Auto-Instrumentation for this request
|
|
6
|
+
client.startTrace({
|
|
7
|
+
method: req.method,
|
|
8
|
+
path: req.originalUrl || req.url,
|
|
9
|
+
ip: req.ip || req.socket?.remoteAddress,
|
|
10
|
+
userAgent: req.headers['user-agent'],
|
|
11
|
+
}, () => {
|
|
7
12
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
res.once('finish', () => {
|
|
14
|
+
try {
|
|
15
|
+
let route = 'UNKNOWN';
|
|
16
|
+
if (req.route && req.route.path) {
|
|
17
|
+
route = (req.baseUrl || '') + req.route.path;
|
|
18
|
+
} else if (res.statusCode === 404) {
|
|
19
|
+
route = 'Not Found';
|
|
20
|
+
} else {
|
|
21
|
+
route = req.path || 'Wildcard';
|
|
22
|
+
}
|
|
12
23
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (req.route && req.route.path) {
|
|
18
|
-
// Combined baseUrl (if mounted on /api) + path (/:id)
|
|
19
|
-
route = (req.baseUrl || '') + req.route.path;
|
|
20
|
-
} else if (res.statusCode === 404) {
|
|
21
|
-
route = 'Not Found';
|
|
22
|
-
} else {
|
|
23
|
-
// Fallback for unmapped routes or static files
|
|
24
|
-
route = req.path || 'Wildcard';
|
|
24
|
+
client.endTrace(res.statusCode, { route });
|
|
25
|
+
} catch (e) {
|
|
26
|
+
// Fail open
|
|
25
27
|
}
|
|
28
|
+
});
|
|
26
29
|
|
|
27
|
-
|
|
28
|
-
method: req.method,
|
|
29
|
-
route: route,
|
|
30
|
-
path: req.originalUrl || req.url,
|
|
31
|
-
status: res.statusCode,
|
|
32
|
-
duration: duration,
|
|
33
|
-
ip: req.ip || req.socket?.remoteAddress,
|
|
34
|
-
userAgent: req.headers['user-agent'],
|
|
35
|
-
});
|
|
36
|
-
} catch (e) {
|
|
37
|
-
// Fail open
|
|
38
|
-
}
|
|
30
|
+
next();
|
|
39
31
|
});
|
|
40
|
-
|
|
41
|
-
next();
|
|
42
32
|
};
|
|
43
33
|
};
|
package/src/wrappers/fastify.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { client } from '../core/client';
|
|
2
|
-
import { SenzorOptions } from '../core/
|
|
2
|
+
import { SenzorOptions } from '../core/types';
|
|
3
3
|
|
|
4
4
|
// We don't import Fastify types to keep zero-deps, but structure matches
|
|
5
5
|
export const senzorPlugin = (fastify: any, options: SenzorOptions, done: Function) => {
|
package/src/wrappers/h3.ts
CHANGED
|
@@ -1,49 +1,37 @@
|
|
|
1
1
|
import { client } from '../core/client';
|
|
2
2
|
import { getRoute } from '../core/normalizer';
|
|
3
3
|
|
|
4
|
-
//
|
|
4
|
+
// Minimal types for H3 to avoid peer-deps
|
|
5
5
|
type EventHandler = (event: any) => any;
|
|
6
6
|
|
|
7
7
|
export const wrapH3 = (handler: EventHandler) => {
|
|
8
|
-
return
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
let error: any = null;
|
|
8
|
+
return (event: any) => {
|
|
9
|
+
const req = event.node.req;
|
|
10
|
+
const path = req.originalUrl || req.url || '/';
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
status = err.statusCode || err.status || 500;
|
|
23
|
-
throw err;
|
|
24
|
-
} finally {
|
|
25
|
-
// Non-blocking collection
|
|
26
|
-
const duration = performance.now() - start;
|
|
27
|
-
const req = event.node.req;
|
|
28
|
-
|
|
29
|
-
const path = req.originalUrl || req.url || '/';
|
|
12
|
+
// Start Trace Context
|
|
13
|
+
return client.startTrace({
|
|
14
|
+
method: req.method || 'GET',
|
|
15
|
+
path: path,
|
|
16
|
+
ip: req.headers['x-forwarded-for'] || req.socket?.remoteAddress,
|
|
17
|
+
userAgent: req.headers['user-agent'],
|
|
18
|
+
}, async () => {
|
|
19
|
+
try {
|
|
20
|
+
const response = await handler(event);
|
|
30
21
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
status
|
|
36
|
-
duration: duration,
|
|
37
|
-
ip: getIp(req),
|
|
38
|
-
userAgent: req.headers['user-agent'],
|
|
39
|
-
});
|
|
22
|
+
// H3/Nitro response status
|
|
23
|
+
let status = 200;
|
|
24
|
+
if (event.node.res.statusCode) status = event.node.res.statusCode;
|
|
25
|
+
// Check if response is an error object
|
|
26
|
+
if (response && response.statusCode) status = response.statusCode;
|
|
40
27
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
28
|
+
client.endTrace(status, { route: getRoute(event, path) });
|
|
29
|
+
return response;
|
|
30
|
+
} catch (err: any) {
|
|
31
|
+
const status = err.statusCode || err.status || 500;
|
|
32
|
+
client.endTrace(status, { route: getRoute(event, path) });
|
|
33
|
+
throw err;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
44
36
|
};
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const getIp = (req: any) => {
|
|
48
|
-
return req.headers['x-forwarded-for'] || req.socket?.remoteAddress;
|
|
49
37
|
};
|
package/src/wrappers/next.ts
CHANGED
|
@@ -1,74 +1,65 @@
|
|
|
1
1
|
import { client } from '../core/client';
|
|
2
2
|
import { normalizePath } from '../core/normalizer';
|
|
3
3
|
|
|
4
|
-
// --- App Router Wrapper (
|
|
4
|
+
// --- App Router Wrapper (Route Handlers) ---
|
|
5
5
|
export const wrapNextRoute = (handler: Function) => {
|
|
6
6
|
return async (req: Request | any, context?: any) => {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
// 1. Extract Info
|
|
8
|
+
const url = req.url ? new URL(req.url) : { pathname: '/' };
|
|
9
|
+
const method = req.method || 'GET';
|
|
10
|
+
const ua = req.headers.get ? req.headers.get('user-agent') : undefined;
|
|
11
|
+
const ip = req.headers.get ? req.headers.get('x-forwarded-for') : undefined;
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
// 2. Run in Context
|
|
14
|
+
return client.startTrace({
|
|
15
|
+
method,
|
|
16
|
+
path: url.pathname,
|
|
17
|
+
userAgent: ua,
|
|
18
|
+
ip: ip
|
|
19
|
+
}, async () => {
|
|
20
|
+
try {
|
|
21
|
+
const response = await handler(req, context);
|
|
22
|
+
const status = response?.status || 200;
|
|
23
|
+
|
|
24
|
+
client.endTrace(status, { route: normalizePath(url.pathname) });
|
|
25
|
+
return response;
|
|
26
|
+
} catch (err: any) {
|
|
27
|
+
client.endTrace(500, { route: normalizePath(url.pathname) });
|
|
28
|
+
throw err;
|
|
14
29
|
}
|
|
15
|
-
|
|
16
|
-
} catch (err: any) {
|
|
17
|
-
status = 500;
|
|
18
|
-
throw err;
|
|
19
|
-
} finally {
|
|
20
|
-
const duration = performance.now() - start;
|
|
21
|
-
|
|
22
|
-
// App Router Request is a standard Web Request object
|
|
23
|
-
const url = req.url ? new URL(req.url) : { pathname: '/' };
|
|
24
|
-
|
|
25
|
-
// In App Router, we often rely on file-system path, but context.params helps
|
|
26
|
-
// For now, robust heuristic normalization is best
|
|
27
|
-
const route = normalizePath(url.pathname);
|
|
28
|
-
|
|
29
|
-
client.track({
|
|
30
|
-
method: req.method || 'GET',
|
|
31
|
-
route: route,
|
|
32
|
-
path: url.pathname,
|
|
33
|
-
status: status,
|
|
34
|
-
duration: duration,
|
|
35
|
-
userAgent: req.headers.get ? req.headers.get('user-agent') : undefined,
|
|
36
|
-
// IP extraction from Web Request is tricky without headers
|
|
37
|
-
ip: req.headers.get ? req.headers.get('x-forwarded-for') : undefined,
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
// Vercel/Serverless Flush Safety
|
|
41
|
-
// We purposefully do NOT await flush here to avoid latency.
|
|
42
|
-
// Ideally user configures transport to sync flush or uses waitUntil
|
|
43
|
-
}
|
|
30
|
+
});
|
|
44
31
|
};
|
|
45
32
|
};
|
|
46
33
|
|
|
47
|
-
// --- Pages Router Wrapper (
|
|
34
|
+
// --- Pages Router Wrapper (API Routes) ---
|
|
48
35
|
export const wrapNextPages = (handler: Function) => {
|
|
49
36
|
return async (req: any, res: any) => {
|
|
50
|
-
const
|
|
37
|
+
const path = req.url ? req.url.split('?')[0] : '/';
|
|
38
|
+
|
|
39
|
+
// 1. Run in Context
|
|
40
|
+
return client.startTrace({
|
|
41
|
+
method: req.method || 'GET',
|
|
42
|
+
path: path,
|
|
43
|
+
userAgent: req.headers['user-agent'],
|
|
44
|
+
ip: req.headers['x-forwarded-for'] || req.socket?.remoteAddress,
|
|
45
|
+
}, async () => {
|
|
46
|
+
|
|
47
|
+
// 2. Hook Response
|
|
48
|
+
const done = () => {
|
|
49
|
+
client.endTrace(res.statusCode || 200, { route: normalizePath(path) });
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
res.once('finish', done);
|
|
53
|
+
res.once('close', done); // Fallback if finish doesn't fire
|
|
51
54
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
status: res.statusCode || 200,
|
|
62
|
-
duration: duration,
|
|
63
|
-
ip: req.headers['x-forwarded-for'] || req.socket?.remoteAddress,
|
|
64
|
-
userAgent: req.headers['user-agent']
|
|
65
|
-
});
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
res.once('finish', done);
|
|
69
|
-
// Handle error case if next/error isn't used
|
|
70
|
-
res.once('close', done);
|
|
71
|
-
|
|
72
|
-
return handler(req, res);
|
|
55
|
+
// 3. Execute
|
|
56
|
+
try {
|
|
57
|
+
return await handler(req, res);
|
|
58
|
+
} catch (e) {
|
|
59
|
+
// Next.js Pages router usually handles errors internally,
|
|
60
|
+
// but we ensure we catch sync errors here
|
|
61
|
+
throw e;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
73
64
|
};
|
|
74
65
|
};
|
package/wiki.md
CHANGED
|
@@ -94,4 +94,27 @@ import { Senzor } from '@senzops/apm-node';
|
|
|
94
94
|
|
|
95
95
|
fastify.register(Senzor.fastifyPlugin, { apiKey: '...' });
|
|
96
96
|
```
|
|
97
|
-
This approach provides **native robustness** for each framework. It captures errors (500s), 404s (Route not found), and correct timing without the user manually writing `track()` calls.
|
|
97
|
+
This approach provides **native robustness** for each framework. It captures errors (500s), 404s (Route not found), and correct timing without the user manually writing `track()` calls.
|
|
98
|
+
|
|
99
|
+
## How Users Add Spans (Waterfall)
|
|
100
|
+
Now your users can do this in their Express/Next.js apps:
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
app.get('/users', async (req, res) => {
|
|
104
|
+
// 1. Start a span
|
|
105
|
+
const dbSpan = Senzor.startSpan('fetch_users_db', 'db');
|
|
106
|
+
|
|
107
|
+
const users = await db.query('SELECT * FROM users');
|
|
108
|
+
|
|
109
|
+
// 2. End span (Duration calculated automatically)
|
|
110
|
+
dbSpan.end({ query: 'SELECT * FROM users' });
|
|
111
|
+
|
|
112
|
+
// 3. Start another span
|
|
113
|
+
const apiSpan = Senzor.startSpan('stripe_api', 'http');
|
|
114
|
+
await axios.get('...');
|
|
115
|
+
apiSpan.end({}, 200);
|
|
116
|
+
|
|
117
|
+
res.json(users);
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
This will automatically appear in your **Waterfall Chart** on the dashboard, nested under the main request Trace.
|