Add Cloudflare Analytics Worker to collect TTFB (Time To First Byte) metrics from the FilBeam bot.#338
Add Cloudflare Analytics Worker to collect TTFB (Time To First Byte) metrics from the FilBeam bot.#338ankur12-1610 wants to merge 4 commits intofilbeam:mainfrom
Conversation
analytics/bin/analytics-worker.js
Outdated
| * @param {unknown} error - The error object to extract from. | ||
| * @returns {{ status: number; message: string }} | ||
| */ | ||
| _getErrorHttpStatusMessage(error) { |
There was a problem hiding this comment.
looks like we don't need the error handler in this worker, could you remove it and replace fetch with _fetch?
bajtos
left a comment
There was a problem hiding this comment.
@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?
|
great catch, no we will need secrets |
yeah exactly, raised a PR so we can continue this discussion (filbeam/bot#33 (comment)) here |
|
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 |
|
@juliangruber @bajtos I have added secret based auth in the recent commit. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
.github/workflows/ci.yml
Outdated
| - name: Deploy Analytics Worker | ||
| uses: cloudflare/wrangler-action@v3 | ||
| with: | ||
| workingDirectory: analytics |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Let's find a more descriptive name, please.
analyticscan mean many diferent things.I propose to use the name
analytics-writerto make it clear that this worker writes analytics entries.
Done.
analytics/bin/analytics-worker.js
Outdated
| * @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' |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
hey so I was referring this https://developers.cloudflare.com/workers/examples/auth-with-headers/, I suppose we can set any custom header.
analytics/bin/analytics-worker.js
Outdated
| blobs: data.blobs, // [url, location, client, cid] | ||
| doubles: data.doubles // [ttfb, status, bytes] |
There was a problem hiding this comment.
We should allow the client to provide indexes as well, and validate that the array has at most one item.
Indexes — (strings) — Used as a sampling key.
(...)
Currently, thewriteDataPoint()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/
There was a problem hiding this comment.
We should allow the client to provide
indexesas well, and validate that the array has at most one item.Indexes — (strings) — Used as a sampling key.
(...)
Currently, thewriteDataPoint()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
|
@bajtos @juliangruber any updates on this, I intend to complete this issue. Thanks! |
|
@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. |
|
@ankur12-1610 I appreciate the effort you put into this contribution ❤️ I am sorry we did not accept it in the end. |
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