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: [Node] Add warnings to all SDK example apps that would check if propper credentials have been set or it still has the defaults. (#90)

* feat: add warning for defaults

* feat: warn
Date: 29 days ago
Size: 1,619 bytes
 

Contents

Class file image Download
//since we need to test crashing the app /*global app*/ const COUNTLY_SERVER_KEY = "https://your.server.ly"; const COUNTLY_APP_KEY = "YOUR_APP_KEY"; if(COUNTLY_APP_KEY === "YOUR_APP_KEY" || COUNTLY_SERVER_KEY === "https://your.server.ly"){ console.warn("Please do not use default set of app key and server url") } var Countly = require("../lib/countly.js"); Countly.init({ app_key: COUNTLY_APP_KEY, url: COUNTLY_SERVER_KEY, //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);