File: examples/apm_example.js

Recommend this page to a friend!
  Classes of Arturs Sosins  >  Countly SDK for Node.js  >  examples/apm_example.js  >  Download  
File: examples/apm_example.js
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Countly SDK for Node.js
Report events to be tracked with by Count.ly API
Author: By
Last change:
Date: 5 months ago
Size: 1,358 bytes
 

Contents

Class file image Download
//since we need to test crashing the app
/*global app*/
var Countly = require("../lib/countly.js");

Countly.init({
    app_key: "YOUR_APP_KEY",
    url: "https://try.count.ly", //your server goes here
    debug: true
});

//report app start trace
Countly.report_app_start();

/**
 *  example express middleware
 *  @param {Object} req - request object
 *  @param {Object} res - response object
 *  @param {Function} next - next middleware call
 */
function expressMiddleware(req, res, next) {
    var trace = {
        type: "network",
        name: req.baseUrl + req.path,
        stz: Date.now(),
    };

    var processed = false;

    /**
     *  Prepare request data
     */
    function processRequest() {
        if (!processed) {
            processed = true;
            trace.etz = Date.now();
            trace.apm_metrics = {
                response_time: trace.etz - trace.stz,
                response_code: res.statusCode,
                response_payload_size: res.getHeader('Content-Length') || res._contentLength,
                request_payload_size: (req.socket && req.socket.bytesRead) ? req.socket.bytesRead : req.getHeader('Content-Length')
            };
            Countly.report_trace(trace);
        }
    }

    res.on('finish', processRequest);

    res.on('close', processRequest);

    next();
}

app.use(expressMiddleware);