UNPKG

@mollitia/prometheus

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