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

查看:126
本文介绍了获取从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是否在上下文处理的metadata中.但是,根据我的测试和文档( https://godoc. org/cloud.google.com/go/functions/metadata#FromContext ),只有EventIDTimestampEventTypeResource存在.

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?

推荐答案

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天全站免登陆