dpvmjk0479 2019-05-15 15:21
浏览 48
已采纳

获取从PubSub事件触发的Google Cloud Functions的执行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)
}

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
}

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.

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.

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

  • 写回答

1条回答 默认 最新

  • doubangzhang6992 2019-05-15 20:48
    关注

    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.

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口