A simple Go service that demonstrates how to consume Google Cloud Logging logs forwarded by a Log Sink from a Pub/Sub push subscription, and sort them into a Google Cloud Storage (GCS) bucket as timestamped JSON files.
- Consumes log entries from a Pub/Sub push subscription.
- Parses and validates the log entries.
- Sorts logs into GCS buckets based on their timestamp.
- Creates JSON files for each log entry.
- Handles errors and retries gracefully.
- Cloud Logging is configured to export logs to a Pub/Sub topic using a Log Sink.
- A Pub/Sub push subscription is created for the topic, with the endpoint pointing to this service.
- When new logs are generated, Cloud Logging sends them to the Pub/Sub topic.
- Pub/Sub pushes the log messages to this service's webhook endpoint.
- The service receives the push message, parses the log entry, extracts the
timestamp, and arbitrary non-standard log fields from the
jsonPayload
of the log entry. In the provided example, the service looks for atenant_id
and ajob_id
. - The service creates a JSON file containing the log entry and uploads it to a GCS bucket. The file is named using the tenant ID, job ID and the log's timestamp to ensure chronological order.
- A Google Cloud Platform (GCP) project.
- The
gcloud
CLI installed and configured. - A GCS bucket.
- A Pub/Sub topic and a push subscription.
The service is configured using environment variables:
Variable | Description | Default |
---|---|---|
TARGET_BUCKET |
The name of the GCS bucket to store logs. | "" |
- Clone the repository:
git clone https://github.com/NucleusEngineering/log-sorter.git
- Set the required environment variables:
export TARGET_BUCKET="your-gcs-bucket-name"
- Run the service:
go run main.go
You can deploy this service to any platform that supports Go applications, such as Google Cloud Run, Google Kubernetes Engine (GKE), or Google App Engine.
A Dockerfile
is included for easy containerization.
- Build the container image:
gcloud builds submit --tag gcr.io/$PROJECT_ID/log-sorter
- Deploy the service to Cloud Run:
gcloud run deploy log-sorter \ --image gcr.io/$PROJECT_ID/log-sorter \ --platform managed \ --region us-central1 \ --set-env-vars="TARGET_BUCKET=your-gcs-bucket-name"
This project is licensed under the Apache 2.0 License.