@lensmcp/node-instrumentation 1.25.43 → 1.25.45

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.
@@ -32,12 +32,19 @@ interface LiveCtx {
32
32
  runId?: string;
33
33
  originNodeId?: string;
34
34
  dbCallCount?: number;
35
+ /** Its DB calls by second — nest-instrumentation's `dbSeconds`, read by its loop detector. */
36
+ dbSeconds?: Map<number, number>;
35
37
  redisCallCount?: number;
36
38
  externalCallCount?: number;
37
39
  /** Hops this request made to the cluster's own services (tap-egress `hopHeaders`, `nextHopHeaders`): `<request>.<n>`. */
38
40
  hopCount?: number;
39
41
  }
40
42
  type CtxGetter = () => LiveCtx | undefined;
43
+ /**
44
+ * One DB call of the request, in its second — nest-instrumentation's `countDbSecond` (context.ts), kept in step here:
45
+ * this package reads that one only by its context, never imports it.
46
+ */
47
+ export declare function countDbSecond(ctx: LiveCtx, at: number): void;
41
48
  type CtxRunner = <T>(ctx: {
42
49
  sessionId: string;
43
50
  requestId: string;
@@ -1 +1 @@
1
- "use strict";var d=Object.defineProperty;var o=(t,e)=>d(t,"name",{value:e,configurable:!0});var l=Object.defineProperty,n=o((t,e)=>l(t,"name",{value:e,configurable:!0}),"n");Object.defineProperty(exports,"__esModule",{value:!0}),exports.FLOW_START=exports.CONTEXT_END=void 0,exports.flowContextStatus=flowContextStatus,exports.resetFlowContextForTests=resetFlowContextForTests,exports.isRequireEsmError=isRequireEsmError,exports.isModuleNotFoundError=isModuleNotFoundError,exports.primeFlowContext=primeFlowContext,exports.attachContextEnd=attachContextEnd,exports.contextEndOf=contextEndOf,exports.startLiveFlow=startLiveFlow,exports.runInFlowContext=runInFlowContext,exports.liveFlowContext=liveFlowContext,exports.flowContext=flowContext;let getCtx=null,runWith=null,status={state:"not-primed"},warned=!1;function flowContextStatus(){return status}o(flowContextStatus,"flowContextStatus"),n(flowContextStatus,"flowContextStatus");function resetFlowContextForTests(){getCtx=null,runWith=null,status={state:"not-primed"},warned=!1}o(resetFlowContextForTests,"resetFlowContextForTests"),n(resetFlowContextForTests,"resetFlowContextForTests");function isRequireEsmError(t){const e=t?.code;return e==="ERR_REQUIRE_ESM"||e==="ERR_REQUIRE_ASYNC_MODULE"}o(isRequireEsmError,"isRequireEsmError"),n(isRequireEsmError,"isRequireEsmError");function isModuleNotFoundError(t){const e=t;return e?.code!=="MODULE_NOT_FOUND"&&e?.code!=="ERR_MODULE_NOT_FOUND"?!1:String(e?.message??"").includes("@lensmcp/nest-instrumentation")}o(isModuleNotFoundError,"isModuleNotFoundError"),n(isModuleNotFoundError,"isModuleNotFoundError");function requireNestInstrumentation(){return require("@lensmcp/nest-instrumentation")}o(requireNestInstrumentation,"requireNestInstrumentation"),n(requireNestInstrumentation,"requireNestInstrumentation");function primeFlowContext(t=requireNestInstrumentation){try{const e=t();getCtx=e.currentLensmcpContext,runWith=e.runInLensmcpContext,status={state:"ready"}}catch(e){if(getCtx=null,runWith=null,isModuleNotFoundError(e)){status={state:"absent"};return}const r=e instanceof Error?e.message:String(e);if(status=isRequireEsmError(e)?{state:"runtime-too-old",detail:r}:{state:"failed",detail:r},!warned){warned=!0;const s=status.state==="runtime-too-old"?`this Node (${process.version}) cannot require an ESM package; @lensmcp/node-instrumentation needs >=20.19`:r;console.warn(`[lensmcp] flow correlation is OFF \u2014 could not load @lensmcp/nest-instrumentation: ${s}`)}}}o(primeFlowContext,"primeFlowContext"),n(primeFlowContext,"primeFlowContext"),exports.CONTEXT_END=Symbol.for("lensmcp.contextEnd");function attachContextEnd(t){let e=!1;const r=[],s={get ended(){return e},onEnd(u){e?runEndListener(u):r.push(u)}};return Object.defineProperty(t,exports.CONTEXT_END,{value:s}),()=>{if(!e){e=!0;for(const u of r.splice(0))runEndListener(u)}}}o(attachContextEnd,"attachContextEnd"),n(attachContextEnd,"attachContextEnd");function runEndListener(t){try{t()}catch{}}o(runEndListener,"runEndListener"),n(runEndListener,"runEndListener");function contextEndOf(t){const e=t?.[exports.CONTEXT_END];return e&&typeof e.onEnd=="function"?e:void 0}o(contextEndOf,"contextEndOf"),n(contextEndOf,"contextEndOf"),exports.FLOW_START=Symbol.for("lensmcp.flowStart");function startLiveFlow(){const t=liveFlowContext();if(!t)return;if(t.flowId)return t.flowId;const e=t[exports.FLOW_START];if(typeof e=="function")try{return e()??t.flowId}catch{return t.flowId}}o(startLiveFlow,"startLiveFlow"),n(startLiveFlow,"startLiveFlow");function runInFlowContext(t,e){if(!runWith)return e();const r={sessionId:"pending",requestId:t.requestId,...t.flowId?{flowId:t.flowId}:{}},s=attachContextEnd(r);let u;try{u=runWith(r,e)}catch{return s(),e()}const i=u;return i&&typeof i.then=="function"?i.then(c=>(s(),c),c=>{throw s(),c}):(s(),u)}o(runInFlowContext,"runInFlowContext"),n(runInFlowContext,"runInFlowContext");function liveFlowContext(){try{return getCtx?.()}catch{return}}o(liveFlowContext,"liveFlowContext"),n(liveFlowContext,"liveFlowContext");function flowContext(){const t=liveFlowContext();if(t)return{...t.flowId?{flowId:t.flowId}:{},...t.requestId?{requestId:t.requestId}:{},...t.runId?{runId:t.runId}:{},...t.originNodeId?{originNodeId:t.originNodeId}:{}}}o(flowContext,"flowContext"),n(flowContext,"flowContext");
1
+ "use strict";var l=Object.defineProperty;var r=(t,e)=>l(t,"name",{value:e,configurable:!0});var d=Object.defineProperty,o=r((t,e)=>d(t,"name",{value:e,configurable:!0}),"o");Object.defineProperty(exports,"__esModule",{value:!0}),exports.FLOW_START=exports.CONTEXT_END=void 0,exports.countDbSecond=countDbSecond,exports.flowContextStatus=flowContextStatus,exports.resetFlowContextForTests=resetFlowContextForTests,exports.isRequireEsmError=isRequireEsmError,exports.isModuleNotFoundError=isModuleNotFoundError,exports.primeFlowContext=primeFlowContext,exports.attachContextEnd=attachContextEnd,exports.contextEndOf=contextEndOf,exports.startLiveFlow=startLiveFlow,exports.runInFlowContext=runInFlowContext,exports.liveFlowContext=liveFlowContext,exports.flowContext=flowContext;function countDbSecond(t,e){let n=t.dbSeconds;n||(t.dbSeconds=n=new Map);const s=Math.floor(e/1e3);n.set(s,(n.get(s)??0)+1),n.size>4096&&n.delete(n.keys().next().value)}r(countDbSecond,"countDbSecond"),o(countDbSecond,"countDbSecond");let getCtx=null,runWith=null,status={state:"not-primed"},warned=!1;function flowContextStatus(){return status}r(flowContextStatus,"flowContextStatus"),o(flowContextStatus,"flowContextStatus");function resetFlowContextForTests(){getCtx=null,runWith=null,status={state:"not-primed"},warned=!1}r(resetFlowContextForTests,"resetFlowContextForTests"),o(resetFlowContextForTests,"resetFlowContextForTests");function isRequireEsmError(t){const e=t?.code;return e==="ERR_REQUIRE_ESM"||e==="ERR_REQUIRE_ASYNC_MODULE"}r(isRequireEsmError,"isRequireEsmError"),o(isRequireEsmError,"isRequireEsmError");function isModuleNotFoundError(t){const e=t;return e?.code!=="MODULE_NOT_FOUND"&&e?.code!=="ERR_MODULE_NOT_FOUND"?!1:String(e?.message??"").includes("@lensmcp/nest-instrumentation")}r(isModuleNotFoundError,"isModuleNotFoundError"),o(isModuleNotFoundError,"isModuleNotFoundError");function requireNestInstrumentation(){return require("@lensmcp/nest-instrumentation")}r(requireNestInstrumentation,"requireNestInstrumentation"),o(requireNestInstrumentation,"requireNestInstrumentation");function primeFlowContext(t=requireNestInstrumentation){try{const e=t();getCtx=e.currentLensmcpContext,runWith=e.runInLensmcpContext,status={state:"ready"}}catch(e){if(getCtx=null,runWith=null,isModuleNotFoundError(e)){status={state:"absent"};return}const n=e instanceof Error?e.message:String(e);if(status=isRequireEsmError(e)?{state:"runtime-too-old",detail:n}:{state:"failed",detail:n},!warned){warned=!0;const s=status.state==="runtime-too-old"?`this Node (${process.version}) cannot require an ESM package; @lensmcp/node-instrumentation needs >=20.19`:n;console.warn(`[lensmcp] flow correlation is OFF \u2014 could not load @lensmcp/nest-instrumentation: ${s}`)}}}r(primeFlowContext,"primeFlowContext"),o(primeFlowContext,"primeFlowContext"),exports.CONTEXT_END=Symbol.for("lensmcp.contextEnd");function attachContextEnd(t){let e=!1;const n=[],s={get ended(){return e},onEnd(u){e?runEndListener(u):n.push(u)}};return Object.defineProperty(t,exports.CONTEXT_END,{value:s}),()=>{if(!e){e=!0;for(const u of n.splice(0))runEndListener(u)}}}r(attachContextEnd,"attachContextEnd"),o(attachContextEnd,"attachContextEnd");function runEndListener(t){try{t()}catch{}}r(runEndListener,"runEndListener"),o(runEndListener,"runEndListener");function contextEndOf(t){const e=t?.[exports.CONTEXT_END];return e&&typeof e.onEnd=="function"?e:void 0}r(contextEndOf,"contextEndOf"),o(contextEndOf,"contextEndOf"),exports.FLOW_START=Symbol.for("lensmcp.flowStart");function startLiveFlow(){const t=liveFlowContext();if(!t)return;if(t.flowId)return t.flowId;const e=t[exports.FLOW_START];if(typeof e=="function")try{return e()??t.flowId}catch{return t.flowId}}r(startLiveFlow,"startLiveFlow"),o(startLiveFlow,"startLiveFlow");function runInFlowContext(t,e){if(!runWith)return e();const n={sessionId:"pending",requestId:t.requestId,...t.flowId?{flowId:t.flowId}:{}},s=attachContextEnd(n);let u;try{u=runWith(n,e)}catch{return s(),e()}const i=u;return i&&typeof i.then=="function"?i.then(c=>(s(),c),c=>{throw s(),c}):(s(),u)}r(runInFlowContext,"runInFlowContext"),o(runInFlowContext,"runInFlowContext");function liveFlowContext(){try{return getCtx?.()}catch{return}}r(liveFlowContext,"liveFlowContext"),o(liveFlowContext,"liveFlowContext");function flowContext(){const t=liveFlowContext();if(t)return{...t.flowId?{flowId:t.flowId}:{},...t.requestId?{requestId:t.requestId}:{},...t.runId?{runId:t.runId}:{},...t.originNodeId?{originNodeId:t.originNodeId}:{}}}r(flowContext,"flowContext"),o(flowContext,"flowContext");
package/lib/tap-pg.js CHANGED
@@ -1 +1 @@
1
- "use strict";var j=Object.defineProperty;var u=(t,e)=>j(t,"name",{value:e,configurable:!0});var U=Object.defineProperty,r=u((t,e)=>U(t,"name",{value:e,configurable:!0}),"r");Object.defineProperty(exports,"__esModule",{value:!0}),exports.taggedAppName=taggedAppName,exports.pgTap=pgTap;const redact_1=require("@lensmcp/core/redact"),sql_shape_1=require("@lensmcp/core/sql-shape"),emit_1=require("./emit"),flood_guard_1=require("./flood-guard"),flow_context_1=require("./flow-context"),sql_accounting_1=require("./sql-accounting"),tap_pg_pool_1=require("./tap-pg-pool"),callsite_1=require("./callsite"),active_work_1=require("./active-work"),emit_2=require("./emit"),sql_values_1=require("./sql-values"),sql_drops_1=require("./sql-drops"),SKIP_OPS=new Set(["BEGIN","START","COMMIT","END","ROLLBACK","SAVEPOINT","RELEASE","SET","RESET","SHOW","DEALLOCATE","DISCARD","LISTEN","UNLISTEN"]),STATEMENT_EVERY_MS=10*6e4,STATEMENT_MAX=64*1024,SLOW_SITE_MS=250,sites=new callsite_1.SiteSampler,buckets=(0,flood_guard_1.createEventFloodGuard)({burst:30,perSec:5}),statementGuard={admit:r(t=>({ok:buckets.admit(t).ok,suppressed:0}),"admit")},statementSentAt=new Map,MAX_SHAPES_REMEMBERED=2e3,firstWord=r(t=>(/^\s*(?:(?:\/\*[\s\S]*?\*\/|--[^\n]*\n?)\s*)*(\w+)/.exec(t)?.[1]??"query").toUpperCase(),"firstWord");function parseSql(t){const e=(0,sql_shape_1.sqlShape)(t),n=(e.target??e.tables[0])?.split(".").pop();return{op:e.verb,...n?{table:n}:{},sqlId:e.id,tables:e.tables,...e.name?{name:e.name}:{}}}u(parseSql,"parseSql"),r(parseSql,"parseSql");const oneLine=r(t=>t.replace(/\s+/g," ").trim(),"oneLine"),clip=r((t,e)=>t.length>e?t.slice(0,e-1)+"\u2026":t,"clip");function paramPreview(t){if(!(!Array.isArray(t)||t.length===0))return t.slice(0,10).map(e=>{if(e==null)return String(e);const n=typeof e=="string"?e:(()=>{try{return JSON.stringify(e)}catch{return String(e)}})();return clip(n,80)})}u(paramPreview,"paramPreview"),r(paramPreview,"paramPreview");const rowsOf=r(t=>t?.rowCount??t?.rows?.length??0,"rowsOf");function resultPreview(t){const e=t?.rows;if(!(!Array.isArray(e)||e.length===0))try{return clip(JSON.stringify((0,redact_1.redactDeep)(e.slice(0,2))),240)}catch{return}}u(resultPreview,"resultPreview"),r(resultPreview,"resultPreview");function connectionOf(t){const e=t,n=e?.connectionParameters,a=typeof e?.database=="string"?e.database:typeof n?.database=="string"?n.database:void 0,s=typeof e?.host=="string"?e.host:typeof n?.host=="string"?n.host:void 0,i=typeof e?.port=="number"||typeof e?.port=="string"?e.port:typeof n?.port=="number"||typeof n?.port=="string"?n.port:5432;return{...a?{database:a}:{},...s?{server:`${s}:${i}`}:{}}}u(connectionOf,"connectionOf"),r(connectionOf,"connectionOf");const APP_NAME_MAX=63;function taggedAppName(t,e,n,a){const s=` lens:${e.replace(/[^\x21-\x7e]/g,"?").replace(/:/g,"_")}${n>0?`:g${n}`:""}${a?`:p${a}`:""}`.slice(0,APP_NAME_MAX);return`${(t??"node").replace(/[^\x20-\x7e]/g,"?").replace(/ lens:\S+$/,"").slice(0,Math.max(0,APP_NAME_MAX-s.length))}${s}`}u(taggedAppName,"taggedAppName"),r(taggedAppName,"taggedAppName");function tagApplicationName(t){if(process.env.LENSMCP_PG_APP_NAME==="0")return;const e=t.getStartupConf;if(typeof e!="function"||e.__lensmcp)return;const n=r(function(){const a=e.call(this);try{a&&typeof a=="object"&&emit_2.PROJECT&&(a.application_name=taggedAppName(typeof a.application_name=="string"?a.application_name:void 0,emit_2.PROJECT,(0,active_work_1.activeWorkGeneration)(),process.pid))}catch{}return a},"wrapped");n.__lensmcp=!0,t.getStartupConf=n}u(tagApplicationName,"tagApplicationName"),r(tagApplicationName,"tagApplicationName");function pgTap(t){const e=t?.Client?.prototype,n=e?.query;if(!e||typeof n!="function")return!1;try{(0,tap_pg_pool_1.pgPoolTap)(t)}catch{}try{tagApplicationName(e)}catch{}if(n.__lensmcp)return!0;const a=r(function(s,i,q){const S=s,f=typeof S=="string"?S:S?.text;if(!f||S&&typeof S=="object"&&typeof S.submit=="function")return n.call(this,s,i,q);if(SKIP_OPS.has(firstWord(f)))return n.call(this,s,i,q);const{op:G,table:c,sqlId:m,tables:N,name:b}=parseSql(f),J=connectionOf(this),g=G.toLowerCase(),v=`${g}:${c??"query"}`,p=Date.now(),M=(0,flow_context_1.flowContext)(),V=(0,callsite_1.captureSite)(),O=(0,tap_pg_pool_1.checkoutSite)(),w=(0,flow_context_1.liveFlowContext)();w&&(w.dbCallCount=(w.dbCallCount??0)+1);const E=(0,sql_accounting_1.beginSqlStatement)(w,M,p),T=clip(oneLine(f),500),x=Array.isArray(i)?i:s?.values,I=paramPreview(x),h=I&&(0,redact_1.redactSqlParams)(f,I),$=r((o,d)=>{const l=Date.now()-p,P=p-(statementSentAt.get(m)??-1/0)>=STATEMENT_EVERY_MS;let C;try{C=sites.site(m,V,{force:!!o||P||l>=SLOW_SITE_MS,...O?{fallback:O}:{}})}catch{}const k={sqlId:m,...N.length?{tables:N}:{},...b?{sqlName:b}:{},...J,...C?{site:C}:{},...P?{statement:clip(f.trim(),STATEMENT_MAX)}:{}},A=(0,sql_values_1.newSqlValuesId)(p),R=A?{valuesId:A}:{};let y;if(o)y=(0,emit_1.lensEmitOutcome)("db","error",`${g}${c?" "+c:""} failed (${l}ms)`,{name:"postgres",kind:"db-query",op:g,...c?{table:c}:{},...k,query:T,...h?{params:h}:{},...R,startedAt:p,error:String(o?.message??o),durationMs:l},`db:error:${v}`,M,"cluster",statementGuard);else{const D=rowsOf(d),W=resultPreview(d);y=(0,emit_1.lensEmitOutcome)("db","info",`${g} ${c??"query"} (${D} rows, ${l}ms)`,{name:"postgres",kind:"db-query",op:g,...c?{table:c}:{},...k,query:T,...h?{params:h}:{},...W?{result:W}:{},...R,startedAt:p,rows:D,durationMs:l},l>=sql_drops_1.SLOW_QUERY_MS?`db:slow:${v}`:`db:${v}`,M,"cluster",statementGuard)}y==="suppressed"&&(0,sql_drops_1.noteDroppedRun)({sqlId:m,op:g,...c?{table:c}:{},...b?{sqlName:b}:{},...statementSentAt.has(m)?{}:{query:T},durationMs:l,failed:!!o,at:p}),A&&y==="written"&&(0,sql_values_1.keepSqlValues)(A,p,f,x,o?void 0:d?.rows),P&&y==="written"&&(statementSentAt.delete(m),statementSentAt.set(m,p),statementSentAt.size>MAX_SHAPES_REMEMBERED&&statementSentAt.delete(statementSentAt.keys().next().value)),(0,sql_accounting_1.endSqlStatement)(E,{durationMs:l,failed:!!o,emitted:y})},"done"),L=typeof q=="function"?q:typeof i=="function"?i:void 0;if(L){const o=r((d,l)=>{try{$(d,l)}catch{}L(d,l)},"wrappedCb");try{return typeof i=="function"?n.call(this,s,o):n.call(this,s,i,o)}catch(d){throw(0,sql_accounting_1.abandonSqlStatement)(E),d}}let _;try{_=n.call(this,s,i)}catch(o){throw(0,sql_accounting_1.abandonSqlStatement)(E),o}return _&&typeof _.then=="function"?_.then(o=>$(void 0,o),o=>$(o,void 0)):(0,sql_accounting_1.abandonSqlStatement)(E),_},"wrapped");return a.__lensmcp=!0,e.query=a,!0}u(pgTap,"pgTap"),r(pgTap,"pgTap");
1
+ "use strict";var j=Object.defineProperty;var u=(t,e)=>j(t,"name",{value:e,configurable:!0});var U=Object.defineProperty,r=u((t,e)=>U(t,"name",{value:e,configurable:!0}),"r");Object.defineProperty(exports,"__esModule",{value:!0}),exports.taggedAppName=taggedAppName,exports.pgTap=pgTap;const redact_1=require("@lensmcp/core/redact"),sql_shape_1=require("@lensmcp/core/sql-shape"),emit_1=require("./emit"),flood_guard_1=require("./flood-guard"),flow_context_1=require("./flow-context"),sql_accounting_1=require("./sql-accounting"),tap_pg_pool_1=require("./tap-pg-pool"),callsite_1=require("./callsite"),active_work_1=require("./active-work"),emit_2=require("./emit"),sql_values_1=require("./sql-values"),sql_drops_1=require("./sql-drops"),SKIP_OPS=new Set(["BEGIN","START","COMMIT","END","ROLLBACK","SAVEPOINT","RELEASE","SET","RESET","SHOW","DEALLOCATE","DISCARD","LISTEN","UNLISTEN"]),STATEMENT_EVERY_MS=10*6e4,STATEMENT_MAX=64*1024,SLOW_SITE_MS=250,sites=new callsite_1.SiteSampler,buckets=(0,flood_guard_1.createEventFloodGuard)({burst:30,perSec:5}),statementGuard={admit:r(t=>({ok:buckets.admit(t).ok,suppressed:0}),"admit")},statementSentAt=new Map,MAX_SHAPES_REMEMBERED=2e3,firstWord=r(t=>(/^\s*(?:(?:\/\*[\s\S]*?\*\/|--[^\n]*\n?)\s*)*(\w+)/.exec(t)?.[1]??"query").toUpperCase(),"firstWord");function parseSql(t){const e=(0,sql_shape_1.sqlShape)(t),n=(e.target??e.tables[0])?.split(".").pop();return{op:e.verb,...n?{table:n}:{},sqlId:e.id,tables:e.tables,...e.name?{name:e.name}:{}}}u(parseSql,"parseSql"),r(parseSql,"parseSql");const oneLine=r(t=>t.replace(/\s+/g," ").trim(),"oneLine"),clip=r((t,e)=>t.length>e?t.slice(0,e-1)+"\u2026":t,"clip");function paramPreview(t){if(!(!Array.isArray(t)||t.length===0))return t.slice(0,10).map(e=>{if(e==null)return String(e);const n=typeof e=="string"?e:(()=>{try{return JSON.stringify(e)}catch{return String(e)}})();return clip(n,80)})}u(paramPreview,"paramPreview"),r(paramPreview,"paramPreview");const rowsOf=r(t=>t?.rowCount??t?.rows?.length??0,"rowsOf");function resultPreview(t){const e=t?.rows;if(!(!Array.isArray(e)||e.length===0))try{return clip(JSON.stringify((0,redact_1.redactDeep)(e.slice(0,2))),240)}catch{return}}u(resultPreview,"resultPreview"),r(resultPreview,"resultPreview");function connectionOf(t){const e=t,n=e?.connectionParameters,a=typeof e?.database=="string"?e.database:typeof n?.database=="string"?n.database:void 0,s=typeof e?.host=="string"?e.host:typeof n?.host=="string"?n.host:void 0,i=typeof e?.port=="number"||typeof e?.port=="string"?e.port:typeof n?.port=="number"||typeof n?.port=="string"?n.port:5432;return{...a?{database:a}:{},...s?{server:`${s}:${i}`}:{}}}u(connectionOf,"connectionOf"),r(connectionOf,"connectionOf");const APP_NAME_MAX=63;function taggedAppName(t,e,n,a){const s=` lens:${e.replace(/[^\x21-\x7e]/g,"?").replace(/:/g,"_")}${n>0?`:g${n}`:""}${a?`:p${a}`:""}`.slice(0,APP_NAME_MAX);return`${(t??"node").replace(/[^\x20-\x7e]/g,"?").replace(/ lens:\S+$/,"").slice(0,Math.max(0,APP_NAME_MAX-s.length))}${s}`}u(taggedAppName,"taggedAppName"),r(taggedAppName,"taggedAppName");function tagApplicationName(t){if(process.env.LENSMCP_PG_APP_NAME==="0")return;const e=t.getStartupConf;if(typeof e!="function"||e.__lensmcp)return;const n=r(function(){const a=e.call(this);try{a&&typeof a=="object"&&emit_2.PROJECT&&(a.application_name=taggedAppName(typeof a.application_name=="string"?a.application_name:void 0,emit_2.PROJECT,(0,active_work_1.activeWorkGeneration)(),process.pid))}catch{}return a},"wrapped");n.__lensmcp=!0,t.getStartupConf=n}u(tagApplicationName,"tagApplicationName"),r(tagApplicationName,"tagApplicationName");function pgTap(t){const e=t?.Client?.prototype,n=e?.query;if(!e||typeof n!="function")return!1;try{(0,tap_pg_pool_1.pgPoolTap)(t)}catch{}try{tagApplicationName(e)}catch{}if(n.__lensmcp)return!0;const a=r(function(s,i,b){const S=s,f=typeof S=="string"?S:S?.text;if(!f||S&&typeof S=="object"&&typeof S.submit=="function")return n.call(this,s,i,b);if(SKIP_OPS.has(firstWord(f)))return n.call(this,s,i,b);const{op:G,table:p,sqlId:m,tables:N,name:h}=parseSql(f),J=connectionOf(this),g=G.toLowerCase(),v=`${g}:${p??"query"}`,l=Date.now(),M=(0,flow_context_1.flowContext)(),V=(0,callsite_1.captureSite)(),O=(0,tap_pg_pool_1.checkoutSite)(),_=(0,flow_context_1.liveFlowContext)();_&&(_.dbCallCount=(_.dbCallCount??0)+1,(0,flow_context_1.countDbSecond)(_,l));const w=(0,sql_accounting_1.beginSqlStatement)(_,M,l),T=clip(oneLine(f),500),x=Array.isArray(i)?i:s?.values,I=paramPreview(x),E=I&&(0,redact_1.redactSqlParams)(f,I),$=r((o,d)=>{const c=Date.now()-l,P=l-(statementSentAt.get(m)??-1/0)>=STATEMENT_EVERY_MS;let C;try{C=sites.site(m,V,{force:!!o||P||c>=SLOW_SITE_MS,...O?{fallback:O}:{}})}catch{}const k={sqlId:m,...N.length?{tables:N}:{},...h?{sqlName:h}:{},...J,...C?{site:C}:{},...P?{statement:clip(f.trim(),STATEMENT_MAX)}:{}},A=(0,sql_values_1.newSqlValuesId)(l),R=A?{valuesId:A}:{};let y;if(o)y=(0,emit_1.lensEmitOutcome)("db","error",`${g}${p?" "+p:""} failed (${c}ms)`,{name:"postgres",kind:"db-query",op:g,...p?{table:p}:{},...k,query:T,...E?{params:E}:{},...R,startedAt:l,error:String(o?.message??o),durationMs:c},`db:error:${v}`,M,"cluster",statementGuard);else{const D=rowsOf(d),W=resultPreview(d);y=(0,emit_1.lensEmitOutcome)("db","info",`${g} ${p??"query"} (${D} rows, ${c}ms)`,{name:"postgres",kind:"db-query",op:g,...p?{table:p}:{},...k,query:T,...E?{params:E}:{},...W?{result:W}:{},...R,startedAt:l,rows:D,durationMs:c},c>=sql_drops_1.SLOW_QUERY_MS?`db:slow:${v}`:`db:${v}`,M,"cluster",statementGuard)}y==="suppressed"&&(0,sql_drops_1.noteDroppedRun)({sqlId:m,op:g,...p?{table:p}:{},...h?{sqlName:h}:{},...statementSentAt.has(m)?{}:{query:T},durationMs:c,failed:!!o,at:l}),A&&y==="written"&&(0,sql_values_1.keepSqlValues)(A,l,f,x,o?void 0:d?.rows),P&&y==="written"&&(statementSentAt.delete(m),statementSentAt.set(m,l),statementSentAt.size>MAX_SHAPES_REMEMBERED&&statementSentAt.delete(statementSentAt.keys().next().value)),(0,sql_accounting_1.endSqlStatement)(w,{durationMs:c,failed:!!o,emitted:y})},"done"),L=typeof b=="function"?b:typeof i=="function"?i:void 0;if(L){const o=r((d,c)=>{try{$(d,c)}catch{}L(d,c)},"wrappedCb");try{return typeof i=="function"?n.call(this,s,o):n.call(this,s,i,o)}catch(d){throw(0,sql_accounting_1.abandonSqlStatement)(w),d}}let q;try{q=n.call(this,s,i)}catch(o){throw(0,sql_accounting_1.abandonSqlStatement)(w),o}return q&&typeof q.then=="function"?q.then(o=>$(void 0,o),o=>$(o,void 0)):(0,sql_accounting_1.abandonSqlStatement)(w),q},"wrapped");return a.__lensmcp=!0,e.query=a,!0}u(pgTap,"pgTap"),r(pgTap,"pgTap");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lensmcp/node-instrumentation",
3
- "version": "1.25.43",
3
+ "version": "1.25.45",
4
4
  "main": "./index.js",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
@@ -15,9 +15,9 @@
15
15
  }
16
16
  },
17
17
  "dependencies": {
18
- "@lensmcp/core": "1.25.43",
19
- "@lensmcp/nest-instrumentation": "1.25.43",
20
- "@lensmcp/protocol-types": "1.25.43",
18
+ "@lensmcp/core": "1.25.45",
19
+ "@lensmcp/nest-instrumentation": "1.25.45",
20
+ "@lensmcp/protocol-types": "1.25.45",
21
21
  "tslib": "^2.3.0"
22
22
  },
23
23
  "optionalDependencies": {