|
@ -1,12 +1,10 @@
|
|
|
package com.yihu.hos.broker.services;
|
|
|
package camel.monitor.processor;
|
|
|
|
|
|
import com.mongodb.MongoClient;
|
|
|
import com.mongodb.client.AggregateIterable;
|
|
|
import com.mongodb.client.MongoCollection;
|
|
|
import com.mongodb.client.MongoDatabase;
|
|
|
import com.mongodb.client.model.UpdateOptions;
|
|
|
import com.yihu.hos.broker.common.constants.MonitorConstant;
|
|
|
import com.yihu.hos.broker.models.LogOffset;
|
|
|
import com.yihu.hos.core.datatype.DateUtil;
|
|
|
import org.bson.Document;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -27,9 +25,12 @@ import java.util.Locale;
|
|
|
* <p>
|
|
|
* Created by chenweida on 2016/1/27.
|
|
|
*/
|
|
|
@Service("ServiceMonitorService")
|
|
|
@Service("serviceMonitorService")
|
|
|
public class ServiceMonitorService {
|
|
|
public static final String BEAN_ID = "ServiceMonitorService";
|
|
|
private static final String DATABASE = "log";
|
|
|
private static final String SERVICE = "service";
|
|
|
private static final String SERVICE_METRICS = "serviceMetrics";
|
|
|
private static final String BUSINESS = "business";
|
|
|
|
|
|
@Autowired
|
|
|
private MongoClient mongoClient;
|
|
@ -38,9 +39,9 @@ public class ServiceMonitorService {
|
|
|
|
|
|
public void monitor() {
|
|
|
try {
|
|
|
MongoDatabase database = mongoClient.getDatabase(MonitorConstant.DATABASE);
|
|
|
MongoDatabase database = mongoClient.getDatabase(DATABASE);
|
|
|
Query query = new Query();
|
|
|
query.addCriteria(Criteria.where("name").is(MonitorConstant.SERVICE)); //Service表中已有的数据时间。
|
|
|
query.addCriteria(Criteria.where("name").is(SERVICE)); //Service表中已有的数据时间。
|
|
|
LogOffset offset = mongoOperations.findOne(query, LogOffset.class);
|
|
|
Date begin = new Date();
|
|
|
Date end = new Date();
|
|
@ -61,7 +62,7 @@ public class ServiceMonitorService {
|
|
|
|
|
|
if (offset == null) {
|
|
|
offset = new LogOffset();
|
|
|
offset.setName(MonitorConstant.SERVICE);
|
|
|
offset.setName(SERVICE);
|
|
|
}
|
|
|
offset.setOffset(end.getTime());
|
|
|
mongoOperations.save(offset);
|
|
@ -72,7 +73,7 @@ public class ServiceMonitorService {
|
|
|
}
|
|
|
|
|
|
private void mapReduce(MongoDatabase database, String beginTime, String endTime) {
|
|
|
Document document = new Document("mapReduce", MonitorConstant.BUSINESS);
|
|
|
Document document = new Document("mapReduce", BUSINESS);
|
|
|
String mapFunc = "function(){emit(this.breadcrumbId, {fireTime:this.fireTime, createTime:this.createTime, bodyLength:this.bodyLength, tenant:this.tenant, routeId:this.routeId});}";
|
|
|
document = document.append("map", mapFunc);
|
|
|
String reduceFunc = "function(key, values){" +
|
|
@ -83,12 +84,12 @@ public class ServiceMonitorService {
|
|
|
document = document.append("reduce", reduceFunc);
|
|
|
String query = "{fireTime:{$gte:new Date(\"" + beginTime + "\"), $lt:new Date(\"" + endTime + "\")}}";
|
|
|
document = document.append("query", Document.parse(query));
|
|
|
document = document.append("out", MonitorConstant.SERVICE);
|
|
|
document = document.append("out", SERVICE);
|
|
|
database.runCommand(document);
|
|
|
}
|
|
|
|
|
|
private AggregateIterable<Document> aggregate(MongoDatabase database, String beginTime, String endTime) {
|
|
|
MongoCollection<Document> serviceCollection = database.getCollection(MonitorConstant.SERVICE);
|
|
|
MongoCollection<Document> serviceCollection = database.getCollection(SERVICE);
|
|
|
List<Document> pipeline = new ArrayList<>();
|
|
|
|
|
|
Document match = new Document();
|
|
@ -121,7 +122,7 @@ public class ServiceMonitorService {
|
|
|
}
|
|
|
|
|
|
private void outPut(MongoDatabase database, AggregateIterable<Document> documents) {
|
|
|
MongoCollection<Document> serviceMetrics = database.getCollection(MonitorConstant.SERVICE_METRICS);
|
|
|
MongoCollection<Document> serviceMetrics = database.getCollection(SERVICE_METRICS);
|
|
|
for (Document document : documents) {
|
|
|
Document id = (Document) document.get("_id");
|
|
|
String time = String.format("%04d", (int) id.get("year")) + String.format("%02d", (int) id.get("month")) + String.format("%02d", (int) id.get("day")) +
|