Skip to content

Add Cloudflare Analytics Worker to collect TTFB (Time To First Byte) metrics from the FilBeam bot.#338

Closed
ankur12-1610 wants to merge 4 commits intofilbeam:mainfrom
ankur12-1610:main
Closed

Add Cloudflare Analytics Worker to collect TTFB (Time To First Byte) metrics from the FilBeam bot.#338
ankur12-1610 wants to merge 4 commits intofilbeam:mainfrom
ankur12-1610:main

Conversation

@ankur12-1610
Copy link

@ankur12-1610 ankur12-1610 commented Oct 8, 2025

Add a Cloudflare Analytics Worker, required to collect and send Time To First Byte (TTFB) metrics to the FilBeam bot.

Related to: filbeam/bot#33

Copy link
Member

@juliangruber juliangruber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! 🙏

* @param {unknown} error - The error object to extract from.
* @returns {{ status: number; message: string }}
*/
_getErrorHttpStatusMessage(error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like we don't need the error handler in this worker, could you remove it and replace fetch with _fetch?

Copy link
Contributor

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juliangruber @ankur12-1610 IIUC, the current implementation creates an endpoint that allows anyone to write data to our CF Analytics Engine. Are we okay with that?

@juliangruber
Copy link
Member

great catch, no we will need secrets

@ankur12-1610
Copy link
Author

@juliangruber @ankur12-1610 IIUC, the current implementation creates an endpoint that allows anyone to write data to our CF Analytics Engine. Are we okay with that?

yeah exactly, raised a PR so we can continue this discussion (filbeam/bot#33 (comment)) here

@juliangruber
Copy link
Member

Since the filbeam/bot is deployed outside of CF, we can't easily use private networking, so indeed we will need an authentication strategy, most easily checking a secret stored in CF secrets

@ankur12-1610
Copy link
Author

@juliangruber @bajtos I have added secret based auth in the recent commit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing the anlytics auth secret. I suspect that is because it's missing from wrangler.toml. Or maybe typings just need to be recreated

Copy link
Author

@ankur12-1610 ankur12-1610 Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing the anlytics auth secret. I suspect that is because it's missing from wrangler.toml. Or maybe typings just need to be recreated

@juliangruber I will need to add analytics auth secret to var in wrangler.toml as per the documentation of Cloudflare (https://developers.cloudflare.com/workers/configuration/environment-variables/#local-development-with-secrets) it is not advised.

- name: Deploy Analytics Worker
uses: cloudflare/wrangler-action@v3
with:
workingDirectory: analytics
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's find a more descriptive name, please. analytics can mean many diferent things.

I propose to use the name analytics-writer to make it clear that this worker writes analytics entries.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's find a more descriptive name, please. analytics can mean many diferent things.

I propose to use the name analytics-writer to make it clear that this worker writes analytics entries.

Done.

* @param {string} AUTH_HEADER_KEY Custom header to check for authentication
* @param {string} authKey Pre-shared authentication key from environment
*/
const AUTH_HEADER_KEY = 'X-Analytics-Auth'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why we cannot use the standard Authorization header with the Bearer scheme?

POST / HTTP/1.1
Authorization: Bearer somesecret
Content-Type: application/json
(etc.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey so I was referring this https://developers.cloudflare.com/workers/examples/auth-with-headers/, I suppose we can set any custom header.

Comment on lines 55 to 56
blobs: data.blobs, // [url, location, client, cid]
doubles: data.doubles // [ttfb, status, bytes]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should allow the client to provide indexes as well, and validate that the array has at most one item.

See https://developers.cloudflare.com/analytics/analytics-engine/get-started/#2-write-data-points-from-your-worker

Indexes — (strings) — Used as a sampling key.
(...)
Currently, the writeDataPoint() API accepts ordered arrays of values. This means that you must provide fields in a consistent order. While the indexes field accepts an array, you currently must only provide a single index. If you attempt to provide multiple indexes, your data point will not be recorded.

Additional limits we may want to validate here:

https://developers.cloudflare.com/analytics/analytics-engine/limits/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should allow the client to provide indexes as well, and validate that the array has at most one item.

See https://developers.cloudflare.com/analytics/analytics-engine/get-started/#2-write-data-points-from-your-worker

Indexes — (strings) — Used as a sampling key.
(...)
Currently, the writeDataPoint() API accepts ordered arrays of values. This means that you must provide fields in a consistent order. While the indexes field accepts an array, you currently must only provide a single index. If you attempt to provide multiple indexes, your data point will not be recorded.

Additional limits we may want to validate here:

https://developers.cloudflare.com/analytics/analytics-engine/limits/

I have added indexes and limits @bajtos

@ankur12-1610
Copy link
Author

@bajtos @juliangruber any updates on this, I intend to complete this issue.

Thanks!

@bajtos
Copy link
Contributor

bajtos commented Dec 16, 2025

@ankur12-1610 I apologise for our unresponsiveness. We decided to shut down the FilBeam bot and rely solely on real traffic. We will need to find a different solution of measuring TTFB as observed by retrieval clients.

@bajtos bajtos closed this Dec 16, 2025
@bajtos
Copy link
Contributor

bajtos commented Dec 16, 2025

@ankur12-1610 I appreciate the effort you put into this contribution ❤️

I am sorry we did not accept it in the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants