I am using Node.js 14
and I am trying to set a cloud function's name as one of its labels for billing purposes (I want to be able to display Cloud Function costs by function using Billing Export and BigQuery). According to docs , the environment variable K_SERVICE
will hold the function's name, and I can set labels like so:
export const myFunc = functions
.runWith({
labels: {
'project-id': process.env.GCLOUD_PROJECT ?? 'not-found',
'function-name': process.env.K_SERVICE ?? 'not-found',
},
})
.https.onRequest((request, response) => {
response.json({ fname: process.env.K_SERVICE ?? 'not-found' });
});
When I deploy this code, it will fail with the following error:
Detailed stack trace: Error: Invalid labels: myFunc. Label values can only contain lowercase letters, international characters, numbers, _ or -.
I need my functions to use camelCase naming, so I can't just change the name to use all lower case letters. So I update my code as follows:
export const myFunc = functions
.runWith({
labels: {
'project-id': (process.env.GCLOUD_PROJECT ?? 'not-found').toLowerCase(),
'function-name': (process.env.K_SERVICE ?? 'not-found').toLowerCase(),
'control-label': 'MY-VAL'.toLowerCase(),
},
})
.https.onRequest((request, response) => {
response.json({ fname: (process.env.K_SERVICE ?? 'not-found').toLowerCase() });
});
Now when I deploy this version, it succeeds to deploy, but after doing a function describe using gcloud functions describe myFunc
I get this in the labels section:
labels:
control-label: my-val
deployment-tool: cli-firebase
function-name: not-found
project-id: my-project-id
Notice several things here:
control-label
label works as expected, showing the value in all lower case (so functions like .toLowerCase()
work inside the .runWith()
method)project-id
label also works as expected, and although its original value is already in lower case, it is fetching the environment variable properly and displays it's value correctly even after applying the .toLowerCase()
function.function-name
label returns as not-found
, which is my default value if the environment variable were not set. But I had previously verified that the environment variable K_SERVICE
is indeed set, because if I don't apply the .toLowerCase()
on it, the deploy will fail saying the value (properly logging its value) has upper case letters.
Also note that when I call the function and return the value as part of the .onRequest()
method, it will correctly return the expected value myfunc
all lowercase and matching the function's name.
So what's going on here? Why would the value of the environment variable change after having the .toLowerCase()
method applied inside the .runWith()
method?
According to the documentation the behavior is normal:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.