Skip to content

Commit 08c6580

Browse files
Fixes bug where indexId was never provided to analytics (#7183)
1 parent 3dce388 commit 08c6580

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

.changeset/seven-parents-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/workers-shared": minor
3+
---
4+
5+
Fixes bug where indexId was never set for router-worker and asset-worker

packages/workers-shared/asset-worker/src/analytics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ export class Analytics {
4848
return this.data[key];
4949
}
5050

51-
write(hostname?: string) {
51+
write() {
5252
if (!this.readyAnalytics) {
5353
return;
5454
}
5555

5656
this.readyAnalytics.logEvent({
5757
version: VERSION,
5858
accountId: 0, // TODO: need to plumb through
59-
indexId: hostname,
59+
indexId: this.data.hostname?.substring(0, 96),
6060
doubles: [
6161
this.data.requestTime ?? -1, // double1
6262
this.data.coloId ?? -1, // double2

packages/workers-shared/router-worker/src/analytics.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Environment, ReadyAnalytics } from "./types";
1+
import type { ReadyAnalytics } from "./types";
22

33
// This will allow us to make breaking changes to the analytic schema
44
const VERSION = 1;
@@ -35,6 +35,11 @@ type Data = {
3535

3636
export class Analytics {
3737
private data: Data = {};
38+
private readyAnalytics?: ReadyAnalytics;
39+
40+
constructor(readyAnalytics?: ReadyAnalytics) {
41+
this.readyAnalytics = readyAnalytics;
42+
}
3843

3944
setData(newData: Partial<Data>) {
4045
this.data = { ...this.data, ...newData };
@@ -44,15 +49,15 @@ export class Analytics {
4449
return this.data[key];
4550
}
4651

47-
write(env: Environment, readyAnalytics?: ReadyAnalytics, hostname?: string) {
48-
if (!readyAnalytics) {
52+
write() {
53+
if (!this.readyAnalytics) {
4954
return;
5055
}
5156

52-
readyAnalytics.logEvent({
57+
this.readyAnalytics.logEvent({
5358
version: VERSION,
5459
accountId: 0, // TODO: need to plumb through
55-
indexId: hostname,
60+
indexId: this.data.hostname?.substring(0, 96),
5661
doubles: [
5762
this.data.requestTime ?? -1, // double1
5863
this.data.coloId ?? -1, // double2

packages/workers-shared/router-worker/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ interface Env {
2424
export default {
2525
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
2626
let sentry: ReturnType<typeof setupSentry> | undefined;
27-
const analytics = new Analytics();
27+
const analytics = new Analytics(env.ANALYTICS);
2828
const performance = new PerformanceTimer(env.UNSAFE_PERFORMANCE);
2929
const startTimeMs = performance.now();
3030

@@ -80,7 +80,7 @@ export default {
8080
throw err;
8181
} finally {
8282
analytics.setData({ requestTime: performance.now() - startTimeMs });
83-
analytics.write(env.ENVIRONMENT, env.ANALYTICS);
83+
analytics.write();
8484
}
8585
},
8686
};

0 commit comments

Comments
 (0)