UNPKG

@mollitia/prometheus

Version:
9 lines (8 loc) 9.9 kB
"use strict";var G=Object.defineProperty;var L=(s,e,t)=>e in s?G(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var p=(s,e,t)=>(L(s,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const I=require("mollitia");function Y(s){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(s){for(const t in s)if(t!=="default"){const r=Object.getOwnPropertyDescriptor(s,t);Object.defineProperty(e,t,r.get?r:{enumerable:!0,get:()=>s[t]})}}return e.default=s,Object.freeze(e)}const l=Y(I);var H=(s=>(s.COUNTER="counter",s.GAUGE="gauge",s))(H||{});class f{constructor(e,t){p(this,"key");p(this,"type");p(this,"labels");p(this,"values");p(this,"description");this.key=e,this.type=H.COUNTER,this.labels=(t==null?void 0:t.labels)||{},this.values={},t!=null&&t.description&&(this.description=t==null?void 0:t.description)}inc(e=1,t){return this.values[t]=this.values[t]||0,this.values[t]+=e,this.values[t]}reset(e){return this.values[e]=0,this.values[e]}scrapHelp(){let e=`# HELP ${this.key} ${this.description} `;return e+=`# TYPE ${this.key} ${this.type} `,e}scrapValues(){let e="";if(Object.keys(this.values).length)for(const t in this.values){let r=`circuit="${t}"`;for(const a in this.labels)r+=`, ${a}="${this.labels[a]}"`;e+=`${this.key}{${r}} ${this.values[t]} `}return e}}class E{constructor(e,t){p(this,"key");p(this,"type");p(this,"labels");p(this,"values");p(this,"description");this.key=e,this.type=H.GAUGE,this.labels=(t==null?void 0:t.labels)||{},this.values={},t!=null&&t.description&&(this.description=t==null?void 0:t.description)}inc(e=1,t){return this.values[t]=this.values[t]||0,this.values[t]+=e,this.values[t]}get(e){return this.values[e]}set(e,t){return this.values[t]=e,this.values[t]}scrapHelp(){let e=`# HELP ${this.key} ${this.description} `;return e+=`# TYPE ${this.key} ${this.type} `,e}scrapValues(){let e="";if(Object.keys(this.values).length)for(const t in this.values){let r=`circuit="${t}"`;for(const c in this.labels)r+=`, ${c}="${this.labels[c]}"`;const a=this.values[t]%1?this.values[t].toFixed(2):this.values[t];e+=`${this.key}{${r}} ${a} `}return this.values={},e}}const T=s=>{if(s.constructor.name===l.Circuit.name){const e=s;return e.prometheus.perMethod?`${e.prometheus.name}_${e.prometheus.funcName}`:s.prometheus.name}else return s.prometheus.name},g=(s,e)=>{var w,v,_,$,b,y,x,m,u,C,S,D,B,M,P,N;let t=(w=e.prometheus)==null?void 0:w.labels;s.constructor.name!==l.Circuit.name&&(t={...t,module:((v=e.prometheus)==null?void 0:v.name)||""});const r=new f(`${(_=e.prometheus)!=null&&_.prefix?`${($=e.prometheus)==null?void 0:$.prefix}_`:""}total_executions`,{description:"Total Executions",labels:t}),a=new f(`${(b=e.prometheus)!=null&&b.prefix?`${(y=e.prometheus)==null?void 0:y.prefix}_`:""}total_success`,{description:"Total Success",labels:t}),c=new f(`${(x=e.prometheus)!=null&&x.prefix?`${(m=e.prometheus)==null?void 0:m.prefix}_`:""}total_failures`,{description:"Total Failures",labels:t}),i={},n=new E(`${(u=e.prometheus)!=null&&u.prefix?`${(C=e.prometheus)==null?void 0:C.prefix}_`:""}duration`,{description:"Maximum Duration of Circuit Execution",labels:{...t,metricType:"MAX"}}),o=new E(`${(S=e.prometheus)!=null&&S.prefix?`${(D=e.prometheus)==null?void 0:D.prefix}_`:""}duration`,{description:"Average Duration of Circuit Execution",labels:{...t,metricType:"AVG"}}),d=new E(`${(B=e.prometheus)!=null&&B.prefix?`${(M=e.prometheus)==null?void 0:M.prefix}_`:""}duration`,{description:"Minimum Duration of Circuit Execution",labels:{...t,metricType:"MIN"}}),k=new E(`${(P=e.prometheus)!=null&&P.prefix?`${(N=e.prometheus)==null?void 0:N.prefix}_`:""}duration`,{description:"Count of Circuit Execution",labels:{...t,metricType:"COUNT"}});return s.on("execute",(O,U)=>{const h=T(O);i[h]=i[h]||0;const q=Date.now();r.inc(1,h),U.then(()=>{const V=k.inc(1,h);V===1&&(i[h]=0);const R=Date.now()-q;i[h]+=R;const W=d.get(h);(!W||W>R)&&d.set(R,h);const j=n.get(h);(!j||j<R)&&n.set(R,h),o.set(i[h]/V,h)}).catch(()=>{c.inc(1,h)})}),{total_executions:r,total_success:a,total_failures:c,duration_max:n,duration_ave:o,duration_min:d,duration_count:k}},z=(s,e)=>g(s,e),X=(s,e)=>{var c,i,n,o;const t=g(s,e),r={...(c=e.prometheus)==null?void 0:c.labels,module:((i=e.prometheus)==null?void 0:i.name)||""},a=new f(`${(n=e.prometheus)!=null&&n.prefix?`${(o=e.prometheus)==null?void 0:o.prefix}_`:""}total_failures_timeout`,{description:"Total Timeout Failures",labels:r});return s.on("timeout",()=>{a.inc(1,s.prometheus.name)}),{...t,total_failures_timeout:a}},J=(s,e)=>{var o,d,k,w,v,_,$,b,y,x;const t=g(s,e),r={...(o=e.prometheus)==null?void 0:o.labels,module:((d=e.prometheus)==null?void 0:d.name)||""},a=new f(`${(k=e.prometheus)!=null&&k.prefix?`${(w=e.prometheus)==null?void 0:w.prefix}_`:""}success_without_retries`,{description:"Success Without Retries",labels:r}),c=new f(`${(v=e.prometheus)!=null&&v.prefix?`${(_=e.prometheus)==null?void 0:_.prefix}_`:""}success_with_retries`,{description:"Success With Retries",labels:r}),i=new f(`${($=e.prometheus)!=null&&$.prefix?`${(b=e.prometheus)==null?void 0:b.prefix}_`:""}failures_without_retries`,{description:"Failures Without Retries",labels:r}),n=new f(`${(y=e.prometheus)!=null&&y.prefix?`${(x=e.prometheus)==null?void 0:x.prefix}_`:""}failures_with_retries`,{description:"Failures With Retries",labels:r});return s.on("success-without-retry",m=>{const u=T(m);a.inc(1,u)}),s.on("success-with-retry",m=>{const u=T(m);c.inc(1,u)}),s.on("failure-without-retry",m=>{const u=T(m);i.inc(1,u)}),s.on("failure-with-retry",m=>{const u=T(m);n.inc(1,u)}),{...t,success_without_retries:a,success_with_retries:c,failures_without_retries:i,failures_with_retries:n}},K=(s,e)=>g(s,e),Q=(s,e)=>g(s,e),Z=(s,e)=>g(s,e),ee=(s,e)=>g(s,e);function te(s){throw new Error(s)}const se=(s,e)=>{var c,i,n,o,d,k,w;const t=g(s,e),r={...(c=e.prometheus)==null?void 0:c.labels,module:((i=e.prometheus)==null?void 0:i.name)||""};let a={...t};if(s instanceof l.SlidingWindowBreaker){let v=function(m){const u=e.prometheus.name;switch(_.reset(u),$.reset(u),b.reset(u),m){case l.BreakerState.CLOSED:{_.inc(1,u);break}case l.BreakerState.HALF_OPENED:{$.inc(1,u);break}case l.BreakerState.OPENED:{b.inc(1,u);break}default:te(m)}};const _=new f(`${(n=e.prometheus)!=null&&n.prefix?`${e.prometheus.prefix}_`:""}breaker_state`,{description:"Breaker State",labels:{...r,state:"closed"}}),$=new f(`${(o=e.prometheus)!=null&&o.prefix?`${e.prometheus.prefix}_`:""}breaker_state`,{description:"Breaker State",labels:{...r,state:"half-opened"}}),b=new f(`${(d=e.prometheus)!=null&&d.prefix?`${e.prometheus.prefix}_`:""}breaker_state`,{description:"Breaker State",labels:{...r,state:"opened"}}),y=new E(`${(k=e.prometheus)!=null&&k.prefix?`${e.prometheus.prefix}_`:""}failure_rate`,{description:"Failure Rate (%)",labels:r}),x=new E(`${(w=e.prometheus)!=null&&w.prefix?`${e.prometheus.prefix}_`:""}slow_rate`,{description:"Slow Rate (%)",labels:r});v(s.state??l.BreakerState.CLOSED),s.on("state-changed",v),s.on("execute",async(m,u)=>{const C=T(s);try{await u}catch{}finally{const S=await s.getState(),D=S.requests.filter(({result:O})=>O===l.SlidingWindowRequestResult.FAILURE).length,B=S.requests.filter(({result:O})=>O===l.SlidingWindowRequestResult.TIMEOUT).length,M=S.requests.length,P=M>0?D/M*100:0,N=M>0?B/M*100:0;y.set(P,C),x.set(N,C)}}),a={...t,closed_breaker_state:_,half_opened_breaker_state:$,opened_breaker_state:b,failure_rate:y,slow_rate:x}}return a},re="0.0.9",F=[],A=[],ae=()=>{const s={};for(const e of F){const t=e.prometheus.name;for(const r in e.prometheus.metrics)s[r]?s[r].circuits[t]=e.prometheus.metrics[r].values[t]:s[r]={circuits:{[t]:e.prometheus.metrics[r].values[t]||0},modules:{}}}for(const e of A){const t=e.prometheus.name;for(const r in e.prometheus.metrics)Object.keys(e.prometheus.metrics[r].values).length&&(s[r]?s[r].modules[t]=e.prometheus.metrics[r].values:s[r]={circuits:{},modules:{[t]:e.prometheus.metrics[r].values}})}return s},ce=()=>{const s={};for(const r of F)for(const a in r.prometheus.metrics)s[a]?s[a].value+=r.prometheus.metrics[a].scrapValues():s[a]={key:r.prometheus.metrics[a].key,help:r.prometheus.metrics[a].scrapHelp(),value:r.prometheus.metrics[a].scrapValues()};for(const r of A)for(const a in r.prometheus.metrics)s[a]?s[a].value+=r.prometheus.metrics[a].scrapValues():s[a]={key:r.prometheus.metrics[a].key,help:r.prometheus.metrics[a].scrapHelp(),value:r.prometheus.metrics[a].scrapValues()};let e="";const t=new Set;for(const r in s){const a=s[r].key;t.has(a)||(e+=s[r].help,t.add(a)),e+=`${s[r].value} `}return e};class ie{onCircuitCreate(e,t){if(t.prometheus){e.prometheus={name:t.prometheus.name,perMethod:t.prometheus.perMethod?t.prometheus.perMethod:!1,labels:t.prometheus.labels||{},metrics:z(e,t),scrap:()=>{let a="";const c=new Set;for(const i in e.prometheus.metrics){const n=e.prometheus.metrics[i].key;c.has(n)||(a+=e.prometheus.metrics[i].scrapHelp(),c.add(n)),a+=e.prometheus.metrics[i].scrapValues()}return a}};const r=e.fn.bind(e);e.fn=(a,c)=>(e.prometheus.funcName=c||a.name,r(a,c)),F.push(e)}}onModuleCreate(e,t){if(t.prometheus){let r;switch(e.constructor.name){case l.Timeout.name:{r=X;break}case l.Retry.name:{r=J;break}case l.Bulkhead.name:{r=K;break}case l.Cache.name:{r=Q;break}case l.Ratelimit.name:{r=Z;break}case l.Fallback.name:{r=ee;break}case l.SlidingWindowBreakerOptions.name:case l.SlidingCountBreaker.name:case l.SlidingTimeBreaker.name:{r=se;break}default:r=()=>({})}e.prometheus={name:t.prometheus.name,labels:t.prometheus.labels||{},metrics:r(e,t),scrap:()=>{let a="";const c=new Set;for(const i in e.prometheus.metrics){const n=e.prometheus.metrics[i].key;c.has(n)||(a+=e.prometheus.metrics[i].scrapHelp(),c.add(n)),a+=e.prometheus.metrics[i].scrapValues()}return a}},A.push(e)}}}exports.PrometheusAddon=ie;exports.circuits=F;exports.metrics=ae;exports.modules=A;exports.scrap=ce;exports.version=re;