Search code examples
node.jsnewrelic

new relic web transaction appearing under root


I am using the node.js agent for the new relic. I am using Node.js custom instrumentation to monitor socket.io, new relic web transaction appearing under root and no data is visible as I am not able to monitor socket.io. Below is the output under Transaction Root Path

Breakdown table

Category        Segment     % Time   Avg calls (per txn)    Avg time (ms)
WebTransaction  Root path   100.0    1.0                    2,150

Server Code

var http = require('http'),
    fs = require('fs'),
    nr = require('newrelic'),
    index = fs.readFileSync(__dirname + '/sock_client.html');

var app = http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(index);
});

// Socket.io server listens to our app
var io = require('socket.io').listen(app);

// Send current time to all connected clients
function sendTime() {
    io.emit('time', { time: new Date().toJSON() });
}

// Send current time every 10 secs
setInterval(sendTime, 10000);

// Emit welcome message on connection
io.on('connection', function(socket) {
    // Use socket to communicate with this particular client only, sending it it's own id
    socket.emit('welcome', { message: 'Welcome!', id: socket.id });

    socket.on('rings', function(data){
        nr.startWebTransaction('websocket/ping/test_v3', function transactionHandler() {
            socket.emit('pong', { message: 'Welcome!' });
            console.log(data);
            nr.addCustomParameters({
                "Discount Code": "Summer Super Sale",
                "Item Code": 31456
            });
        });
    });

    socket.on('pings', function(data){
        nr.startWebTransaction('websocket/ping/test_v5', function transactionHandler() {
            let trans = nr.getTransaction();
            someAsyncBeha("console.log", function(){
                trans.end();
            })
            console.log(data);
        });
    });


});

function someAsyncBeha(data, cb){

    setTimeout(function() {
        console.log("Goodbye!");
        console.log(data);
        cb();
    }, 5000);

};

Client Code

<!doctype html>
<html>
<head>

    <link rel="shortcut icon" href="/favicon.png">
    <script src='/socket.io/socket.io.js'></script>
    <script>
        var socket = io();

        socket.on('welcome', function(data) {
            addMessage(data.message);

            // Respond with a message including this clients' id sent from the server
            socket.emit('rings', {data: 'foo!', id: data.id});
        });
        socket.on('time', function(data) {
            addMessage(data.time);

            socket.emit('pings', {data: 'foo!', id: data.time});
        });
        socket.on('error', console.error.bind(console));
        socket.on('message', console.log.bind(console));
        socket.on('pong', console.log.bind(console));

        function addMessage(message) {
            var text = document.createTextNode(message),
                    el = document.createElement('li'),
                    messages = document.getElementById('messages');

            el.appendChild(text);
            messages.appendChild(el);
        }
    </script>
</head>
<body>
<ul id='messages'></ul>
</body>
</html>

Solution

  • The issue was in the new relic module version, there are two suggestions.

    First, upgrade the Node.js agent to v2.2.0

    This version includes a bug fix that should resolve the issue you're seeing.

    Second, move nr = require('newrelic'); to the top of the requires.

    This ensures the agent catches everything in your application before the code has a chance to spin up. Without this, the code in the other requires can launch before the agent has a chance to inject its methods, resulting in no tracking for those methods.