使用Python查询GCP Stackdriver日志 [英] Using Python to Query GCP Stackdriver logs
问题描述
我正在使用Python3查询Stackdriver的GCP日志.不幸的是,具有重要数据的日志条目以"NoneType"而不是"dict"或"str"的形式返回给我.结果"entry.payload"的类型为"None",而"entry.payload_pb"具有我想要的数据,但出现乱码.
I am using Python3 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.
是否有一种方法可以让Stackdriver以干净的格式返回此数据,或者有一种我可以解析它的方法? 如果没有,是否有一种方法可以查询比我做的更好的数据并产生干净的数据?
Is there a way to get Stackdriver to return this data in a clean format, or is there a way I can parse it? If not, is there a way I should query this data that is better than what I am doing and yields clean data?
我的代码如下:
#!/usr/bin/python3
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)
"entry.payload_pb"数据始终像这样开始:
The "entry.payload_pb" data always starts like this:
type_url: "type.googleapis.com/google.cloud.audit.AuditLog"
value: "\032;\n9gcp-user@my-project.iam.gserviceaccount.com"I\n\r129.105.16.28\0228
推荐答案
实际上我错过了,但是您可以通过将环境变量GOOGLE_CLOUD_DISABLE_GRPC
设置为非空字符串,例如GOOGLE_CLOUD_DISABLE_GRPC=true
.
Actually I missed that but you can disable gRPC
and make the API return a dict
(JSON) payload by setting the environment variable GOOGLE_CLOUD_DISABLE_GRPC
to a non-empty string, e.g. GOOGLE_CLOUD_DISABLE_GRPC=true
.
这将填充payload
而不是payload_pb
-比编译可能过期的原型缓冲区更容易!
This will populate the payload
instead of payload_pb
- easier than compiling a proto buffer which may be out-of-date !
这篇关于使用Python查询GCP Stackdriver日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!