@lensmcp/nest-instrumentation 1.25.6 → 1.25.8
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 +6 -0
- package/lib/instrument.js +1 -1
- package/lib/lensmcp.module.js +1 -1
- package/lib/trace-interceptor.js +2 -2
- package/package.json +4 -4
package/lib/context.d.ts
CHANGED
|
@@ -31,6 +31,12 @@ export interface LensmcpRequestContext {
|
|
|
31
31
|
originNodeId?: string;
|
|
32
32
|
/** W3C Trace Context header. */
|
|
33
33
|
traceparent?: string;
|
|
34
|
+
/**
|
|
35
|
+
* This request's own id — unique even where `requestId` is not (a service that forwards one `x-request-id` to every
|
|
36
|
+
* call it makes). On its `server-request` event and on its SQL account (node-instrumentation's `db-request-account`),
|
|
37
|
+
* so the lens folds the account into the run it belongs to, not into whichever run shares the request id.
|
|
38
|
+
*/
|
|
39
|
+
runId?: string;
|
|
34
40
|
/**
|
|
35
41
|
* Monotonic operation counters for this request, mutated in place.
|
|
36
42
|
* Incremented by the zero-touch taps (@lensmcp/node-instrumentation: pg,
|
package/lib/instrument.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var B=Object.defineProperty;var p=(
|
|
1
|
+
"use strict";var B=Object.defineProperty;var p=(t,r)=>B(t,"name",{value:r,configurable:!0});var _=Object.defineProperty,l=p((t,r)=>_(t,"name",{value:r,configurable:!0}),"i");import{fingerprint as g,ulid as b}from"@lensmcp/core";import{redactText as L}from"@lensmcp/core/redact";import{captureFor as F,capturedValue as M}from"./capture.js";import{currentLensmcpContext as G}from"./context.js";import{activeOptions as W}from"./state.js";export const LENSMCP_TRACED=Symbol.for("@lensmcp/nest-instrumentation/traced"),LENSMCP_IGNORE=Symbol.for("@lensmcp/nest-instrumentation/ignore");const w=300;function j(t){if(t instanceof Error){const d=t.code,c=L(t.message??"");return{name:t.name||t.constructor?.name||"Error",...c?{message:c.length>w?`${c.slice(0,w)}\u2026`:c}:{},...typeof d=="string"&&d.length<=64?{code:d}:{}}}const r=typeof t=="string"?L(t):void 0;return{name:t==null?"nothing":typeof t=="object"?"non-Error object":`non-Error ${typeof t}`,...r?{message:r.slice(0,w)}:{}}}p(j,"G"),l(j,"thrownOf");export function LensmcpIgnore(){return((t,r)=>{if(r===void 0)t[LENSMCP_IGNORE]=!0;else{const d=t[r];typeof d=="function"&&(d[LENSMCP_IGNORE]=!0)}})}p(LensmcpIgnore,"LensmcpIgnore"),l(LensmcpIgnore,"LensmcpIgnore");export function isTraced(t){return typeof t=="function"&&!!t[LENSMCP_TRACED]}p(isTraced,"isTraced"),l(isTraced,"isTraced");export function isIgnored(t){return!!t&&(typeof t=="function"||typeof t=="object")&&!!t[LENSMCP_IGNORE]}p(isIgnored,"isIgnored"),l(isIgnored,"isIgnored");export function traceWrap(t,r,d={}){if(isTraced(t))return t;const c=d.db??!1,h=l(function(...m){let i;try{i=W()}catch{return t.apply(this,m)}if(!i.trace.services)return t.apply(this,m);const f=Date.now(),e=G(),O=e?.dbCallCount??0,R=e?.redisCallCount??0,T=e?.externalCallCount??0,S=e?.loopDbCalls??0,P=e?.loopRedisCalls??0,N=e?.loopExternalCalls??0,I=d.capture??F(r),x=I==="args"||I==="both"?M(m):void 0,v=l((o,n,C)=>{const s=Date.now()-f,a={id:b(),sessionId:i.sessionId,timestamp:Date.now(),source:"nestjs",category:"backend",severity:o==="error"?"error":"info",context:{sessionId:i.sessionId,flowId:e?.flowId,requestId:e?.requestId,...e?.runId?{runId:e.runId}:{}},fingerprint:g({kind:"method-call",identity:r}),title:`${r} (${s}ms)${o==="error"?" \u2717":""}`,message:n instanceof Error?n.message:void 0,raw:{kind:"span",span:{name:r,flowId:e?.flowId,requestId:e?.requestId,startTime:f,endTime:Date.now(),durationMs:s,status:o,outcome:o==="error"?"threw":"returned",...o==="error"?{error:j(n)}:{},...x!==void 0?{args:x}:{},...o==="ok"&&(I==="result"||I==="both")?{result:M(C)}:{}}}};i.emit(a)},"emitSpan"),D=l(()=>{e&&(e.dbCallCount=(e.dbCallCount??0)+1),i.emit({id:b(),sessionId:i.sessionId,timestamp:Date.now(),source:"db",category:"db",severity:"info",context:{sessionId:i.sessionId,flowId:e?.flowId,requestId:e?.requestId,...e?.runId?{runId:e.runId}:{}},fingerprint:g({kind:"db-query",identity:r}),title:`query ${r}`,raw:{kind:"db-query",query:{signature:r}}})},"emitDbQuery"),E=l(()=>{if(c||!e)return;const o=(e.dbCallCount??0)-O,n=(e.redisCallCount??0)-R,C=(e.externalCallCount??0)-T,s=Math.max(0,o-((e.loopDbCalls??0)-S)),a=Math.max(0,n-((e.loopRedisCalls??0)-P)),u=Math.max(0,C-((e.loopExternalCalls??0)-N));if(s<3&&a<3&&u<3)return;const A=Date.now()-f,y=[];s>=3&&y.push(`${s} DB calls`),a>=3&&y.push(`${a} redis calls`),u>=3&&y.push(`${u} external calls`),s>=3&&(e.loopDbCalls=(e.loopDbCalls??0)+s),a>=3&&(e.loopRedisCalls=(e.loopRedisCalls??0)+a),u>=3&&(e.loopExternalCalls=(e.loopExternalCalls??0)+u);const $=o-s+(n-a)+(C-u);i.emit({id:b(),sessionId:i.sessionId,timestamp:Date.now(),source:"nestjs",category:"backend",severity:"warning",context:{sessionId:i.sessionId,flowId:e.flowId,requestId:e.requestId,...e.runId?{runId:e.runId}:{}},fingerprint:g({kind:"loop",identity:r}),title:`loop in ${r} (${y.join(", ")})`,raw:{kind:"loop",loop:{iterations:Math.max(s,a,u),durationMs:A,startedAt:f,dbCallsInsideLoop:s,redisCallsInsideLoop:a,externalCallsInsideLoop:u,awaitedOperationsInsideLoop:s+a+u,...$>0?{reportedBeneath:$}:{}}}})},"emitLoopIfNeeded"),q=l(o=>{c&&D(),v("ok",void 0,o),E()},"onOk"),k=l(o=>{c&&D(),v("error",o),E()},"onErr");try{const o=t.apply(this,m);return o instanceof Promise?o.then(n=>(q(n),n),n=>{throw k(n),n}):(q(o),o)}catch(o){throw k(o),o}},"wrapped");return h[LENSMCP_TRACED]=!0,h}p(traceWrap,"traceWrap"),l(traceWrap,"traceWrap");
|
package/lib/lensmcp.module.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var m=Object.defineProperty;var t=(o,r)=>m(o,"name",{value:r,configurable:!0});var d=Object.defineProperty,n=t((o,r)=>d(o,"name",{value:r,configurable:!0}),"t"),s;import{__decorate as a}from"tslib";import{Module as c}from"@nestjs/common";import{APP_INTERCEPTOR as p}from"@nestjs/core";import{configureMemoryTracker as f}from"@lensmcp/memory-tracker";import{TraceInterceptor as u}from"./trace-interceptor.js";import{LensmcpProviderTracker as I}from"./provider-tracker.js";import{setActiveOptions as l}from"./state.js";import{currentLensmcpContext as v}from"./context.js";let i=s=class{static{t(this,"s")}static{n(this,"LensmcpModule")}static forRoot(o){const r=l(o);return r.memory.mode!=="off"&&f({sessionId:r.sessionId,sink:r.emit,contextGetter:n(()=>{const e=v();if(e)return{sessionId:e.sessionId,flowId:e.flowId,requestId:e.requestId,...e.runId?{runId:e.runId}:{},originNodeId:e.originNodeId}},"contextGetter")}),{module:s,providers:[{provide:p,useClass:u},I],exports:[],global:!0}}};i=s=a([c({})],i);export{i as LensmcpModule};
|
package/lib/trace-interceptor.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";var Z=Object.defineProperty;var u=(t,e)=>Z(t,"name",{value:e,configurable:!0});var V=Object.defineProperty,a=u((t,e)=>V(t,"name",{value:e,configurable:!0}),"d");import{__decorate as ee}from"tslib";import{Injectable as te}from"@nestjs/common";import{Observable as L}from"rxjs";import{catchError as
|
|
1
|
+
"use strict";var Z=Object.defineProperty;var u=(t,e)=>Z(t,"name",{value:e,configurable:!0});var V=Object.defineProperty,a=u((t,e)=>V(t,"name",{value:e,configurable:!0}),"d");import{__decorate as ee}from"tslib";import{Injectable as te}from"@nestjs/common";import{Observable as L}from"rxjs";import{catchError as re,tap as se}from"rxjs/operators";import{fingerprint as k,headlessFlowId as oe,headlessFlowsEnabled as ne,isProbePath as ae,requestFacts as ie,responseFacts as de,shapeOf as U,ulid as h}from"@lensmcp/core";import{activeMemoryOptions as ue,onFlowStart as ce,onFlowSettled as fe}from"@lensmcp/memory-tracker";import{LENSMCP_CONTEXT_STORAGE as pe}from"./context.js";import{attachContextEnd as le}from"./context-end.js";import{activeOptions as v}from"./state.js";const me=Symbol.for("lensmcp.exchangeId");let j=class{static{u(this,"S")}static{a(this,"TraceInterceptor")}intercept(t,e){const s=v();if(!s.trace.requests)return e.handle();const d=t.switchToHttp(),r=d.getRequest(),o=d.getResponse(),n=y(r.headers?.["x-request-id"])??h(),c=y(r.headers?.["x-lensmcp-flow-id"]),f=y(r.headers?.["x-internal-token"])!==void 0,l=c??(!f&&ne()&&!ae(r.url)?oe(n):void 0),i={sessionId:s.sessionId,requestId:n,parentRequestId:y(r.headers?.["x-lensmcp-parent-request-id"]),flowId:l,originNodeId:y(r.headers?.["x-lensmcp-origin-node-id"]),traceparent:y(r.headers?.traceparent),runId:h(),dbCallCount:0},m=r[me],p=(r.method??"GET").toUpperCase(),I=r.route?.path??r.url??"<unknown>",N=t.getClass?.()?.name,O=t.getHandler?.()?.name,q=Date.now();l&&!c&&J(i,p,I,q);const D=s.memory.mode!=="off";D&&ce(n);const M=a(()=>{if(!D)return;const g=ue()?.settleMs??1e3;setTimeout(()=>fe(n,i.flowId),g).unref?.()},"settleAndCheck"),Q=le(i);let C;typeof o?.once=="function"&&ge()&&o.once("finish",()=>z(s.projectName,i,p,I,r,o,C));let P=!1,E=typeof o?.once!="function";const A=a(()=>{P&&E&&Q()},"endIfOver");if(!E){const g=a(()=>{E=!0,A()},"responseOut");o.once?.("finish",g),o.once?.("close",g)}return new L(g=>{let F;return pe.run(i,()=>{F=e.handle().pipe(se({next:a(w=>{C??={value:w},g.next(w)},"next"),complete:a(()=>{x(s.projectName,i,p,I,o.statusCode??200,q,void 0,m),T(i,N,O,q,"ok"),M(),g.complete()},"complete")}),re(w=>{const H=w?.getResponse?.();H!==void 0&&(C??={value:H});const _=R(w);if(_!==void 0||typeof o.once!="function")x(s.projectName,i,p,I,_??S(o),q,w,m);else{let B=!1;const G=a(W=>{if(B)return;B=!0;const Y=W&&o.statusCode&&o.statusCode>=400?o.statusCode:S(o);x(s.projectName,i,p,I,Y,q,w,m)},"settle");o.once("finish",()=>G(!0)),o.once("close",()=>G(!1))}return T(i,N,O,q,"error"),M(),g.error(w),new L})).subscribe()}),()=>{F?.unsubscribe(),P=!0,A()}})}};j=ee([te()],j);export{j as TraceInterceptor};const $=new Map,Ie=6e4,ge=a(()=>process.env.LENSMCP_HTTP_SHAPES!=="0","shapesOn");function z(t,e,s,d,r,o,n){try{const c=o.statusCode??200,f=`${s} ${d} ${Math.floor(c/100)}`,l=Date.now();if(l-($.get(f)??0)<Ie)return;$.set(f,l),$.size>2e3&&$.delete($.keys().next().value);const i=r.headers??{},m=r.body!==void 0&&r.body!==null&&typeof r.body=="object"&&!Buffer.isBuffer(r.body)?U(r.body):void 0,p=n&&n.value!==void 0&&typeof n.value=="object"&&n.value!==null&&!Buffer.isBuffer(n.value)&&typeof n.value.pipe!="function"?U(n.value):void 0,I={id:h(),sessionId:e.sessionId,timestamp:l,source:"nestjs",category:"backend",severity:"info",context:{sessionId:e.sessionId,flowId:e.flowId,requestId:e.requestId,...e.runId?{runId:e.runId}:{}},fingerprint:k({kind:"http-exchange",identity:f}),title:`${s} ${d} \u2192 ${c} (its shape)`,raw:{kind:"http-exchange",method:s,route:d,status:c,request:{...ie(i,r.query,r.params),...m?{body:m}:{}},response:{...de(o.getHeaders?.()??{},typeof i.origin=="string"?i.origin:void 0),...p?{body:p}:{}}}};b(I,t),v().emit(I)}catch{}}u(z,"pe"),a(z,"recordExchange");function R(t){const e=t;if(typeof e?.getStatus=="function")return e.getStatus();if(typeof e?.status=="number"&&e.status>=100)return e.status;if(typeof e?.statusCode=="number"&&e.statusCode>=100)return e.statusCode}u(R,"L"),a(R,"ownStatus");const we=new Set(["TypeError","ReferenceError","SyntaxError","RangeError","EvalError","URIError"]);export function requestOutcome(t,e){return e instanceof Error&&we.has(e.name)&&R(e)===void 0?"fault":t<400?"ok":t<500?"refused":t===500?"fault":"declared"}u(requestOutcome,"requestOutcome"),a(requestOutcome,"requestOutcome");function X(t){return t==="ok"?"info":t==="refused"?"warning":"error"}u(X,"Ie"),a(X,"severityOf");function S(t){return t.statusCode&&t.statusCode>=400?t.statusCode:500}u(S,"U"),a(S,"inferStatus");function y(t){if(typeof t=="string")return t;if(Array.isArray(t)&&typeof t[0]=="string")return t[0]}u(y,"h"),a(y,"stringHeader");function J(t,e,s,d){const r=v(),o=`request:${e} ${s}`,n={id:h(),sessionId:t.sessionId,timestamp:d,source:"nestjs",category:"backend",severity:"info",context:{sessionId:t.sessionId,flowId:t.flowId,requestId:t.requestId,originType:"request",originNodeId:o},fingerprint:k({kind:"flow-origin",identity:`${e}:${s}`}),title:`${e} ${s}`,raw:{kind:"flow-origin",originNodeId:o,method:e,route:s}};b(n,r.projectName),r.emit(n)}u(J,"me"),a(J,"emitRequestOrigin");function T(t,e,s,d,r){if(!e||!s)return;const o=v(),n=Date.now()-d,c=`${e}.${s}`,f={id:h(),sessionId:t.sessionId,timestamp:Date.now(),source:"nestjs",category:"backend",severity:r==="error"?"error":"info",context:{sessionId:t.sessionId,flowId:t.flowId,requestId:t.requestId,...t.runId?{runId:t.runId}:{}},fingerprint:k({kind:"method-call",identity:c}),title:`${c} (${n}ms)${r==="error"?" \u2717":""}`,raw:{kind:"span",span:{name:c,role:"controller",flowId:t.flowId,requestId:t.requestId,startTime:d,endTime:Date.now(),durationMs:n,status:r}}};b(f,v().projectName),o.emit(f)}u(T,"X"),a(T,"emitControllerSpan");function K(t){if(!(t instanceof Error))return{message:String(t)};const e=t.stack?t.stack.split(`
|
|
2
2
|
`).slice(0,30).join(`
|
|
3
|
-
`):void 0;return{...t.name?{name:t.name}:{},message:t.message,...e?{stack:e}:{}}}u(K,"ye"),a(K,"errorDetail");function b(t,e){if(!e)return;const
|
|
3
|
+
`):void 0;return{...t.name?{name:t.name}:{},message:t.message,...e?{stack:e}:{}}}u(K,"ye"),a(K,"errorDetail");function b(t,e){if(!e)return;const s=t.raw;s&&s.project===void 0&&(s.project=e)}u(b,"$"),a(b,"stampProject");function x(t,e,s,d,r,o,n,c){const f=v(),l=Date.now()-o,i=requestOutcome(r,n),m={id:h(),sessionId:e.sessionId,timestamp:Date.now(),source:"nestjs",category:"backend",severity:X(i),context:{sessionId:e.sessionId,flowId:e.flowId,requestId:e.requestId,...e.runId?{runId:e.runId}:{},...e.parentRequestId?{parentRequestId:e.parentRequestId}:{},originNodeId:e.originNodeId,traceparent:e.traceparent},fingerprint:k({kind:"server-request",identity:`${s}:${d}:${Math.floor(r/100)}xx`}),title:`${s} ${d} \u2192 ${r} (${l}ms)`,message:n instanceof Error?n.message:void 0,raw:{kind:"server-request",request:{method:s,route:d,status:r,outcome:i,durationMs:l,startedAt:o,endedAt:Date.now(),flowId:e.flowId,requestId:e.requestId,...e.parentRequestId?{parentRequestId:e.parentRequestId}:{},...typeof c=="string"?{exchangeId:c}:{},...e.runId?{runId:e.runId}:{},...n?{error:K(n)}:{}}}},p={...m,id:h(),raw:{kind:"span",span:{name:`nest.request ${s} ${d}`,traceId:e.traceparent,flowId:e.flowId,requestId:e.requestId,startTime:o,endTime:Date.now(),durationMs:l,status:i==="ok"?"ok":"error"}}};b(m,t),b(p,t),f.emit(m),f.emit(p)}u(x,"O"),a(x,"emitRequest");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lensmcp/nest-instrumentation",
|
|
3
|
-
"version": "1.25.
|
|
3
|
+
"version": "1.25.8",
|
|
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.25.
|
|
18
|
-
"@lensmcp/memory-tracker": "1.25.
|
|
19
|
-
"@lensmcp/protocol-types": "1.25.
|
|
17
|
+
"@lensmcp/core": "1.25.8",
|
|
18
|
+
"@lensmcp/memory-tracker": "1.25.8",
|
|
19
|
+
"@lensmcp/protocol-types": "1.25.8",
|
|
20
20
|
"tslib": "^2.3.0"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|