Getting functionName and executionId inside running Firebase function - node.js

Our product is heavily based on Node.js v10 firebase functions, and up until now, we have been using the firebase functions logger SDK for logging purposes. Nevertheless, it is not enough for us anymore as we need to send some additional properties with each log for better filtering in GCP Logging Explorer.
Unfortunately, the very helpful functionName and executionId labels are not attached to the logs triggered by the Cloud Logging SDK. Are these labels exposed somehow by the Node.js Firebase SDK in order for me to attach them manually to the metadata?
Regards

We have the exact same stack at work and we just create a logger that sits on top of the firebase logger and do basically manual logging.
// Use the firebase functions logger to report all logs from out logger to
// Can do logger.info, .error, .log, .warn
// Logger is a custom function function that sits on top of console or firebase logger
// This means initialise a logger singleton using the firebase functions logger as a base
logger(functions.logger)
E.g. a typical function log would be:
// Use the logger global singleton initialised above
logger.log(`[endpoint-name.methodName] - Execution failed with error code ${error.code} and message ${error.message}`)
We just then use the function log search field to find an instance of a particular error. We also report all internal errors to sentry and use that too debug.
Hope this gives you some ideas?

Related

Firebase docs reference unknown module

Firebase has this piece of information here at https://firebase.google.com/docs/functions/locations:
Client-side location selection for callable functions Regarding the callable function, client callable setups should follow the same guidelines as HTTP functions. The client can also specify a region, and must do so if the function runs in any region other than us-central1.
To set regions on the client, specify the desired region at
initialization:
var functions = firebase.app().functions('us-central1');
I've been trying to find which node module firebase is referring to but I have had no luck.
I know that it is not 'firebase-admin' or 'firebase-functions' but thats about it.
Anyone have any ideas what this might be referring to?
Edit:
I have now also tried using this with the imports require('firebase') and require('firebase/app') (as suggested) but neither of those seem to work. I also tried generating the app with const app = firebase.initializeApp({}); and then running app.functions("region") or app().functions("region") but i keep receiving TypeErrors saying functions is not a function and app is not a function respectively.
firebase.app().functions('us-central1'); is a part of the firebase-js-sdk. With source code documented on GitHub
NPM component is #firebase/functions, that is documented on the npmjs.com.
However, it is not intended for standalone usage, and should be used along with package Firebase.
You can install it using:
$ npm i firebase

Getting ExecutionContext in other libraries/projects in Azure Function App

The execution context that is injected to a function (https://github.com/Azure/azure-functions-host/wiki/Retrieving-information-about-the-currently-running-function), is it possible to get it in some other helper libraries.
I want to get the InvocationId of the current function in some other libraries. For e.g. let's say I have written a logger and I need to add the Invocation ID for every log. One trivial way to achieve this would be to pass the Invocation ID from the function to all the helpers, but it may not be possible especially if one is working with legacy code.
In App services we could solve this problem by getting access to the HttpContext via the IHttpContextAccessor.
Is there any alternative to this in Azure function?

aws-xray captureAWS annotations

I've begun using AWSXRay in order to get more insight into why performance is not ideal in my lambda function. This lambda function runs a gql service meaning it has lots of outbound requests to other lambda functions as well as dynamodb for caching.
I've added tracing to all aws-sdk client calls by utilizing the following in my handler. It mutates the imported AWS module so that all subsequent usage of AWS clients successful includes aws-xray tracing, regardless of what module imports it. Awesome!
import AWS from 'aws-sdk';
import AWSXRay from 'aws-xray-sdk';
AWSXRay.captureAWS(AWS);
Heres an example of the output:
The Problem
The problem is that none of the "Traces" have any annotation regarding the parameters of the requests. Both the annotation and metadata of each trace is empty:
The Hope
The hope is that there is a way to configure the AWSXRay CaptureAWS modifications so that they include the arguments of each aws-client request in the annotations or metadata.
The Question
Is it possible to request that AWSXRay.captureAWS(AWS); includes the parameters passed to the aws sdk client invocations in either the annotations or the metadata of the traces it produces?
The resources section contains high level arguments for some clients e.g. DynamoDB table name. Not all arguments are captured by default. This is because they may contain information that the users do not wish to track in their trace and may also be verbose.
For now opt-in is not available in X-Ray SDK for arbitrary API parameters. As a workaround for now, I would suggest that you wrap your sdk calls in a local subsegment and record the parameters you want to capture as annotations or Metadata for that subsegment. Let me know if you need any help in locating docs that allow you to create your own subsegments.

context.log vs console.log in Azure function apps

In the nodejs examples for Azure function apps, there is a passed in context obj to the function and it is possible to do context.log in the same manner as you can with console.log to output messages.
What is the difference between these two methods and does it matter which you use? Thx.
This documentation should answer your question :)
In Functions, you use the context.log methods to write trace output to the console. In Functions v2.x, trace outputs using console.log are captured at the Function App level. This means that outputs from console.log are not tied to a specific function invocation, and hence aren't displayed in a specific function's logs. They do, however, propagate to Application Insights. In Functions v1.x, you cannot use console.log to write to the console.
Long story short - context.log is best!
You can redirect console.log to context.log with my npm package so you dont have to pass down the context everywhere.
https://www.npmjs.com/package/azure-function-log-intercept
Source here if you just want to create your own module
https://github.com/BrianRosamilia/azure-function-log-intercept/blob/master/index.js

Logging firebase warnings

I am getting the following warning on the console of my nodejs app:
FIREBASE WARNING: Provided authentication credentials for the app
named "[DEFAULT]" are invalid. This usually indicates your app was not
initialized correctly. Make sure the "credential" property provided to
initializeApp() is authorized to access the specified "databaseURL"
and is from the correct project.
Is there a way to send this kind of warnings to a logger?
I don't find nothing of the kind for nodeJs Admin SDK
Just to make absolutely clear, i am not concert with the error itself, only how to log all relevant problems, occurred on my code or on the library code, into one single file that i can look at.
Also, the warning is not possible to log by the api caller because it is not send it back to the caller.
Specify a custom log function via admin.database().enableLogging(). See the examples in the reference docs.

Resources