InfoQServerless 架构下如何实现日志的实时输出?( 四 )

retmsg = {}retmsg['errNo'] =0retmsg['errMsg'] ="ok"retmsg['websocket'] = {"action":"connecting","secConnectionID": connectionID}if"secWebSocketProtocol"inevent['websocket'].keys:retmsg['websocket']['secWebSocketProtocol'] = event['websocket']if"secWebSocketExtensions"inevent['websocket'].keys:ext = event['websocket']['secWebSocketExtensions']retext = exts = ext.split(";")print(exts)foreinexts:e = e.strip(" ")ife =="permessage-deflate":ife =="client_max_window_bits":passretmsg['websocket']['secWebSocketExtensions'] =";".join(retext)print("connecting: connection id:%s"% event['websocket']['secConnectionID'])上报函数:用户开启实时日志成功之后 , 业务函数上报数据 。 body = json.loads(event["body"])url = os.environ.get("url")retmsg = {}retmsg['websocket'] = {}retmsg['websocket']['action'] ="data send"retmsg['websocket']['secConnectionID'] = body["coid"]retmsg['websocket']['dataType'] ='text'retmsg['websocket']['data'] = body["data"]print(retmsg)requests.post(url, json=retmsg)returnTrue清理函数:客户端关闭链接时触发的函数 , 部分操作是注册函数的逆操作 。 importjson, osimportrequeststry:"Value":"close"params = json.dumps({"FunctionName": name,"Namespace": namespace,"ShowCode":"FALSE"})continueprint(environmentVariablesList)req = models.UpdateFunctionConfigurationRequestparams = json.dumps({"FunctionName": name,},"Namespace": namespace})resp = client.UpdateFunctionConfiguration(req)print(resp.to_json_string)returnTrueprint(e)returnFalseregion = os.environ.get("bucket_region")secreetId = os.environ.get("TENCENTCLOUD_SECRETID")secretKey = os.environ.get("TENCENTCLOUD_SECRETKEY")token = os.environ.get("TENCENTCLOUD_SESSIONTOKEN")config = CosConfig(Region=region, SecretId=secreetId, SecretKey=secretKey, Token=token)client = CosS3Client(config)response = client.get_object(Key=connectionID,)response['Body'].get_stream_to_file('/tmp/connid.json')withopen('/tmp/connid.json')asf:data = http://news.hoteastday.com/a/json.loads(f.read)data["function"],data["namespace"],data["region"],):retmsg = {}retmsg['websocket']['action'] ="closing"retmsg['websocket']['secConnectionID'] = connectionIDrequests.post(os.environ.get("url"), json=retmsg) return retmsg业务函数上报数据的逻辑 , 实际上就是修改常见组件的日志方法 , 以 Python 为例 , 例如重写


推荐阅读