From fde45387cd75f0f3e24586d7ce57b9e459ca4db6 Mon Sep 17 00:00:00 2001 From: SwenChan Date: Thu, 21 Sep 2017 19:01:09 +0800 Subject: [PATCH] =?UTF-8?q?!2=20=E6=96=B0=E5=BB=BA=E4=B8=80=E4=B8=AAtopic?= =?UTF-8?q?=E6=94=B6=E9=9B=86log=EF=BC=8C=E5=8F=AF=E4=BB=A5=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E9=A1=B9=E7=9B=AE=E5=90=8D=E3=80=81=E6=9C=88=E4=BB=BD?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=BB=BA=E8=A1=A8=20Merge=20pull=20request?= =?UTF-8?q?=20!2=20from=20june/feat-new-log-collector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 5 ++++- docker/Dockerfile | 14 ++++++++------ server/lib/amqp.js | 26 ++++++++++++++++++++++++-- server/model/klgLogTemplate.js | 27 +++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 server/model/klgLogTemplate.js diff --git a/Makefile b/Makefile index 71e87b7..ab99347 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,5 @@ +TAG = $(shell date +%Y%m%d%H%M%S) + build: - cp docker/Dockerfile . && docker build -t r.p.cailve.cn/logcollector . && rm Dockerfile + cp docker/Dockerfile . && docker build -t r.p.cailve.cn/logcollector:${TAG} . && rm Dockerfile + docker push r.p.cailve.cn/logcollector:${TAG} diff --git a/docker/Dockerfile b/docker/Dockerfile index 6b17a2f..dbe858e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,6 +1,8 @@ -FROM node:6-alpine -WORKDIR /apps -COPY . . -RUN npm i --registry=https://registry.npm.taobao.org \ - && npm cache clean -CMD ["node","server/app.js"] +FROM node:6-alpine +WORKDIR /apps +COPY . . +RUN mkdir -p /log/ +RUN mkdir -p /apps/log/ +RUN npm i --registry=https://registry.npm.taobao.org \ + && npm cache clean +CMD ["node","server/app.js"] diff --git a/server/lib/amqp.js b/server/lib/amqp.js index 626927f..a604acd 100644 --- a/server/lib/amqp.js +++ b/server/lib/amqp.js @@ -2,7 +2,10 @@ const co = require('co'); const URL = require('url-parse'); const config = require('../config'); const log = require('../model/log'); +const klgLogTemplate = require('../model/klgLogTemplate'); const logger = require('./logger').logger; +const db = require('../config/mongo'); +const moment = require('moment-timezone'); const bus = require('servicebus').bus({ url: config.app.rabbitmqUri, @@ -12,7 +15,7 @@ const bus = require('servicebus').bus({ bus.listen('koala.logCollector.saveReq', function (data) { data.interface = new URL(data.url).pathname; logger.debug(data); - const operation = co.wrap(function * () { + const operation = co.wrap(function* () { return yield log.saveReq(data); }); operation().then(function (result) { @@ -24,7 +27,7 @@ bus.listen('koala.logCollector.saveReq', function (data) { bus.listen('koala.logCollector.saveRes', function (data) { logger.debug(data); - const operation = co.wrap(function * () { + const operation = co.wrap(function* () { return yield log.saveRes(data); }); operation().then(function (result) { @@ -33,3 +36,22 @@ bus.listen('koala.logCollector.saveRes', function (data) { logger.warn(err); }); }); + +bus.listen('klg.log.collector.in.save', function (data) { + logger.debug(data); + + let service = (data.service || 'klg_project').toLowerCase(); + service = service.replace(new RegExp(/\-/g), '_'); + service += `_${moment().tz('Asia/Shanghai').format('YYYYMM')}`; + const model = db.main.model(service, klgLogTemplate); + + const operation = co.wrap(function* () { + return yield model.create(data); + }); + operation().then(function (result) { + logger.debug(result); + }).catch(function (err) { + logger.warn(err); + }); +}); + diff --git a/server/model/klgLogTemplate.js b/server/model/klgLogTemplate.js new file mode 100644 index 0000000..6753df1 --- /dev/null +++ b/server/model/klgLogTemplate.js @@ -0,0 +1,27 @@ +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; + +const schema = new Schema({ + machine: {type: String, required: true, default: '', index: true}, + service: {type: String, required: true, default: ''}, + type: {type: String, required: true, enum: ['in', 'out'], default: 'in', index: true}, + method: {type: String, required: true, default: ''}, + agent: {type: String, required: true, default: ''}, + ip: {type: String, required: true, default: ''}, + requestId: {type: String, required: true, default: '', index: true}, + url: {type: String, required: true, default: ''}, + interface: {type: String, required: true, default: '', index: true}, + userId: {type: String, required: true, default: '', index: true}, + orderId: {type: String, required: true, default: '', index: true}, + execTime: {type: Number, required: true, default: 0}, + request: {type: Object, required: true, default: {}}, + response: {type: Object, required: true, default: {}}, + extend: {type: Object, required: true, default: {}}, + responseTime: {type: Number, required: true, default: 0}, + requestTime: {type: Number, required: true, default: 0, index: true}, +}); + +schema.set('timestamps', true); // createAt, updatedAt -> UTC +schema.set('minimize', false); + +module.exports = schema; -- Gitee