From ac26483e8df89eae3bbc62d656e8aa0747e30b8a Mon Sep 17 00:00:00 2001 From: Gustavo Baratto Date: Fri, 16 Oct 2015 18:39:17 -0700 Subject: [PATCH] add capability to also remove prefixes --- README.md | 8 ++++++++ lib/datadog.js | 11 ++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4055342..2cf25dd 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,14 @@ A plugin to connect etsy's statsD to Datadog ```js datadogApiKey: "your_api_key" // You can get it from this page: https://app.datadoghq.com/account/settings#api datadogPrefix: "your_prefix" // Your metrics will be prefixed by this prefix +datadogRemovePrefix: 2 // Number of period delimited prefixes to remove. If you use this option with *datadogPrefix* remove will happen prior to addition. +``` +### Example: + +If the metric name is called **"hosts.foo.bar.count"**, it will be rewritten to **"application.bar.count"**: +``` +datadogRemovePrefix: 2 +datadogPrefix: "application" ``` ## How to enable diff --git a/lib/datadog.js b/lib/datadog.js index 1dc6179..bae194c 100644 --- a/lib/datadog.js +++ b/lib/datadog.js @@ -25,6 +25,7 @@ var datadogApiHost; var datadogApiKey; var datadogStats = {}; var datadogPrefix; +var datadogRemovePrefix; var Datadog = function(api_key, options) { options = options || {}; @@ -182,11 +183,14 @@ var flush_stats = function datadog_post_stats(ts, metrics) { }; var get_prefix = function datadog_get_prefix(key) { + var new_key = key; + if (datadogRemovePrefix !== undefined) { + new_key = key.split(".").slice(datadogRemovePrefix).join('.'); + } if (datadogPrefix !== undefined) { - return [datadogPrefix, key].join('.'); - } else { - return key; + new_key = [datadogPrefix, new_key].join('.'); } + return new_key; } var backend_status = function datadog_status(writeCb) { @@ -205,6 +209,7 @@ exports.init = function datadog_init(startup_time, config, events, log) { datadogApiKey = config.datadogApiKey; datadogApiHost = config.datadogApiHost; datadogPrefix = config.datadogPrefix; + datadogRemovePrefix = config.datadogRemovePrefix; if (!datadogApiHost) { datadogApiHost = 'https://app.datadoghq.com';