App Hosting Log Analytics

Jeff Huleatt

portrait of Jeff Huleatt

Firebase App Hosting gives an overview of traffic in the in-console metrics (especially route-based metrics), but it’s possible to do more custom reporting with Log Analytics.

A Common Table Expression (CTE) can give us a table to work with that only contains App Hosting logs:

WITH
  vars AS (
    SELECT 'my-apphosting-backend' AS apphosting_backend_id,
            'us-central1' AS apphosting_backend_location
  ),
  all_logs AS (
    SELECT
      *
    FROM
      `my-apphosting-backend.global._Default._AllLogs`
  ),
  apphosting_logs AS  (
    SELECT *
    FROM all_logs
    WHERE (
      (
        all_logs.resource.type = "cloud_run_revision" AND 
        SAFE.STRING(all_logs.resource.labels["service_name"]) = (SELECT apphosting_backend_id FROM vars) AND
        SAFE.STRING(all_logs.resource.labels["location"]) = (SELECT apphosting_backend_location FROM vars)
      ) 
      OR (
        all_logs.resource.type = "firebaseapphosting.googleapis.com%2fbackend" AND
        SAFE.STRING(all_logs.resource.labels["backend_id"]) = (SELECT apphosting_backend_id FROM vars)  AND
        SAFE.STRING(all_logs.resource.labels["location"]) = "global"
      )
    ) 
  )
Substitute your own values for apphosting_backend_id, apphosting_backend_location, and the log source used for all_logs.

Now, there’s an apphosting_logs table to query. Here’s how to get the top 10 referrers:

SELECT
  http_request.referer,
  COUNT(*) AS request_count
FROM
  apphosting_logs
WHERE
  http_request.referer IS NOT NULL
GROUP BY http_request.referer
ORDER BY request_count DESC
LIMIT 10