@agimon-ai/log-sink-mcp 0.2.13 → 0.3.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/cli.cjs +11 -11
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.mjs +22 -22
- package/dist/cli.mjs.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{stdio-D5FY3NXS.cjs → stdio-D-S2Mhwk.cjs} +17 -17
- package/dist/stdio-D-S2Mhwk.cjs.map +1 -0
- package/dist/stdio-DSDgdLCE.mjs +245 -0
- package/dist/stdio-DSDgdLCE.mjs.map +1 -0
- package/package.json +3 -3
- package/dist/stdio-7vqC2Ouf.mjs +0 -245
- package/dist/stdio-7vqC2Ouf.mjs.map +0 -1
- package/dist/stdio-D5FY3NXS.cjs.map +0 -1
package/dist/cli.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const e=require(`./chunk-DjWAcSYV.cjs`),t=require(`./stdio-
|
|
2
|
+
const e=require(`./chunk-DjWAcSYV.cjs`),t=require(`./stdio-D-S2Mhwk.cjs`);let n=require(`node:fs`),r=require(`node:path`),i=require(`node:url`),a=require(`commander`),o=require(`@agimon-ai/foundation-port-registry`),s=require(`@agimon-ai/foundation-process-registry`),c=require(`@hono/node-server`),l=require(`node:fs/promises`);l=e.t(l);let u=require(`hono`),d=require(`hono/cors`),f=require(`hono-rate-limiter`),p=require(`zod`),m=require(`hono/html`),h=require(`hono/jsx/jsx-runtime`),g=function(e){return e.TRACE=`trace`,e.DEBUG=`debug`,e.INFO=`info`,e.WARN=`warn`,e.ERROR=`error`,e.FATAL=`fatal`,e}({});const _=p.z.object({service:p.z.string().optional(),level:p.z.string().optional(),traceId:p.z.string().optional(),search:p.z.string().optional(),limit:p.z.coerce.number().min(1).max(1e3).default(25),offset:p.z.coerce.number().min(0).default(0)});function v(e){let n=new u.Hono,r=e.get(t.p.LogQueryService),i=e.get(t.p.LogSearchService);return n.get(`/logs`,async e=>{try{let{service:t,level:n,traceId:a,search:o,limit:s,offset:c}=_.parse({service:e.req.query(`service`),level:e.req.query(`level`),traceId:e.req.query(`traceId`),search:e.req.query(`search`),limit:e.req.query(`limit`),offset:e.req.query(`offset`)}),l,u=0;if(o){let e={};t&&(e.service=t),n&&(e.level=n);let r=await i.searchLogsPaginated(o,{...e,offset:c},s);l=r.results,u=r.total}else{let e=n?n.split(`,`):[g.INFO,g.WARN,g.ERROR,g.FATAL],i=await r.queryLogs({level:e,service:t||void 0,traceId:a||void 0,limit:s,offset:c});l=i.logs,u=i.total}return e.json({logs:l,total:u,hasMore:c+s<u})}catch(t){return console.error(`Failed to query logs:`,t),e.json({error:`Failed to query logs`,message:t instanceof Error?t.message:String(t)},500)}}),n.get(`/services`,async e=>{try{let t=await r.getActiveServices();return e.json({services:t,total:t.length})}catch(t){return console.error(`Failed to get services:`,t),e.json({error:`Failed to get services`,message:t instanceof Error?t.message:String(t)},500)}}),n.get(`/stats`,async e=>{try{let t=await r.getStatistics(),n=t.reduce((e,t)=>e+t.count,0),i=t.filter(e=>e.level===`error`||e.level===`fatal`).reduce((e,t)=>e+t.count,0),a=new Set(t.map(e=>e.service)).size;return e.json({totalLogs:n,errors:i,services:a,breakdown:t})}catch(t){return console.error(`Failed to get statistics:`,t),e.json({error:`Failed to get statistics`,message:t instanceof Error?t.message:String(t)},500)}}),n}function y(e){return e.toLocaleString(`en-US`,{year:`numeric`,month:`2-digit`,day:`2-digit`,hour:`2-digit`,minute:`2-digit`,second:`2-digit`,hour12:!1})}const b=`dashboard-container`,ee=`dashboard-header`,x=`controls-section`,te=`input-field`,ne=`select-field`,S=`btn`,re=`table-container`,C=`log-table`,w=`level-trace`,ie=`level-debug`,ae=`level-info`,T=`level-warn`,E=`level-error`,D=`level-fatal`,oe=`stats-container`,O=`stat-card`,k=`trace-link`,A=`timestamp-cell`,j=`service-cell`,se=`pagination-container`,M=`pagination-btn`,ce=`page-info`,N=`log-entry-group`,P=`metadata-row`,F=`message-row`,I=`message-full-cell`,L=`attributes-cell`,le=`
|
|
3
3
|
* {
|
|
4
4
|
margin: 0;
|
|
5
5
|
padding: 0;
|
|
@@ -314,7 +314,7 @@ const e=require(`./chunk-DjWAcSYV.cjs`),t=require(`./stdio-D5FY3NXS.cjs`);let n=
|
|
|
314
314
|
color: #555;
|
|
315
315
|
margin-top: 0.25rem;
|
|
316
316
|
}
|
|
317
|
-
`;function ue(e){let t=e.toLowerCase();return t===`trace`?
|
|
317
|
+
`;function ue(e){let t=e.toLowerCase();return t===`trace`?w:t===`debug`?ie:t===`info`?ae:t===`warn`?T:t===`error`?E:t===`fatal`?D:``}function de(e){if(!e||typeof e!=`object`)return null;let t=e,n={};for(let[e,r]of Object.entries(t))e===`trace.id`||e===`span.id`||(n[e]=r);return Object.keys(n).length>0?n:null}function fe({logs:e}){return(0,h.jsx)(`div`,{class:`table-container`,children:(0,h.jsxs)(`table`,{class:C,children:[(0,h.jsx)(`thead`,{children:(0,h.jsxs)(`tr`,{children:[(0,h.jsx)(`th`,{children:`Timestamp`}),(0,h.jsx)(`th`,{children:`Level`}),(0,h.jsx)(`th`,{children:`Service`}),(0,h.jsx)(`th`,{children:`Trace ID`})]})}),e.map(e=>{let t=de(e.metadata);return(0,h.jsxs)(`tbody`,{class:N,children:[(0,h.jsxs)(`tr`,{class:P,children:[(0,h.jsx)(`td`,{class:A,children:y(e.timestamp)}),(0,h.jsx)(`td`,{class:ue(e.level),children:e.level.toUpperCase()}),(0,h.jsx)(`td`,{class:j,children:e.service}),(0,h.jsx)(`td`,{children:e.traceId?(0,h.jsxs)(`span`,{class:k,onclick:`dashboard.filterByTrace('${e.traceId}')`,children:[e.traceId.slice(0,8),`...`]}):(0,h.jsx)(`span`,{children:`-`})})]}),(0,h.jsx)(`tr`,{class:F,children:(0,h.jsxs)(`td`,{colspan:4,class:I,children:[(0,h.jsx)(`div`,{children:e.message}),t&&(0,h.jsx)(`pre`,{class:L,children:JSON.stringify(t,null,2)})]})})]},e.id)})]})})}function pe(){return(0,h.jsx)(`input`,{id:`search-input`,type:`text`,class:`input-field`,placeholder:`Search logs...`,oninput:`dashboard.handleSearchInput(event)`})}function me({services:e}){return(0,h.jsxs)(`select`,{id:`service-select`,class:`select-field`,onchange:`dashboard.handleServiceChange(event)`,children:[(0,h.jsx)(`option`,{value:``,children:`All Services`}),e.map(e=>(0,h.jsx)(`option`,{value:e,children:e},e))]})}function he({stats:e}){return(0,h.jsxs)(`div`,{class:`stats-container`,children:[(0,h.jsxs)(`div`,{class:O,children:[(0,h.jsx)(`h3`,{children:`Total Logs`}),(0,h.jsx)(`div`,{class:`value`,children:e.totalLogs.toLocaleString()})]}),(0,h.jsxs)(`div`,{class:O,children:[(0,h.jsx)(`h3`,{children:`Errors`}),(0,h.jsx)(`div`,{class:`value`,children:e.errors.toLocaleString()})]}),(0,h.jsxs)(`div`,{class:O,children:[(0,h.jsx)(`h3`,{children:`Services`}),(0,h.jsx)(`div`,{class:`value`,children:e.services})]})]})}function ge({initialLogs:e,services:t,stats:n}){return(0,h.jsxs)(`div`,{class:`dashboard-container`,children:[(0,h.jsxs)(`div`,{class:`dashboard-header`,children:[(0,h.jsx)(`h1`,{children:`Log Dashboard`}),(0,h.jsx)(`p`,{children:`Real-time log streaming with auto-refresh (3 seconds)`})]}),(0,h.jsx)(he,{stats:n}),(0,h.jsxs)(`div`,{class:`controls-section`,children:[(0,h.jsx)(pe,{}),(0,h.jsx)(me,{services:t}),(0,h.jsx)(`button`,{id:`refresh-btn`,class:`btn`,onclick:`dashboard.manualRefresh()`,children:`Refresh Now`})]}),(0,h.jsx)(fe,{logs:e}),(0,h.jsxs)(`div`,{id:`pagination-controls`,class:`pagination-container`,children:[(0,h.jsx)(`button`,{id:`prev-btn`,class:M,onclick:`dashboard.prevPage()`,disabled:!0,children:`Previous`}),(0,h.jsx)(`span`,{id:`page-info`,class:`page-info`,children:`Page 1`}),(0,h.jsx)(`button`,{id:`next-btn`,class:M,onclick:`dashboard.nextPage()`,children:`Next`})]}),(0,h.jsx)(`script`,{children:(0,m.raw)(`
|
|
318
318
|
class DashboardManager {
|
|
319
319
|
constructor() {
|
|
320
320
|
this.filters = {
|
|
@@ -384,7 +384,7 @@ class DashboardManager {
|
|
|
384
384
|
}
|
|
385
385
|
|
|
386
386
|
updateLogTable(logs) {
|
|
387
|
-
const table = document.querySelector('.${
|
|
387
|
+
const table = document.querySelector('.${C}');
|
|
388
388
|
if (!table) return;
|
|
389
389
|
|
|
390
390
|
// Remove existing tbody elements (except thead)
|
|
@@ -448,9 +448,9 @@ class DashboardManager {
|
|
|
448
448
|
|
|
449
449
|
getLevelClass(level) {
|
|
450
450
|
const classes = {
|
|
451
|
-
trace: '${
|
|
452
|
-
debug: '${
|
|
453
|
-
info: '${
|
|
451
|
+
trace: '${w}',
|
|
452
|
+
debug: '${ie}',
|
|
453
|
+
info: '${ae}',
|
|
454
454
|
warn: '${T}',
|
|
455
455
|
error: '${E}',
|
|
456
456
|
fatal: '${D}',
|
|
@@ -871,10 +871,10 @@ window.addEventListener('beforeunload', () => {
|
|
|
871
871
|
color: #555;
|
|
872
872
|
margin-top: 0.25rem;
|
|
873
873
|
}
|
|
874
|
-
`)})]}),(0,h.jsx)(`body`,{children:t})]})}function _e(e){let n=new u.Hono,r=e.get(t.p.LogQueryService);return n.get(`/`,async e=>{try{let t=await r.getActiveServices(),n=await r.getStatistics(),i=await r.filterByLevel([g.INFO,g.WARN,g.ERROR,g.FATAL],100),a={totalLogs:n.reduce((e,t)=>e+t.count,0),errors:n.filter(e=>e.level===`error`||e.level===`fatal`).reduce((e,t)=>e+t.count,0),services:new Set(n.map(e=>e.service)).size};return e.html((0,h.jsx)(R,{title:`Log Dashboard`,children:(0,h.jsx)(ge,{initialLogs:i,services:t,stats:a})}))}catch(t){return console.error(`Failed to render dashboard:`,t),e.html((0,h.jsx)(R,{title:`Log Dashboard - Error`,children:(0,h.jsxs)(`div`,{style:`padding: 2rem; text-align: center;`,children:[(0,h.jsx)(`h1`,{children:`Failed to load dashboard`}),(0,h.jsx)(`p`,{children:t instanceof Error?t.message:String(t)}),(0,h.jsx)(`a`,{href:`/`,style:`color: #2196f3; text-decoration: underline;`,children:`Retry`})]})}),500)}}),n}const z=5*1024*1024,ve=1e3,B=2e3,ye=200,V=8e3,be=p.z.object({timestamp:p.z.string().datetime().optional(),level:p.z.enum([`trace`,`debug`,`info`,`warn`,`error`,`fatal`]),message:p.z.string().min(1).max(V),traceId:p.z.string().regex(/^[0-9a-f]{32}$/i,`traceId must be 32 hex characters`).optional(),spanId:p.z.string().regex(/^[0-9a-f]{16}$/i,`spanId must be 16 hex characters`).optional(),parentSpanId:p.z.string().regex(/^[0-9a-f]{16}$/i,`parentSpanId must be 16 hex characters`).optional(),service:p.z.string().min(1).max(200),hostname:p.z.string().max(255).optional(),pid:p.z.number().int().nonnegative().optional(),metadata:p.z.record(p.z.string(),p.z.unknown()).optional(),errorType:p.z.string().max(255).optional(),errorMessage:p.z.string().max(8e3).optional(),errorStack:p.z.string().max(32e3).optional()}),xe=p.z.object({logs:p.z.array(be).min(1).max(1e3)});function H(e){if(!e||!/^\d+$/.test(e))throw Error(`OTLP timestamp must be a numeric unix-nanoseconds string`);let t=Number(BigInt(e)/BigInt(1e6));return new Date(t)}function U(e,t){return typeof e==`string`&&RegExp(`^[0-9a-f]{${t}}$`,`i`).test(e)}function W(e){if(!e)return null;let t=Number(e);return!Number.isFinite(t)||t<=0?null:t>z?`Request body exceeds ${z} bytes`:null}async function G(e){try{return await e.req.json()}catch(e){throw Error(`Invalid JSON body: ${e instanceof Error?e.message:String(e)}`)}}function Se(e){if(!Array.isArray(e.resourceSpans)||e.resourceSpans.length===0)throw Error(`Invalid OTLP trace request: missing resourceSpans`);let t=0;for(let n of e.resourceSpans){if(!Array.isArray(n.scopeSpans))throw Error(`Invalid OTLP trace request: scopeSpans must be an array`);for(let e of n.scopeSpans){if(!Array.isArray(e.spans))throw Error(`Invalid OTLP trace request: spans must be an array`);for(let n of e.spans){if(t+=1,t>B)throw Error(`OTLP trace request exceeds ${B} spans`);if(!U(n.traceId,32))throw Error(`Invalid OTLP trace request: traceId must be 32 hex characters`);if(!U(n.spanId,16))throw Error(`Invalid OTLP trace request: spanId must be 16 hex characters`);if(n.parentSpanId&&!U(n.parentSpanId,16))throw Error(`Invalid OTLP trace request: parentSpanId must be 16 hex characters`);if(!n.name||n.name.length>V)throw Error(`Invalid OTLP trace request: span name is missing or too long`);H(n.startTimeUnixNano)}}}}function Ce(e){if(!Array.isArray(e.resourceLogs)||e.resourceLogs.length===0)throw Error(`Invalid OTLP logs request: missing resourceLogs`);let t=0;for(let n of e.resourceLogs){if(!Array.isArray(n.scopeLogs))throw Error(`Invalid OTLP logs request: scopeLogs must be an array`);for(let e of n.scopeLogs){if(!Array.isArray(e.logRecords))throw Error(`Invalid OTLP logs request: logRecords must be an array`);for(let n of e.logRecords){if(t+=1,t>B)throw Error(`OTLP logs request exceeds ${B} log records`);if(n.traceId&&!U(n.traceId,32))throw Error(`Invalid OTLP logs request: traceId must be 32 hex characters`);if(n.spanId&&!U(n.spanId,16))throw Error(`Invalid OTLP logs request: spanId must be 16 hex characters`);if(n.severityText&&n.severityText.length>32)throw Error(`Invalid OTLP logs request: severityText is too long`);H(n.timeUnixNano)}}}}function we(e){return(e.resource?.attributes?.find(e=>e.key===`service.name`))?.value?.stringValue||`unknown-service`}function K(e,t){let n=e?.find(e=>e.key===t);if(!n)return;let{value:r}=n;if(r.stringValue!==void 0)return r.stringValue;if(r.intValue!==void 0)return r.intValue;if(r.doubleValue!==void 0)return String(r.doubleValue);if(r.boolValue!==void 0)return String(r.boolValue)}function Te(e){return(e.resource?.attributes?.find(e=>e.key===`service.name`))?.value?.stringValue||`unknown-service`}function Ee(e){return e===void 0?`info`:e<=4?`trace`:e<=8?`debug`:e<=12?`info`:e<=16?`warn`:e<=20?`error`:`fatal`}function De(e,t){let n=H(e.timeUnixNano),r=e.severityText?.toLowerCase()||Ee(e.severityNumber),i=``;if(e.body?.stringValue)i=e.body.stringValue;else if(e.body?.kvlistValue){let t={};for(let n of e.body.kvlistValue.values){let e=n.value.stringValue??n.value.intValue??n.value.doubleValue??n.value.boolValue;t[n.key]=e}i=JSON.stringify(t)}let a={},o=null,s=null,c=null;if(e.attributes)for(let t of e.attributes){let e=t.value.stringValue??t.value.intValue??t.value.doubleValue??t.value.boolValue;t.key===`exception.type`?o=String(e):t.key===`exception.message`?s=String(e):t.key===`exception.stacktrace`?c=String(e):a[t.key]=e}return{timestamp:n,level:r,message:i||`[LOG] ${t}`,traceId:e.traceId??null,spanId:e.spanId??null,parentSpanId:null,service:t,hostname:null,pid:null,metadata:Object.keys(a).length>0?a:null,errorType:o,errorMessage:s,errorStack:c}}function Oe(e,t){let n=H(e.startTimeUnixNano),r=e.status?.code,i=`info`;r===2?i=`error`:r===1&&(i=`info`);let a=K(e.attributes,`exception.type`),o=K(e.attributes,`exception.message`),s=K(e.attributes,`exception.stacktrace`),c={};if(e.attributes)for(let t of e.attributes){let e=t.value.stringValue??t.value.intValue??t.value.doubleValue??t.value.boolValue;c[t.key]=e}return c.spanName=e.name,c.spanKind=e.kind,e.status?.message&&(c.statusMessage=e.status.message),{timestamp:n,level:i,message:`[SPAN] ${e.name}`,traceId:e.traceId,spanId:e.spanId,parentSpanId:e.parentSpanId??null,service:t,hostname:null,pid:null,metadata:c,errorType:a??null,errorMessage:o??null,errorStack:s??null}}function ke(e){let n=new u.Hono,r=e.get(t.p.LogStorageService);n.use(`*`,(0,d.cors)({origin:e=>e??null,credentials:!0,allowMethods:[`GET`,`POST`,`OPTIONS`],allowHeaders:[`Content-Type`,`Accept`,`Authorization`]})),n.get(`/health`,async e=>{try{return e.json({status:`healthy`,service:`log-sink-mcp-http`,serviceName:`log-sink-mcp-http`,timestamp:new Date().toISOString()})}catch(t){return e.json({status:`unhealthy`,error:t instanceof Error?t.message:String(t)},503)}});let i=(0,f.rateLimiter)({windowMs:60*1e3,limit:100,standardHeaders:`draft-6`,keyGenerator:e=>e.req.header(`x-forwarded-for`)||e.req.header(`x-real-ip`)||`unknown`});n.post(`/logs`,i,async e=>{try{let t=W(e.req.header(`content-length`));if(t)return e.json({success:!1,error:t},413);let n=await G(e),i=xe.safeParse(n);if(!i.success)return e.json({success:!1,error:`Validation failed`,details:i.error.issues},400);let{logs:a}=i.data,o=a.map(e=>({...e,timestamp:e.timestamp?new Date(e.timestamp):new Date})),s=await r.insertBatch(o);return e.json({success:!0,count:s.length,message:`Successfully inserted ${s.length} logs`},201)}catch(t){let n=t instanceof Error?t.message:String(t);return n.startsWith(`Invalid JSON body`)?e.json({success:!1,error:n},400):(console.error(`Failed to insert logs:`,t),e.json({success:!1,error:`Failed to insert logs`},500))}}),n.post(`/v1/traces`,i,async e=>{try{let t=W(e.req.header(`content-length`));if(t)return e.json({success:!1,error:t},413);let n=await G(e);Se(n);let i=[];for(let e of n.resourceSpans){let t=we(e);for(let n of e.scopeSpans)for(let e of n.spans){let n=Oe(e,t);i.push(n)}}let a=await r.insertBatch(i);return e.json({success:!0,count:a.length,message:`Successfully inserted ${a.length} trace spans as logs`},201)}catch(t){let n=t instanceof Error?t.message:String(t);return n.startsWith(`Invalid JSON body`)||n.startsWith(`Invalid OTLP`)||n.startsWith(`OTLP`)?e.json({success:!1,error:n},400):(console.error(`Failed to insert OTLP traces:`,t),e.json({success:!1,error:`Failed to insert OTLP traces`},500))}}),n.post(`/v1/logs`,i,async e=>{try{let t=W(e.req.header(`content-length`));if(t)return e.json({success:!1,error:t},413);let n=await G(e);Ce(n);let i=[];for(let e of n.resourceLogs){let t=Te(e);for(let n of e.scopeLogs)for(let e of n.logRecords){let n=De(e,t);i.push(n)}}let a=await r.insertBatch(i);return e.json({success:!0,count:a.length,message:`Successfully inserted ${a.length} OTLP logs`},201)}catch(t){let n=t instanceof Error?t.message:String(t);return n.startsWith(`Invalid JSON body`)||n.startsWith(`Invalid OTLP`)||n.startsWith(`OTLP`)?e.json({success:!1,error:n},400):(console.error(`Failed to insert OTLP logs:`,t),e.json({success:!1,error:`Failed to insert OTLP logs`},500))}});let a=v(e);n.route(`/api`,a);let o=_e(e);return n.route(`/`,o),n}const Ae=256*1024*1024,je=.9,Me=300*1e3,Ne=1e3;function q(e,t){if(e===void 0||e===``)return t;let n=Number.parseInt(e,10);if(!Number.isFinite(n)||n<=0)throw Error(`Invalid database retention value: ${e}`);return n}function J(e={}){let t=q(e.dbMaxBytes??process.env.LOG_SINK_DB_MAX_BYTES,268435456),n=q(e.dbTargetBytes??process.env.LOG_SINK_DB_TARGET_BYTES,Math.floor(t*.9));return{maxBytes:t,targetBytes:Math.min(n,t),intervalMs:q(e.dbRetentionIntervalMs??process.env.LOG_SINK_DB_RETENTION_INTERVAL_MS,3e5),batchSize:q(e.dbRetentionBatchSize??process.env.LOG_SINK_DB_RETENTION_BATCH_SIZE,1e3)}}const Pe=String(o.DEFAULT_PORT_RANGE.min),Fe=t.d();function Ie(e,t){if(!e)return;let n=r.default.resolve(e);return r.default.extname(n)===`.json`?r.default.join(r.default.dirname(n),t):r.default.join(n,t)}function Le(){return new s.ProcessRegistryService(process.env.PROCESS_REGISTRY_PATH)}async function Re(e,t,n,r,i){let a=Le(),o=await a.registerProcess({repositoryPath:e,serviceName:t,serviceType:`tool`,environment:n,pid:process.pid,port:r,host:`127.0.0.1`,command:process.argv[1],args:process.argv.slice(2),metadata:{dbPath:i,transport:`http`}});if(!o.success||!o.record)throw Error(o.error||`Failed to register process for ${t}`);let s=!1;return{release:async r=>{if(s)return;s=!0;let i=await a.releaseProcess({repositoryPath:e,serviceName:t,serviceType:`tool`,pid:process.pid,environment:n,kill:r?.kill??!1,releasePort:r?.releasePort??!1});if(!i.success&&i.error&&!i.error.includes(`No matching process entry`))throw Error(i.error||`Failed to release process for ${t}`)}}}const ze=new a.Command(`http-serve`).description(`Start HTTP server for log ingestion with configurable port, database path, and in-memory mode`).option(`-p, --port <port>`,`Port to listen on`,Pe).option(`--db-path <path>`,`Path to SQLite database file`,Fe).option(`--in-memory`,`Use in-memory database (for testing)`,!1).option(`--db-max-bytes <bytes>`,`Maximum SQLite database size before trimming`).option(`--db-target-bytes <bytes>`,`Target SQLite database size after trimming`).option(`--db-retention-interval-ms <ms>`,`How often to check database size`,`300000`).option(`--db-retention-batch-size <count>`,`Number of oldest log rows to delete per trim batch`,`1000`).option(`--registry-path <path>`,`Custom registry path or directory for service discovery`).action(async e=>{try{let n=Number.parseInt(e.port,10),r={min:3e3,max:Math.max(4999,n)},i=process.env.NODE_ENV||`development`,a=t.f(process.cwd()),s=`log-sink-mcp-http`;e.registryPath&&(process.env.PORT_REGISTRY_PATH=e.registryPath,process.env.PROCESS_REGISTRY_PATH=Ie(e.registryPath,`processes.json`)??process.env.PROCESS_REGISTRY_PATH);let l=new o.PortRegistryService(process.env.PORT_REGISTRY_PATH);console.log(`🚀 Starting HTTP server for log ingestion...`),console.log(` Port: ${n}`),console.log(` Database: ${e.inMemory?`In-Memory`:e.dbPath}`),console.log(` Registry path: ${process.env.PORT_REGISTRY_PATH??`${process.env.HOME}/.port-registry/ports.json`}`);let u=t.u(),d=u.get(t.p.LogStorageService),f=u.get(t.p.LogRetentionService);await d.initializeDatabase(e.dbPath,e.inMemory);let p=f.startSizeRetentionMonitor(J({dbMaxBytes:e.dbMaxBytes,dbTargetBytes:e.dbTargetBytes,dbRetentionIntervalMs:e.dbRetentionIntervalMs,dbRetentionBatchSize:e.dbRetentionBatchSize}));console.log(`✓ Database initialized`);let m=ke(u),h=await l.reservePort({repositoryPath:a,serviceName:s,serviceType:`tool`,environment:i,preferredPort:n,pid:process.pid,host:`127.0.0.1`,force:!0,portRange:r,metadata:{healthCheckUrl:`http://localhost:${n}/health`,dbPath:e.dbPath}});if(!h.success||!h.record)throw Error(h.error||`Failed to reserve service in global registry`);let g=h.record.port;if(g!==n){let t=await l.reservePort({repositoryPath:a,serviceName:s,serviceType:`tool`,environment:i,preferredPort:g,pid:process.pid,host:`127.0.0.1`,force:!0,portRange:r,metadata:{healthCheckUrl:`http://localhost:${g}/health`,dbPath:e.dbPath}});if(!t.success||!t.record)throw Error(t.error||`Failed to update service metadata in registry`);h=t}if(!h.record)throw Error(`Port reservation lost before startup`);let _=h.record,v=_.port,y=await Re(a,s,i,v,e.dbPath),ee=`http://localhost:${v}/health`,te;try{te=(0,c.serve)({fetch:m.fetch,port:v})}catch(e){try{await l.releasePort({repositoryPath:a,serviceName:s,serviceType:`tool`,environment:i,pid:process.pid})}catch{}try{await y.release({kill:!1,releasePort:!1})}catch{}throw e}console.log(`✓ HTTP server listening on http://localhost:${v}`),console.log(` Health check: ${ee}`),console.log(` Log ingestion: POST http://localhost:${v}/logs`);let ne=_.port,re=_.serviceName||s;console.log(`✓ Service registered: ${re} on ${ne}`),console.log(`
|
|
875
|
-
Press Ctrl+C to stop the server`);let
|
|
876
|
-
`)}function Q(e){return e.option(`--db-path <path>`,`Path to SQLite database file`,Be).option(`--in-memory`,`Use in-memory database`,!1)}async function Ge(e,n){let r=t.u(),i=r.get(t.p.LogStorageService);await i.initializeDatabase(e.dbPath,e.inMemory);try{return await n(r)}finally{await i.disconnect()}}async function Ke(e,t,n,r=He){try{let i=await Ge(e,async e=>t(e).execute(n)),a=We(i);return i.isError?(r.stderr(a),1):(r.stdout(a),0)}catch(e){return r.stderr(`Error executing logs command: ${e instanceof Error?e.message:String(e)}`),1}}async function $(e,t,n){let r=await Ke(e,t,n);r!==0&&process.exit(r)}function qe(e,t){if(!(!e&&!t)){if(!e||!t)throw new a.InvalidArgumentError(`Provide both --start-time and --end-time together.`);return{start:e,end:t}}}function Je(){let e=new a.Command(`logs`).description(`Run log analysis commands that mirror MCP tool capabilities`),n=Q(new a.Command(`query`).alias(`query-logs`).description(`Query and filter log entries by level, trace ID, service, or time range`).option(`--level <level...>`,`Log level(s) to filter by`).option(`--trace-id <traceId>`,`Trace ID to filter by`).option(`--service <service...>`,`Service name(s) to filter by`).option(`--start-time <iso8601>`,`Start time for log range (ISO 8601)`).option(`--end-time <iso8601>`,`End time for log range (ISO 8601)`).option(`--limit <number>`,`Maximum number of log entries to return`,X).action(async e=>{await $(e,e=>new t.i(e),{level:Z(e.level),traceId:e.traceId,service:e.service,startTime:e.startTime,endTime:e.endTime,limit:e.limit})})),r=Q(new a.Command(`search`).alias(`search-logs`).description(`Search log entries with full-text search and optional filters`).argument(`<search-query>`,`FTS5 search query`).option(`--mode <mode>`,`Search strategy: fts, semantic, or hybrid`,`hybrid`).option(`--service <service...>`,`Service name(s) to filter by`).option(`--level <level...>`,`Log level(s) to filter by`).option(`--start-time <iso8601>`,`Start time for log range (ISO 8601)`).option(`--end-time <iso8601>`,`End time for log range (ISO 8601)`).option(`--limit <number>`,`Maximum number of search results to return`,X).action(async(e,n)=>{await $(n,e=>new t.r(e),{searchQuery:e,mode:n.mode,service:n.service,level:Z(n.level),startTime:n.startTime,endTime:n.endTime,limit:n.limit})})),i=Q(new a.Command(`trace`).alias(`get-trace-timeline`).description(`Get the complete trace timeline for a trace ID`).argument(`<trace-id>`,`Trace ID to inspect`).action(async(e,n)=>{await $(n,e=>new t.a(e),{traceId:e})})),o=Q(new a.Command(`analyze-errors`).alias(`errors`).description(`Analyze error patterns and group similar errors`).option(`--trace-id <traceId>`,`Optional trace ID to scope the analysis`).option(`--start-time <iso8601>`,`Start time for error analysis (ISO 8601)`).option(`--end-time <iso8601>`,`End time for error analysis (ISO 8601)`).option(`--limit <number>`,`Maximum number of error entries to analyze`,X).action(async e=>{await $(e,e=>new t.l(e),{traceId:e.traceId,timeRange:qe(e.startTime,e.endTime),limit:e.limit})})),s=Q(new a.Command(`stats`).alias(`get-log-stats`).description(`Get aggregated log statistics grouped by level, service, or both`).option(`--start-time <iso8601>`,`Start time for statistics (ISO 8601)`).option(`--end-time <iso8601>`,`End time for statistics (ISO 8601)`).option(`--group-by <groupBy>`,`Group statistics by level, service, or both`,Ue).action(async e=>{await $(e,e=>new t.s(e),{startTime:e.startTime,endTime:e.endTime,groupBy:e.groupBy})})),c=Q(new a.Command(`services`).alias(`get-services`).description(`Get the list of unique services present in the log database`).action(async e=>{await $(e,e=>new t.o(e),{})})),l=Q(new a.Command(`clear`).alias(`clear-logs`).description(`Clear all logs from the database`).action(async e=>{await $(e,e=>new t.c(e),{})}));return e.addCommand(n).addCommand(r).addCommand(i).addCommand(o).addCommand(s).addCommand(c).addCommand(l)}const Ye=Je();function Xe(e,t){if(!e)return;let n=r.default.resolve(e);return r.default.extname(n)===`.json`?r.default.join(r.default.dirname(n),t):r.default.join(n,t)}function Ze(){return new s.ProcessRegistryService(process.env.PROCESS_REGISTRY_PATH)}async function Qe(e){let t=Ze(),n=await t.registerProcess({repositoryPath:process.cwd(),serviceName:e,serviceType:`tool`,environment:process.env.NODE_ENV||`development`,pid:process.pid,host:`127.0.0.1`,command:process.argv[1],args:process.argv.slice(2),metadata:{transport:`stdio`,command:`mcp-serve`},force:!0});if(!n.success||!n.record)throw Error(n.error||`Failed to register process for ${e}`);let r=!1;return{release:async()=>{if(r)return;r=!0;let n=await t.releaseProcess({repositoryPath:process.cwd(),serviceName:e,serviceType:`tool`,environment:process.env.NODE_ENV||`development`,pid:process.pid,kill:!1,releasePort:!1});if(!n.success&&!n.error?.includes(`No matching process entry`))throw Error(n.error||`Failed to release process for ${e}`)}}}function $e(){return t.d()}const et=new a.Command(`mcp-serve`).description(`Start MCP server with stdio transport`).option(`--cleanup`,`Stop HTTP server on MCP server shutdown`,!1).option(`--db-path <path>`,`Path to SQLite database file`,$e()).option(`--in-memory`,`Use in-memory database (for testing)`,!1).option(`--db-max-bytes <bytes>`,`Maximum SQLite database size before trimming`).option(`--db-target-bytes <bytes>`,`Target SQLite database size after trimming`).option(`--db-retention-interval-ms <ms>`,`How often to check database size`,`300000`).option(`--db-retention-batch-size <count>`,`Number of oldest log rows to delete per trim batch`,`1000`).option(`--registry-path <path>`,`Custom registry path or directory for service discovery`).option(`--registry-dir <path>`,`Custom registry directory for service discovery`).action(async e=>{let n;try{let r=e.registryPath||e.registryDir;r&&(process.env.PORT_REGISTRY_PATH=r,process.env.PROCESS_REGISTRY_PATH=Xe(r,`processes.json`)??process.env.PROCESS_REGISTRY_PATH);let i=t.u(),a=i.get(t.p.LogStorageService),o=i.get(t.p.LogRetentionService),s=i.get(t.p.HttpServerManager);await a.initializeDatabase(e.dbPath,e.inMemory);let c=o.startSizeRetentionMonitor(J({dbMaxBytes:e.dbMaxBytes,dbTargetBytes:e.dbTargetBytes,dbRetentionIntervalMs:e.dbRetentionIntervalMs,dbRetentionBatchSize:e.dbRetentionBatchSize})),l=await s.ensureRunning(3100,e.dbPath);l.running||console.error(`Warning: HTTP server failed to start: ${l.error}`);let u=new t.t(t.n(i));n=await Qe(`log-sink-mcp-mcp-serve`);let d=async t=>{console.error(`\nReceived ${t}, shutting down gracefully...`);try{await u.stop(),e.cleanup&&l.running&&(await s.stop(),console.error(`HTTP server stopped`)),c(),await a.disconnect(),await n?.release(),process.exit(0)}catch(e){console.error(`Error during shutdown:`,e),process.exit(1)}};process.on(`SIGINT`,()=>d(`SIGINT`)),process.on(`SIGTERM`,()=>d(`SIGTERM`)),await u.start()}catch(e){await n?.release().catch(()=>void 0),console.error(`Failed to start MCP server:`,e),process.exit(1)}});function tt(e,t){if(!e)return;let n=r.default.resolve(e);return r.default.extname(n)===`.json`?r.default.join(r.default.dirname(n),t):r.default.join(n,t)}function nt(){return new s.ProcessRegistryService(process.env.PROCESS_REGISTRY_PATH)}async function rt(e){let t=nt(),n=await t.registerProcess({repositoryPath:process.cwd(),serviceName:e,serviceType:`tool`,environment:process.env.NODE_ENV||`development`,pid:process.pid,host:`127.0.0.1`,command:process.argv[1],args:process.argv.slice(2),metadata:{transport:`stdio`,command:`start`},force:!0});if(!n.success||!n.record)throw Error(n.error||`Failed to register process for ${e}`);let r=!1;return{release:async()=>{if(r)return;r=!0;let n=await t.releaseProcess({repositoryPath:process.cwd(),serviceName:e,serviceType:`tool`,environment:process.env.NODE_ENV||`development`,pid:process.pid,kill:!1,releasePort:!1});if(!n.success&&!n.error?.includes(`No matching process entry`))throw Error(n.error||`Failed to release process for ${e}`)}}}const it=new a.Command(`start`).description(`Start HTTP and/or MCP servers with singleton coordination`).option(`--mcp-only`,`Start only the MCP server (stdio transport)`,!1).option(`--http-only`,`Start only the HTTP server`,!1).option(`-p, --port <port>`,`Port for HTTP server`,String(o.DEFAULT_PORT_RANGE.min)).option(`--db-path <path>`,`Path to SQLite database file`,t.d()).option(`--in-memory`,`Use in-memory database (for testing)`,!1).option(`--db-max-bytes <bytes>`,`Maximum SQLite database size before trimming`).option(`--db-target-bytes <bytes>`,`Target SQLite database size after trimming`).option(`--db-retention-interval-ms <ms>`,`How often to check database size`,`300000`).option(`--db-retention-batch-size <count>`,`Number of oldest log rows to delete per trim batch`,`1000`).option(`--registry-path <path>`,`Custom registry path or directory for service discovery`).option(`--registry-dir <path>`,`Custom registry directory for service discovery`).action(async e=>{let n;try{let r=e.registryPath||e.registryDir;r&&(process.env.PORT_REGISTRY_PATH=r,process.env.PROCESS_REGISTRY_PATH=tt(r,`processes.json`)??process.env.PROCESS_REGISTRY_PATH),e.dbMaxBytes&&(process.env.LOG_SINK_DB_MAX_BYTES=e.dbMaxBytes),e.dbTargetBytes&&(process.env.LOG_SINK_DB_TARGET_BYTES=e.dbTargetBytes),e.dbRetentionIntervalMs&&(process.env.LOG_SINK_DB_RETENTION_INTERVAL_MS=e.dbRetentionIntervalMs),e.dbRetentionBatchSize&&(process.env.LOG_SINK_DB_RETENTION_BATCH_SIZE=e.dbRetentionBatchSize);let i=t.u(),a=i.get(t.p.LogStorageService);await a.initializeDatabase(e.dbPath,e.inMemory);let o=Number.parseInt(e.port,10),s=!e.mcpOnly,c=!e.httpOnly,l=i.get(t.p.LogRetentionService),u=c?l.startSizeRetentionMonitor(J({dbMaxBytes:e.dbMaxBytes,dbTargetBytes:e.dbTargetBytes,dbRetentionIntervalMs:e.dbRetentionIntervalMs,dbRetentionBatchSize:e.dbRetentionBatchSize})):()=>void 0;if(console.log(`🚀 Starting log-sink-mcp services...`),console.log(` Database: ${e.inMemory?`In-Memory`:e.dbPath}`),s){let n=await i.get(t.p.HttpServerManager).ensureRunning(o,e.dbPath);n.running?(console.log(`✓ HTTP Server: Running on http://localhost:${n.port} (PID: ${n.pid})`),console.log(` Health check: http://localhost:${n.port}/health`),console.log(` Log ingestion: POST http://localhost:${n.port}/logs`)):(console.error(`✗ HTTP Server failed to start: ${n.error}`),c||process.exit(1))}if(c){n=await rt(`log-sink-mcp-start`),console.log(`✓ MCP Server: Starting with stdio transport...`);let e=new t.t(t.n(i));await e.start(),console.log(`✓ MCP Server: Ready for connections`);let r=async r=>{console.log(`\n\n${r} received. Shutting down gracefully...`);try{await e.stop(),console.log(`✓ MCP server stopped`),s&&(await i.get(t.p.HttpServerManager).stop(),console.log(`✓ HTTP server stopped`)),u(),await a.disconnect(),console.log(`✓ Database disconnected`),await n?.release(),console.log(`✓ Process registry entry released`),console.log(`Goodbye! 👋`),process.exit(0)}catch(e){console.error(`Error during shutdown:`,e),process.exit(1)}};process.on(`SIGINT`,()=>r(`SIGINT`)),process.on(`SIGTERM`,()=>r(`SIGTERM`)),console.log(`
|
|
874
|
+
`)})]}),(0,h.jsx)(`body`,{children:t})]})}function _e(e){let n=new u.Hono,r=e.get(t.p.LogQueryService);return n.get(`/`,async e=>{try{let t=await r.getActiveServices(),n=await r.getStatistics(),i=await r.filterByLevel([g.INFO,g.WARN,g.ERROR,g.FATAL],100),a={totalLogs:n.reduce((e,t)=>e+t.count,0),errors:n.filter(e=>e.level===`error`||e.level===`fatal`).reduce((e,t)=>e+t.count,0),services:new Set(n.map(e=>e.service)).size};return e.html((0,h.jsx)(R,{title:`Log Dashboard`,children:(0,h.jsx)(ge,{initialLogs:i,services:t,stats:a})}))}catch(t){return console.error(`Failed to render dashboard:`,t),e.html((0,h.jsx)(R,{title:`Log Dashboard - Error`,children:(0,h.jsxs)(`div`,{style:`padding: 2rem; text-align: center;`,children:[(0,h.jsx)(`h1`,{children:`Failed to load dashboard`}),(0,h.jsx)(`p`,{children:t instanceof Error?t.message:String(t)}),(0,h.jsx)(`a`,{href:`/`,style:`color: #2196f3; text-decoration: underline;`,children:`Retry`})]})}),500)}}),n}const z=5*1024*1024,ve=1e3,B=2e3,ye=200,V=8e3,be=p.z.object({timestamp:p.z.string().datetime().optional(),level:p.z.enum([`trace`,`debug`,`info`,`warn`,`error`,`fatal`]),message:p.z.string().min(1).max(V),traceId:p.z.string().regex(/^[0-9a-f]{32}$/i,`traceId must be 32 hex characters`).optional(),spanId:p.z.string().regex(/^[0-9a-f]{16}$/i,`spanId must be 16 hex characters`).optional(),parentSpanId:p.z.string().regex(/^[0-9a-f]{16}$/i,`parentSpanId must be 16 hex characters`).optional(),service:p.z.string().min(1).max(200),hostname:p.z.string().max(255).optional(),pid:p.z.number().int().nonnegative().optional(),metadata:p.z.record(p.z.string(),p.z.unknown()).optional(),errorType:p.z.string().max(255).optional(),errorMessage:p.z.string().max(8e3).optional(),errorStack:p.z.string().max(32e3).optional()}),xe=p.z.object({logs:p.z.array(be).min(1).max(1e3)});function H(e){if(!e||!/^\d+$/.test(e))throw Error(`OTLP timestamp must be a numeric unix-nanoseconds string`);let t=Number(BigInt(e)/BigInt(1e6));return new Date(t)}function U(e,t){return typeof e==`string`&&RegExp(`^[0-9a-f]{${t}}$`,`i`).test(e)}function W(e){if(!e)return null;let t=Number(e);return!Number.isFinite(t)||t<=0?null:t>z?`Request body exceeds ${z} bytes`:null}async function G(e){try{return await e.req.json()}catch(e){throw Error(`Invalid JSON body: ${e instanceof Error?e.message:String(e)}`)}}function Se(e){if(!Array.isArray(e.resourceSpans)||e.resourceSpans.length===0)throw Error(`Invalid OTLP trace request: missing resourceSpans`);let t=0;for(let n of e.resourceSpans){if(!Array.isArray(n.scopeSpans))throw Error(`Invalid OTLP trace request: scopeSpans must be an array`);for(let e of n.scopeSpans){if(!Array.isArray(e.spans))throw Error(`Invalid OTLP trace request: spans must be an array`);for(let n of e.spans){if(t+=1,t>B)throw Error(`OTLP trace request exceeds ${B} spans`);if(!U(n.traceId,32))throw Error(`Invalid OTLP trace request: traceId must be 32 hex characters`);if(!U(n.spanId,16))throw Error(`Invalid OTLP trace request: spanId must be 16 hex characters`);if(n.parentSpanId&&!U(n.parentSpanId,16))throw Error(`Invalid OTLP trace request: parentSpanId must be 16 hex characters`);if(!n.name||n.name.length>V)throw Error(`Invalid OTLP trace request: span name is missing or too long`);H(n.startTimeUnixNano)}}}}function Ce(e){if(!Array.isArray(e.resourceLogs)||e.resourceLogs.length===0)throw Error(`Invalid OTLP logs request: missing resourceLogs`);let t=0;for(let n of e.resourceLogs){if(!Array.isArray(n.scopeLogs))throw Error(`Invalid OTLP logs request: scopeLogs must be an array`);for(let e of n.scopeLogs){if(!Array.isArray(e.logRecords))throw Error(`Invalid OTLP logs request: logRecords must be an array`);for(let n of e.logRecords){if(t+=1,t>B)throw Error(`OTLP logs request exceeds ${B} log records`);if(n.traceId&&!U(n.traceId,32))throw Error(`Invalid OTLP logs request: traceId must be 32 hex characters`);if(n.spanId&&!U(n.spanId,16))throw Error(`Invalid OTLP logs request: spanId must be 16 hex characters`);if(n.severityText&&n.severityText.length>32)throw Error(`Invalid OTLP logs request: severityText is too long`);H(n.timeUnixNano)}}}}function we(e){return(e.resource?.attributes?.find(e=>e.key===`service.name`))?.value?.stringValue||`unknown-service`}function K(e,t){let n=e?.find(e=>e.key===t);if(!n)return;let{value:r}=n;if(r.stringValue!==void 0)return r.stringValue;if(r.intValue!==void 0)return r.intValue;if(r.doubleValue!==void 0)return String(r.doubleValue);if(r.boolValue!==void 0)return String(r.boolValue)}function Te(e){return(e.resource?.attributes?.find(e=>e.key===`service.name`))?.value?.stringValue||`unknown-service`}function Ee(e){return e===void 0?`info`:e<=4?`trace`:e<=8?`debug`:e<=12?`info`:e<=16?`warn`:e<=20?`error`:`fatal`}function De(e,t){let n=H(e.timeUnixNano),r=e.severityText?.toLowerCase()||Ee(e.severityNumber),i=``;if(e.body?.stringValue)i=e.body.stringValue;else if(e.body?.kvlistValue){let t={};for(let n of e.body.kvlistValue.values){let e=n.value.stringValue??n.value.intValue??n.value.doubleValue??n.value.boolValue;t[n.key]=e}i=JSON.stringify(t)}let a={},o=null,s=null,c=null;if(e.attributes)for(let t of e.attributes){let e=t.value.stringValue??t.value.intValue??t.value.doubleValue??t.value.boolValue;t.key===`exception.type`?o=String(e):t.key===`exception.message`?s=String(e):t.key===`exception.stacktrace`?c=String(e):a[t.key]=e}return{timestamp:n,level:r,message:i||`[LOG] ${t}`,traceId:e.traceId??null,spanId:e.spanId??null,parentSpanId:null,service:t,hostname:null,pid:null,metadata:Object.keys(a).length>0?a:null,errorType:o,errorMessage:s,errorStack:c}}function Oe(e,t){let n=H(e.startTimeUnixNano),r=e.status?.code,i=`info`;r===2?i=`error`:r===1&&(i=`info`);let a=K(e.attributes,`exception.type`),o=K(e.attributes,`exception.message`),s=K(e.attributes,`exception.stacktrace`),c={};if(e.attributes)for(let t of e.attributes){let e=t.value.stringValue??t.value.intValue??t.value.doubleValue??t.value.boolValue;c[t.key]=e}return c.spanName=e.name,c.spanKind=e.kind,e.status?.message&&(c.statusMessage=e.status.message),{timestamp:n,level:i,message:`[SPAN] ${e.name}`,traceId:e.traceId,spanId:e.spanId,parentSpanId:e.parentSpanId??null,service:t,hostname:null,pid:null,metadata:c,errorType:a??null,errorMessage:o??null,errorStack:s??null}}function ke(e){let n=new u.Hono,r=e.get(t.p.LogStorageService);n.use(`*`,(0,d.cors)({origin:e=>e??null,credentials:!0,allowMethods:[`GET`,`POST`,`OPTIONS`],allowHeaders:[`Content-Type`,`Accept`,`Authorization`]})),n.get(`/health`,async e=>{try{return e.json({status:`healthy`,service:`log-sink-mcp-http`,serviceName:`log-sink-mcp-http`,timestamp:new Date().toISOString()})}catch(t){return e.json({status:`unhealthy`,error:t instanceof Error?t.message:String(t)},503)}});let i=(0,f.rateLimiter)({windowMs:60*1e3,limit:100,standardHeaders:`draft-6`,keyGenerator:e=>e.req.header(`x-forwarded-for`)||e.req.header(`x-real-ip`)||`unknown`});n.post(`/logs`,i,async e=>{try{let t=W(e.req.header(`content-length`));if(t)return e.json({success:!1,error:t},413);let n=await G(e),i=xe.safeParse(n);if(!i.success)return e.json({success:!1,error:`Validation failed`,details:i.error.issues},400);let{logs:a}=i.data,o=a.map(e=>({...e,timestamp:e.timestamp?new Date(e.timestamp):new Date})),s=await r.insertBatch(o);return e.json({success:!0,count:s.length,message:`Successfully inserted ${s.length} logs`},201)}catch(t){let n=t instanceof Error?t.message:String(t);return n.startsWith(`Invalid JSON body`)?e.json({success:!1,error:n},400):(console.error(`Failed to insert logs:`,t),e.json({success:!1,error:`Failed to insert logs`},500))}}),n.post(`/v1/traces`,i,async e=>{try{let t=W(e.req.header(`content-length`));if(t)return e.json({success:!1,error:t},413);let n=await G(e);Se(n);let i=[];for(let e of n.resourceSpans){let t=we(e);for(let n of e.scopeSpans)for(let e of n.spans){let n=Oe(e,t);i.push(n)}}let a=await r.insertBatch(i);return e.json({success:!0,count:a.length,message:`Successfully inserted ${a.length} trace spans as logs`},201)}catch(t){let n=t instanceof Error?t.message:String(t);return n.startsWith(`Invalid JSON body`)||n.startsWith(`Invalid OTLP`)||n.startsWith(`OTLP`)?e.json({success:!1,error:n},400):(console.error(`Failed to insert OTLP traces:`,t),e.json({success:!1,error:`Failed to insert OTLP traces`},500))}}),n.post(`/v1/logs`,i,async e=>{try{let t=W(e.req.header(`content-length`));if(t)return e.json({success:!1,error:t},413);let n=await G(e);Ce(n);let i=[];for(let e of n.resourceLogs){let t=Te(e);for(let n of e.scopeLogs)for(let e of n.logRecords){let n=De(e,t);i.push(n)}}let a=await r.insertBatch(i);return e.json({success:!0,count:a.length,message:`Successfully inserted ${a.length} OTLP logs`},201)}catch(t){let n=t instanceof Error?t.message:String(t);return n.startsWith(`Invalid JSON body`)||n.startsWith(`Invalid OTLP`)||n.startsWith(`OTLP`)?e.json({success:!1,error:n},400):(console.error(`Failed to insert OTLP logs:`,t),e.json({success:!1,error:`Failed to insert OTLP logs`},500))}});let a=v(e);n.route(`/api`,a);let o=_e(e);return n.route(`/`,o),n}const Ae=256*1024*1024,je=.9,Me=300*1e3,Ne=1e3;function q(e,t){if(e===void 0||e===``)return t;let n=Number.parseInt(e,10);if(!Number.isFinite(n)||n<=0)throw Error(`Invalid database retention value: ${e}`);return n}function J(e={}){let t=q(e.dbMaxBytes??process.env.LOG_SINK_DB_MAX_BYTES,268435456),n=q(e.dbTargetBytes??process.env.LOG_SINK_DB_TARGET_BYTES,Math.floor(t*.9));return{maxBytes:t,targetBytes:Math.min(n,t),intervalMs:q(e.dbRetentionIntervalMs??process.env.LOG_SINK_DB_RETENTION_INTERVAL_MS,3e5),batchSize:q(e.dbRetentionBatchSize??process.env.LOG_SINK_DB_RETENTION_BATCH_SIZE,1e3)}}const Pe=String(o.DEFAULT_PORT_RANGE.min),Fe=t.d(),Ie=new a.Command(`http-serve`).description(`Start HTTP server for log ingestion with configurable port, database path, and in-memory mode`).option(`-p, --port <port>`,`Port to listen on`,Pe).option(`--db-path <path>`,`Path to SQLite database file`,Fe).option(`--in-memory`,`Use in-memory database (for testing)`,!1).option(`--db-max-bytes <bytes>`,`Maximum SQLite database size before trimming`).option(`--db-target-bytes <bytes>`,`Target SQLite database size after trimming`).option(`--db-retention-interval-ms <ms>`,`How often to check database size`,`300000`).option(`--db-retention-batch-size <count>`,`Number of oldest log rows to delete per trim batch`,`1000`).option(`--registry-path <path>`,`Custom registry path or directory for service discovery`).action(async e=>{try{let n=Number.parseInt(e.port,10),r={min:3e3,max:Math.max(4999,n)},i=process.env.NODE_ENV||`development`,a=t.f(process.cwd()),l=`log-sink-mcp-http`;e.registryPath&&(process.env.PORT_REGISTRY_PATH=e.registryPath,process.env.PROCESS_REGISTRY_PATH=(0,s.resolveSiblingRegistryPath)(e.registryPath,`processes.json`)??process.env.PROCESS_REGISTRY_PATH);let u=new o.PortRegistryService(process.env.PORT_REGISTRY_PATH);console.log(`🚀 Starting HTTP server for log ingestion...`),console.log(` Port: ${n}`),console.log(` Database: ${e.inMemory?`In-Memory`:e.dbPath}`),console.log(` Registry path: ${process.env.PORT_REGISTRY_PATH??`${process.env.HOME}/.port-registry/ports.json`}`);let d=t.u(),f=d.get(t.p.LogStorageService),p=d.get(t.p.LogRetentionService);await f.initializeDatabase(e.dbPath,e.inMemory);let m=p.startSizeRetentionMonitor(J({dbMaxBytes:e.dbMaxBytes,dbTargetBytes:e.dbTargetBytes,dbRetentionIntervalMs:e.dbRetentionIntervalMs,dbRetentionBatchSize:e.dbRetentionBatchSize}));console.log(`✓ Database initialized`);let h=ke(d),g=await u.reservePort({repositoryPath:a,serviceName:l,serviceType:`tool`,environment:i,preferredPort:n,pid:process.pid,host:`127.0.0.1`,force:!0,portRange:r,metadata:{healthCheckUrl:`http://localhost:${n}/health`,dbPath:e.dbPath}});if(!g.success||!g.record)throw Error(g.error||`Failed to reserve service in global registry`);let _=g.record.port;if(_!==n){let t=await u.reservePort({repositoryPath:a,serviceName:l,serviceType:`tool`,environment:i,preferredPort:_,pid:process.pid,host:`127.0.0.1`,force:!0,portRange:r,metadata:{healthCheckUrl:`http://localhost:${_}/health`,dbPath:e.dbPath}});if(!t.success||!t.record)throw Error(t.error||`Failed to update service metadata in registry`);g=t}if(!g.record)throw Error(`Port reservation lost before startup`);let v=g.record,y=v.port,b=await(0,s.createProcessLease)({repositoryPath:a,serviceName:l,serviceType:`tool`,environment:i,pid:process.pid,port:y,host:`127.0.0.1`,command:process.argv[1],args:process.argv.slice(2),metadata:{dbPath:e.dbPath,transport:`http`}}),ee=`http://localhost:${y}/health`,x;try{x=(0,c.serve)({fetch:h.fetch,port:y})}catch(e){try{await b.release({kill:!1})}catch{}throw e}console.log(`✓ HTTP server listening on http://localhost:${y}`),console.log(` Health check: ${ee}`),console.log(` Log ingestion: POST http://localhost:${y}/logs`);let te=v.port,ne=v.serviceName||l;console.log(`✓ Service registered: ${ne} on ${te}`),console.log(`
|
|
875
|
+
Press Ctrl+C to stop the server`);let S=async e=>{console.log(`\n\n${e} received. Shutting down gracefully...`);try{x.close(),console.log(`✓ Server closed`);try{await b.release({kill:!1}),console.log(`✓ Process deregistered`)}catch{}m(),await f.disconnect(),console.log(`✓ Database disconnected`),console.log(`Goodbye! 👋`),process.exit(0)}catch(e){console.error(`Error during shutdown:`,e),process.exit(1)}};process.on(`SIGINT`,()=>S(`SIGINT`)),process.on(`SIGTERM`,()=>S(`SIGTERM`))}catch(e){console.error(`Error starting HTTP server:`,e),process.exit(1)}}),Le=t.d(),Y=new Set(Object.values(g)),Re=new Set([`level`,`service`,`both`]),ze={stdout:e=>console.log(e),stderr:e=>console.error(e)};function X(e){let t=Number.parseInt(e,10);if(!Number.isInteger(t)||t<=0)throw new a.InvalidArgumentError(`Expected a positive integer.`);return t}function Be(e){if(!Re.has(e))throw new a.InvalidArgumentError(`Expected one of: 'level', 'service', 'both'.`);return e}function Z(e){if(!(!e||e.length===0))return e.map(e=>{if(!Y.has(e))throw new a.InvalidArgumentError(`Invalid log level '${e}'. Expected one of: ${Array.from(Y).join(`, `)}`);return e})}function Ve(e){return e.content.map(e=>e.type===`text`?e.text:JSON.stringify(e)).join(`
|
|
876
|
+
`)}function Q(e){return e.option(`--db-path <path>`,`Path to SQLite database file`,Le).option(`--in-memory`,`Use in-memory database`,!1)}async function He(e,n){let r=t.u(),i=r.get(t.p.LogStorageService);await i.initializeDatabase(e.dbPath,e.inMemory);try{return await n(r)}finally{await i.disconnect()}}async function Ue(e,t,n,r=ze){try{let i=await He(e,async e=>t(e).execute(n)),a=Ve(i);return i.isError?(r.stderr(a),1):(r.stdout(a),0)}catch(e){return r.stderr(`Error executing logs command: ${e instanceof Error?e.message:String(e)}`),1}}async function $(e,t,n){let r=await Ue(e,t,n);r!==0&&process.exit(r)}function We(e,t){if(!(!e&&!t)){if(!e||!t)throw new a.InvalidArgumentError(`Provide both --start-time and --end-time together.`);return{start:e,end:t}}}function Ge(){let e=new a.Command(`logs`).description(`Run log analysis commands that mirror MCP tool capabilities`),n=Q(new a.Command(`query`).alias(`query-logs`).description(`Query and filter log entries by level, trace ID, service, or time range`).option(`--level <level...>`,`Log level(s) to filter by`).option(`--trace-id <traceId>`,`Trace ID to filter by`).option(`--service <service...>`,`Service name(s) to filter by`).option(`--start-time <iso8601>`,`Start time for log range (ISO 8601)`).option(`--end-time <iso8601>`,`End time for log range (ISO 8601)`).option(`--limit <number>`,`Maximum number of log entries to return`,X).action(async e=>{await $(e,e=>new t.i(e),{level:Z(e.level),traceId:e.traceId,service:e.service,startTime:e.startTime,endTime:e.endTime,limit:e.limit})})),r=Q(new a.Command(`search`).alias(`search-logs`).description(`Search log entries with full-text search and optional filters`).argument(`<search-query>`,`FTS5 search query`).option(`--mode <mode>`,`Search strategy: fts, semantic, or hybrid`,`hybrid`).option(`--service <service...>`,`Service name(s) to filter by`).option(`--level <level...>`,`Log level(s) to filter by`).option(`--start-time <iso8601>`,`Start time for log range (ISO 8601)`).option(`--end-time <iso8601>`,`End time for log range (ISO 8601)`).option(`--limit <number>`,`Maximum number of search results to return`,X).action(async(e,n)=>{await $(n,e=>new t.r(e),{searchQuery:e,mode:n.mode,service:n.service,level:Z(n.level),startTime:n.startTime,endTime:n.endTime,limit:n.limit})})),i=Q(new a.Command(`trace`).alias(`get-trace-timeline`).description(`Get the complete trace timeline for a trace ID`).argument(`<trace-id>`,`Trace ID to inspect`).action(async(e,n)=>{await $(n,e=>new t.a(e),{traceId:e})})),o=Q(new a.Command(`analyze-errors`).alias(`errors`).description(`Analyze error patterns and group similar errors`).option(`--trace-id <traceId>`,`Optional trace ID to scope the analysis`).option(`--start-time <iso8601>`,`Start time for error analysis (ISO 8601)`).option(`--end-time <iso8601>`,`End time for error analysis (ISO 8601)`).option(`--limit <number>`,`Maximum number of error entries to analyze`,X).action(async e=>{await $(e,e=>new t.l(e),{traceId:e.traceId,timeRange:We(e.startTime,e.endTime),limit:e.limit})})),s=Q(new a.Command(`stats`).alias(`get-log-stats`).description(`Get aggregated log statistics grouped by level, service, or both`).option(`--start-time <iso8601>`,`Start time for statistics (ISO 8601)`).option(`--end-time <iso8601>`,`End time for statistics (ISO 8601)`).option(`--group-by <groupBy>`,`Group statistics by level, service, or both`,Be).action(async e=>{await $(e,e=>new t.s(e),{startTime:e.startTime,endTime:e.endTime,groupBy:e.groupBy})})),c=Q(new a.Command(`services`).alias(`get-services`).description(`Get the list of unique services present in the log database`).action(async e=>{await $(e,e=>new t.o(e),{})})),l=Q(new a.Command(`clear`).alias(`clear-logs`).description(`Clear all logs from the database`).action(async e=>{await $(e,e=>new t.c(e),{})}));return e.addCommand(n).addCommand(r).addCommand(i).addCommand(o).addCommand(s).addCommand(c).addCommand(l)}const Ke=Ge();function qe(){return t.d()}const Je=new a.Command(`mcp-serve`).description(`Start MCP server with stdio transport`).option(`--cleanup`,`Stop HTTP server on MCP server shutdown`,!1).option(`--db-path <path>`,`Path to SQLite database file`,qe()).option(`--in-memory`,`Use in-memory database (for testing)`,!1).option(`--db-max-bytes <bytes>`,`Maximum SQLite database size before trimming`).option(`--db-target-bytes <bytes>`,`Target SQLite database size after trimming`).option(`--db-retention-interval-ms <ms>`,`How often to check database size`,`300000`).option(`--db-retention-batch-size <count>`,`Number of oldest log rows to delete per trim batch`,`1000`).option(`--registry-path <path>`,`Custom registry path or directory for service discovery`).option(`--registry-dir <path>`,`Custom registry directory for service discovery`).action(async e=>{let n;try{let r=e.registryPath||e.registryDir;r&&(process.env.PORT_REGISTRY_PATH=r,process.env.PROCESS_REGISTRY_PATH=(0,s.resolveSiblingRegistryPath)(r,`processes.json`)??process.env.PROCESS_REGISTRY_PATH);let i=t.u(),a=i.get(t.p.LogStorageService),o=i.get(t.p.LogRetentionService),c=i.get(t.p.HttpServerManager);await a.initializeDatabase(e.dbPath,e.inMemory);let l=o.startSizeRetentionMonitor(J({dbMaxBytes:e.dbMaxBytes,dbTargetBytes:e.dbTargetBytes,dbRetentionIntervalMs:e.dbRetentionIntervalMs,dbRetentionBatchSize:e.dbRetentionBatchSize})),u=await c.ensureRunning(3100,e.dbPath);u.running||console.error(`Warning: HTTP server failed to start: ${u.error}`);let d=new t.t(t.n(i));n=await(0,s.createProcessLease)({repositoryPath:process.cwd(),serviceName:`log-sink-mcp-mcp-serve`,serviceType:`tool`,environment:process.env.NODE_ENV||`development`,host:`127.0.0.1`,command:process.argv[1],args:process.argv.slice(2),metadata:{transport:`stdio`,command:`mcp-serve`}});let f=async t=>{console.error(`\nReceived ${t}, shutting down gracefully...`);try{await d.stop(),e.cleanup&&u.running&&(await c.stop(),console.error(`HTTP server stopped`)),l(),await a.disconnect(),await n?.release(),process.exit(0)}catch(e){console.error(`Error during shutdown:`,e),process.exit(1)}};process.on(`SIGINT`,()=>f(`SIGINT`)),process.on(`SIGTERM`,()=>f(`SIGTERM`)),await d.start()}catch(e){await n?.release().catch(()=>void 0),console.error(`Failed to start MCP server:`,e),process.exit(1)}}),Ye=new a.Command(`start`).description(`Start HTTP and/or MCP servers with singleton coordination`).option(`--mcp-only`,`Start only the MCP server (stdio transport)`,!1).option(`--http-only`,`Start only the HTTP server`,!1).option(`-p, --port <port>`,`Port for HTTP server`,String(o.DEFAULT_PORT_RANGE.min)).option(`--db-path <path>`,`Path to SQLite database file`,t.d()).option(`--in-memory`,`Use in-memory database (for testing)`,!1).option(`--db-max-bytes <bytes>`,`Maximum SQLite database size before trimming`).option(`--db-target-bytes <bytes>`,`Target SQLite database size after trimming`).option(`--db-retention-interval-ms <ms>`,`How often to check database size`,`300000`).option(`--db-retention-batch-size <count>`,`Number of oldest log rows to delete per trim batch`,`1000`).option(`--registry-path <path>`,`Custom registry path or directory for service discovery`).option(`--registry-dir <path>`,`Custom registry directory for service discovery`).action(async e=>{let n;try{let r=e.registryPath||e.registryDir;r&&(process.env.PORT_REGISTRY_PATH=r,process.env.PROCESS_REGISTRY_PATH=(0,s.resolveSiblingRegistryPath)(r,`processes.json`)??process.env.PROCESS_REGISTRY_PATH),e.dbMaxBytes&&(process.env.LOG_SINK_DB_MAX_BYTES=e.dbMaxBytes),e.dbTargetBytes&&(process.env.LOG_SINK_DB_TARGET_BYTES=e.dbTargetBytes),e.dbRetentionIntervalMs&&(process.env.LOG_SINK_DB_RETENTION_INTERVAL_MS=e.dbRetentionIntervalMs),e.dbRetentionBatchSize&&(process.env.LOG_SINK_DB_RETENTION_BATCH_SIZE=e.dbRetentionBatchSize);let i=t.u(),a=i.get(t.p.LogStorageService);await a.initializeDatabase(e.dbPath,e.inMemory);let o=Number.parseInt(e.port,10),c=!e.mcpOnly,l=!e.httpOnly,u=i.get(t.p.LogRetentionService),d=l?u.startSizeRetentionMonitor(J({dbMaxBytes:e.dbMaxBytes,dbTargetBytes:e.dbTargetBytes,dbRetentionIntervalMs:e.dbRetentionIntervalMs,dbRetentionBatchSize:e.dbRetentionBatchSize})):()=>void 0;if(console.log(`🚀 Starting log-sink-mcp services...`),console.log(` Database: ${e.inMemory?`In-Memory`:e.dbPath}`),c){let n=await i.get(t.p.HttpServerManager).ensureRunning(o,e.dbPath);n.running?(console.log(`✓ HTTP Server: Running on http://localhost:${n.port} (PID: ${n.pid})`),console.log(` Health check: http://localhost:${n.port}/health`),console.log(` Log ingestion: POST http://localhost:${n.port}/logs`)):(console.error(`✗ HTTP Server failed to start: ${n.error}`),l||process.exit(1))}if(l){n=await(0,s.createProcessLease)({repositoryPath:process.cwd(),serviceName:`log-sink-mcp-start`,serviceType:`tool`,environment:process.env.NODE_ENV||`development`,host:`127.0.0.1`,command:process.argv[1],args:process.argv.slice(2),metadata:{transport:`stdio`,command:`start`}}),console.log(`✓ MCP Server: Starting with stdio transport...`);let e=new t.t(t.n(i));await e.start(),console.log(`✓ MCP Server: Ready for connections`);let r=async r=>{console.log(`\n\n${r} received. Shutting down gracefully...`);try{await e.stop(),console.log(`✓ MCP server stopped`),c&&(await i.get(t.p.HttpServerManager).stop(),console.log(`✓ HTTP server stopped`)),d(),await a.disconnect(),console.log(`✓ Database disconnected`),await n?.release(),console.log(`✓ Process registry entry released`),console.log(`Goodbye! 👋`),process.exit(0)}catch(e){console.error(`Error during shutdown:`,e),process.exit(1)}};process.on(`SIGINT`,()=>r(`SIGINT`)),process.on(`SIGTERM`,()=>r(`SIGTERM`)),console.log(`
|
|
877
877
|
Press Ctrl+C to stop the server`)}else console.log(`
|
|
878
|
-
✓ HTTP server started in background`),console.log(`Use "log-sink-mcp stop" to stop the HTTP server`),n&&await n.release().catch(()=>void 0),process.exit(0)}catch(e){n&&await n.release().catch(()=>void 0),console.error(`Error starting services:`,e),process.exit(1)}}),
|
|
879
|
-
`),console.log(`─`.repeat(50));let e=await t.u().get(t.p.HttpServerManager).getStatus();e.running?(console.log(`HTTP Server: 🟢 Running`),console.log(` PID: ${e.pid}`),console.log(` Port: ${e.port}`),console.log(` Health: http://localhost:${e.port}/health`)):(console.log(`HTTP Server: 🔴 Not Running`),e.error&&console.log(` Error: ${e.error}`)),console.log(``);try{let e=t.d(),n=((await l.stat(e)).size/1024/1024).toFixed(2);console.log(`Database: ${e} (${n} MB)`)}catch{console.log(`Database: Not found or not accessible`)}console.log(`─`.repeat(50)),process.exit(0)}catch(e){console.error(`Error getting status:`,e),process.exit(1)}}),
|
|
878
|
+
✓ HTTP server started in background`),console.log(`Use "log-sink-mcp stop" to stop the HTTP server`),n&&await n.release().catch(()=>void 0),process.exit(0)}catch(e){n&&await n.release().catch(()=>void 0),console.error(`Error starting services:`,e),process.exit(1)}}),Xe=new a.Command(`status`).description(`Show status of HTTP server and diagnostics`).action(async()=>{try{console.log(`📊 log-sink-mcp Status
|
|
879
|
+
`),console.log(`─`.repeat(50));let e=await t.u().get(t.p.HttpServerManager).getStatus();e.running?(console.log(`HTTP Server: 🟢 Running`),console.log(` PID: ${e.pid}`),console.log(` Port: ${e.port}`),console.log(` Health: http://localhost:${e.port}/health`)):(console.log(`HTTP Server: 🔴 Not Running`),e.error&&console.log(` Error: ${e.error}`)),console.log(``);try{let e=t.d(),n=((await l.stat(e)).size/1024/1024).toFixed(2);console.log(`Database: ${e} (${n} MB)`)}catch{console.log(`Database: Not found or not accessible`)}console.log(`─`.repeat(50)),process.exit(0)}catch(e){console.error(`Error getting status:`,e),process.exit(1)}}),Ze=new a.Command(`stop`).description(`Stop HTTP server and clean up registry/PID files`).action(async()=>{try{console.log(`🛑 Stopping log-sink-mcp services...`),await t.u().get(t.p.HttpServerManager).stop()?(console.log(`✓ HTTP server stopped`),console.log(`✓ Registry and PID files cleaned up`)):console.log(`ℹ No HTTP server was running`),console.log(`Done! 👋`),process.exit(0)}catch(e){console.error(`Error stopping services:`,e),process.exit(1)}}),Qe=(0,r.dirname)((0,i.fileURLToPath)(require(`url`).pathToFileURL(__filename).href)),$e=JSON.parse((0,n.readFileSync)((0,r.join)(Qe,`../package.json`),`utf-8`));async function et(){let e=new a.Command;e.name(`log-sink-mcp`).description(`Log sink MCP server with HTTP ingestion and AI analysis`).version($e.version),e.addCommand(Ye),e.addCommand(Ze),e.addCommand(Xe),e.addCommand(Ie),e.addCommand(Je),e.addCommand(Ke),await e.parseAsync(process.argv)}et();
|
|
880
880
|
//# sourceMappingURL=cli.cjs.map
|