<commit_before>var Statsd = require('strong-statsd');
var agent = require('./agent');
var assert = require('assert');
var cluster = require('cluster');
var debug = require('./debug')('metrics');
var expandString = require('./expander').expand;
var fmt = require('util').format;
var url = require('url');

try {
  var syslog = require('strong-fork-syslog');
} catch(er) {
  debug('syslog optional dep not present');
};

function sendMetrics(parentCtl, callback) {
  callback = callback || function(){};

  // Scenarios:
  //   no STRONGLOOP_METRICS, return
  //
  //   called from master:
  //     needs to .start() with expanded scope
  //       if invalid, warn, and clear env... so children don't warn
  //     needs to wait for start
  //     needs to reset STRONGLOOP_METRICS *before* starting cluster
  //       statsd://:PORT/SCOPE-TEMPLATE
  //
  //
  //   called from worker:
  //     needs to .start() with expanded scope (will always be a statsd: URL)
  //
  //   called from single-instance:
  //     needs to .start() with expanded scope (can reset env, its ok)

  var self = this;
  var endpoints = parse(process.env.STRONGLOOP_METRICS);

  function parse(url) {
    if (url == null)
      return [];
    try {
      return JSON.parse(url);
    } catch(er) {
      return [url];
    }
  }

  if (process.send && cluster.isMaster) {
    endpoints.push('internal:');
  }

  if (!endpoints.length) {
    process.nextTick(callback);
    return false;
  }

  var server = Statsd({
    scope: '%a.%h.%w',
    expandScope: expandScope,
    flushInterval: process.env.STRONGLOOP_FLUSH_INTERVAL,
    syslog: syslog,
  });

  debug('metrics endpoints: %j', endpoints);

  endpoints.forEach(function(endpoint) {
    try {
      server.backend(endpoint);
      if (cluster.isMaster) {
        self.logger.info('supervisor reporting metrics to `%s`', endpoint);
      }
    } catch(er) {
      console.error('Invalid metrics endpoint `%s`: %s', endpoint, er.message);
      process.exit(1);
    }
  });

  // Internal metrics support, forwarded over node ipc from cluster master
  // to parent.
  server.on('metrics', forwardMetrics);

  server.start(function(er) {
    assert.ifError(er);

    process.env.STRONGLOOP_METRICS = server.url;
    agent().use(function(name, value) {
      server.send(name, value);
    });
    return callback(server);
  });

  if (this.setupChildLogger && server.child) {
    var statsdWorker = {
      process: server.child,
      id: 'statsd',
    };
    this.setupChildLogger(statsdWorker);
  }

  function expandScope(scope) {
    return expandString(scope, {
      id: (cluster.worker && cluster.worker.id) |0,
      pid: process.pid,
      hostname: agent().config.hostname,
      appName: agent().config.appName,<commit_msg>Fix log tagging of statsd<commit_after>var Statsd = require('strong-statsd');
var agent = require('./agent');
var assert = require('assert');
var cluster = require('cluster');
var debug = require('./debug')('metrics');
var expandString = require('./expander').expand;
var fmt = require('util').format;
var url = require('url');

try {
  var syslog = require('strong-fork-syslog');
} catch(er) {
  debug('syslog optional dep not present');
};

function sendMetrics(parentCtl, callback) {
  callback = callback || function(){};

  // Scenarios:
  //   no STRONGLOOP_METRICS, return
  //
  //   called from master:
  //     needs to .start() with expanded scope
  //       if invalid, warn, and clear env... so children don't warn
  //     needs to wait for start
  //     needs to reset STRONGLOOP_METRICS *before* starting cluster
  //       statsd://:PORT/SCOPE-TEMPLATE
  //
  //
  //   called from worker:
  //     needs to .start() with expanded scope (will always be a statsd: URL)
  //
  //   called from single-instance:
  //     needs to .start() with expanded scope (can reset env, its ok)

  var self = this;
  var endpoints = parse(process.env.STRONGLOOP_METRICS);

  function parse(url) {
    if (url == null)
      return [];
    try {
      return JSON.parse(url);
    } catch(er) {
      return [url];
    }
  }

  if (process.send && cluster.isMaster) {
    endpoints.push('internal:');
  }

  if (!endpoints.length) {
    process.nextTick(callback);
    return false;
  }

  var server = Statsd({
    scope: '%a.%h.%w',
    expandScope: expandScope,
    flushInterval: process.env.STRONGLOOP_FLUSH_INTERVAL,
    syslog: syslog,
  });

  debug('metrics endpoints: %j', endpoints);

  endpoints.forEach(function(endpoint) {
    try {
      server.backend(endpoint);
      if (cluster.isMaster) {
        self.logger.info('supervisor reporting metrics to `%s`', endpoint);
      }
    } catch(er) {
      console.error('Invalid metrics endpoint `%s`: %s', endpoint, er.message);
      process.exit(1);
    }
  });

  // Internal metrics support, forwarded over node ipc from cluster master
  // to parent.
  server.on('metrics', forwardMetrics);

  server.start(function(er) {
    assert.ifError(er);

    process.env.STRONGLOOP_METRICS = server.url;
    agent().use(function(name, value) {
      server.send(name, value);
    });
    return callback(server);
  });

  if (this.setupChildLogger && server.child) {
    var statsdWorker = {
      process: {
        stdout: server.child.stdout,
        stderr: server.child.stderr,
        pid: process.pid,
      },
      id: 'statsd',
    };
    this.setupChildLogger(statsdWorker);
  }

  function expandScope(scope) {
    return expandString(scope, {
      id: (cluster.worker && cluster.worker.id) |0,
      pid: process.pid,
      hostname: agent().config.hostname,
      appName: agent().config.appName,
{'fraction_non_alphanumeric': 0.14663589561512516, 'fraction_numerical': 0.0007101011894194922, 'mean_word_length': 2.837874659400545, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '176536', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1761, 'n_words': 438}