Last active
March 5, 2019 17:59
-
-
Save joshghent/08f34be81eda82de7c6c330b68d37e20 to your computer and use it in GitHub Desktop.
Grafana
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Client } from "node-statsd-client"; | |
import { IGraphiteController } from "../interfaces"; | |
const PREFIX = "MY-API-NAME"; | |
export default class GraphiteController implements IGraphiteController { | |
private _client: any; | |
// the graphite port will always be this for every environment | |
private _port: number = 8125; | |
private static _instance: GraphiteController; | |
// Check if we are running tests, if so, deactive the graphite logging | |
private testing: boolean = process.env.NODE_ENV === "testing"; | |
constructor(config: any) { | |
if (!this.testing) { | |
try { | |
this._client = new Client(config.statsd, this._port); | |
} catch (err) { | |
throw new Error(`There was an error connecting to Graphite: ${err}`); | |
} | |
} | |
} | |
public static getInstance(config: any): GraphiteController { | |
if (!this._instance) { | |
this._instance = new GraphiteController(config); | |
} | |
return this._instance; | |
} | |
public write(activityType: string, error: boolean = false): void { | |
if (!this.testing) { | |
this._client.increment(`${PREFIX}.${activityType}${error ? ".error" : ""}`); | |
} | |
} | |
/** | |
* Writes a graphite timing | |
* This is used to measure the time a function or piece of logic takes | |
* @param {string} activityType | |
* @param {Date} startDate - a new Date() object. Create this as a variable at the top of the function and then pass it in | |
* @returns void | |
*/ | |
public writeTiming(activityType: string, startDate: Date): void { | |
if (!this.testing) { | |
this._client.timing(`${PREFIX}.${activityType}`, (new Date().getTime() - startDate.getTime())); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Router } from "express"; | |
import { Config } from "./configuration"; | |
// Import or require the graphite controller and activity labels | |
import { GraphiteController, GraphiteLabel } from "../graphite"; | |
const router = Router() as Router; | |
// Load your config | |
const config = Config.getConfig(); | |
// Import the graphite controller | |
const graphiteController = GraphiteController.getInstance(config); | |
router.get("/", async (req, res, next) => { | |
try { | |
const record = await UserController.get(req.query, req.jwt.accountId); | |
if (record) { | |
// Just before the response is sent, we log the route being called | |
graphiteController.write(“GetUsers”); | |
res.json({ success: true, data: record }); | |
} else { | |
res.status(404).send(); | |
} | |
} catch (err) { | |
next(new InternalServerError("There was an error getting data from the database")); | |
} | |
}); | |
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Router } from "express"; | |
import { Config } from "./configuration"; | |
// Import or require the graphite controller and activity labels | |
import { GraphiteController, GraphiteLabel } from "../graphite"; | |
const router = Router() as Router; | |
// Load your config | |
const config = Config.getConfig(); | |
// Import the graphite controller | |
const graphiteController = GraphiteController.getInstance(config); | |
router.get("/", async (req, res, next) => { | |
try { | |
const startTime = new Date(); | |
const record = await UserController.get(req.query, req.jwt.accountId); | |
if (record) { | |
// Just before the response is sent, we log the route being called | |
graphiteController.write("GetUsers"); | |
graphiteController.writeTiming("GetUsers", startTime); | |
res.json({ success: true, data: record }); | |
} else { | |
res.status(404).send(); | |
} | |
} catch (err) { | |
graphiteController.write("GetUsers", true); | |
next(new InternalServerError("There was an error getting data from the database")); | |
} | |
}); | |
export default router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment