import{t as e}from"./chunk.document_DMcRsBXN.esm.js";import{n as t,r as i,t as a}from"./chunk.tslib-es6_CHHZATl5.esm.js";function s(e,t){if(!{}.hasOwnProperty.call(e,t))throw new TypeError("attempted to use private field on non-instance");return e}var r=0;function o(e){return"__private_"+r+++"_"+e}function n(e){return Object.entries(e).map((([e,t])=>({key:e,value:{stringValue:String(t)}})))}function u(e){if(Array.isArray(e))return{arrayValue:{values:e.map((e=>u(e)))}};switch(typeof e){case"boolean":return{boolValue:Boolean(e)};case"number":return{doubleValue:Number(e)};default:return{stringValue:String(e)}}}const l=function(e,t,i){const a=[0];for(let s=0;s{e.attributes={...s(this,h)[h],...e.attributes}}));const e=s(this,d)[d];s(this,d)[d]=[],await this.exportByKeepalive(e,((e,t)=>s(this,c)[c].exportMetrics(this.aggregateMetrics(e),t)))}async exportLogs(){const e=s(this,p)[p];s(this,p)[p]=[],await this.exportByKeepalive(e,((e,t)=>s(this,c)[c].exportLogs(this.formatLogs(e),t)))}aggregateMetrics(e){const t={};return e.forEach((e=>{switch(e.type){case"histogram":!function(e,t){var i;const{name:a,value:s,unit:r,timeUnixNano:o,attributes:u}=t,c=null!==(i=t.bounds)&&void 0!==i?i:l,h=new Array(c.length+1).fill(0);e[a]||={name:a,unit:r||"1",histogram:{aggregationTemporality:1,dataPoints:[]}};for(let e=0;e0?d:0,bucketCounts:a>0?[1]:[]};e[i].exponentialHistogram.dataPoints.push({attributes:n(null!=o?o:{}),startTimeUnixNano:r,timeUnixNano:r,count:1,sum:l,scale:c,zeroCount:p,positive:v,negative:m,min:l,max:l,zeroThreshold:0})}(t,e);break;case"counter":!function(e,t){const{name:i,value:a,unit:s,timeUnixNano:r,attributes:o}=t;e[i]||={name:i,unit:s||"1",sum:{aggregationTemporality:1,isMonotonic:!0,dataPoints:[]}},e[i].sum.dataPoints.push({startTimeUnixNano:r,timeUnixNano:r,asDouble:a,attributes:n(null!=o?o:{})})}(t,e);break;case"gauge":!function(e,t){const{name:i,value:a,unit:s,timeUnixNano:r,attributes:o}=t;e[i]||={name:i,unit:s||"1",gauge:{dataPoints:[]}},e[i].gauge.dataPoints.push({startTimeUnixNano:r,timeUnixNano:r,asDouble:a,attributes:n(null!=o?o:{})})}(t,e)}})),Object.values(t)}async exportByKeepalive(e,t){if(0===e.length)return;const i=[],a=[];e.forEach((e=>{var t;null===(t=e.requiresKeepalive)||void 0===t||t?i.push(e):a.push(e)}));const s=[i.length>0?t(i,{keepalive:!0}):void 0,a.length>0?t(a,{keepalive:!1}):void 0].filter((e=>void 0!==e));await Promise.all(s)}formatLogs(e){return e.map((e=>{const t={timeUnixNano:e.timeUnixNano,observedTimeUnixNano:e.timeUnixNano,attributes:(i={...s(this,h)[h],...e.attributes},Object.entries(i).map((([e,t])=>({key:e,value:u(t)}))))};var i;return e.body&&(t.body={stringValue:e.body}),t}))}};var v,g,b=o("url"),f=o("serviceName"),y=o("logger"),x=o("fetchFn"),w=o("maxPayloadSizeBytes"),N=class{constructor(e,t,i){var a;Object.defineProperty(this,b,{writable:!0,value:void 0}),Object.defineProperty(this,f,{writable:!0,value:void 0}),Object.defineProperty(this,y,{writable:!0,value:void 0}),Object.defineProperty(this,x,{writable:!0,value:void 0}),Object.defineProperty(this,w,{writable:!0,value:void 0}),s(this,b)[b]=e.replace(/\/v1\/(logs|metrics|traces)\/?$/,""),s(this,f)[f]=t,s(this,y)[y]=null==i?void 0:i.logger,s(this,x)[x]=null==i?void 0:i.fetchFn,s(this,w)[w]=null!==(a=null==i?void 0:i.maxPayloadSizeBytes)&&void 0!==a?a:51200}async exportMetrics(e,t){var i;const a=null===(i=null==t?void 0:t.keepalive)||void 0===i||i;await this.exportBatches("/v1/metrics",[...e],(e=>({resourceMetrics:[{resource:{attributes:[{key:"service.name",value:{stringValue:s(this,f)[f]}}]},scopeMetrics:[{scope:{name:"open-telemetry-mini-client",version:"1.1.0",attributes:[]},metrics:e}]}]})),a)}async exportLogs(e,t){var i;const a=null===(i=null==t?void 0:t.keepalive)||void 0===i||i;await this.exportBatches("/v1/logs",[...e],(e=>({resourceLogs:[{resource:{attributes:[{key:"service.name",value:{stringValue:s(this,f)[f]}}]},scopeLogs:[{scope:{name:"open-telemetry-mini-client",version:"1.1.0",attributes:[]},logRecords:e}]}]})),a)}async exportTo(e,t,i){var a;const r=JSON.stringify(e),o=(new TextEncoder).encode(r).length;if(o>s(this,w)[w])throw new k(`Payload size ${o} exceeds ${s(this,w)[w]} bytes`);const n=await this.exporterFetch()(`${s(this,b)[b]}${t}`,{method:"POST",keepalive:i,headers:{"Content-Type":"application/json"},body:r});if(null===(a=s(this,y)[y])||void 0===a||a.log({status:n.status}),!n.ok){if(400===n.status){const e=await n.text();throw new P(`Invalid OpenTelemetry Data: ${e}`)}if(429===n.status||503===n.status){const t=await n.text(),i=n.headers.get("Retry-After"),a=i?{seconds:Number(i)}:void 0;throw new P("Server did not accept data",{errorData:t,retryAfter:a,body:e})}if(401===n.status||403===n.status){const t=await n.text();throw new T(`Authentication failed: ${n.status} ${401===n.status?"Unauthorized":"Forbidden"}`,{errorData:t,body:e})}throw new P(`Server responded with ${n.status}`)}}exporterFetch(){return s(this,x)[x]||fetch}async exportBatches(e,t,i,a){let s=t.length;for(;t.length>0;)try{const r=t.slice(0,s);await this.exportTo(i(r),e,a),t.splice(0,s)}catch(e){if(!(e instanceof k&&s>1))throw e;s=Math.ceil(s/2)}}},P=class extends Error{constructor(e,t){super(e),this.metadata=void 0,this.name="OpenTelemetryClientError",this.metadata=t}},k=class extends Error{constructor(...e){super(...e),this.name="PayloadTooLargeError"}},T=class extends Error{constructor(e,t){super(e),this.name="AuthenticationFailedError",this.metadata=void 0,this.name="AuthenticationFailedError",this.metadata=t}},U=class{constructor({exporter:e,getKeepalive:t}){v.set(this,void 0),g.set(this,void 0),i(this,v,e,"f"),i(this,g,t,"f")}exportMetrics(e,i){return a(this,void 0,void 0,(function*(){var a;try{yield t(this,v,"f").exportMetrics(e,Object.assign(Object.assign({},i),{keepalive:t(this,g,"f").call(this)}))}catch(t){if(t instanceof P){const s=null===(a=t.metadata)||void 0===a?void 0:a.retryAfter;if(s)return void(yield new Promise(((t,a)=>{setTimeout((()=>this.exportMetrics(e,i).then(t,a)),1e3*s.seconds)})))}throw t}}))}exportLogs(e,i){return a(this,void 0,void 0,(function*(){var a;try{yield t(this,v,"f").exportLogs(e,Object.assign(Object.assign({},i),{keepalive:t(this,g,"f").call(this)}))}catch(t){if(t instanceof P){const s=null===(a=t.metadata)||void 0===a?void 0:a.retryAfter;if(s)return void(yield new Promise(((t,a)=>{setTimeout((()=>this.exportLogs(e,i).then(t,a)),1e3*s.seconds)})))}throw t}}))}};v=new WeakMap,g=new WeakMap;const M={blockedRequest:"Blocked Request",emptyeEventCreatedAtMs:"event_created_at_ms metadata field cannot be empty",errorParsingCreatedAtMs:"Error parsing: X-Monorail-Edge-Event-Created-At-Ms",failedToReadRequestBody:"Failed to read request body",incorrectContentType:"Incorrect Content-Type. Expected: application/json or text/plain",methodNotAllowed:"Method Not Allowed",noPermissionToGetURL:"Your client does not have permission to get URL",noResponseFromEdge:"No response from edge",schemaValidationError:"Schema validation error"};function j(e){const t=Object.values(M).find((([t,i])=>e.message.includes(i)));return(null==t?void 0:t[0])||"otherErrors"}function O(){{const t=new N("https://otlp-http-production.shopifysvc.com/v1/metrics","shop-js");return new U({exporter:t,getKeepalive:()=>"readyState"in e&&"complete"===e.readyState})}}export{j as n,m as r,O as t}; //# sourceMappingURL=chunk.utils_DX-ZLLMT.esm.js.map