@lensmcp/nest-instrumentation 1.26.2 → 1.26.4
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/lib/context.d.ts +5 -0
- package/lib/instrument.js +1 -1
- package/lib/state.d.ts +2 -0
- package/lib/state.js +1 -1
- package/lib/types.d.ts +12 -0
- package/package.json +4 -4
package/lib/context.d.ts
CHANGED
|
@@ -55,6 +55,11 @@ export interface LensmcpRequestContext {
|
|
|
55
55
|
*/
|
|
56
56
|
loopRedisCalls?: number;
|
|
57
57
|
loopExternalCalls?: number;
|
|
58
|
+
/**
|
|
59
|
+
* Traced calls begun in this request so far. A call that ends with the count where it left it called no traced
|
|
60
|
+
* method — nor did anything else of the request begin one meanwhile (`trace.leafMinMs`).
|
|
61
|
+
*/
|
|
62
|
+
spanCount?: number;
|
|
58
63
|
/**
|
|
59
64
|
* The request's DB calls by the second they ran in (epoch second → calls), kept by the taps that count
|
|
60
65
|
* `dbCallCount` (`countDbSecond`): a loop that lived seconds says how many of its calls fell within two of them.
|
package/lib/instrument.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var de=Object.defineProperty;var h=(t,n)=>de(t,"name",{value:n,configurable:!0});var ce=Object.defineProperty,u=h((t,n)=>ce(t,"name",{value:n,configurable:!0}),"c");import{fingerprint as O,HEADLESS_JOB_FLOW_PREFIX as ue,ulid as q}from"@lensmcp/core";import{redactText as K}from"@lensmcp/core/redact";import{isControlFlowThrow as pe}from"@lensmcp/core/queue-signals";import{captureFor as me,capturedValue as U}from"./capture.js";import{countDbSecond as fe,currentLensmcpContext as Ie,currentSpanId as we,dbShapesEnd as he,LENSMCP_SPAN_STORAGE as Ce,noteDbShape as ve}from"./context.js";import{activeOptions as be}from"./state.js";import{performance as Y}from"node:perf_hooks";export const LENSMCP_TRACED=Symbol.for("@lensmcp/nest-instrumentation/traced"),LENSMCP_IGNORE=Symbol.for("@lensmcp/nest-instrumentation/ignore");const R=300,ye=2e3;function Z(t,n,l,a){if(!t)return;const f=Math.floor(n/1e3),I=Math.floor(l/1e3);let o=0,r=0;for(const[p,S]of t)p<f||p>I||(o+=S,r=Math.max(r,S+(p<I?t.get(p+1)??0:0)));return o>=a?Math.min(a,r):void 0}h(Z,"we"),u(Z,"dbBurstOf");const M=3,ge=8;function ee(t,n){if(!t)return;const l=Math.max(0,n-t.base),a=new Map;let f=0;for(let o=l;o<t.entries.length;o++){const r=t.entries[o];if(r.r)continue;f++;let p=a.get(r.s);p||a.set(r.s,p={calls:0,values:new Set,unknown:!1}),p.calls++,r.v===void 0?p.unknown=!0:p.values.size<ge&&p.values.add(r.v)}const I=[];for(const[o,r]of a)r.calls>1&&I.push({sqlId:o,calls:r.calls,values:r.values.size,unknown:r.unknown});return I.sort((o,r)=>r.calls-o.calls||Number(T(r))-Number(T(o))),{calls:f,distinctStatements:a.size,repeated:I,from:l}}h(ee,"Ce"),u(ee,"ownDbOf");const T=u(t=>t.values>1||t.unknown,"isVarying");function te(t,n,l){for(let a=n;a<t.entries.length;a++){const f=t.entries[a];!f.r&&l.has(f.s)&&(f.r=!0)}}h(te,"Se"),u(te,"markReported");function se(t){if(t instanceof Error){const l=t.code,a=K(t.message??"");return{name:t.name||t.constructor?.name||"Error",...a?{message:a.length>R?`${a.slice(0,R)}\u2026`:a}:{},...typeof l=="string"&&l.length<=64?{code:l}:{}}}const n=typeof t=="string"?K(t):void 0;return{name:t==null?"nothing":typeof t=="object"?"non-Error object":`non-Error ${typeof t}`,...n?{message:n.slice(0,R)}:{}}}h(se,"xe"),u(se,"thrownOf");export function LensmcpIgnore(){return((t,n)=>{if(n===void 0)t[LENSMCP_IGNORE]=!0;else{const l=t[n];typeof l=="function"&&(l[LENSMCP_IGNORE]=!0)}})}h(LensmcpIgnore,"LensmcpIgnore"),u(LensmcpIgnore,"LensmcpIgnore");export function isTraced(t){return typeof t=="function"&&!!t[LENSMCP_TRACED]}h(isTraced,"isTraced"),u(isTraced,"isTraced");export function isIgnored(t){return!!t&&(typeof t=="function"||typeof t=="object")&&!!t[LENSMCP_IGNORE]}h(isIgnored,"isIgnored"),u(isIgnored,"isIgnored");export function traceWrap(t,n,l={}){if(isTraced(t))return t;const a=l.db??!1,f=u(function(...I){let o;try{o=be()}catch{return t.apply(this,I)}if(!o.trace.services)return t.apply(this,I);const r=Date.now();let p=r,S=0;const e=Ie(),$=q(),A=we(),D=e?.dbCallCount??0,P=e?.redisCallCount??0,_=e?.externalCallCount??0,oe=he(e),ne=e?.loopRedisCalls??0,re=e?.loopExternalCalls??0,ae=e?e.spanCount=(e.spanCount??0)+1:0,b=l.capture??me(n),B=b==="args"||b==="both"?U(I):void 0,E=u((s,i,v,C,d)=>{const c=Date.now()-r;if(s==="ok"&&!l.explicit&&b===void 0&&!e?.flowId&&!e?.requestId&&!e?.runId&&c<o.trace.flowlessMinMs||s==="ok"&&!d&&!l.explicit&&b===void 0&&!a&&e&&(e.flowId||e.requestId||e.runId)&&c<o.trace.leafMinMs&&e.spanCount===ae&&(e.dbCallCount??0)===D&&(e.redisCallCount??0)===P&&(e.externalCallCount??0)===_)return;const y={id:q(),sessionId:o.sessionId,timestamp:Date.now(),source:"nestjs",category:"backend",severity:s==="error"?"error":"info",context:{sessionId:o.sessionId,flowId:e?.flowId,requestId:e?.requestId,...e?.runId?{runId:e.runId}:{}},fingerprint:O({kind:"method-call",identity:n}),title:`${n} (${c}ms)${s==="error"?" \u2717":""}`,message:i instanceof Error?i.message:void 0,raw:{kind:"span",...o.projectName?{project:o.projectName}:{},span:{name:n,spanId:$,...A?{parentSpanId:A}:{},flowId:e?.flowId,requestId:e?.requestId,startTime:r,endTime:Date.now(),durationMs:c,syncStartedAt:p,syncDurationMs:S,status:s,outcome:d?"signalled":s==="error"?"threw":"returned",...d?{signal:d}:{},...s==="error"?{error:se(i)}:{},...B!==void 0?{args:B}:{},...s==="ok"&&!d&&(b==="result"||b==="both")?{result:U(v)}:{},...C&&C.calls>=M?{db:{calls:C.calls,distinctStatements:C.distinctStatements,...(C.repeated[0]?.calls??0)<M?{class:"sequential"}:{}}}:{}}}};o.emit(y)},"emitSpan"),F=u(()=>{const s=Date.now();e&&(e.dbCallCount=(e.dbCallCount??0)+1,fe(e,s),ve(e,n)),o.emit({id:q(),sessionId:o.sessionId,timestamp:Date.now(),source:"db",category:"db",severity:"info",context:{sessionId:o.sessionId,flowId:e?.flowId,requestId:e?.requestId,...e?.runId?{runId:e.runId}:{}},fingerprint:O({kind:"db-query",identity:n}),title:`query ${n}`,raw:{kind:"db-query",...o.projectName?{project:o.projectName}:{},query:{signature:n},startedAt:r,durationMs:s-r,span:{parentSpanId:$}}})},"emitDbQuery"),V=u(()=>{if(!(a||!e||(e.dbCallCount??0)-D<M))return ee(e.dbShapes,oe)},"ownDb"),W=u(s=>{if(a||!e)return;const i=(e.dbCallCount??0)-D,v=(e.redisCallCount??0)-P,C=(e.externalCallCount??0)-_,d=Math.max(0,v-((e.loopRedisCalls??0)-ne)),c=Math.max(0,C-((e.loopExternalCalls??0)-re)),y=(s?.repeated??[]).filter(w=>w.calls>=M),m=y[0];if(!m&&d<3&&c<3)return;const x=m?.calls??0,j=m?T(m)?"loop":"repeated-read":void 0,H=Date.now(),N=H-r,g=m&&N>=ye?Z(e.dbSeconds,r,H,x):void 0,L=e.isStream?.()===!0,ie=L&&g!==void 0&&g<3&&d<3&&c<3,k=[];if(m&&s){const w=j==="loop"?m.unknown?"":" with varying values":" with the same values",X=`${s.calls} DB call${s.calls===1?"":"s"}, ${s.distinctStatements} statement${s.distinctStatements===1?"":"s"}`;k.push(L&&g!==void 0?`${x}\xD7 one statement over ${Math.round(N/1e3)} s of a stream, at most ${g} within two seconds \u2014 ${X}`:`${x}\xD7 one statement${w} \u2014 ${X}`)}d>=3&&k.push(`${d} redis calls`),c>=3&&k.push(`${c} external calls`),m&&e.dbShapes&&s&&te(e.dbShapes,s.from,new Set(y.map(w=>w.sqlId))),d>=3&&(e.loopRedisCalls=(e.loopRedisCalls??0)+d),c>=3&&(e.loopExternalCalls=(e.loopExternalCalls??0)+c);const J=s?.calls??0,Q=(s?Math.max(0,i-J):0)+(v-d)+(C-c),le=j==="repeated-read"&&d<3&&c<3?"repeated read":"loop";o.emit({id:q(),sessionId:o.sessionId,timestamp:Date.now(),source:"nestjs",category:"backend",severity:ie?"info":"warning",context:{sessionId:o.sessionId,flowId:e.flowId,requestId:e.requestId,...e.runId?{runId:e.runId}:{}},fingerprint:O({kind:"loop",identity:n}),title:`${le} in ${n} (${k.join(", ")})`,raw:{kind:"loop",...o.projectName?{project:o.projectName}:{},loop:{iterations:Math.max(x,d,c),durationMs:N,startedAt:r,dbCallsInsideLoop:x,...m&&s?{class:j,method:n,sqlId:m.sqlId,calls:s.calls,distinctStatements:s.distinctStatements,...m.unknown?{}:{distinctValues:m.values},...y.length>1?{repeated:y.slice(0,3).map(w=>({sqlId:w.sqlId,calls:w.calls,...w.unknown?{}:{distinctValues:w.values}}))}:{}}:{},redisCallsInsideLoop:d,externalCallsInsideLoop:c,awaitedOperationsInsideLoop:J+d+c,...g!==void 0?{dbBurst:g}:{},...L?{stream:!0}:{},...Q>0?{reportedBeneath:Q}:{}}}})},"emitLoopIfNeeded"),z=u(s=>{a&&F();const i=V();E("ok",void 0,s,i),W(i)},"onOk"),G=u(s=>{a&&F();const i=V(),v=e?.requestId&&!e.requestId.startsWith(ue)?void 0:pe(s);v?E("ok",void 0,void 0,i,v):E("error",s,void 0,i),W(i)},"onErr");try{const s=Ce.run($,()=>{p=Date.now();const i=Y.now();try{return t.apply(this,I)}finally{S=Math.round((Y.now()-i)*100)/100}});return s instanceof Promise?s.then(i=>(z(i),i),i=>{throw G(i),i}):(z(s),s)}catch(s){throw G(s),s}},"wrapped");return f[LENSMCP_TRACED]=!0,f}h(traceWrap,"traceWrap"),u(traceWrap,"traceWrap");
|
package/lib/state.d.ts
CHANGED
|
@@ -2,5 +2,7 @@ import type { LensmcpModuleOptions, ResolvedOptions } from './types.js';
|
|
|
2
2
|
export declare function setActiveOptions(opts: LensmcpModuleOptions): ResolvedOptions;
|
|
3
3
|
/** The floor a call outside any flow must reach to emit its span: the environment's, else the option's, else 10 ms. */
|
|
4
4
|
export declare function flowlessMinMs(option: number | undefined, env?: NodeJS.ProcessEnv): number;
|
|
5
|
+
/** The floor a call inside a flow that called nothing must reach to emit its span: the environment's, else the option's, else 0 — every one kept. */
|
|
6
|
+
export declare function leafMinMs(option: number | undefined, env?: NodeJS.ProcessEnv): number;
|
|
5
7
|
export declare function activeOptions(): ResolvedOptions;
|
|
6
8
|
export declare function currentGeneration(): number;
|
package/lib/state.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var u=Object.defineProperty;var
|
|
1
|
+
"use strict";var u=Object.defineProperty;var o=(e,t)=>u(e,"name",{value:t,configurable:!0});var m=Object.defineProperty,n=o((e,t)=>m(e,"name",{value:t,configurable:!0}),"s");import{ulid as d}from"@lensmcp/core";import{defaultEventSink as p}from"./event-sink.js";import{performance as M}from"node:perf_hooks";import{threadId as f}from"node:worker_threads";let i,c=0;export function setActiveOptions(e){const t=e.emit??p(),s=`node:${process.pid}:${f}:${M.timeOrigin}`,r={projectName:e.projectName,sessionId:e.sessionId??process.env.LENSMCP_SESSION_ID??d(),emit:n(a=>t({...a,raw:{...a.raw,loopId:s}}),"emit"),trace:{requests:e.trace?.requests??!0,guards:e.trace?.guards??!0,controllers:e.trace?.controllers??!0,services:e.trace?.services??!0,db:e.trace?.db??!1,redis:e.trace?.redis??!1,queues:e.trace?.queues??!1,autoInstrumentMethods:e.trace?.autoInstrumentMethods??!1,suppressNoiseMethods:e.trace?.suppressNoiseMethods??!0,flowlessMinMs:flowlessMinMs(e.trace?.flowlessMinMs),leafMinMs:leafMinMs(e.trace?.leafMinMs)},memory:{mode:e.memory?.mode??"off",scope:e.memory?.scope??"tagged"}};return i=r,c+=1,r}o(setActiveOptions,"setActiveOptions"),n(setActiveOptions,"setActiveOptions");export function flowlessMinMs(e,t=process.env){const s=t.LENSMCP_TRACE_FLOWLESS_MIN_MS,r=s!==void 0&&s.trim()!==""?Number(s):e;return r!==void 0&&Number.isFinite(r)&&r>=0?r:10}o(flowlessMinMs,"flowlessMinMs"),n(flowlessMinMs,"flowlessMinMs");export function leafMinMs(e,t=process.env){const s=t.LENSMCP_TRACE_LEAF_MIN_MS,r=s!==void 0&&s.trim()!==""?Number(s):e;return r!==void 0&&Number.isFinite(r)&&r>=0?r:0}o(leafMinMs,"leafMinMs"),n(leafMinMs,"leafMinMs");export function activeOptions(){if(!i)throw new Error("@lensmcp/nest-instrumentation: LensmcpModule.forRoot(...) has not been registered. Did you forget to add it to your AppModule?");return i}o(activeOptions,"activeOptions"),n(activeOptions,"activeOptions");export function currentGeneration(){return c}o(currentGeneration,"currentGeneration"),n(currentGeneration,"currentGeneration");
|
package/lib/types.d.ts
CHANGED
|
@@ -50,6 +50,18 @@ export interface LensmcpModuleOptions {
|
|
|
50
50
|
* captured method, and every call in a flow still emit theirs; the loop (N+1) check runs either way.
|
|
51
51
|
*/
|
|
52
52
|
flowlessMinMs?: number;
|
|
53
|
+
/**
|
|
54
|
+
* Inside a request, flow or job run, a traced call that returned in under this many ms and called nothing — no
|
|
55
|
+
* traced method began after it in its request, and no statement, cache command or outbound call ran meanwhile —
|
|
56
|
+
* emits no span (default 0: every one is kept; `LENSMCP_TRACE_LEAF_MIN_MS` overrides it). Such a span says only
|
|
57
|
+
* that the method ran. On FoodGuard (2026-11-12, overseer-89) a route read 17 times a second left about 28 events
|
|
58
|
+
* around each request, the method spans among them at «0ms» — `TypeOrmOrmClient.assertAvailable`,
|
|
59
|
+
* `ZodValidationPipe.transform`, `Repository.createQueryBuilder` — and at the event history's size cap that volume
|
|
60
|
+
* pushed everything else out in 13 hours. `1` leaves out the calls that read 0 ms. The request's own event, its
|
|
61
|
+
* controller's span, a call that threw or was signalled, a `@TraceMethod`, a captured method and every call that
|
|
62
|
+
* did call something keep theirs; the loop (N+1) check runs either way.
|
|
63
|
+
*/
|
|
64
|
+
leafMinMs?: number;
|
|
53
65
|
};
|
|
54
66
|
/** Memory tracking (Phase 7). */
|
|
55
67
|
memory?: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lensmcp/nest-instrumentation",
|
|
3
|
-
"version": "1.26.
|
|
3
|
+
"version": "1.26.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lensmcp/core": "1.26.
|
|
18
|
-
"@lensmcp/memory-tracker": "1.26.
|
|
19
|
-
"@lensmcp/protocol-types": "1.26.
|
|
17
|
+
"@lensmcp/core": "1.26.4",
|
|
18
|
+
"@lensmcp/memory-tracker": "1.26.4",
|
|
19
|
+
"@lensmcp/protocol-types": "1.26.4",
|
|
20
20
|
"tslib": "^2.3.0"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|