Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #62 from librato/chore/tagging-improvements
Browse files Browse the repository at this point in the history
Support global tag of host from config/hostname
  • Loading branch information
bryanmikaelian authored Feb 6, 2017
2 parents 78da68b + 0a76784 commit 2d68246
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/librato.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ url_parse = require('url').parse,
var tunnelAgent = null;

var debug, logAll;
var api, email, token, period, sourceName, sourceRegex, includeMetrics, excludeMetrics;
var api, email, token, period, sourceName, hostName, sourceRegex, includeMetrics, excludeMetrics;

// How long to wait before retrying a failed post, in seconds
var retryDelaySecs = 5;
Expand Down Expand Up @@ -295,12 +295,20 @@ var flush_stats = function librato_flush(ts, metrics)
measureName = parse_and_set_tags(measureName, measure);

// Use first capturing group as source name
if (!writeToLegacy && sourceRegex && (match = measureName.match(sourceRegex)) && match[1]) {
if (sourceRegex && (match = measureName.match(sourceRegex)) && match[1]) {
measure.source = sanitize_name(match[1]);
// Remove entire matching string from the measure name & add global prefix.
measure.name = sanitize_name(measureName.slice(0, match.index) + measureName.slice(match.index + match[0].length));

// Create a measurement-level tag named source
measure.tags.source = measure.source;
} else {
measure.name = sanitize_name(measureName);

// Use the global config sourceName as a source tag, if it exists.
if (sourceName !== null) {
measure.tags.source = sourceName;
}
}

if (brokenMetrics[measure.name]) {
Expand Down Expand Up @@ -538,6 +546,7 @@ exports.init = function librato_init(startup_time, config, events, logger)
email = config.librato.email;
token = config.librato.token;
sourceName = config.librato.source;
hostName = config.librato.host;
sourceRegex = config.librato.sourceRegex;
snapTime = config.librato.snapTime;
includeMetrics = config.librato.includeMetrics;
Expand Down Expand Up @@ -576,10 +585,10 @@ exports.init = function librato_init(startup_time, config, events, logger)
skipInternalMetrics = config.librato.skipInternalMetrics;
}

if (sourceName == null) {
if (hostName == null) {
var os = require('os');

sourceName = os.hostname();
hostName = os.hostname();
}

if (config.librato.proxy && config.librato.proxy.uri) {
Expand Down Expand Up @@ -626,8 +635,8 @@ exports.init = function librato_init(startup_time, config, events, logger)
writeToLegacy = config.librato.writeToLegacy;
}

// Set host as a global tag
tags['host'] = sourceName;
// Set host as a global tag. Defaults to os.hostname()
tags.host = hostName;
}

if (!email || !token) {
Expand Down

0 comments on commit 2d68246

Please sign in to comment.