lib/instrumental.js
function instrumental_send(payload) {
  var state = "cold";
  var tlsOptionsVariation = selectTlsOptionsVariation();

  var client = instrumental_connection(host, port, tlsOptionsVariation, function(_client){
    state = "connected";

    var cleanString = function(value) {
      return String(value).replace(/\s+/g, "_");
    }

    // Write the authentication header
    _client.write(
      "hello version node/statsd-instrumental-backend/" + cleanString(package.version) +
        " hostname " + cleanString(hostname) +
        " pid " + cleanString(process.pid) +
        " runtime " + cleanString("node/" + process.versions.node) +
        " platform " + cleanString(process.platform + "-" + process.arch) + "\n" +
      "authenticate " + key + "\n"
    );
  });

  // We need to handle the timeout. I think we should only care about read
  // timeouts. That is we write out data, if we don't hear back in timeout
  // seconds then the data probably hasn't reached the server.
  client.setTimeout(timeout, function() {
    // ZOMG FAILED WRITING WE ARE BAD AT COMPUTER SCIENCE
    if(state == "connected") {
      // We're waiting to hear back from the server and it has timed out. It's
      // unlikely that the server will suddenly wake up and send us our data so
      // lets disconnect and go shopping.
      client.end();
    }
  });

  // HOW WE HANDLE ERRORS. We should probably reconnect, maybe retry.
  client.addListener("error", function(exception){
    if(debug) {
      log("Client error:", exception);
    }
    instrumentalStats.last_exception = Math.round(new Date().getTime() / 1000);
  });

  // What do we do when instrumental talks to us
  var totalBuffer = "";
  client.on("data", function(buffer) {
    totalBuffer = totalBuffer + buffer;

    if(debug) {
      log("Received:", buffer);
    }

    // Authorization success
    if(totalBuffer == "ok\nok\n") {
      error = false;

      if(debug) {
        log("Sending:", payload.join("\n"));
      }

      client.end(payload.join("\n") + "\n", function() {
        tlsOptionsVariation.lastSuccessAt = new Date();
        if (debug) log("payload sent, tlsOptionsVariation: ", tlsOptionsVariation);
        state = "sent";
      });

      instrumentalStats.last_flush = Math.round(new Date().getTime() / 1000);

    // Authorization failure
    } else if(totalBuffer.length >= "ok\nok\n".length) {
      // TODO: Actually do something with this
      error = true;

      instrumentalStats.last_exception = Math.round(new Date().getTime() / 1000);
    }
  });
}
function instrumental_send(payload) {
  var state = "cold";
  var tlsOptionsVariation = selectTlsOptionsVariation();

  var client = instrumental_connection(host, port, tlsOptionsVariation, function(_client){
    state = "connected";

    var cleanString = function(value) {
      return String(value).replace(/\s+/g, "_");
    }

    // Write the authentication header
    _client.write(
      "hello version node/statsd-instrumental-backend/" + cleanString(package.version) +
        " hostname " + cleanString(hostname) +
        " pid " + cleanString(process.pid) +
        " runtime " + cleanString("node/" + process.versions.node) +
        " platform " + cleanString(process.platform + "-" + process.arch) + "\n" +
      "authenticate " + key + "\n"
    );
  });

  // We need to handle the timeout. I think we should only care about read
  // timeouts. That is we write out data, if we don't hear back in timeout
  // seconds then the data probably hasn't reached the server.
  client.setTimeout(timeout, function() {
    // ZOMG FAILED WRITING WE ARE BAD AT COMPUTER SCIENCE
    if(state == "connected") {
      // We're waiting to hear back from the server and it has timed out. It's
      // unlikely that the server will suddenly wake up and send us our data so
      // lets disconnect and go shopping.
      client.end();
    }
  });

  // HOW WE HANDLE ERRORS. We should probably reconnect, maybe retry.
  client.addListener("error", function(exception){
    if(debug) {
      log("Client error:", exception);
    }
    instrumentalStats.last_exception = Math.round(new Date().getTime() / 1000);
  });

  // What do we do when instrumental talks to us
  var totalBuffer = "";
  client.on("data", function(buffer) {
    totalBuffer = totalBuffer + buffer;

    if(debug) {
      log("Received:", buffer);
    }

    // Authorization success
    if(totalBuffer == "ok\nok\n") {
      error = false;

      if(debug) {
        log("Sending:", payload.join("\n"));
      }

      client.end(payload.join("\n") + "\n", function() {
        tlsOptionsVariation.lastSuccessAt = new Date();
        if (debug) log("payload sent, tlsOptionsVariation: ", tlsOptionsVariation);
        state = "sent";
      });

      instrumentalStats.last_flush = Math.round(new Date().getTime() / 1000);

    // Authorization failure
    } else if(totalBuffer.length >= "ok\nok\n".length) {
      // TODO: Actually do something with this
      error = true;

      instrumentalStats.last_exception = Math.round(new Date().getTime() / 1000);
    }
  });
}
function instrumental_send ( payload ) { var state = "cold" ; var tlsOptionsVariation = selectTlsOptionsVariation ( ) ; var client = instrumental_connection ( host , port , tlsOptionsVariation , function ( _client ) { state = "connected" ; var cleanString = function ( value ) { return String ( value ) . replace ( / \s+ / g , "_" ) ; } // Write the authentication header _client . write ( "hello version node/statsd-instrumental-backend/" + cleanString ( package . version ) + " hostname " + cleanString ( hostname ) + " pid " + cleanString ( process . pid ) + " runtime " + cleanString ( "node/" + process . versions . node ) + " platform " + cleanString ( process . platform + "-" + process . arch ) + "\n" + "authenticate " + key + "\n" ) ; } ) ; // We need to handle the timeout. I think we should only care about read // timeouts. That is we write out data, if we don't hear back in timeout // seconds then the data probably hasn't reached the server. client . setTimeout ( timeout , function ( ) { // ZOMG FAILED WRITING WE ARE BAD AT COMPUTER SCIENCE if ( state == "connected" ) { // We're waiting to hear back from the server and it has timed out. It's // unlikely that the server will suddenly wake up and send us our data so // lets disconnect and go shopping. client . end ( ) ; } } ) ; // HOW WE HANDLE ERRORS. We should probably reconnect, maybe retry. client . addListener ( "error" , function ( exception ) { if ( debug ) { log ( "Client error:" , exception ) ; } instrumentalStats . last_exception = Math . round ( new Date ( ) . getTime ( ) / 1000 ) ; } ) ; // What do we do when instrumental talks to us var totalBuffer = "" ; client . on ( "data" , function ( buffer ) { totalBuffer = totalBuffer + buffer ; if ( debug ) { log ( "Received:" , buffer ) ; } // Authorization success if ( totalBuffer == "ok\nok\n" ) { error = false ; if ( debug ) { log ( "Sending:" , payload . join ( "\n" ) ) ; } client . end ( payload . join ( "\n" ) + "\n" , function ( ) { tlsOptionsVariation . lastSuccessAt = new Date ( ) ; if ( debug ) log ( "payload sent, tlsOptionsVariation: " , tlsOptionsVariation ) ; state = "sent" ; } ) ; instrumentalStats . last_flush = Math . round ( new Date ( ) . getTime ( ) / 1000 ) ; // Authorization failure } else if ( totalBuffer . length >= "ok\nok\n" . length ) { // TODO: Actually do something with this error = true ; instrumentalStats . last_exception = Math . round ( new Date ( ) . getTime ( ) / 1000 ) ; } } ) ; }
Push data to instrumental
Push data to instrumental