获取从 PubSub 事件触发的 Google Cloud Functions 的执行 ID [英] Get execution ID for Google Cloud Functions triggered from PubSub event

查看:19
本文介绍了获取从 PubSub 事件触发的 Google Cloud Functions 的执行 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于从 HTTP 触发的 Google Cloud Functions,可以通过检查 HTTP 请求的标头(Function-Execution-Id")来检索执行 ID:

For Google Cloud Functions triggered from HTTP, it is possible to retrieve the execution id by inspecting the headers of the HTTP request ("Function-Execution-Id") :

package p

import (
    "fmt"
    "net/http"
)

func F(w http.ResponseWriter, r *http.Request) {
    executionID := r.Header.Get("Function-Execution-Id")
    fmt.Println(executionID)
}

但是,对于由 PubSub 事件触发的 GCF,我找不到如何检索此执行 ID:

However, for GCF triggered by PubSub events, I can't find how to retrieve this execution ID :

package p

import (
    "context"
)

type PubSubMessage struct {
    Data []byte `json:"data"`
}

func F(ctx context.Context, m PubSubMessage) error {
    executionID := "" // ???
    fmt.Println(executionID)
    return nil
}

我查看了 PubSubMessage (https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage),但它只包含 data + 一个空的 attributes 地图.

I have looked into the PubSubMessage (https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage), but it only contains data + an empty attributes map.

我还检查了执行 ID 是否在上下文处理的 元数据 中.但是,根据我的测试和文档(https://godoc.org/cloud.google.com/go/functions/metadata#FromContext),只有 EventIDTimestampEventType 和 <代码>资源存在.

I have also checked if execution ID is in the metadata handled by the context. However, from my tests, and the docs (https://godoc.org/cloud.google.com/go/functions/metadata#FromContext), only EventID, Timestamp, EventType and Resource are present.

如何检索由 PubSub 事件触发的 GCF 函数的执行 ID?

How can I retrieve the execution id of a GCF function triggered by a PubSub event?

推荐答案

这似乎不再准确.请参阅 ProGirlXOXO 的其他答案.

Pub/Sub 触发的事件没有执行 ID;相反,它有一个包含在上下文元数据中的 EventID,它是事件的唯一 ID.

A Pub/Sub-triggered event does not have an execution ID; instead it has an EventID contained in the context metadata, which is a unique ID for the event.

您可以通过如下方式访问EventID:

You can access the EventID as follows:

import (
    "context"
    "log"
    "cloud.google.com/go/functions/metadata"
)

func F(ctx context.Context, m PubSubMessage) error {
    ctxMetadata, err := metadata.FromContext(ctx)
    if err != nil {
        log.Fatal(err);
    }
    log.Println("EventID: " + ctxMetadata.EventID)
    return nil
}

这篇关于获取从 PubSub 事件触发的 Google Cloud Functions 的执行 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆