import{t as e}from"./chunk.document_DMcRsBXN.esm.js";function t(e,t){if(!{}.hasOwnProperty.call(e,t))throw new TypeError("attempted to use private field on non-instance");return e}var i=0;function a(e){return"__private_"+i+++"_"+e}function r(e){return Object.entries(e).map((([e,t])=>({key:e,value:{stringValue:String(t)}})))}function s(e){if(Array.isArray(e))return{arrayValue:{values:e.map((e=>s(e)))}};switch(typeof e){case"boolean":return{boolValue:Boolean(e)};case"number":return{doubleValue:Number(e)};default:return{stringValue:String(e)}}}const o=function(e,t,i){const a=[0];for(let r=0;r{e.attributes={...t(this,u)[u],...e.attributes}}));const e=t(this,l)[l];t(this,l)[l]=[],await this.exportByKeepalive(e,((e,i)=>t(this,n)[n].exportMetrics(this.aggregateMetrics(e),i)))}async exportLogs(){const e=t(this,c)[c];t(this,c)[c]=[],await this.exportByKeepalive(e,((e,i)=>t(this,n)[n].exportLogs(this.formatLogs(e),i)))}aggregateMetrics(e){const t={};return e.forEach((e=>{switch(e.type){case"histogram":!function(e,t){var i;const{name:a,value:s,unit:n,timeUnixNano:u,attributes:l}=t,c=null!==(i=t.bounds)&&void 0!==i?i:o,h=new Array(c.length+1).fill(0);e[a]||(e[a]={name:a,unit:n||"1",histogram:{aggregationTemporality:1,dataPoints:[]}});for(let e=0;e0?p:0,bucketCounts:a>0?[1]:[]};e[i].exponentialHistogram.dataPoints.push({attributes:r(null!=n?n:{}),startTimeUnixNano:o,timeUnixNano:o,count:1,sum:l,scale:c,zeroCount:d,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:o,attributes:n}=t;e[i]||(e[i]={name:i,unit:s||"1",sum:{aggregationTemporality:1,isMonotonic:!0,dataPoints:[]}}),e[i].sum.dataPoints.push({startTimeUnixNano:o,timeUnixNano:o,asDouble:a,attributes:r(null!=n?n:{})})}(t,e);break;case"gauge":!function(e,t){const{name:i,value:a,unit:s,timeUnixNano:o,attributes:n}=t;e[i]||(e[i]={name:i,unit:s||"1",gauge:{dataPoints:[]}}),e[i].gauge.dataPoints.push({startTimeUnixNano:o,timeUnixNano:o,asDouble:a,attributes:r(null!=n?n:{})})}(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 r=[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(r)}formatLogs(e){return e.map((e=>{const i={timeUnixNano:e.timeUnixNano,observedTimeUnixNano:e.timeUnixNano,attributes:(a={...t(this,u)[u],...e.attributes},Object.entries(a).map((([e,t])=>({key:e,value:s(t)}))))};var a;return e.body&&(i.body={stringValue:e.body}),i}))}};var p=a("url"),d=a("serviceName"),m=a("logger"),v=a("fetchFn"),g=a("maxPayloadSizeBytes"),b=class{constructor(e,i,a){var r;Object.defineProperty(this,p,{writable:!0,value:void 0}),Object.defineProperty(this,d,{writable:!0,value:void 0}),Object.defineProperty(this,m,{writable:!0,value:void 0}),Object.defineProperty(this,v,{writable:!0,value:void 0}),Object.defineProperty(this,g,{writable:!0,value:void 0}),t(this,p)[p]=e.replace(/\/v1\/(logs|metrics|traces)\/?$/,""),t(this,d)[d]=i,t(this,m)[m]=null==a?void 0:a.logger,t(this,v)[v]=null==a?void 0:a.fetchFn,t(this,g)[g]=null!==(r=null==a?void 0:a.maxPayloadSizeBytes)&&void 0!==r?r:51200}async exportMetrics(e,i){var a;const r=null===(a=null==i?void 0:i.keepalive)||void 0===a||a;await this.exportBatches("/v1/metrics",[...e],(e=>({resourceMetrics:[{resource:{attributes:[{key:"service.name",value:{stringValue:t(this,d)[d]}}]},scopeMetrics:[{scope:{name:"open-telemetry-mini-client",version:"1.1.0",attributes:[]},metrics:e}]}]})),r)}async exportLogs(e,i){var a;const r=null===(a=null==i?void 0:i.keepalive)||void 0===a||a;await this.exportBatches("/v1/logs",[...e],(e=>({resourceLogs:[{resource:{attributes:[{key:"service.name",value:{stringValue:t(this,d)[d]}}]},scopeLogs:[{scope:{name:"open-telemetry-mini-client",version:"1.1.0",attributes:[]},logRecords:e}]}]})),r)}async exportTo(e,i,a){var r;const s=JSON.stringify(e),o=(new TextEncoder).encode(s).length;if(o>t(this,g)[g])throw new y(`Payload size ${o} exceeds ${t(this,g)[g]} bytes`);const n=await this.exporterFetch()(`${t(this,p)[p]}${i}`,{method:"POST",keepalive:a,headers:{"Content-Type":"application/json"},body:s});if(null===(r=t(this,m)[m])||void 0===r||r.log({status:n.status}),!n.ok){if(400===n.status){const e=await n.text();throw new f(`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 f("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 x(`Authentication failed: ${n.status} ${401===n.status?"Unauthorized":"Forbidden"}`,{errorData:t,body:e})}throw new f(`Server responded with ${n.status}`)}}exporterFetch(){return t(this,v)[v]||fetch}async exportBatches(e,t,i,a){let r=t.length;for(;t.length>0;)try{const s=t.slice(0,r);await this.exportTo(i(s),e,a),t.splice(0,r)}catch(e){if(!(e instanceof y&&r>1))throw e;r=Math.ceil(r/2)}}},f=class extends Error{constructor(e,t){super(e),this.metadata=void 0,this.name="OpenTelemetryClientError",this.metadata=t}},y=class extends Error{constructor(...e){super(...e),this.name="PayloadTooLargeError"}},x=class extends Error{constructor(e,t){super(e),this.name="AuthenticationFailedError",this.metadata=void 0,this.name="AuthenticationFailedError",this.metadata=t}};function w(e,t,i){(function(e,t){if(t.has(e))throw new TypeError("Cannot initialize the same private elements twice on an object")})(e,t),t.set(e,i)}function P(e,t,i){if("function"==typeof e?e===t:e.has(t))return arguments.length<3?t:i;throw new TypeError("Private element is not present on this object")}function N(e,t,i){return e.set(P(e,t),i),i}function T(e,t){return e.get(P(e,t))}var k=new WeakMap,U=new WeakMap,M=class{constructor({exporter:e,getKeepalive:t}){w(this,k,void 0),w(this,U,void 0),N(k,this,e),N(U,this,t)}async exportMetrics(e,t){try{await T(k,this).exportMetrics(e,{...t,keepalive:T(U,this).call(this)})}catch(a){if(a instanceof f){var i;const r=null===(i=a.metadata)||void 0===i?void 0:i.retryAfter;if(r)return void await new Promise(((i,a)=>{setTimeout((()=>this.exportMetrics(e,t).then(i,a)),1e3*r.seconds)}))}throw a}}async exportLogs(e,t){try{await T(k,this).exportLogs(e,{...t,keepalive:T(U,this).call(this)})}catch(a){if(a instanceof f){var i;const r=null===(i=a.metadata)||void 0===i?void 0:i.retryAfter;if(r)return void await new Promise(((i,a)=>{setTimeout((()=>this.exportLogs(e,t).then(i,a)),1e3*r.seconds)}))}throw a}}};const E={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(E).find((([t,i])=>e.message.includes(i)));return(null==t?void 0:t[0])||"otherErrors"}const O="https://otlp-http-production.shopifysvc.com/v1/metrics";function A(){{const t=new b(O,"shop-js");return new M({exporter:t,getKeepalive:()=>"readyState"in e&&"complete"===e.readyState})}}export{N as a,T as i,A as n,w as o,j as r,h as s,O as t}; //# sourceMappingURL=chunk.utils_Cjeq_SSZ.esm.js.map