firebase-functions icon indicating copy to clipboard operation
firebase-functions copied to clipboard

Functions v2 missing labels with Firebase structured logs

Open metadiego opened this issue 2 years ago • 31 comments

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: Screen Shot 2022-12-27 at 10 04 32 AM

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: Screen Shot 2022-12-27 at 9 49 40 AM

[REQUIRED] Actual behavior

v2 function does not include any labels.

Were you able to successfully deploy your functions?

Yes.

No

metadiego avatar Dec 27 '22 09:12 metadiego

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Dec 27 '22 09:12 google-oss-bot

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.

dan-wu-open avatar Jan 09 '23 21:01 dan-wu-open

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?

metadiego avatar Feb 10 '23 09:02 metadiego

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

dan-wu-open avatar Feb 10 '23 18:02 dan-wu-open

Oh got it, thanks for the clarification @dan-wu-open. Guess this issue is in the right place then.

metadiego avatar Feb 11 '23 00:02 metadiego

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).

pgainullin avatar Jun 06 '23 03:06 pgainullin

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.

indicava avatar Sep 25 '23 16:09 indicava

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.

glumb avatar Oct 01 '23 05:10 glumb

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..

brunapereira avatar Oct 10 '23 22:10 brunapereira

Need this as well

clivi-kj avatar Oct 14 '23 21:10 clivi-kj

Has anyone figured out a workaround to filter by one execution?

clivi-kj avatar Oct 14 '23 21:10 clivi-kj

Is anyone at Google actually listening to these issues? The V2 functions seem broken in so many ways.

lox avatar Oct 22 '23 23:10 lox

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 || ''
    }
  }
}

glumb avatar Oct 23 '23 06:10 glumb

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.log no 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_id is gone, appears to be replaced by trace

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.

HonahleeWill avatar Oct 23 '23 06:10 HonahleeWill

How does one get at the headers outside of onRequest / onCall @HonahleeWill?

lox avatar Oct 23 '23 06:10 lox

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).

HonahleeWill avatar Oct 23 '23 06:10 HonahleeWill

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.

lox avatar Oct 23 '23 06:10 lox

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 avatar Oct 23 '23 23:10 HonahleeWill

@HonahleeWill nope, will still not help due to https://github.com/firebase/firebase-functions/pull/1440 not being released.

lox avatar Oct 23 '23 23:10 lox

@lox #1440 is released now (v4.5.0)

ciriousjoker avatar Nov 03 '23 12:11 ciriousjoker

Hello @lox @metadiego. With the release of #1440, is the logging working as expected?

exaby73 avatar Nov 09 '23 16:11 exaby73

  • I suspect that this won't solve the issue, because #1440 only wrapped Firestore function trigger handlers in wrapTraceContext
  • onRequest is already wrapping its handler, but onCall isn't yet (#1487)

ciriousjoker avatar Nov 14 '23 10:11 ciriousjoker

That is a bummer :(

lox avatar Nov 14 '23 10:11 lox

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!

google-oss-bot avatar Nov 23 '23 02:11 google-oss-bot

What information is needed? I don't believe this is fixed yet.

lox avatar Nov 23 '23 02:11 lox

@lox The label wasn't automatically removed since the OP didn't reply. Sorry about that, remove it and labeled it

exaby73 avatar Nov 29 '23 06:11 exaby73

Any estimates on when this will be fixed?

fgatti675 avatar Dec 16 '23 20:12 fgatti675

One year, still an issue :(

stamler avatar Jan 29 '24 18:01 stamler

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

abdalamichel avatar Feb 29 '24 03:02 abdalamichel

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.

orestesgaolin avatar Feb 29 '24 08:02 orestesgaolin