我正在尝试在我的GKE应用程序中使用GCP标签,我想记录与GKE关联的标签,例如namespace_name,location,cluster_name,project_id和pod_name,但是默认情况下,所有内容都记录在全局资源类型下,并且仅在GCP日志查看器中填充了project_id
示例:
我找到了一种通过使用Resource将资源类型更改为k8s_container并填充其他标签的方法,但是我似乎找不到一种无需将值直接硬编码到应用程序中即可填充标签的方法,请参见下面的示例:>
handler = CloudLoggingHandler(client,resource=Resource("k8s_container",labels={
'namespace_name': "my_namespace",
'location': "us-west1",
'cluster_name': "my-cluster",
'project_id': "my-project-id",
'pod_name': "my-pod"
}))
完整的应用程序供参考:
import logging
import google.cloud.logging
from google.cloud.logging.handlers import CloudLoggingHandler
from google.cloud.logging.resource import Resource
client = google.cloud.logging.Client().from_service_account_json(GOOGLE_SA_ACCOUNT)
handler = CloudLoggingHandler(client,resource=Resource("k8s_container",labels={
'namespace_name': "my_namespace",
'location': "us-west1",
'cluster_name': "my-cluster",
'project_id': "my-project-id",
'pod_name': "my-pod"
}))
google.cloud.logging.handlers.setup_logging(handler)
logging.getLogger().setLevel(logging.ERROR)
try:
requests.head('https://www.exampleurladdress.com').status_code == 200
except requests.exceptions.RequestException as err:
logging.error(err
我想知道是否有某种方法可以直接从GKE正确填充它们?
谢谢。