1

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.

Artsiom Miksiuk
  • 3,896
  • 9
  • 33
  • 49

2 Answers2

1

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.

Artsiom Miksiuk
  • 3,896
  • 9
  • 33
  • 49
1

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
);
Paweł Badeński
  • 399
  • 3
  • 12
  • Well, I also faced this error when I returned back to *trace-agent* on another project. We didn't need trace-agent so critically on those project so we postponed the integration. I checked old project that already had established *trace-agent* and no new errors appeared. For now it is working well in context of my initial question. – Artsiom Miksiuk Jun 26 '17 at 14:46
  • I have one addition to this error, that you mentioned. I think, that this error actually belongs to root level express application of GCF. I have an assumption, that user defined HTTP GCF are wrapped in express application, and root level express (that are not controlled by us and was defined by Google's engineers) passing request to our exported function that, handling then requests. In any case, connecting trace-agent as it was proposed by documentation is working even with this error. – Artsiom Miksiuk Jul 25 '17 at 21:15