提交 e008b16f 编写于 作者: Y youyong205

Merge pull request #193 from ainilife/biz

refine and bug fix
CAT
CAT [![Build Status](https://travis-ci.org/dianping/cat.png?branch=biz)](https://travis-ci.org/dianping/cat)
===
<hr>
Central Application Tracking [![Build Status](https://travis-ci.org/dianping/cat.png?branch=biz)](https://travis-ci.org/dianping/cat)
CAT的全称是Central Application Tracking,是基于Java开发的实时应用监控平台,包括实时系统监控、应用监控以及业务监控。
CAT主要通过以下几种埋点类型收集信息:
* Event 用来记录次数,表名单位时间内消息发生次数,比如记录系统异常,它和transaction相比缺少了时间的统计,开销比transaction要小。
* Transaction 适合记录跨越系统边界的程序访问行为,,比如远程调用,数据库调用,也适合执行时间较长的业务逻辑监控。
* Heartbeat Heartbeta表示程序内定期产生的统计信息, 如CHPU%, MEM%, 连接池状态, 系统负载等。
* Metric 用于业务监控埋点的API。
Requirements
---------------------
* Java 6
* Maven
* MySQL
Quick Started
---------------------
#####1、安装Java 6+,Maven以及MySQL
#####2、cd到CAT目录下,用maven构建项目
mvn eclipse:clean eclipse:eclipse
#####3、安装CAT的maven plugin,使用它配置CAT的环境
cd cat-maven-plugin;mvn cat:install
确保系统的临时目录程序拥有读写权限,Linux为/tmp/目录
#####5、如果你安装了hadoop集群,请到/data/appdatas/cat/server.xml中配置对应hadoop信息,并将localmode设置为false。默认情况下,CAT在localmode=true的开发模式下工作。
#####1、在CAT目录下,用maven构建项目
mvn clean install
#####2、安装CAT的maven plugin,使用它配置CAT的环境
mkdir -p /data/appdata/cat; chmod 777 /data/appdata/cat
mkdir -p /data/applogs/cat; chmod 777 /data/applogs/cat
mvn cat:install
#####3、如果安装了hadoop集群,需到/data/appdatas/cat/server.xml中配置对应hadoop信息。将localmode设置为false,默认情况下,CAT在开发模式(localmode=true)下工作。
#####4、运行CAT
开发模式下有两种方式启动:
方式一:
cd cat-home;mvn jetty:run
然后浏览http://localhost:2281
方式二:
将项目导入到eclipse中,运行cat-home项目里得‘com.dianping.cat.TestServer’来启动CAT。
cd cat-home;mvn jetty:run
然后打开浏览器,输入http://localhost:2281。
或者在cat目录下输入
mvn eclipse:clean eclipse:eclipse
然后将项目导入到eclipse中,运行cat-home项目里得‘com.dianping.cat.TestServer’来启动CAT。
Copyright and license
---------------------
......
......@@ -42,4 +42,13 @@ public class TrafficDistributionGroupStrategy implements ABTestGroupStrategy {
m_percentA = 50;
}
}
public void init(int... percents) {
if (percents.length == 2) {
if (percents[0] + percents[1] == 100) {
m_percentControl = percents[0];
m_percentA = percents[1];
}
}
}
}
......@@ -92,7 +92,7 @@ public class MessageIdFactory {
String tmpDir = System.getProperty("java.io.tmpdir");
File mark = new File(tmpDir, "cat-" + domain + ".mark");
System.out.println("Message id mark file: " + mark.getCanonicalPath());
//System.out.println("Message id mark file: " + mark.getCanonicalPath());
m_markFile = new RandomAccessFile(mark, "rw");
m_byteBuffer = m_markFile.getChannel().map(MapMode.READ_WRITE, 0, 20);
......
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="config" type="ConfigType"/>
<xs:complexType name="ConfigType">
<xs:sequence>
......
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="status" type="StatusType"/>
<xs:complexType name="StatusType">
<xs:sequence>
......
......@@ -4,6 +4,9 @@ import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import com.dianping.cat.abtest.spi.internal.ABTestCodecTest;
import com.dianping.cat.abtest.spi.internal.ABTestEntityManagerTest;
import com.dianping.cat.abtest.spi.internal.groupstrategy.TrafficDistributionGroupStrategyTest;
import com.dianping.cat.configuration.ConfigTest;
import com.dianping.cat.log4j.CatAppenderTest;
import com.dianping.cat.message.EventTest;
......@@ -26,6 +29,12 @@ CatAppenderTest.class,
/* .message */
MessageTest.class,
TrafficDistributionGroupStrategyTest.class,
ABTestCodecTest.class,
ABTestEntityManagerTest.class,
EventTest.class,
HeartbeatTest.class,
......
......@@ -9,7 +9,7 @@ import junit.framework.Assert;
import org.junit.Test;
import org.unidal.lookup.ComponentTestCase;
public class ABTestContextManagerTest extends ComponentTestCase {
public class ABTestCodecTest extends ComponentTestCase {
@Test
public void testCodec() throws Exception {
check("1=ab:A|cd:B&2=ab:A|cd:B", "1=ab:A|cd:B&2=ab:A|cd:B");
......
package com.dianping.cat.abtest.spi.internal;
public class ABTestContextTest {
}
package com.dianping.cat.abtest.spi.internal.groupstrategy;
import junit.framework.Assert;
import org.junit.Test;
import org.unidal.lookup.ComponentTestCase;
import com.dianping.cat.abtest.spi.ABTestContext;
import com.dianping.cat.abtest.spi.ABTestEntity;
import com.dianping.cat.abtest.spi.ABTestGroupStrategy;
import com.dianping.cat.abtest.spi.internal.DefaultABTestContext;
public class TrafficDistributionGroupStrategyTest extends ComponentTestCase {
@Test
public void test() throws Exception {
testApply(50, 50);
testApply(40, 60);
testApply(55, 45);
}
public void testApply(int expectDefault, int expectA) throws Exception {
TrafficDistributionGroupStrategy strategy = (TrafficDistributionGroupStrategy) lookup(ABTestGroupStrategy.class,
TrafficDistributionGroupStrategy.ID);
ABTestContext ctx = new DefaultABTestContext(new ABTestEntity());
strategy.init(expectDefault, expectA);
int countDefault = 0;
int countA = 0;
for (int i = 0; i < 100; i++) {
strategy.apply(ctx);
if (ctx.getGroupName().equals(ABTestContext.DEFAULT_GROUP)) {
countDefault++;
} else {
countA++;
}
}
Assert.assertEquals(expectDefault, countDefault);
Assert.assertEquals(expectA, countA);
}
}
......@@ -24,6 +24,6 @@ public class ProblemReportAllBuilderTest {
builder.visitProblemReport(reportOld);
builder.visitProblemReport(reportNew);
Assert.assertEquals(expected, report.toString());
Assert.assertEquals(expected.replaceAll("\r", ""), report.toString().replaceAll("\r", ""));
}
}
......@@ -36,8 +36,6 @@ public class LocalMessageBucket implements MessageBucket, LogEnabled {
private AtomicBoolean m_dirty = new AtomicBoolean();
private int m_rawSize;
private String m_dataFile;
private long m_lastAccessTime;
......@@ -143,10 +141,6 @@ public class LocalMessageBucket implements MessageBucket, LogEnabled {
return m_writer;
}
public double getCompressionRate() {
return m_rawSize * 1.0 / m_dataFile.length();
}
@Override
public long getLastAccessTime() {
return m_lastAccessTime;
......
......@@ -43,8 +43,6 @@ public class HtmlMessageCodecTest extends ComponentTestCase {
Cat.logMetricForDuration("logMetricForDuration", 100);
Cat.logMetricForSum("logMetricForSum", 100);
Cat.logMetricForSum("logMetricForSum", 100, 100);
Cat.logError(new RuntimeException());
Cat.logError("message", new RuntimeException());
Cat.logEvent("RemoteLink", "Call", Message.SUCCESS, "Cat-0a010680-384736-2061");
Cat.logEvent("EventType", "EventName");
Cat.logHeartbeat("logHeartbeat", "logHeartbeat", Message.SUCCESS, null);
......@@ -146,6 +144,8 @@ public class HtmlMessageCodecTest extends ComponentTestCase {
String messageId = "Cat-0a010680-384736-2061";
String parentMessageId = "Cat-0a010680-384736-2062";
tree.setHostName("localhost-cat");
tree.setIpAddress("192.168.1.1");
tree.setRootMessageId(messageId);
tree.setParentMessageId(parentMessageId);
codec.encode(tree, buf);
......@@ -153,7 +153,7 @@ public class HtmlMessageCodecTest extends ComponentTestCase {
String content = Files.forIO().readFrom(HtmlMessageCodecTest.class.getResourceAsStream("MessageTree.txt"),
"utf-8");
Assert.assertEquals(content.trim(), tree.toString().trim());
Assert.assertEquals(6795, buf.readableBytes());
Assert.assertEquals(1929, buf.readableBytes());
}
@Test
......
......@@ -15,7 +15,7 @@ import org.unidal.lookup.ComponentTestCase;
public abstract class StringBucketTestCase extends ComponentTestCase {
protected final static int threadNum = 10;// notice: max 9, for creating asc order id bellow
protected final static int threadNum = 200;// notice: max 9, for creating asc order id bellow
protected final static int timesPerThread = 1000; // notice: must be powers 10, fro creating asc order id bellow
......
......@@ -5,7 +5,6 @@ import java.io.IOException;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
......@@ -21,7 +20,6 @@ import com.dianping.cat.message.spi.codec.PlainTextMessageCodec;
import com.dianping.cat.message.spi.internal.DefaultMessageTree;
@RunWith(JUnit4.class)
@Ignore
public class LocalMessageBucketManagerTest extends ComponentTestCase {
private DefaultMessageTree newMessageTree(String id, int i, long timestamp) {
DefaultMessageTree tree = new DefaultMessageTree();
......
PT1 Cat youyongmatoMacBook-Air.local 192.168.22.92 main 1 main Cat-c0a80746-384747-36441 Cat-0a010680-384736-2062 Cat-0a010680-384736-2061 null
PT1 Cat localhost-cat 192.168.1.1 main 1 main Cat-c0a80746-384747-36441 Cat-0a010680-384736-2062 Cat-0a010680-384736-2061 null
t2012-01-02 15:33:41.987 transaction transaction
L2012-01-02 15:33:41.987 logTrace <trace> 0
L2012-01-02 15:33:41.987 logTrace <trace> 0 data
......@@ -8,8 +8,6 @@ M2012-01-02 15:33:41.987 logMetricForCount C 4
M2012-01-02 15:33:41.987 logMetricForDuration T 100
M2012-01-02 15:33:41.987 logMetricForSum S 100.00
M2012-01-02 15:33:41.987 logMetricForSum S,C 100.00,100
E2012-01-02 15:33:41.987 RuntimeException java.lang.RuntimeException ERROR java.lang.RuntimeException\n\tat com.dianping.cat.message.spi.core.HtmlMessageCodecTest.buildMessageTree(HtmlMessageCodecTest.java:46)\n\tat com.dianping.cat.message.spi.core.HtmlMessageCodecTest.testEncode(HtmlMessageCodecTest.java:143)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n\tat java.lang.reflect.Method.invoke(Method.java:597)\n\tat org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)\n\tat org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)\n\tat org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)\n\tat org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)\n\tat org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)\n\tat org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)\n\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)\n\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)\n\tat org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)\n\tat org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)\n\tat org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)\n\tat org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)\n\tat org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)\n\tat org.junit.runners.ParentRunner.run(ParentRunner.java:236)\n\tat org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)\n\tat org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)\n\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)\n\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)\n\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)\n\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)\n
E2012-01-02 15:33:41.987 RuntimeException java.lang.RuntimeException ERROR message java.lang.RuntimeException\n\tat com.dianping.cat.message.spi.core.HtmlMessageCodecTest.buildMessageTree(HtmlMessageCodecTest.java:47)\n\tat com.dianping.cat.message.spi.core.HtmlMessageCodecTest.testEncode(HtmlMessageCodecTest.java:143)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n\tat java.lang.reflect.Method.invoke(Method.java:597)\n\tat org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)\n\tat org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)\n\tat org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)\n\tat org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)\n\tat org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)\n\tat org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)\n\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)\n\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)\n\tat org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)\n\tat org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)\n\tat org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)\n\tat org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)\n\tat org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)\n\tat org.junit.runners.ParentRunner.run(ParentRunner.java:236)\n\tat org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)\n\tat org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)\n\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)\n\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)\n\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)\n\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)\n
E2012-01-02 15:33:41.987 RemoteLink Call 0 Cat-0a010680-384736-2061
E2012-01-02 15:33:41.987 EventType EventName 0
H2012-01-02 15:33:41.987 logHeartbeat logHeartbeat 0
......
......@@ -25,6 +25,8 @@ public class InstallMojo extends AbstractMojo {
private String m_path = "/data/appdatas/cat";
private String m_logPath = "/data/applogs/cat";
private String m_clientPath = m_path + File.separator + "client.xml";
private String m_serverPath = m_path + File.separator + "server.xml";
......@@ -91,6 +93,7 @@ public class InstallMojo extends AbstractMojo {
getLog().info("Preparing Cat environment ... DONE");
getLog().info("Use following command line to start local Cat server:");
getLog().info(" cd cat-home; mvn jetty:run");
getLog().info("Please open http://localhost:2281/cat in your browser");
}
}
......@@ -101,16 +104,18 @@ public class InstallMojo extends AbstractMojo {
return conn;
}
private boolean setupConfigurationFiles() {
private boolean setupConfigurationFiles() throws MojoFailureException {
File path = new File(m_path);
File logPath = new File(m_logPath);
if (!path.exists()) {
path.mkdirs();
logPath.mkdirs();
}
if (!path.canRead() || !path.canWrite()) {
if (!path.canRead() || !path.canWrite() || !logPath.canRead() || !logPath.canWrite()) {
getLog().error("Don't have privilege to read/write " + m_path);
return false;
throw new MojoFailureException("Don't have privilege to read/write " + m_path);
}
getLog().info("Generating the configuration files to " + m_path + " ...");
......@@ -153,7 +158,7 @@ public class InstallMojo extends AbstractMojo {
}
}
private boolean setupDatabase() {
private boolean setupDatabase() throws MojoFailureException {
Connection conn = null;
Statement stmt = null;
boolean isSuccess = false;
......@@ -175,6 +180,7 @@ public class InstallMojo extends AbstractMojo {
isSuccess = true;
} catch (Throwable e) {
getLog().error(e);
throw new MojoFailureException(e.getMessage());
} finally {
try {
if (stmt != null) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册