@prisma/extension-optimize 0.0.0-dev.202408261306 → 0.0.0-dev.202408261452
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/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/package.json +3 -5
package/dist/index.d.ts
CHANGED
|
@@ -28,10 +28,10 @@ type OptimizeOptions = {
|
|
|
28
28
|
*/
|
|
29
29
|
minSendInterval?: number;
|
|
30
30
|
/**
|
|
31
|
-
* Whether to show a
|
|
31
|
+
* Whether to show a CLI notification when the dashboard is ready.
|
|
32
32
|
*/
|
|
33
|
-
|
|
33
|
+
showNotification?: boolean;
|
|
34
34
|
};
|
|
35
|
-
declare function withOptimize({ enable, ingestionUrl, dashboardUrl, minSendInterval,
|
|
35
|
+
declare function withOptimize({ enable, ingestionUrl, dashboardUrl, minSendInterval, showNotification, token, }: OptimizeOptions): (client: any) => _prisma_client_extension.PrismaClientExtends<_prisma_client_runtime_library.InternalArgs<{}, {}, {}, {}> & _prisma_client_runtime_library.DefaultArgs>;
|
|
36
36
|
|
|
37
37
|
export { type OptimizeOptions, PROD_DASHBOARD_URL, PROD_INGESTION_URL, withOptimize };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`:(
|
|
1
|
+
"use strict";var X=Object.create;var l=Object.defineProperty;var Y=Object.getOwnPropertyDescriptor;var ee=Object.getOwnPropertyNames;var te=Object.getPrototypeOf,ne=Object.prototype.hasOwnProperty;var se=(n,e)=>{for(var t in e)l(n,t,{get:e[t],enumerable:!0})},x=(n,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of ee(e))!ne.call(n,i)&&i!==t&&l(n,i,{get:()=>e[i],enumerable:!(s=Y(e,i))||s.enumerable});return n};var C=(n,e,t)=>(t=n!=null?X(te(n)):{},x(e||!n||!n.__esModule?l(t,"default",{value:n,enumerable:!0}):t,n)),ie=n=>x(l({},"__esModule",{value:!0}),n);var ce={};se(ce,{PROD_DASHBOARD_URL:()=>G,PROD_INGESTION_URL:()=>V,withOptimize:()=>de});module.exports=ie(ce);var Q=require("@opentelemetry/api"),B=require("@prisma/client/extension"),J=C(require("@prisma/debug"));function re(n,e){let t;return async function(s,i,r){let p=new URL(s,n),d=new Headers({"Content-Type":"application/json",Authorization:`Bearer ${e}`});t&&d.set("prisma-optimize-jwt",t);let o=await fetch(p,{method:i,headers:d,body:JSON.stringify(r)});if(!o.ok){let a=await o.text().catch(()=>"<unreadable>");console.error(`[optimize] HTTP ${o.status} ${o.statusText}: ${a}`)}let m=o.headers.get("prisma-optimize-jwt");return m&&(t=m),o}}function b(n,e){let t=re(n,e);return{request:t,post:(s,i)=>t(s,"POST",i)}}async function P(n,e){return await(await n.post("/schema",{schema:e})).text()}var q=C(require("kleur"));var{bold:oe,underline:ae}=q.default;function v(n){let e="See your Optimize dashboard at:",t=e.length,s=`${ae(oe(n))}`,i=n.length,r=Math.max(t,i)+2;console.log("\u250C"+"\u2500".repeat(r)+"\u2510"),console.log("\u2502 "+e+" ".repeat(r-t-2)+" \u2502"),console.log("\u2502 "+s+" ".repeat(r-i-2)+" \u2502"),console.log("\u2514"+"\u2500".repeat(r)+"\u2518")}var O=require("@opentelemetry/api"),k=require("@opentelemetry/context-async-hooks"),M=require("@opentelemetry/instrumentation"),F=require("@opentelemetry/resources"),z=require("@opentelemetry/sdk-trace-base"),g=require("@opentelemetry/semantic-conventions"),N=require("@prisma/instrumentation");var A=C(require("@prisma/debug"));async function E(n,e){await n.post("/ingest",e)}function I(n,...e){setTimeout(()=>{n(...e)},0)}function c(n){return n[0]*1e3+n[1]/1e6}var h=(0,A.default)("prisma:extension:optimize"),u="0000000000000000",_=(r=>(r.ClientOperation="prisma:client:operation",r.ClientConnect="prisma:client:connect",r.Engine="prisma:engine",r.EngineConnection="prisma:engine:connection",r.EngineQuery="prisma:engine:db_query",r))(_||{}),y=class{spanId;timestamp;model;operation;args;statementSpans;duration;connect;error;completionFlags={clientSpanClosed:!1,engineSpanClosed:!1};constructor(e){this.spanId=e.spanId,this.timestamp=e.timestamp,this.model=e.model,this.operation=e.operation,this.args=e.args,this.statementSpans=[]}isCompleted(){return this.completionFlags.clientSpanClosed&&this.completionFlags.engineSpanClosed}statementSpansToString(){return this.statementSpans.sort((t,s)=>{let i=c(t.startTime),r=c(s.startTime);return i-r}).reduce((t,s)=>{let i=s.attributes["db.statement"];return typeof i=="string"?`${t}${i}
|
|
2
|
+
`:(h("sql attribute is not a string: %o",i),t)},"")}toIngestRequestItem(e){if(this.duration===void 0)throw new Error("`duration` is `undefined`, this should not happen");return{ts:this.timestamp,model:this.model??null,operation:this.operation,args:this.args,latency:this.duration,connect:this.connect??!1,sql:this.statementSpansToString(),error:this.error??null,hash:e}}},f=class{#t=new Map;#n=new Map;#s=new Map;#o=new Set;#e=[];#a=Promise.resolve();#p=!1;#c;#m;#i=new Set;#l=Promise.resolve();#d=!1;#u;setSchemaHash=()=>{};__test_internalMetrics__={rescheduledCountTotal:0};constructor(e,t){this.#c=e,this.#m=t,this.#u=new Promise(s=>{this.setSchemaHash=s})}createRequest(e){let t=new y(e);this.#t.set(e.spanId,t)}setRequestError(e,t){let s=this.#t.get(e);if(!s)throw new Error(`Unknown request ${e}`);s.error=t}#S(e){for(let t=e;t!==u;t=this.#n.get(t)){if(t===void 0)return{type:"UndeliveredSpanInTree"};let s=this.#t.get(t);if(s)return{type:"Ok",request:s}}return{type:"NotInRequestTree"}}onStart(e,t){if(!T(e))return;let s=e.spanContext().spanId;if(!e.parentSpanId){this.#n.set(s,u);return}if(this.#n.set(s,e.parentSpanId),e.parentSpanId===u)return;let i=this.#s.get(e.parentSpanId);i?i.add(s):this.#s.set(e.parentSpanId,new Set([s])),e.name==="prisma:engine:connection"&&this.#o.add(s)}onEnd(e){T(e)?this.#h(e):(e.parentSpanId===void 0||e.parentSpanId===u)&&this.#r(e.spanContext().spanId)}async forceFlush(){await this.#a,await this.#g()}async shutdown(){await this.forceFlush()}#h(e){let t=e.spanContext().spanId,s,i=this.#S(t);switch(i.type){case"Ok":s=i.request;break;case"UndeliveredSpanInTree":this.#C(e);break;case"NotInRequestTree":this.#r(t);break}s&&(e.name==="prisma:client:connect"&&(s.connect=c(e.duration)),e.name==="prisma:engine"&&(s.completionFlags.engineSpanClosed=!0),e.name==="prisma:engine:db_query"&&!this.#o.has(e.parentSpanId)&&s.statementSpans.push(e),e.name==="prisma:client:operation"&&(s.duration=c(e.duration),s.completionFlags.clientSpanClosed=!0,h("latency otel: %d",s.duration)),s.isCompleted()&&this.#w(s))}#C(e){this.#e.push(e),this.__test_internalMetrics__.rescheduledCountTotal++;let{spanId:t,traceId:s}=e.spanContext();h("rescheduled ",e.name,t,e.parentSpanId,s),!this.#p&&(this.#p=!0,this.#a=Promise.all([this.#a,new Promise(I).then(()=>this.#I())]))}#I(){let e=this.#e;this.#e=[],this.#p=!1;for(let t of e)this.#h(t)}#r(e){this.#n.delete(e),this.#o.delete(e);let t=this.#s.get(e);if(this.#s.delete(e),this.#y(e),t)for(let s of t)this.#r(s)}#y(e){for(let t=this.#f(e);t!==-1;t=this.#f(e))this.#e.splice(t,1)}#f(e){return this.#e.findIndex(t=>t.spanContext().spanId===e)}#w(e){I(()=>{this.#t.delete(e.spanId),this.#r(e.spanId),this.#i.add(e),this.#R()})}async#R(){await this.#l,!this.#d&&(this.#d=!0,setTimeout(()=>{this.#l=this.#g().finally(()=>{this.#d=!1})},this.#m))}async#g(){if(this.#i.size===0)return;let e=await this.#u,t=[...this.#i].map(s=>s.toIngestRequestItem(e));this.#i.clear(),h("sending batch of %d requests",t.length);try{await E(this.#c,t)}catch(s){console.error(s.message??s)}}};function T(n){return Object.values(_).includes(n.name)}function j(n,e){let t=new k.AsyncHooksContextManager().enable();O.context.setGlobalContextManager(t);let s=new z.BasicTracerProvider({resource:new F.Resource({[g.SEMRESATTRS_SERVICE_NAME]:"extension-optimize",[g.SEMRESATTRS_SERVICE_VERSION]:"0.0.0"})}),i=new f(n,e);return s.addSpanProcessor(i),(0,M.registerInstrumentations)({tracerProvider:s,instrumentations:[new N.PrismaInstrumentation]}),s.register(),i}function L(n){return n._previewFeatures?.includes("tracing")}function U(n){return n._engineConfig.inlineSchema}function $(n){if(n instanceof Error)return n.stack??n.message;switch(typeof n){case"undefined":return"undefined";case"object":{let e;return n!==null&&typeof n.toString=="function"&&(e=n.toString()),typeof e=="string"&&e!=="[object Object]"?e:JSON.stringify(n)}default:return String(n)}}var H=require("@prisma/client/extension"),D=H.Prisma.defineExtension(n=>n.$extends({}));var V="https://optimize-ingestion.datacdn.workers.dev/",G="https://optimize.prisma.io",pe=(0,J.default)("prisma:extension:optimize");function de({enable:n=!0,ingestionUrl:e=V,dashboardUrl:t=G,minSendInterval:s=50,showNotification:i=!0,token:r}){return n?(i&&v(t),B.Prisma.defineExtension(p=>{let d=b(e,r),o=j(d,s);if(!L(p))throw new Error('Please enable the "tracing" preview feature and regenerate the client.');let m=U(p);return P(d,m).then(a=>{pe("uploaded schema with hash",a),o.setSchemaHash(a)},a=>{console.error(a),console.error("Failed to upload the schema, no data will be sent to Prisma Optimize")}),p.$extends({query:{async $allOperations({query:a,model:Z,operation:K,args:w}){let S=Q.trace.getActiveSpan()?.spanContext().spanId;if(!S)throw new Error("prisma:client:operation span is expected to be entered in the client extension when tracing is enabled");o.createRequest({spanId:S,timestamp:Date.now(),model:Z,operation:K,args:w});try{return await a(w)}catch(R){let W=$(R);throw o.setRequestError(S,W),R}}}})})):D}0&&(module.exports={PROD_DASHBOARD_URL,PROD_INGESTION_URL,withOptimize});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma/extension-optimize",
|
|
3
|
-
"version": "0.0.0-dev.
|
|
3
|
+
"version": "0.0.0-dev.202408261452",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,9 +17,7 @@
|
|
|
17
17
|
"@prisma/debug": "5.12.1",
|
|
18
18
|
"@prisma/instrumentation": "5.18.0",
|
|
19
19
|
"kleur": "4.1.5",
|
|
20
|
-
"
|
|
21
|
-
"readline-sync": "1.4.10",
|
|
22
|
-
"xdg-app-paths": "8.3.0"
|
|
20
|
+
"readline-sync": "1.4.10"
|
|
23
21
|
},
|
|
24
22
|
"devDependencies": {
|
|
25
23
|
"@hono/node-server": "1.9.1",
|
|
@@ -28,7 +26,7 @@
|
|
|
28
26
|
"hono": "4.4.6",
|
|
29
27
|
"tsup": "8.0.2",
|
|
30
28
|
"vitest": "1.4.0",
|
|
31
|
-
"common": "0.0.0-dev.
|
|
29
|
+
"common": "0.0.0-dev.202408261452"
|
|
32
30
|
},
|
|
33
31
|
"peerDependencies": {
|
|
34
32
|
"@prisma/client": "5.x"
|