google cloud functions default environment variables not set

Viewed 1131

Are there any conditions to the default environment variables being set on google cloud function?

I have the following code:

func init() {
    projectID := os.Getenv("GCP_PROJECT")
    log.Printf("projectID: %s\n", projectID)

    functionName := os.Getenv("FUNCTION_NAME")
    log.Printf("functoinName: %s\n", functionName)

    region := os.Getenv("FUNCTION_REGION")
    log.Printf("region: %s\n", region)
}

and the values are empty.

Even if I do:

func GameUpdate(ctx context.Context, e FirestoreEvent) error {
    functionName := os.Getenv("FUNCTION_NAME")
    log.Printf("functoinName: %s\n", functionName)
}

They are still empty.

According to documentation, I would expect them to be set and available. But they are not :|

EDIT:

I am using go 1.13 as runtime and as Armatorix mentioned, these env variables are not available in that runtime...

Why I needed them was to write a wrapper for cloud.google.com/go/logging to be able to tag the severity of the logs.

I ended up prepending my stdout logs with [INFO]/[ERROR], and creating a tag from it \[([A-Z]+)\].*. Bonus is that I don't have to do a network call in my function to ship the logs.

Still disappointing that these environment variables are not available.

3 Answers

So I've read the same documentation. Here you've got the info that it works like this with go1.11 (And it works, I tested it out). BUT for go1.13 these are not set. You can still do it manually. Also I've checked which envs are set on 1.13 version.

From os.Envrion()

PATH=/layers/google.go.build/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
FUNCTION_SIGNATURE_TYPE=http
DEBIAN_FRONTEND=noninteractive
HOME=/root
K_REVISION=9
FUNCTION_TARGET=HelloWorld
PORT=8080
GOROOT=/usr/local/go/
CGO_ENABLED=1
PWD=/srv
K_SERVICE=function-1

So the env that you probably want to use is K_SERVICE

I have created a Feature Request on your behalf, in order for Cloud Functions Engineering team to implement the automatic set of these Environment Variables to the newer Runtime Versions, such as Node.js 10 and Go1.13.

You may "star" the issue so that it gets visibility and also include yourself in the "CC" section, in order to receive further updates posted on this thread.

I hope this helps.

I created a library for that very purpose: github.com/ncruces/go-gcf/logging

But you're right, on the Go 1.13 runtime, those environment variables are missing. On the migration guide they suggest setting them when you deploy.

Later I found that the recommended way of doing this is to use structured logging.

// Structured logging can be used to set severity levels.
// See https://cloud.google.com/logging/docs/structured-logging.
fmt.Println(`{"message": "This has ERROR severity", "severity": "error"}`)

So now, I'm in the process of "deprecating" my library, and creating a new one, with a simpler approach: github.com/ncruces/go-gcp/glog

This is simple enough that a library isn't really required, but it helps to correctly JSON escape message.

Related