Permalink
Browse files

Opcion de mandar metrica con hora de comienzo del evento y no con el …

…tiempo total, sirve para access logs
  • Loading branch information...
1 parent d6764d9 commit b17d5dbe1d5ff93c49232e3d444206a35ae509ca @matiasrege matiasrege committed Sep 3, 2012
View
1 .classpath
@@ -6,6 +6,7 @@
<classpathentry kind="lib" path="newrelic/lib/guava-r05.jar"/>
<classpathentry kind="lib" path="newrelic/lib/newrelic.jar"/>
<classpathentry kind="lib" path="newrelic/lib/slf4j-api-1.6.1.jar"/>
+ <classpathentry kind="lib" path="newrelic/lib/commons-cli-1.2.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="newrelic/lib/mina-core-2.0.4.jar"/>
<classpathentry kind="lib" path="newrelic/lib/mina-filter-compression-2.0.4.jar"/>
View
6 newrelic-node.js
@@ -9,16 +9,16 @@ log.setLevel("info");
switch(process.env.NODE_ENV){
case 'testing':
- exec("java -Xms16M -Xmx16M -Xss256k -javaagent:lib/newrelic.jar -Dnewrelic.config.file=newrelic-test.yml -Dnewrelic.environment=production -cp ./lib/slf4j-api-1.6.1.jar:./lib/mina-core-2.0.4.jar:./lib/mina-transport-apr-2.0.4.jar:./lib/mina-filter-compression-2.0.4.jar:./lib/commons-lang-2.6.jar:./lib/mina-statemachine-2.0.4.jar:./lib/mina-integration-beans-2.0.4.jar:./lib/commons-logging-1.0.3.jar:./lib/mina-integration-xbean-2.0.4.jar:./lib/mina-integration-ognl-2.0.4.jar:./lib/mina-integration-jmx-2.0.4.jar:./lib/guava-r05.jar:./bin/ Monitor 9999 5120000 1> monitor.log 2> monitor.err",
+ exec("java -Xms16M -Xmx16M -Xss256k -javaagent:lib/newrelic.jar -Dnewrelic.config.file=newrelic-test.yml -Dnewrelic.environment=production -cp ./lib/slf4j-api-1.6.1.jar:./lib/mina-core-2.0.4.jar:./lib/mina-transport-apr-2.0.4.jar:./lib/mina-filter-compression-2.0.4.jar:./lib/commons-lang-2.6.jar:./lib/mina-statemachine-2.0.4.jar:./lib/mina-integration-beans-2.0.4.jar:./lib/commons-logging-1.0.3.jar:./lib/mina-integration-xbean-2.0.4.jar:./lib/mina-integration-ognl-2.0.4.jar:./lib/mina-integration-jmx-2.0.4.jar:./lib/guava-r05.jar:./lib/commons-cli-1.2.jar:./bin/ Monitor -port 9999 - buffer 5120000 1> monitor.log 2> monitor.err",
{cwd:__dirname + "/newrelic/", stdio: 'ignore'}, function (error, stdout, stderr) {/*ver que hacer si se muere el proceso java*/});
case 'development':
//entorno de desarollo, no hago nada
case 'production':
- exec("java -Xms16M -Xmx16M -Xss256k -javaagent:lib/newrelic.jar -Dnewrelic.config.file=newrelic.yml -Dnewrelic.environment=production -cp ./lib/slf4j-api-1.6.1.jar:./lib/mina-core-2.0.4.jar:./lib/mina-transport-apr-2.0.4.jar:./lib/mina-filter-compression-2.0.4.jar:./lib/commons-lang-2.6.jar:./lib/mina-statemachine-2.0.4.jar:./lib/mina-integration-beans-2.0.4.jar:./lib/commons-logging-1.0.3.jar:./lib/mina-integration-xbean-2.0.4.jar:./lib/mina-integration-ognl-2.0.4.jar:./lib/mina-integration-jmx-2.0.4.jar:./lib/guava-r05.jar:./bin/ Monitor 9999 5120000 1> monitor.log 2> monitor.err",
+ exec("java -Xms16M -Xmx16M -Xss256k -javaagent:lib/newrelic.jar -Dnewrelic.config.file=newrelic.yml -Dnewrelic.environment=production -cp ./lib/slf4j-api-1.6.1.jar:./lib/mina-core-2.0.4.jar:./lib/mina-transport-apr-2.0.4.jar:./lib/mina-filter-compression-2.0.4.jar:./lib/commons-lang-2.6.jar:./lib/mina-statemachine-2.0.4.jar:./lib/mina-integration-beans-2.0.4.jar:./lib/commons-logging-1.0.3.jar:./lib/mina-integration-xbean-2.0.4.jar:./lib/mina-integration-ognl-2.0.4.jar:./lib/mina-integration-jmx-2.0.4.jar:./lib/guava-r05.jar:./lib/commons-cli-1.2.jar:./bin/ Monitor -port 9999 -buffer 5120000 1> monitor.log 2> monitor.err",
{cwd:__dirname + "/newrelic/", stdio: 'ignore'}, function (error, stdout, stderr) {/*ver que hacer si se muere el proceso java*/});
default:
- exec("java -Xms16M -Xmx16M -Xss256k -javaagent:lib/newrelic.jar -Dnewrelic.config.file=newrelic.yml -Dnewrelic.environment=production -cp ./lib/slf4j-api-1.6.1.jar:./lib/mina-core-2.0.4.jar:./lib/mina-transport-apr-2.0.4.jar:./lib/mina-filter-compression-2.0.4.jar:./lib/commons-lang-2.6.jar:./lib/mina-statemachine-2.0.4.jar:./lib/mina-integration-beans-2.0.4.jar:./lib/commons-logging-1.0.3.jar:./lib/mina-integration-xbean-2.0.4.jar:./lib/mina-integration-ognl-2.0.4.jar:./lib/mina-integration-jmx-2.0.4.jar:./lib/guava-r05.jar:./bin/ Monitor 9999 5120000 1> monitor.log 2> monitor.err",
+ exec("java -Xms16M -Xmx16M -Xss256k -javaagent:lib/newrelic.jar -Dnewrelic.config.file=newrelic.yml -Dnewrelic.environment=production -cp ./lib/slf4j-api-1.6.1.jar:./lib/mina-core-2.0.4.jar:./lib/mina-transport-apr-2.0.4.jar:./lib/mina-filter-compression-2.0.4.jar:./lib/commons-lang-2.6.jar:./lib/mina-statemachine-2.0.4.jar:./lib/mina-integration-beans-2.0.4.jar:./lib/commons-logging-1.0.3.jar:./lib/mina-integration-xbean-2.0.4.jar:./lib/mina-integration-ognl-2.0.4.jar:./lib/mina-integration-jmx-2.0.4.jar:./lib/guava-r05.jar:./lib/commons-cli-1.2.jar:./bin/ Monitor -port 9999 -buffer 5120000 1> monitor.log 2> monitor.err",
{cwd:__dirname + "/newrelic/", stdio: 'ignore'}, function (error, stdout, stderr) {/*ver que hacer si se muere el proceso java*/});
}
View
BIN newrelic/bin/Monitor.class
Binary file not shown.
View
BIN newrelic/bin/MonitorHandler$Stats.class
Binary file not shown.
View
BIN newrelic/bin/MonitorHandler.class
Binary file not shown.
View
BIN newrelic/lib/commons-cli-1.2.jar
Binary file not shown.
View
45 newrelic/src/Monitor.java
@@ -3,6 +3,11 @@
import java.nio.charset.Charset;
import java.util.Arrays;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
@@ -12,27 +17,47 @@
*/
public class Monitor {
- private Monitor(int port, int buffer, boolean debug) throws IOException {
+ private Monitor(CommandLine cmd) throws IOException {
NioSocketAcceptor acceptor = new NioSocketAcceptor(4);
TextLineCodecFactory factory = new TextLineCodecFactory(Charset.forName("UTF-8"));
- factory.setDecoderMaxLineLength(buffer);
+ if (cmd.hasOption("buffer"))
+ factory.setDecoderMaxLineLength(Integer.valueOf(cmd.getOptionValue("buffer")));
+
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(factory));
MonitorHandler handler = new MonitorHandler();
- handler.setDebug(debug);
- handler.setBuffer(buffer);
+
+ if (cmd.hasOption("debug"))
+ handler.setDebug(Boolean.valueOf(cmd.getOptionValue("debug")));
+
+ if (cmd.hasOption("formatdate"))
+ handler.setFormatDate(cmd.getOptionValue("formatdate"));
+
+ if (cmd.hasOption("buffer"))
+ handler.setBuffer(Integer.valueOf(cmd.getOptionValue("buffer")));
acceptor.setHandler(handler);
- acceptor.bind(new InetSocketAddress(port));
+ int port = cmd.hasOption("port") ? Integer.valueOf(cmd.getOptionValue("port")) : 9999;
+ acceptor.bind(new InetSocketAddress(port));
System.out.println("Listening on port " + port);
}
- public static void main(String[] args) throws IOException {
+
+ public static void main(String[] args) throws IOException, ParseException {
+ // create Options object
+ Options options = new Options();
+
+ // add t option
+ options.addOption("f", "formatdate", true, "format date");
+ options.addOption("b", "buffer", true, "buffer size");
+ options.addOption("d", "debug", true, "debug mode");
+ options.addOption("p", "port", true, "port");
+
+ CommandLineParser parser = new PosixParser();
+ CommandLine cmd = parser.parse( options, args);
+
System.out.println("Iniciando monitor con parametros: "+Arrays.toString(args));
- if (args.length > 2)
- new Monitor(Integer.valueOf(args[0]), Integer.valueOf(args[1]), Boolean.valueOf(args[2]));
- else
- new Monitor(Integer.valueOf(args[0]), Integer.valueOf(args[1]), false);
+ new Monitor(cmd);
System.out.println("Server started.");
}
}
View
27 newrelic/src/MonitorHandler.java
@@ -1,5 +1,8 @@
import java.text.DecimalFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
import java.util.Collections;
+import java.util.Date;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
@@ -31,6 +34,7 @@
private AtomicLong procTime = new AtomicLong(0);
private Timer timer;
private int buffer;
+ private SimpleDateFormat sdf = new SimpleDateFormat("[dd/MMM/yyyy:HH:mm:ss Z]");
public void setBuffer(int buffer) {
this.buffer = buffer;
@@ -59,12 +63,23 @@ public void messageReceived(IoSession session, Object msg) throws Exception {
for (Object key : timespent.keySet()) {
Object timeObj = timespent.get(key);
- Long time;
+ Long time = 0L;
if (timeObj instanceof Double) {
time = (long) (((Double) timeObj)*1000);
- } else
+ } else if (timeObj instanceof Long) {
time = (Long)timeObj;
+ } else if (timeObj instanceof String) {
+ try {
+ synchronized (sdf) {
+ Date requestTime = sdf.parse((String) timeObj);
+ time = begin - requestTime.getTime();
+ }
+ } catch (ParseException e) {
+ reportParserError(msg.toString(), e);
+ return;
+ }
+ }
totaltime += time;
@@ -75,7 +90,7 @@ public void messageReceived(IoSession session, Object msg) throws Exception {
StatsEngine.getApdexStats(MetricSpec.lookup(MetricNames.APDEX + "/Uri/" + path)).recordApdexResponseTime(time);
}
}
-
+
StatsEngine.getResponseTimeStats(MetricSpec.DISPATCHER).recordResponseTime(totaltime);
StatsEngine.getApdexStats(MetricSpec.APDEX).recordApdexResponseTime(totaltime);
@@ -128,8 +143,10 @@ public void setDebug(boolean debug) {
this.debug = debug;
}
-
-
+ public void setFormatDate(String format) {
+ sdf = new SimpleDateFormat(format);
+ }
+
public class Stats extends TimerTask {
private DecimalFormat dformat = new DecimalFormat("#####.00");
View
2 package.json
@@ -15,7 +15,7 @@
"node-logging" : "0.1.x",
"request": "2.9.x"
}
-, "scripts": { "test": "export NODE_ENV=testing && mocha --globals myThis,myHolder,myCallee,State_myThis --reporter spec -t 2000 -s 3000 ${TESTFILE} && echo \"Visite https://rpm.newrelic.com/accounts/39947/applications/841797 para ver las metricas logueadas por los test.\" && pkill -9 -f Monitor"}
+, "scripts": { "test": "export NODE_ENV=testing && mocha --globals myThis,myHolder,myCallee,State_myThis --reporter spec -t 2000 -s 3000 ${TESTFILE} && echo \"Visite https://rpm.newrelic.com/accounts/39947/applications/841797 para ver las metricas logueadas por los test.\""}
, "devDependencies": {
"mocha" : "0.14.x",
"should" : "0.6.x"
View
14 test/test.js
@@ -130,5 +130,19 @@ describe("process java" ,function(){
});
});
+
+ it("envio de mensaje con timespent en date",function(done){
+ this.timeout(2000);
+ newrelic.log({timespent:{URI_WEB_TRANSACTION: "[03/Sep/2012:10:20:06 -0300]"}, path:"/test", httpStatus:201, httpMethod:"GET"});
+ //termina de implementar
+ done()
+ });
+
+ it("envio de mensaje con timespent en date en formato incorrecto",function(done){
+ this.timeout(2000);
+ newrelic.log({timespent:{URI_WEB_TRANSACTION: "[31/Aug/2012:15:23:06 AASDASD"}, path:"/test", httpStatus:201, httpMethod:"GET"});
+ //termina de implementar
+ done()
+ });
})

0 comments on commit b17d5db

Please sign in to comment.