google-cloud-python icon indicating copy to clipboard operation
google-cloud-python copied to clipboard

Stackdriver Logging payload_pb unreadable

Open VenturaAtDD opened this issue 8 years ago • 3 comments

I am using Python3 (google-cloud-logging (1.4.0)) to query Stackdriver for GCP logs. Unfortunately, the log entries that have important data are returned to me as "NoneType" instead of as a "dict" or a "str". The resulting "entry.payload" is type "None" and the "entry.payload_pb" has the data I want, but it is garbled (most other log entries are readable and present in "entry.payload").

When I use gcloud (0.18.3) to query for these records based on their insertIDs, I get a clean copy in entry.payload, so I know GCP writes the data in a sane way (I can access it through the Stackdriver portion of the GCP console as well).

My code looks something like this:

from google.cloud.logging import Client, ASCENDING, DESCENDING from google.oauth2.service_account import Credentials

projectName = 'my_project' myFilter = 'logName="projects/' + projectName + '/logs/compute.googleapis.com%2Factivity_log"'

client = Client(project = projectName) entries = client.list_entries(order_by=DESCENDING, page_size = 500, filter_ = myFilter) for entry in entries: if isinstance(entry.payload, dict): print(entry.payload) if isinstance(entry.payload, str): print(entry.payload) if isinstance(entry.payload, None): print(entry.payload_pb)

The garbled entry.payload_pb entries all start the same and look something like this:

type_url: "type.googleapis.com/google.cloud.audit.AuditLog" value: "\032;\[email protected]"I\n\r129.105.16.28\0228

VenturaAtDD avatar May 12 '18 23:05 VenturaAtDD

ProtobufEntry.payload_pb is an instance of google.protobuf.any_pb2.Any. Per the protobuf docs, you can unpack it into a known message type via:

from my_package import my_message_pb2

if entry.payload_pb.Is(my_messsage_pb2.MyMessage.DESCRIPTOR):
    message = my_message_pb2.MyMessage()
    entry.payload_pb.Unpack(message)
    # do something with message

tseaver avatar May 16 '18 17:05 tseaver

For reference, here are the docs for the AuditLog message.

tseaver avatar May 16 '18 17:05 tseaver

I'm having the same issue. Is there a recommended way to import AuditLog so we can unpack? thanks

ollyjshaw avatar Jun 11 '19 16:06 ollyjshaw