GoogleCloudPlatform / functions-framework-go

FaaS (Function as a service) framework for writing portable Go functions
https://godoc.org/github.com/GoogleCloudPlatform/functions-framework-go
Apache License 2.0
457 stars 63 forks source link

Error converting CloudEvent to Background Event for Firestore Trigger Gen2 #251

Open yurifedoseev opened 3 weeks ago

yurifedoseev commented 3 weeks ago

Hello, I'm migrating an existing Gen1 Firestore Trigger to Gen2 via this tutorial https://cloud.google.com/functions/docs/calling/cloud-firestore

The gen2 function is deployed successfully however it fails on parsing an incoming request before even passing the data to the handler. I see in the logs:

INFO 2024-08-19T06:10:04.013460Z [httpRequest.requestMethod: POST] [httpRequest.status: 200] [httpRequest.responseSize: 8.34 KiB] [httpRequest.latency: 3 ms] [httpRequest.userAgent: APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)] https://storage-config-shop-products-gen2-rdl5god7nq-uc.a.run.app/?__GCP_CloudEventsMode=CE_PUBSUB_BINDING
DEFAULT 2024-08-19T06:10:04.017480Z error converting CloudEvent to Background Event: unable to unmarshal CloudEvent data:

... all document fields
<bunch of document fields dumped>
bonusPercent, error: invalid character 'ß' looking for beginning of value
Error: unexpected end of JSON input, parsing background event:

image

The firestore document itself is a regular doc with sub-objects and with an array field. I've tried to simplify the doc just to be a plain map: the result is the same.

The same GEN 1 function is working fine with no issue.

Deployment:

The function is registered as in the example:

func init() {
    // gen2 register
    slog.Info("register MyFunc as a cloud event")
    functions.CloudEvent("MyFunc", MyFunc)
}

func MyFunc(ctx context.Context, event firestore.FirestoreEvent) error {
 ...
}

I see register MyFuncas a cloud event message in the logs on runtime start

yurifedoseev commented 3 weeks ago

The source of the issue is that go.mod and go.sum files are excluded in .gcloudignore.functions. A couple years ago it wasn't possible to deploy vendor dependencies together with go.mod. I've removed this files from .gcloudignore.functions and it solved the error.

Looks like the build process adds a very old version of github.com/GoogleCloudPlatform/functions-framework-go if go.mod is missing (even with uploading go vendor)