这是我的代码的超级缩写版本(我将在注释中添加指向所有内容的链接)
import ...
...
def main(data, context):
log_client = logging.Client()
log_name = 'cloudfunctions.googleapis.com%2Fcloud-functions'
res = Resource(type="cloud_function",
labels={
"function_name": "refresh_classes",
"region": os.environ.get("FUNC_REGION")
})
logger = log_client.logger(log_name.format(os.environ.get("PROJECT_ID")))
db = sqlalchemy.create_engine(
sqlalchemy.engine.url.URL(
drivername="mysql+pymysql",
username=os.environ.get("DB_USER"),
password=os.environ.get("DB_PASS"),
host=os.environ.get("DB_HOST"),
port=3306,
database=PRIMARY_TABLE_NAME
),
pool_size=5,
max_overflow=2,
pool_timeout=30,
pool_recycle=1800
)
start_time = perf_counter()
check_if_table_exists(db)
for i in range(START_IDX, END_IDX):
print(i)
logger.log_text(f"Checking class with id {i}", resource=res, severity="INFO")
...
logger.log_text(f"Total seconds elapsed: {perf_counter() - start_time}", resource=res, severity="INFO")
if __name__ == '__main__':
main('data', 'context')
当我在本地配置上述GOOGLE_APPLICATION_CREDENTIALS
并运行本地Cloud MySQL代理的情况下运行上述云功能时,将进行详细的云日志记录,并且该功能将顺利完成,完全符合我的期望:
但是,当我将整个组件部署到GCP并尝试通过控制台(云消息传递触发器)触发它时,我得到的只是日志记录:
实际文本
{
insertId: "******"
labels: {
execution_id: "******"
}
logName: "projects/******/logs/cloudfunctions.googleapis.com%2Fcloud-functions"
receiveTimestamp: "2020-05-29T22:11:13.435688367Z"
resource: {
labels: {
function_name: "******"
project_id: "******"
region: "us-central1"
}
type: "cloud_function"
}
severity: "DEBUG"
textPayload: "Function execution started"
timestamp: "2020-05-29T22:11:03.069889708Z"
trace: "projects/******/traces/******"
}
{
insertId: "******"
labels: {
execution_id: "******"
}
logName: "projects/******/logs/cloudfunctions.googleapis.com%2Fcloud-functions"
receiveTimestamp: "2020-05-29T22:11:16.331311285Z"
resource: {
labels: {
function_name: "******"
project_id: "******"
region: "us-central1"
}
type: "cloud_function"
}
severity: "DEBUG"
textPayload: "Function execution took 12362 ms, finished with status: 'crash'"
timestamp: "2020-05-29T22:11:15.430033249Z"
trace: "projects/******/traces/******"
}
*我真的不知道什么是敏感信息,什么不是,所以我只是给一些随机的东西加注了
在撰写本文时,我意识到更多的日志记录可能会有所帮助,因此我将Google记录器插入了记录器设置,数据库设置和我运行的表检查之间。
该功能在安装Google记录器之前就崩溃了。
因此,在这一点上,我现在还不确定是什么破坏了我的功能,而且我也不知道如何查找,因为Google Cloud日志记录没有帮助。错误json具有trace
属性,这看起来确实很有希望,因为此时我需要的只是Python堆栈跟踪,但我不知道是否有办法看到它。
我应该注意,我通过GCP的Cloud Function控制台配置了环境变量。
原则上,有两件事会有所帮助:
答案 0 :(得分:1)
所以我最终弄清楚了,如果您不是通过Cloud Scheduler触发您的云功能,而是通过Test Function
手动运行该功能
GCP将给您引发异常。就我而言,我的Cloud MySQL连接失败
Error: function terminated. Recommended action: inspect logs for termination reason. Details:
(pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '**.***.**.***' (timed out)")
(Background on this error at: http://sqlalche.me/e/e3q8)
所以我只需要引入一个环境变量即可在通过代理和unix sockets进行连接之间进行切换,例如:
if os.environ.get("ENV") == "local":
db = sqlalchemy.create_engine(
sqlalchemy.engine.url.URL(
drivername="mysql+pymysql",
username=os.environ.get("DB_USER"),
password=os.environ.get("DB_PASS"),
host=os.environ.get("DB_HOST"),
port=3306,
database=PRIMARY_TABLE_NAME
),
pool_size=5,
max_overflow=2,
pool_timeout=30,
pool_recycle=1800
)
else:
db = sqlalchemy.create_engine(
sqlalchemy.engine.url.URL(
drivername="mysql+pymysql",
username=os.environ.get("DB_USER"),
password=os.environ.get("DB_PASS"),
database=PRIMARY_TABLE_NAME,
query={"unix_socket": "/cloudsql/{}".format(os.environ.get("CLOUD_SQL_CONNECTION_NAME"))}
),
pool_size=5,
max_overflow=2,
pool_timeout=30,
pool_recycle=1800
)