Skip to content

Commit

Permalink
Merge pull request #1 from rqbazan/add-types
Browse files Browse the repository at this point in the history
  • Loading branch information
herrmannplatz authored Jul 8, 2020
2 parents 73f9a46 + 047acbd commit b11596f
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- 8
- 10

cache:
directories:
Expand Down
17 changes: 17 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference types="node" />

import { FastifyPlugin } from 'fastify';
import { StatsD } from 'hot-shots';

export interface FastifyDatadogOptions {
dogstatsd: StatsD
stat?: string
tags?: string[]
method?: boolean
path?: boolean
responseCode?: boolean
}

declare const fastifyDatadog: FastifyPlugin<FastifyDatadogOptions>;

export default fastifyDatadog;
54 changes: 27 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
'use strict'

const fp = require('fastify-plugin')

const startTimeSymbol = Symbol('startTime')

const fastifyDatadog = (fastify, {
dogstatsd,
stat = 'node.fastify.router',
tags = [],
method = false,
path = false,
responseCode = false
} = {}, next) => {
async function fastifyDatadog (fastify, options = {}) {
const {
dogstatsd,
stat = 'node.fastify.router',
tags = [],
method = false,
path = false,
responseCode = false
} = options

if (dogstatsd == null) {
throw new Error('Missing dogstatsd option.')
}

fastify.addHook('onRequest', (req, reply, next) => {
const now = () => {
const [seconds, nanoseconds] = process.hrtime()

return seconds * 1e3 + nanoseconds / 1e6
}

fastify.addHook('onRequest', async (req, reply) => {
req[startTimeSymbol] = now()
next()
})

fastify.addHook('onSend', ({ raw: request }, reply, payload, next) => {
const { context, res: { statusCode } } = reply
const { config: { url: route } } = context
fastify.addHook('onSend', async (req, reply) => {
const { context, statusCode } = reply

var statTags = [`route:${route}`, ...tags]
const statTags = [`route:${context.config.url}`, ...tags]

if (method) {
statTags.push(`method:${request.method.toLowerCase()}`)
statTags.push(`method:${req.method.toLowerCase()}`)
}

if (path !== false) {
statTags.push(`path:${request.url}`)
if (path) {
statTags.push(`path:${req.url}`)
}

if (responseCode) {
Expand All @@ -40,20 +47,13 @@ const fastifyDatadog = (fastify, {
dogstatsd.increment(`${stat}.response_code.all`, 1, statTags)
}

dogstatsd.histogram(`${stat}.response_time`, now() - request[startTimeSymbol], 1, statTags)
const resposeTime = now() - req[startTimeSymbol]

next()
dogstatsd.histogram(`${stat}.response_time`, resposeTime, 1, statTags)
})

const now = () => {
const [seconds, nanoseconds] = process.hrtime()
return seconds * 1e3 + nanoseconds / 1e6
}

next()
}

module.exports = fp(fastifyDatadog, {
fastify: '>=1.0.0',
fastify: '3.x',
name: 'fastify-datadog'
})
20 changes: 20 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fastify from 'fastify';
import { StatsD } from 'hot-shots';
import datadogPlugin from '.';

const app1 = fastify();

app1.register(datadogPlugin, {
dogstatsd: new StatsD()
});

const app2 = fastify();

app2.register(datadogPlugin, {
dogstatsd: new StatsD(),
stat: 'stat-test',
tags: ['tag-1', 'tag-2'],
method: true,
path: true,
responseCode: true
});
8 changes: 4 additions & 4 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('fastify-datadog', () => {
it('should track response time', async () => {
fastify.register(fastifyDatatog, { dogstatsd: dogstatsdMock })

fastify.get('/users/:id', (req, reply) => reply.send(200))
fastify.get('/users/:id', async () => 200)

await fastify.inject('/users/123456')

Expand All @@ -35,7 +35,7 @@ describe('fastify-datadog', () => {
it('should track method', async () => {
fastify.register(fastifyDatatog, { dogstatsd: dogstatsdMock, method: true })

fastify.get('/users/:id', (req, reply) => reply.send(200))
fastify.get('/users/:id', async () => 200)

await fastify.inject('/users/123456')

Expand All @@ -49,7 +49,7 @@ describe('fastify-datadog', () => {
it('should track response code', async () => {
fastify.register(fastifyDatatog, { dogstatsd: dogstatsdMock, responseCode: true })

fastify.get('/users/:id', (req, reply) => reply.send(200))
fastify.get('/users/:id', async () => 200)

await fastify.inject('/users/123456')

Expand All @@ -67,7 +67,7 @@ describe('fastify-datadog', () => {
it('should track method', async () => {
fastify.register(fastifyDatatog, { dogstatsd: dogstatsdMock, path: true })

fastify.get('/users/:id', (req, reply) => reply.send(200))
fastify.get('/users/:id', async () => 200)

await fastify.inject('/users/123456')

Expand Down
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"version": "0.0.0-semantic-release",
"description": "module fastify-datadog",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"commit": "git-cz",
"test": "jest --coverage",
"test": "jest --coverage && tsd",
"posttest": "standard",
"precommit": "npm test"
},
Expand All @@ -18,16 +19,23 @@
"author": "herrmannplatz <[email protected]>",
"license": "MIT",
"dependencies": {
"fastify-plugin": "1.3.0"
"fastify-plugin": "^1.6.1"
},
"devDependencies": {
"@types/node": "^14.0.14",
"commitizen": "^3.0.4",
"cz-conventional-changelog": "^2.1.0",
"fastify": "^1.13.1",
"fastify": "^3.0.0",
"hot-shots": "^7.7.1",
"husky": "^1.1.4",
"jest": "^23.6.0",
"jest": "^26.1.0",
"semantic-release": "^15.12.0",
"standard": "^12.0.1"
"standard": "^12.0.1",
"tsd": "^0.13.1",
"typescript": "^3.9.6"
},
"peerDependencies": {
"fastify": "^3.0.0"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit b11596f

Please sign in to comment.