We have an application that uses Cloud Logging in GCP to log a sequence of events, like so (this is just a simplified example):
(Request 1) Log 1: Received HTTP request with JSON input
(Request 1) Log 2: Checked input format is correct
(Request 1) Log 3: Made a call to Datastore to fetch some data
(Request 1) Log 4: Modified JSON payload with new data
(Request 1) Log 5: Made HTTP request with new Payload
(Request 1) Log 6: Request successful
As you can see, the logs are informing the developer about a bunch of milestones that are reached throughout the lifetime of an HTTP request. However, we received a big number of these requests at the same time and when you look at Cloud Logging in GCP Console, it looks something like:
(Request 1) Log 1: Received HTTP request with JSON input
(Request 2) Log 1: Received HTTP request with JSON input
(Request 1) Log 2: Checked input format is correct
(Request 1) Log 3: Made a call to Datastore to fetch some data
(Request 1) Log 4: Modified JSON payload with new data
(Request 2) Log 2: Checked input format is correct
(Request 2) Log 3: Made a call to Datastore to fetch some data
(Request 1) Log 5: Made HTTP request with new Payload
(Request 1) Log 6: Request successful
It would be much easier for me to debug a specific HTTP request that went wrong if I could simply group all the logs from that request into a single object.
(Request 1): {
Log 1: Received HTTP request with JSON input
Log 2: Checked input format is correct
Log 3: Made a call to Datastore to fetch some data
Log 4: Modified JSON payload with new data
Log 5: Made HTTP request with new Payload
Log 6: Request successful
}
(Request 2): {
Log 1: Received HTTP request with JSON input
Log 2: Checked input format is correct
Log 3: Made a call to Datastore to fetch some data
Log 4: Modified JSON payload with new data
Log 5: Made HTTP request with new Payload
Log 6: Request successful
}
Is there any feature in Cloud Logging that I can leverage to achieve the solution above? How have people gotten around this issue before? Looking for any tips or help that could point me in the right directions.
Thank you.
Google Cloud Logging entries accept labels. If your application adds a request identifier label when it creates log entries you can effectively filter these entries in the Log Explore.
All the Cloud Logging client libraries support adding labels when writing entries. For instance, the Java client library supports building log entries by adding an individual label or a collection of labels. For example,
LogEntry entry = LogEntry.newBuilder(StringPayload.of(message))
.addLabel("request_id", "your_request_identifier")
.build()
If standard logging packages are used as wrappers to the Cloud Logging client libraries, it might be difficult to inject labels.