Subrequests metrics

Hi there, I wrote a workers to cache request from my graphql endpoint. I mostly following the example here https://blog.cloudflare.com/introducing-the-workers-cache-api-giving-you-control-over-how-your-content-is-cached As a result I correctly get cached request from the Cloudflare Cache API with status "cf-cache-status: HIT". However, if I go to the analytics dashboard to "Workers (per zone) > Subrequests > Cached requests" it says 0. "Total requests" is correctly populated with the request I made. I'm looking at the documentation here https://developers.cloudflare.com/workers/observability/metrics-and-analytics/#subrequests and the way I'm doing it should be correct. Any pointers on why it's not showing any requests cached?
5 Replies
jeremy
jeremy4mo ago
Are you sure? Reading at the following documentation, I'm not understanding it this way: - "Subrequests are requests triggered by calling fetch from within a Worker." - "Cached: The number of cached responses returned." When I'm doing a request to my endpoint, the worker kicks in, and it goes through the Worker fetch logic. If cache.match exists, it returns the cached request, if not, it calls origin (with fetch) and cache the response I’m calling cache.match to see if the request is already cached. If not cached, I’m calling fetch sorry to be a wall, that's why I am asking for help. I'm not understanding this properly. Here is a walkthrough: - api endpoint: api.domain.com - requests are made to api.domain.com - workers is trigger on route api.domain.com Code snippet:
response = await cache.match(cacheKey)

if (!response) {
response = await fetch(request.url, newRequest)
event.waitUntil(cache.put(cacheKey, response.clone()))
}
response = await cache.match(cacheKey)

if (!response) {
response = await fetch(request.url, newRequest)
event.waitUntil(cache.put(cacheKey, response.clone()))
}
Request n°1:
response = await cache.match(cacheKey)
// response = undefined
// then we are calling fetch to origin, and caching response with cache.put
// cf-cache-status: DYNAMIC
response = await cache.match(cacheKey)
// response = undefined
// then we are calling fetch to origin, and caching response with cache.put
// cf-cache-status: DYNAMIC
Request n°2:
response = await cache.match(cacheKey)
// response = something here
// we return the response because it exists in the cache
// cf-cache-status: HIT
response = await cache.match(cacheKey)
// response = something here
// we return the response because it exists in the cache
// cf-cache-status: HIT
okay, now I get it, thanks for the patience is there a place in the dashboard where I can see the hit ratio analytics for the cache.match from the worker? I guess with the "Cache Analytics" on the Pro plan minimum? alright, I see, thanks Would "Cache Analytics" from Pro plan would give me this data without doing the math myself, or it's just something not available in the dashboard?
Chaika
Chaika4mo ago
awkwardly Cache Analytics actually filter out those requests in the dashboards they show you, but having Pro would give you access to the dataset which you could then use, just kind of messy
Chaika
Chaika4mo ago
there's a request source just for worker cache api
No description
No description
Chaika
Chaika4mo ago
the dashboards Cache Analytics filter only for eyeball requests/requests directly from users
jeremy
jeremy4mo ago
I see, or I can use the following graphql query:
{
viewer {
zones {
workersZoneInvocationsAdaptiveGroups(filter: {httpResponseStatus:200, date_gt:"2024-04-20"}, limit:10) {
dimensions {
httpResponseStatus
status
}
sum {
requests
subrequests
}
}
}
}
}
{
viewer {
zones {
workersZoneInvocationsAdaptiveGroups(filter: {httpResponseStatus:200, date_gt:"2024-04-20"}, limit:10) {
dimensions {
httpResponseStatus
status
}
sum {
requests
subrequests
}
}
}
}
}
and do subrequest/requests? would that be the same result? I'm planning to pay for the workers add-on, but not the whole pro plan, which I wouldn't really need