InfoQServerless 架构下如何实现日志的实时输出?
作者 | 刘宇策划 | 田晓旭Serverless 白皮书中曾描述过 Serverless 的一些缺点 , 例如难以调试、冷启动严重等等 。 其中难以调试是表现在多个方面的 , 有一个方面是日志输出 。
当我们把 Serverless 架构应用于实际项目 , 就会发现调试成为了效率的重要影响因素 。 以日志输出为例 , 某个函数被触发之后未得到预期结果 , 大家第一想法就是查看日志 , 但这时输出的日志可能并未是我们想要的 , 而且云厂商输出日志的延时也非常高 。
日志输出现状以腾讯云云函数为例 , 我们可以看一下其日志输出情况:
本文插图
通过这个测试功能 , 可以很快获取到函数的结果 , 并查看日志信息 。
- 通过 API 网关、COS 等触发云函数 , 此处以 API 网关为例:
本文插图
通过函数日志查看何时会刷出这个日志:
本文插图
这个过程大概有 11S , 通过代码来进行更加详细的测试:
importjson,timefrom tencentcloud.commonimportcredentialfrom tencentcloud.common.profile.client_profileimportClientProfilefrom tencentcloud.common.profile.http_profileimportHttpProfilefrom tencentcloud.common.exception.tencent_cloud_sdk_exceptionimportTencentCloudSDKExceptionfrom tencentcloud.scf.v20180416importscf_client, modelstry:cred = credential.Credential("","")httpProfile =HttpProfilehttpProfile.endpoint ="scf.tencentcloudapi.com"clientProfile =ClientProfileclientProfile.httpProfile = httpProfileclient = scf_client.ScfClient(cred,"ap-guangzhou", clientProfile)req = models.InvokeRequestparams = '{"FunctionName":"test"}'req.from_json_string(params)resp = client.Invoke(req)functionRequestId = json.loads(resp.to_json_string)["Result"]["FunctionRequestId"]print(time.time, functionRequestId)whileTrue:time.sleep(0.2)req = models.GetFunctionLogsRequestresp = client.GetFunctionLogs(req)iffunctionRequestIdinstr(resp.to_json_string):breakprint(time.time)1584108001.141546ee7243dd-6532-11ea-8bce-5254000c8aa41584108005.2496068这次输出结果是 4S , 再做一个多次调用的时间对比图: importjsonimporttimeimportnumpyimportmatplotlib.pyplot as plttry:timeList = foriinrange(0,100):startTime = int(time.time)whileTrue:breakendTime = int(time.time)timeList.append(endTime - startTime)print("最大时间", int(max(timeList)))print("最小时间", int(min(timeList)))print("平均时间", int(numpy.mean(timeList)))plt.figure
推荐阅读
- 湖人队|扎心?一张图看湖人如何应对马赛克三分雨,神龟5米范围没人理他
- 搜狐新闻|准大学生数码三件套如何选?这些元气好物值得种草
- 想好好做医生的胖子|五十岁女性经常有周身无力和疼痛的困扰!该如何应对?医生告诉您
- 儿童饮食|秋冬季如何让宝宝爱上吃水果?稍微蒸一下美味翻倍,营养价值更高
- 队员|北极科考中“遭遇”北极熊,看科考队员如何与它们和谐相处
- 中国历史发展过程|中国历史发展过程!中国游戏外挂发展史:“辅助工具”是如何成长为“苍
- 金色年华录|都是男扮女装谈恋爱,《少女大人》比《女世子》如何?
- 上海市嘉定区人民政府网站|每年消耗3350吨蔬菜150吨肉,嘉定这家中央厨房如何践行“光盘行动”?
- 上海嘉定|如何让《民法典》走进群众心里?嘉定这些街镇和单位这样做
- 没蜡笔的小新|“整容脸”应该被鄙视吗?医生告诉你千篇一律的好看皮囊如何而来
