<commit_before>var AWS  = require('aws-sdk'),
    fmt = require('fmt'),
    util = require('util'),
    _    = require('underscore')

var Backend = require('./backend')

exports.init = function(startupTime, config, emitter, logger) {
  config = _.defaults(config.cloudwatch || {}, {
    debug: config.debug,
    dumpMessages: config.dumpMessages,
    dimensions: { 'InstanceId': '' }
  })

  if (!config.namespace) {
    logger.log('cloudwatch config is missing "namespace"')
    return false
  }

  AWS.config.update(config);
  AWS.config.apiVersions = {
    cloudwatch: '2010-08-01',
  };

  getRegion(config, function(err, region) {
    if (err) {
      if (config.debug) {
        logger.log('cloudwatch backend could not access meta-data service');
      }
    }

    AWS.config.update({region: region });
    startup(config, startupTime, emitter, logger);
  });

  return true;
}

function getRegion(options, callback) {
  if (options.region) {
    return callback(null, region);
  }

  var metadata = new AWS.MetadataService()
  metadata.request('/latest/meta-data/placement/availability-zone', function(err, data) {
    if (err) {
      return callback(err);
    }
    
    var x = data.split('-');
    x[2] = x[2][0];
    return callback(null, x.join('-'));
  });
}

function startup(config, time, emitter, logger) {
  new Backend(config, time, function(flush, status) {
    if (flush) emitter.on('flush', flush)
    if (status) emitter.on('status', status)
  }, logger)
}<commit_msg>Fixed issue #2 when region option is passed<commit_after>var AWS  = require('aws-sdk'),
    fmt = require('fmt'),
    util = require('util'),
    _    = require('underscore')

var Backend = require('./backend')

exports.init = function(startupTime, config, emitter, logger) {
  config = _.defaults(config.cloudwatch || {}, {
    debug: config.debug,
    dumpMessages: config.dumpMessages,
    dimensions: { 'InstanceId': '' }
  })

  if (!config.namespace) {
    logger.log('cloudwatch config is missing "namespace"')
    return false
  }

  AWS.config.update(config);
  AWS.config.apiVersions = {
    cloudwatch: '2010-08-01',
  };

  getRegion(config, function(err, region) {
    if (err) {
      if (config.debug) {
        logger.log('cloudwatch backend could not access meta-data service');
      }
    }

    AWS.config.update({region: region });
    startup(config, startupTime, emitter, logger);
  });

  return true;
}

function getRegion(options, callback) {
  if (options.region) {
    return callback(null, options.region);
  }

  var metadata = new AWS.MetadataService()
  metadata.request('/latest/meta-data/placement/availability-zone', function(err, data) {
    if (err) {
      return callback(err);
    }
    
    var x = data.split('-');
    x[2] = x[2][0];
    return callback(null, x.join('-'));
  });
}

function startup(config, time, emitter, logger) {
  new Backend(config, time, function(flush, status) {
    if (flush) emitter.on('flush', flush)
    if (status) emitter.on('status', status)
  }, logger)
}
{'fraction_non_alphanumeric': 0.16005291005291006, 'fraction_numerical': 0.007605820105820105, 'mean_word_length': 3.3029871977240397, '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': '4731170', 'n_tokens_mistral': 985, 'n_tokens_neox': 957, 'n_words': 217}