forked from shopyourway/metrics-js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from ysa23/chore/graphite-batching
Chore: Batch reporting for Graphite
- Loading branch information
Showing
13 changed files
with
691 additions
and
121 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const { Socket } = require('./socket'); | ||
|
||
const redundantDotsRegex = new RegExp('\\.\\.+', 'g'); | ||
|
||
function StatsdSocket({ | ||
port = 8125, | ||
host, | ||
batch = true, | ||
maxBufferSize = 1000, | ||
flushInterval = 1000, | ||
tags: defaultTags, | ||
prefix, | ||
}) { | ||
if (defaultTags && (Array.isArray(defaultTags) || typeof defaultTags !== 'object')) throw new TypeError('tags should be an object'); | ||
|
||
const metricPrefix = typeof prefix === 'string' && prefix.length ? removeRedundantDots(`${prefix}.`) : ''; | ||
|
||
const socket = new Socket({ | ||
host, port, batch, maxBufferSize, flushInterval, | ||
}); | ||
|
||
function send({ | ||
key, value, type, tags, callback, | ||
}) { | ||
validate({ name: 'key', value: key, type: 'string' }); | ||
validate({ name: 'value', value, type: 'number' }); | ||
validate({ name: 'type', value: type, type: 'string' }); | ||
if (tags && (Array.isArray(tags) || typeof tags !== 'object')) throw new TypeError('tags should be an object'); | ||
if (callback && typeof callback !== 'function') throw new TypeError('callback should be a function'); | ||
|
||
const metric = `${metricPrefix}${key}:${value}|${type}${stringifyTags(tags)}`; | ||
|
||
socket.send({ message: metric, callback }); | ||
} | ||
|
||
function validate({ name, value, type }) { | ||
if (value === undefined || value === null || (typeof value === 'string' && value === '')) throw new TypeError(`${name} is missing`); | ||
// eslint-disable-next-line valid-typeof | ||
if (typeof value !== type) throw new TypeError(`${name} is not a ${type}: ${value}: ${typeof value}`); | ||
} | ||
|
||
function close() { | ||
socket.close(); | ||
} | ||
|
||
function stringifyTags(tags) { | ||
if (!tags && !defaultTags) { | ||
return ''; | ||
} | ||
|
||
const allTags = { | ||
...defaultTags, | ||
...tags, | ||
}; | ||
|
||
return `|#${Object.entries(allTags).map(([key, value]) => `${key}:${value}`).join(',')}`; | ||
} | ||
|
||
return { | ||
send, | ||
close, | ||
}; | ||
} | ||
|
||
function removeRedundantDots(str) { | ||
return str.replace(redundantDotsRegex, '.'); | ||
} | ||
|
||
module.exports = { | ||
StatsdSocket, | ||
}; |
Oops, something went wrong.