I'm trying to setup my node.js application to use Cloud Trace and Cloud Error Reporting, I'm following the documentation exactly and it doesn't work. Already installed the google-cloud/trace-agent and @Google-cloud/error-reporting client libraries and added the following code:
// here are my application imports
if (process.env.NODE_ENV === 'production') {
require('@google-cloud/trace-agent').start();
}
const gcpErrorReporting = new ErrorReporting()
const app = express()
// here are the application routes
if (process.env.NODE_ENV === 'production') {
app.use(gcpErrorReporting.express)
}
I generated some 400 and 500 erros in the application, but the Trace and Error Reporting pages are empty. Cloud Logging has a lot of useless information but not the errors.
Can someone please help? I'm favoring GCP solution but at this point I'm thinking it would be simpler to use some better documented and supported solution. Thank you in advance.
Hello reno, within your code snippet I cannot confirm if you have authenticated [1] and pointed to the desired GCP project, this woud not be needed if running within a GCP product (i.e GCE, GKE, Appengine, etc...). Can you confirm if running locally or GCP?
I'd suggest to explicit indicate your SA and project ID when initializing the client.
require('@google-cloud/trace-agent').start({
projectId: 'your-project-id',
keyFilename: '/path/to/key.json',
});
1 - https://cloud.google.com/trace/docs/setup/nodejs#running_locally_and_elsewhere
Hi Hector, thank you for your answer.
I generated the credentials file for the Compute Engine service account, which has both Trace Agent and Error Reporting Writer roles and added them to the initialization of Trace Agent and Error Reporting client libraries.
Then I ran the app both in local and production (Compute Engine) environments, and generated some 400 and 500 errors, but still nothing shows in the dashboard. If I generate a new error with the report() method it shows in the Error Reporting dashboard as expected. Looks like just the express instrumentation it's not working. I did it as in my first code snippet, at the end of the app file, after all calls to express.use() as stated in the docs.
Thanks for the clarification, from what I see for the latest version of the package it´s not needed to have the ´production´ validation manually.
//if (process.env.NODE_ENV === 'production') {
// app.use(gcpErrorReporting.express)
//}
const gcpErrorReporting = new ErrorReporting({
// other configs
reportMode: 'production'
});
// routes
app.use(gcpErrorReporting.express);
Instead we can specify it when creating the ErrorReporting object [1], so we can validate the package version is 3.0.5 and use the configuration wrapper.
Then we can also validate how the errors are being reported, for example are you using the following pattern when handling your exceptions
app.get('/api', (req, res, next) => {
try {
// ...
}
catch (ex) {
res.send('Something broke!');
next(new Error('Custom error message'));
}
});
Otherwise you have to manually handle the exceptions as follows[2]