firebase-functions
firebase-functions copied to clipboard
Functions v2 missing labels with Firebase structured logs
Firebase logger structured logs for v2 Cloud functions are missing labels.
This causes a secondary issue, which is that, when trying to access function logs from Firebase console, the logs are empty. Reason being that the URL generated when you click 'View Logs' (see screenshot below) assumes that the function has appropriate labels, but it does not:

Related issues
Maybe this: https://github.com/firebase/firebase-functions/issues/703
[REQUIRED] Version info
**node:**16 **firebase-functions:**3.24.1 **firebase-tools:**11.19.0 **firebase-admin:**11.3.0
[REQUIRED] Test case
Create a log from a v2 Cloud Function. Invoke the function. Go to Firebase console > Functions. Click on 'View logs' for previously created function. Logs will be empty because labels are missing.
[REQUIRED] Steps to reproduce
Create a v2 Cloud Function and, using logger, create a structured log, for example:
functions.logger.debug('Hello world', { structuredData: true });
Inspect the output in your logs.
[REQUIRED] Expected behavior
v2 function logs are similar to v1 function logs, which include labels: function name and version, like this example v1 log label:

[REQUIRED] Actual behavior
v2 function does not include any labels.
Were you able to successfully deploy your functions?
Yes.
No
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
Ah yes, I ran into this as well, and created a ticket here https://github.com/firebase/firebase-js-sdk/issues/6925
Basically, V2. functions are grouped under the "Cloud Run Revision" resource type. If you look in my linked ticket, you'll see that they had this same bug in the cloud function logging packages for Go + Java.
Hey @dan-wu-open, thanks for your reply. I don't understand why your ticket was closed, though, as the issue seems to be unsolved. I don't see how the reply to your problem - linking to https://github.com/firebase/firebase-functions - addresses the issue. Am I missing something?
I think it's because the root cause is with firebase-functions, so we need to make a change in that package, rather than in firebase-js-sdk
Oh got it, thanks for the clarification @dan-wu-open. Guess this issue is in the right place then.
As a work-around using resource.labels.configuration_name instead of resource.labels.function_name seems to work (although note that underscrores in the function names will need to be replaced with hyphens).
I just migrated my first project from gen1 to gen2 callable functions and the logging is just terrible. It's not just that the labels are (still) missing but with gen1 function execution time appeared in the "function finished..." built in log message. Also any custom logging done in code is also missing labels. Huge downgrade from gen1 log message structure.
I also just upgraded to gen 2.
When looking at the logs I was surprised that it is no longer possible to filter logs which belong to one invocation, as 'execution_id' is missing. This is a crucial mechanism to inspect the logs in order to trace what happened during this particular invocation.
How to do that without the labels? No mention of this topic in the migration guide either.
Is there any track of this issue? I just upgrade to v2 and I don't have the execution id anymore, nor the function name anywhere in the logs. And the logs of execution started and execution finished are missing. Is there going to be any feature related to that in the following releases? I'm kinda disapointed..
Need this as well
Has anyone figured out a workaround to filter by one execution?
Is anyone at Google actually listening to these issues? The V2 functions seem broken in so many ways.
Has anyone figured out a workaround to filter by one execution?
We created a custom wrapper for the logging. First for every invocation, a random value is created and used as execution ID. We pass this object as last argument to the log methods.
export function createExtendedLogContext(): ExtendedLogContext {
// create a random execution id with 10 digits and chars
const execution_id = Math.random().toString(36).substring(2, 12)
return {
'logging.googleapis.com/labels': {
execution_id,
function_name: process.env.FUNCTION_NAME || ''
}
}
}
Just ran into this issue when migrating from v1 function to v2.
With v1 functions I made no changes to my codebase, and all logging through console.log had an execution id attached. This allowed me to match log lines with requests using the labels.execution_id.
The v1 docs say that you need to include a compatibility patch to get this behaviour, but I did not. Seems this was the default behaviour.
Moving to v2 I noticed:
console.logno longer patched by default- Including the patch as per v2 docs does not do anything
My workaround:
- Use the firebase log functions from
"firebase-functions/logger" labels.execution_idis gone, appears to be replaced bytrace
If you want to do something like @glumb I would suggest looking at the "x-cloud-trace-context" header that cloud functions receive. You can extract the trace id from this header. This would allow you to join your log requests with the standard log lines.
Really surprised to see no mention of this change on the version comparison chart.
How does one get at the headers outside of onRequest / onCall @HonahleeWill?
How does one get at the headers outside of onRequest / onCall @HonahleeWill?
Not gonna be possible as far as I know. The trace is generated per-request, so you will need access to the request headers to read it. You will need to generate a new logging context for each request (also ensure that context isn't shared by other requests).
Looks like the code in https://github.com/firebase/firebase-functions/blob/3e7a4b77967e46a067712445c499c5df005b8e31/src/logger/index.ts#L158 is reading from traceId set somewhere, but I wasn't able to find where in firebase-functions that traceId was actually set.
I ended up just writing my own patch for console logging. This might help you @lox:
import { log as logFirebase, debug as debugFirebase, info as infoFirebase, warn as warnFirebase, error as errorFirebase } from 'firebase-functions/logger';
console.log = logFirebase;
console.debug = debugFirebase;
console.info = infoFirebase;
console.warn = warnFirebase;
console.error = errorFirebase;
@HonahleeWill nope, will still not help due to https://github.com/firebase/firebase-functions/pull/1440 not being released.
@lox #1440 is released now (v4.5.0)
Hello @lox @metadiego. With the release of #1440, is the logging working as expected?
- I suspect that this won't solve the issue, because #1440 only wrapped Firestore function trigger handlers in
wrapTraceContext onRequestis already wrapping its handler, butonCallisn't yet (#1487)
That is a bummer :(
Hey @metadiego. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 3 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!
What information is needed? I don't believe this is fixed yet.
@lox The label wasn't automatically removed since the OP didn't reply. Sorry about that, remove it and labeled it
Any estimates on when this will be fixed?
One year, still an issue :(
any updates ? i have migrates from v1 to v2 and every trace indicator is missing from onDocumentCreated or onDocumentUpdated.
Also is really hard to follow traces for new v2 cloud functions. Have to edit Summary fields all the times and its not integrated with Log Explorer as v1 was.
I m considering moving back to v1 because every issue is a tragedy to analyze
We retracted from v2 to v1 mostly due to this issue. Fortunately it was just a fraction of functions that we've experimented with v2.