census-instrumentation/opencensus-node

Outgoing HTTP requests not logged for Node 10

jdbennet2001 opened this issue · 4 comments

Please answer these questions before submitting a bug report.

What version of OpenCensus are you using?

0.0.14

What version of Node are you using?

v10.15.3

What did you do?

Invoke a minimal node / express as:

'use strict';

const express = require('express');
const cookieParser = require('cookie-parser');
const tracing = require('@opencensus/nodejs');
const propagation = require('@opencensus/propagation-b3');


const rp          = require('request-promise');

const b3 = new propagation.B3Format();

const startOptions = {propagation: b3};

const tracer = tracing.start(startOptions).tracer;

const port  = 3000;

const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

app.get('/sample', function(req, res){
  console.log( `sample headers: ${JSON.stringify(req.headers)}`);
  rp('https://jsonplaceholder.typicode.com/todos/1').then(data =>{
    res.send(data);
  });
  
});

var http = require('http');
app.set('port', port);
var server = http.createServer(app);
    server.listen(port);
console.log( `Listening to ${port}`)
module.exports = app;
curl --request GET \
  --url http://localhost:3000/sample 

What did you expect to see?

One SERVER and one CLIENT span

What did you see instead?

One SERVER span only:

RootSpan: {traceId: 7cfcd1756c36421682cffb28fff36898, spanId: 06175dc89ce015e8, name: /sample }
	ChildSpans:

Additional context

I've pushed the sample code to:
https://github.com/jdbennet2001/OpenCensusExample to be helpful.

Invoking the same code with node eight yields:

RootSpan: {traceId: 7cfcd1756c36421682cffb28fff36898, spanId: 3d3012a3532fa656, name: /sample }
	ChildSpans:
		{spanId: 1c72653c9acd38a5, name: GET /todos/1}

which is correct.

Thanks for reporting this! Will update you shortly with the findings.

As per my observation, only @opencensus/instrumentation-http plugin is loaded for automatic tracing. But shared example, contains https request, so tracer will not capture this request due to missing @opencensus/instrumentation-https plugin. Once I changed to http request, everything is working as expected.

AFAIK https.request used to depend on http.request in Node<8.9, that's why you don't see any issue in Node < 8.9.

Two solutions:

  1. Use http endpoint (If possible).
  2. Ensure that you start the OpenCensus tracer BEFORE initializing your express app, if you want to enable automatic tracing for built-in plugins (like http, https) - Recommended.
    Example:
    'use strict';
    const tracing = require('@opencensus/nodejs');
    const propagation = require('@opencensus/propagation-b3');
    
    const b3 = new propagation.B3Format();
    const startOptions = {propagation: b3};
    const tracer = tracing.start(startOptions).tracer;
    const port = 3000;
    
    const express = require('express');
    const cookieParser = require('cookie-parser');
    const rp = require('request-promise');
    const app = express();
    
    // Rest of the code
    
    Please refer this example: https://github.com/census-instrumentation/opencensus-node/blob/master/examples/express/index.js#L19-L23

I tried both the solutions and confirmed to be working for me. Let me know your thoughts.

Thanks for the investigation. I'll close this while I verify and optimize my workflow.