GCP 上的云功能:没有退出日志

Cloud function on GCP: no exit log

我有一个由主题 pubsub 触发的 google 云函数。

大多数情况下,一切正常,如下所示:

2021-11-06 15:12:38.830 GMT vote_stage oga5hymuzx4p Function execution started
2021-11-06 15:13:45.392 GMT vote_stage oga5hymuzx4p Function execution took 66563 ms, finished with status: 'ok' 

但是很遗憾,二十一次,我只能看到这个:

2021-11-06 15:08:45.139 GMT vote_stage rvfnd4wo5nwd Function execution started

怎么可能没有退出日志?

下面是函数的代码:

def vote_stage(event, context):
    assert context == context
    call_datetime = reusable.time.get_now()
    game_id = event['attributes']['game_id']
    logger.info(f'start, game_id={game_id}')
    game = build_game(game_id)
    resp = ut.exceptions.ExceptionsHandler(game).handle_vote_stage_exceptions()
    if resp:
        logger.info(f'exception, game_id={game_id}')
        return resp
    game.dict['vote_stage_last_trigger'] = reusable.time.get_now()
    ut.firestore.FirestoreEditor(game).set_game(merge=True)

    while True:
        game = build_game(game_id)
        ut.slack.SlackOperator(game).update_vote_stage_lower()
        c1 = len(game.frozen_guessers) == 1
        c2 = game.time_left_to_vote <= 0
        c3 = not game.remaining_potential_voters
        if c1 or c2 or c3:
            game.dict['frozen_voters'] = deepcopy(game.dict['voters'])
            game.dict['vote_stage_over'] = True
            ut.firestore.FirestoreEditor(game).set_game(merge=True)
            game.stage_triggerer.trigger_pre_result_stage()
            logger.info(f'pre_result_stage triggered, game_id={game_id}')
            return make_response('', 200)
        if ut.time.datetime1_minus_datetime2(
                reusable.time.get_now(),
                call_datetime) > game.self_trigger_threshold:
            game.stage_triggerer.trigger_vote_stage()
            logger.info(f'vote_stage self-triggered, game_id={game_id}')
            return make_response('', 200)
        time.sleep(game.refresh_interval)

此处,game.self_trigger_threshold = 60 秒,game.refresh_interval = 9 秒。 vote_stage 函数的超时时间为 540s。

方法 game.stage_triggerer.trigger_pre_result_stage() 向主题“topic_pre_result_stage”发布消息,触发“pre_result_stage”云函数。

方法 game.stage_triggerer.trigger_vote_stage() 向主题“topic_vote_stage”发布消息,触发“vote_stage”云函数。

我找到的唯一内部日志是:

2021-11-06 15:08:49.594 GMT vote_stage rvfnd4wo5nwd 2021-11-06 15:08:49,594 - INFO - start, game_id=20211106_150714&T01LTPERV4Z&C02EKL5PQAX&U01LFAZNKPV&2694551026434.1707796879169.ce017050e1e74e97332c2a04d4b19c08

这是旧版日志查看器的屏幕截图(拍摄于 2021 年 11 月 7 日),显示了此函数执行的所有日志(execution_id = rvfnd4wo5nwd):

这是旧版日志查看器的屏幕截图(拍摄于 2021 年 11 月 7 日),其中显示了 vote_stage 函数的所有级别为“错误”的日志:

可以看到,none 是从 2021-11-06 开始的。

Cloud Functions 中丢失的日志条目通常不是预期的,可能是由于进程在到达日志转发到云监控阶段之前终止而导致的。丢失的日志条目通常不是问题,但可能表示某些配置不平衡。过早的资源终止可能是由于耗尽了一些限制造成的。看起来您的函数需要花费大量时间。

vote_stage oga5hymuzx4p Function execution took 66563 ms, 
finished with status: 'ok'

默认超时为 1 分钟,可以延长到 9 分钟,因为您的函数表示它有 540 秒的超时。

在您的设置中,该函数的超时时间接近限制,它恰好消耗了大部分执行时间。因此,可能不会捕获日志。尝试 Cloud Functions 配置并提供更多资源可能会有所帮助,但这还不够,因为这里不知道根本原因。

我建议的最佳选择是联系 Google Cloud Support 以正确检查后台发生的情况并正确了解函数的流量特征。 GCP 提供免费试用版,您可以试用,并且也提供对它的支持。

我没有错误了。我做了以下更改:我将 publisher.publish(**kwargs) 替换为 publisher.publish(**kwargs).result() 该函数用于触发pubsub主题。

如此处所述https://cloud.google.com/functions/docs/troubleshooting:如果您的函数创建异步任务,它还必须显式等待这些任务完成。