Google Cloud Storage logging
The Google Cloud Storage integration keeps a complete, long lived record of gateway traffic as newline-delimited JSON files in a bucket you own, one line per request. It is the simplest way to retain every request for analysis with BigQuery or your own pipelines without running another observability service. A companion integration publishes the same payloads to Pub/Sub for consumers that need them as they happen.
A real-world scenario
A data team wants a year of model usage to analyse cost per product in BigQuery, and the gateway's database should stay small. They turn on the bucket integration with a short spend log retention period in the gateway. Every request is written to the bucket as it completes, the database only keeps the last 30 days, and a BigQuery external table over the bucket answers the cost questions for the whole year.
Turn it on from the Admin UI
- Sign in as a proxy admin and open Settings, then Logging & Alerts. Click Add Callback.
- Pick Google Cloud Storage in the Callback dropdown and fill in the bucket name and the path to the service account JSON on the gateway host.
- Click Add Callback. The table shows a
gcs_bucketrow with the badge Success. - Open the row's actions menu and click Test, then list the bucket for a new object.
Turn it on in the config file
spanway_settings:
callbacks: ["gcs_bucket"]
| Variable | Purpose |
|---|---|
GCS_BUCKET_NAME | Bucket to write to. |
GCS_PATH_SERVICE_ACCOUNT | Path to the service account JSON used to authenticate. Omit to use application default credentials. |
GCS_USE_BATCHED_LOGGING | true (the default) buffers logs and writes one <date>/batch-<id>.ndjson file per batch, which keeps the number of objects and API calls low. false writes one object per request instead, named <date>/<response id> (<date>/failure-<id> for a failed request), which is easier to browse but costs one write per request. |
GCS_BATCH_SIZE, GCS_FLUSH_INTERVAL | When batching is on, a batch is written once it holds GCS_BATCH_SIZE requests (default 2048) or every GCS_FLUSH_INTERVAL seconds (default 20), whichever comes first. |
GCS_PUBSUB_PROJECT_ID, GCS_PUBSUB_TOPIC_ID | For the Pub/Sub companion (gcs_pubsub), the Google Cloud project and the topic the payloads are published to. |
The service account needs permission to create objects in the bucket. Teams can log to their own buckets by attaching the gcs_bucket callback with gcs_bucket_name and gcs_path_service_account in callback_vars, see team logging.
Check that objects arrive
-
Send one request through the gateway:
curl -X POST http://localhost:4000/v1/chat/completions \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"model": "claude-haiku-5-5", "messages": [{"role": "user", "content": "Hi"}], "metadata": {"tags": ["bucket-test"]}}' -
Wait up to 20 seconds and list the bucket. Expected result: a new
batch-<id>.ndjsonobject under today's date (or, withGCS_USE_BATCHED_LOGGING=false, an object named after the response id), whose lines are JSON documents withmodel,messages,response, token counts,response_cost, the start and end time, and the key alias, team, user and tags.
What is written
Each object is the gateway's standard logging payload, with the request id, model, provider, messages and response (unless message logging is turned off), token usage, cost, timestamps, the key alias, user, team, organization, end user, tags and metadata. Objects are grouped by date folder, so you can load a day of traffic into BigQuery with a wildcard path such as gs://<bucket>/2026-11-23/*.
spanway_settings:
callbacks: ["gcs_bucket"]
turn_off_message_logging: true
With message logging off, prompts and responses are left out and the objects hold usage, cost and identity only.
Pub/Sub
spanway_settings:
callbacks: ["gcs_pubsub"]
The Pub/Sub logger publishes the same payload to a topic, which suits pipelines that need logs in near real time, for example to feed a data warehouse through a streaming subscription. From the Admin UI pick Google Cloud Pub/Sub and fill in the project ID, the topic ID and the service account path.
Operational notes
Writes happen after the response is returned, so logging never adds latency to requests. If the bucket is unreachable the error is logged and the request is unaffected. Pair this integration with cost tracking retention settings to keep the database small while the bucket holds history.
Errors
| Status | Where | Meaning | Fix |
|---|---|---|---|
| No objects appear | Bucket | The service account cannot create objects, the bucket name is wrong, or batching is on and the batch has not flushed. | Grant the object creator role, check GCS_BUCKET_NAME, and wait for GCS_FLUSH_INTERVAL. |
Objects hold {} for messages and response | Bucket | Message logging is off or the request was under zero data retention. | By design, see what Spanway stores. |
| 400 naming the bucket and the service account | Saving the callback in the Admin UI | The bucket changed while the stored credential path was kept. | Enter the service account path again with the new bucket. |
403 naming scoped_integrations | Team callback | Per team buckets need the license feature. | Use the gateway wide callback, or add the feature. |
Logs
| Level | Line | Meaning |
|---|---|---|
| ERROR | Spanway.LoggingError: [Non-Blocking] Exception occurred while success logging <traceback> | The write to the bucket failed. The traceback holds the Google API answer, usually a permission or bucket name problem. The request was unaffected. |
Related
- Logging for every destination.
- Team logging for per team buckets.
- Cost tracking for the retention settings that pair with a bucket.
- Error reference and log reference.