Artsiom Miksiuk
Artsiom Miksiuk

Reputation: 4303

Google Cloud Functions with Trace Agent connection

I need to connect monitoring and tracing tools for our application. Our main code is on Express 4 running on Google Cloud Functions. All requests incoming from front nginx proxy server that handle domain and pretty routes names. Unfortunately, trace agent traces this requests, that coming on nginx front proxy without any additional information, and this is not enough to collect useful information about app. I found the Stack Driver custom API, which, as I understand might help to collect appropriate data on runtime, but I don't understand how I can connect it to Google Cloud Functions app. All other examples saying, that we must extend our startup script, but Google Cloud Functions fully automated thing, there is no such possibility here.

Upvotes: 1

Views: 696

Answers (2)

Paweł Badeński
Paweł Badeński

Reputation: 409

Placing require("@google-cloud/trace-agent") as the very first import didn't work for me. I still kept getting:

ERROR:@google-cloud/trace-agent: express tracing might not work as /var/tmp/worker/node_modules/express/index.js was loaded before the trace agent was initialized.

However I managed to work around it by manually patching express:

var traceApi = require('@google-cloud/trace-agent').get();
require("@google-cloud/trace-agent/src/plugins/plugin-express")[0].patch(
  require(Object.keys(require('module')._cache).find( _ => _.indexOf("express") !== -1)),
  traceApi
);

Upvotes: 1

Artsiom Miksiuk
Artsiom Miksiuk

Reputation: 4303

Found solution. I included require("@google-cloud/trace-agent"); not at the top of the index.js. It should be included before all other modules. After that it started to work.

Upvotes: 1

Related Questions