# Follow instructions at https://docs.fastly.com/guides/streaming-logs/log-streaming-google-bigquery
# to set up your BigQuery table and add a BigQuery logger to your
# Fastly service.  This example assumes you have set up a logger
# called 'myBigQueryLogger' and your BigQuery table has the
# following schema:
#
# timestamp:TIMESTAMP,time_elapsed:FLOAT,is_tls:BOOLEAN,client_ip:STRING,geo_city:STRING,geo_country_code:STRING,request:STRING,host:STRING,url:STRING,request_referer:STRING,request_user_agent:STRING,request_accept_language:STRING,request_accept_charset:STRING,cache_status:STRING

log "syslog " req.service_id " myBigQueryLogger :: {"

  # Gotcha: BQ does not accept all ISO8616-compatible timestamps.
  # Take care to use a format that can be ingested if you are
  # populating a BQ TIMESTAMP field
  {""timestamp":""} strftime({"%Y-%m-%dT%H:%M:%S"}, time.start) {"", "}
  {""time_elapsed":"} time.elapsed.usec {", "}
  {""is_tls":"} if(req.is_ssl, "true", "false") {", "}
  {""client_ip":""} req.http.Fastly-Client-IP {"", "}
  {""geo_city":""} client.geo.city {"", "}
  {""geo_country_code":""} client.geo.country_code {"", "}
  {""request":""} req.request {"", "}
  {""host":""} req.http.host {"", "}
  {""url":""} cstr_escape(req.url) {"", "}
  {""request_referer":""} cstr_escape(req.http.Referer) {"", "}
  {""request_user_agent":""} cstr_escape(req.http.User-Agent) {"", "}
  {""request_accept_language":""} cstr_escape(req.http.Accept-Language) {"", "}
  {""request_accept_charset":""} cstr_escape(req.http.Accept-Charset) {"", "}
  {""cache_status":""} regsub(fastly_info.state, "^(HIT-(SYNTH)|(HITPASS|HIT|MISS|PASS|ERROR|PIPE)).*", "\2\3")  {"""}
"}";