Skip to content

Commit

Permalink
add pages functions metrics
Browse files Browse the repository at this point in the history
Signed-off-by: rare-magma <[email protected]>
  • Loading branch information
rare-magma committed Sep 29, 2024
1 parent b857bfd commit aba9d25
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ systemctl --user list-timers
- cloudflare_stats_responses: Request statistics broken down by response status code
- cloudflare_stats: General request statistics
- cloudflare_stats_workers: Workers statistics grouped by hour
- cloudflare_stats_pf: Pages Functions statistics grouped by hour
- cloudflare_stats_kv_ops: KV operation statistics grouped by hour
- cloudflare_stats_kv_storage: KV storage statistics
Expand All @@ -171,6 +172,7 @@ cloudflare_stats_ip,zone="example.com",ipType="searchEngine" requests=21 1703894
cloudflare_stats_responses,zone="example.com",status=403 requests=1 1703894400
cloudflare_stats,zone="example.com" bytes=2032039,cachedBytes=40607,cachedRequests=17,encryptedBytes=2020727,encryptedRequests=251,pageViews=178,requests=266,threats=0,uniqueVisitors=2 1703894400
cloudflare_stats_workers,account=aa0a0aa000a0000aa00a00aa0e000a0a,worker=worker-name status="scriptThrewException",cpuTimeP50=1246,cpuTimeP99=1246,durationP50=0.001246,durationP99=0.001246,responseBodySizeP50=0,responseBodySizeP99=0,wallTimeP50=1605,wallTimeP99=1605,clientDisconnects=0,cpuTimeUs=1246,duration=0.001246,errors=1,requests=1,responseBodySize=0,subrequests=0,wallTime=1605 1727340566
cloudflare_stats_pf,account=aa0a0aa000a0000aa00a00aa0e000a0a,scriptName=pages-worker--1111111-production status="success",usageModel="standard",cpuTimeP50=3492,cpuTimeP99=3700,durationP50=0.004010875,durationP99=0.016313376,clientDisconnects=0,duration=0.024276250000000003,errors=0,requests=3,responseBodySize=4614,subrequests=0,wallTime=194210 1727431200
cloudflare_stats_kv_ops,account=aa0a0aa000a0000aa00a00aa0e000a0a,namespace=999999aba99dd9999ef99ab78965ab1c actionType="read",result="hot_read",responseStatusCode=200,latencyMsP50=116,latencyMsP99=116,objectBytes=1737,requests=1 1727445600
cloudflare_stats_kv_storage,account=aa0a0aa000a0000aa00a00aa0e000a0a,namespace=999999aba99dd9999ef99ab78965ab1c byteCount=5369,keyCount=1 1727442000
```
Expand Down
101 changes: 101 additions & 0 deletions cloudflare_exporter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,107 @@ if [[ $cf_nb_invocations -gt 0 ]]; then

fi

PAGES_FUNCTIONS_GRAPHQL_QUERY=$(
cat <<END_HEREDOC
{ "query":
"query {
viewer {
accounts(filter: { accountTag: \$accountTag }) {
pagesFunctionsInvocationsAdaptiveGroups(
filter: { datetimeHour_geq: \$datetimeStart, datetimeHour_leq: \$datetimeEnd }
limit: 10000
) {
sum {
clientDisconnects
duration
errors
requests
responseBodySize
subrequests
wallTime
}
quantiles {
cpuTimeP50
cpuTimeP99
durationP50
durationP99
}
dimensions {
datetimeHour
scriptName
status
usageModel
}
}
}
}
}",
"variables": {
"accountTag": "$CLOUDFLARE_ACCOUNT_TAG",
"datetimeStart": "$ISO_CURRENT_DATE_TIME_1H_AGO",
"datetimeEnd": "$ISO_CURRENT_DATE_TIME"
}
}
END_HEREDOC
)

cf_pf_json=$(
$CURL --silent --fail --show-error --compressed \
--request POST \
--header "Content-Type: application/json" \
--header "$CF_EMAIL_HEADER" \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--data "$(echo -n $PAGES_FUNCTIONS_GRAPHQL_QUERY)" \
"$CF_URL"
)

cf_pf_nb_errors=$(echo $cf_pf_json | $JQ ".errors | length")

if [[ $cf_pf_nb_errors -gt 0 ]]; then
cf_pf_errors=$(echo $cf_pf_json | $JQ --raw-output ".errors[] | .message")
printf "Cloudflare API request failed with: \n%s\nAborting\n" "$cf_pf_errors" >&2
exit 1
fi

cf_pf_nb_invocations=$(echo $cf_pf_json | $JQ ".data.viewer.accounts[0].pagesFunctionsInvocationsAdaptiveGroups | length")

if [[ $cf_pf_nb_invocations -gt 0 ]]; then
cf_pf_json_parsed=$(echo $cf_pf_json | $JQ ".data.viewer.accounts[0].pagesFunctionsInvocationsAdaptiveGroups")
cf_stats_pf=$(
echo "$cf_pf_json_parsed" |
$JQ --raw-output "
(.[] |
[\"${CLOUDFLARE_ACCOUNT_TAG}\",
.dimensions.scriptName,
.dimensions.status,
.dimensions.usageModel,
.quantiles.cpuTimeP50,
.quantiles.cpuTimeP99,
.quantiles.durationP50,
.quantiles.durationP99,
.sum.clientDisconnects,
.sum.duration,
.sum.errors,
.sum.requests,
.sum.responseBodySize,
.sum.subrequests,
.sum.wallTime,
(.dimensions.datetimeHour | fromdateiso8601)
])
| @tsv" |
$AWK '{printf "cloudflare_stats_pf,account=%s,scriptName=%s status=\"%s\",usageModel=\"%s\",cpuTimeP50=%s,cpuTimeP99=%s,durationP50=%s,durationP99=%s,clientDisconnects=%s,duration=%s,errors=%s,requests=%s,responseBodySize=%s,subrequests=%s,wallTime=%s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16}'
)

echo "$cf_stats_pf" | $GZIP |
$CURL --silent --fail --show-error \
--request POST "${INFLUXDB_URL}" \
--header 'Content-Encoding: gzip' \
--header "Authorization: Token $INFLUXDB_API_TOKEN" \
--header "Content-Type: text/plain; charset=utf-8" \
--header "Accept: application/json" \
--data-binary @-
fi

if [[ -n "${CLOUDFLARE_KV_NAMESPACES}" ]]; then

for kv_namespace_id in $(echo "${CLOUDFLARE_KV_NAMESPACES}"); do
Expand Down

0 comments on commit aba9d25

Please sign in to comment.