miniflare 0.0.0-e4fe35cc5 → 0.0.0-e5ebdb143

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.
@@ -8619,6 +8619,17 @@ var require_index_cjs = __commonJS({
8619
8619
  }
8620
8620
  });
8621
8621
 
8622
+ // ../workers-shared/utils/performance.ts
8623
+ var PerformanceTimer = class {
8624
+ performanceTimer;
8625
+ constructor(performanceTimer) {
8626
+ this.performanceTimer = performanceTimer;
8627
+ }
8628
+ now() {
8629
+ return this.performanceTimer ? this.performanceTimer.timeOrigin + this.performanceTimer.now() : Date.now();
8630
+ }
8631
+ };
8632
+
8622
8633
  // ../workers-shared/utils/sentry.ts
8623
8634
  var import_toucan_js = __toESM(require_index_cjs());
8624
8635
  function setupSentry(request, context, dsn, clientId, clientSecret) {
@@ -8654,20 +8665,87 @@ function setupSentry(request, context, dsn, clientId, clientSecret) {
8654
8665
  return sentry.setUser({ userAgent, colo }), sentry;
8655
8666
  }
8656
8667
 
8668
+ // ../workers-shared/router-worker/src/analytics.ts
8669
+ var Analytics = class {
8670
+ data = {};
8671
+ readyAnalytics;
8672
+ constructor(readyAnalytics) {
8673
+ this.readyAnalytics = readyAnalytics;
8674
+ }
8675
+ setData(newData) {
8676
+ this.data = { ...this.data, ...newData };
8677
+ }
8678
+ getData(key) {
8679
+ return this.data[key];
8680
+ }
8681
+ write() {
8682
+ this.readyAnalytics && this.readyAnalytics.logEvent({
8683
+ version: 1,
8684
+ accountId: 0,
8685
+ // TODO: need to plumb through
8686
+ indexId: this.data.hostname?.substring(0, 96),
8687
+ doubles: [
8688
+ this.data.requestTime ?? -1,
8689
+ // double1
8690
+ this.data.coloId ?? -1,
8691
+ // double2
8692
+ this.data.metalId ?? -1,
8693
+ // double3
8694
+ this.data.coloTier ?? -1,
8695
+ // double4
8696
+ this.data.userWorkerAhead === void 0 ? -1 : Number(this.data.userWorkerAhead)
8697
+ ],
8698
+ blobs: [
8699
+ this.data.hostname?.substring(0, 256),
8700
+ // blob1 - trim to 256 bytes
8701
+ this.data.dispatchtype,
8702
+ // blob2
8703
+ this.data.error?.substring(0, 256),
8704
+ // blob3 - trim to 256 bytes
8705
+ this.data.version,
8706
+ // blob4
8707
+ this.data.coloRegion
8708
+ // blob5
8709
+ ]
8710
+ });
8711
+ }
8712
+ };
8713
+
8657
8714
  // ../workers-shared/router-worker/src/index.ts
8658
8715
  var src_default = {
8659
8716
  async fetch(request, env, ctx) {
8660
- let sentry, maybeSecondRequest = request.clone();
8717
+ let sentry, analytics = new Analytics(env.ANALYTICS), performance = new PerformanceTimer(env.UNSAFE_PERFORMANCE), startTimeMs = performance.now();
8661
8718
  try {
8662
- return sentry = setupSentry(
8719
+ sentry = setupSentry(
8663
8720
  request,
8664
8721
  ctx,
8665
8722
  env.SENTRY_DSN,
8666
8723
  env.SENTRY_ACCESS_CLIENT_ID,
8667
8724
  env.SENTRY_ACCESS_CLIENT_SECRET
8668
- ), env.CONFIG.has_user_worker ? await env.ASSET_WORKER.unstable_canFetch(request) ? await env.ASSET_WORKER.fetch(maybeSecondRequest) : env.USER_WORKER.fetch(maybeSecondRequest) : await env.ASSET_WORKER.fetch(request);
8725
+ );
8726
+ let url = new URL(request.url);
8727
+ sentry && (sentry.setUser({ username: url.hostname }), sentry.setTag("colo", env.COLO_METADATA.coloId), sentry.setTag("metal", env.COLO_METADATA.metalId)), env.COLO_METADATA && env.VERSION_METADATA && env.CONFIG && analytics.setData({
8728
+ coloId: env.COLO_METADATA.coloId,
8729
+ metalId: env.COLO_METADATA.metalId,
8730
+ coloTier: env.COLO_METADATA.coloTier,
8731
+ coloRegion: env.COLO_METADATA.coloRegion,
8732
+ hostname: url.hostname,
8733
+ version: env.VERSION_METADATA.id,
8734
+ userWorkerAhead: env.CONFIG.invoke_user_worker_ahead_of_assets
8735
+ });
8736
+ let maybeSecondRequest = request.clone();
8737
+ if (env.CONFIG.invoke_user_worker_ahead_of_assets) {
8738
+ if (!env.CONFIG.has_user_worker)
8739
+ throw new Error(
8740
+ "Fetch for user worker without having a user worker binding"
8741
+ );
8742
+ return env.USER_WORKER.fetch(maybeSecondRequest);
8743
+ }
8744
+ return env.CONFIG.has_user_worker ? await env.ASSET_WORKER.unstable_canFetch(request) ? (analytics.setData({ dispatchtype: "asset" /* ASSETS */ }), env.ASSET_WORKER.fetch(maybeSecondRequest)) : (analytics.setData({ dispatchtype: "worker" /* WORKER */ }), env.USER_WORKER.fetch(maybeSecondRequest)) : (analytics.setData({ dispatchtype: "asset" /* ASSETS */ }), env.ASSET_WORKER.fetch(request));
8669
8745
  } catch (err) {
8670
- throw sentry && sentry.captureException(err), err;
8746
+ throw err instanceof Error && analytics.setData({ error: err.message }), sentry && sentry.captureException(err), err;
8747
+ } finally {
8748
+ analytics.setData({ requestTime: performance.now() - startTimeMs }), analytics.write();
8671
8749
  }
8672
8750
  }
8673
8751
  };