Skip to content

Commit 1d4c0f5

Browse files
author
Soichi Hayashi
committed
switching to post metrics to influxdb
1 parent c6ea8ae commit 1d4c0f5

File tree

5 files changed

+82
-21
lines changed

5 files changed

+82
-21
lines changed

api/config/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ exports.metrics = {
109109
api: "http://10.0.0.10:2080", //TODO
110110
}
111111

112+
exports.influxdb = {
113+
connection: {
114+
url: "http://brainlife_influxdb_1:8086",
115+
token: "mydevtoken",
116+
},
117+
org: "brainlife",
118+
bucket: "brainlife",
119+
location: "localhost",
120+
121+
countInterval: 10*1000,
122+
healthInterval: 10*1000,
123+
}
124+
112125
//for event handler
113126
exports.event = {
114127
amqp: {
@@ -221,6 +234,7 @@ exports.groupanalysis = {
221234
},
222235
}
223236

237+
224238
/*
225239
function connect_dc(cb) {
226240
var conn = new ssh2.Client();

bin/daily_metrics.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
#!/usr/bin/env node
22

3-
const winston = require('winston');
4-
53
const config = require('../api/config');
6-
config.logger.winston.transports[0].level = 'error';
74
const db = require('../api/models');
5+
const influx = require('@influxdata/influxdb-client');
86

97
const mongoose = require("mongoose");
108
mongoose.set("debug", false); //suppress log
119

1210
let graphite_prefix = process.argv[2];
13-
if(!graphite_prefix) {
14-
//console.error("usage: bl_metrics.js <graphite_prefix>");
15-
graphite_prefix = "dev"
16-
}
11+
if(!graphite_prefix) graphite_prefix = "dev"
1712

1813
function count_apps(d) {
1914
return new Promise((resolve, reject)=>{
2015
db.Apps.count({create_date: {$lt: d}, removed: false}, (err, count)=>{
2116
if(err) return reject(err);
2217
const time = Math.round(d.getTime()/1000);
2318
console.log(graphite_prefix+".app.count "+count+" "+time);
24-
resolve();
19+
resolve(count);
2520
});
2621
});
2722
}
@@ -42,7 +37,7 @@ function count_dataset(d) {
4237
if(err) return reject(err);
4338
const time = Math.round(d.getTime()/1000);
4439
console.log(graphite_prefix+".dataset.count "+count+" "+time);
45-
resolve();
40+
resolve(count);
4641
});
4742
});
4843
});
@@ -54,7 +49,7 @@ function count_project(d) {
5449
if(err) return reject(err);
5550
const time = Math.round(d.getTime()/1000);
5651
console.log(graphite_prefix+".project.count "+count+" "+time);
57-
resolve();
52+
resolve(count);
5853
});
5954
});
6055
}
@@ -66,7 +61,7 @@ function count_public_project(d) {
6661
if(err) return reject(err);
6762
const time = Math.round(d.getTime()/1000);
6863
console.log(graphite_prefix+".project.public "+count+" "+time);
69-
resolve();
64+
resolve(count);
7065
});
7166
});
7267
}
@@ -77,19 +72,28 @@ function count_private_project(d) {
7772
if(err) return reject(err);
7873
const time = Math.round(d.getTime()/1000);
7974
console.log(graphite_prefix+".project.private "+count+" "+time);
80-
resolve();
75+
resolve(count);
8176
});
8277
});
8378
}
8479

8580
db.init(async function(err) {
8681
if(err) throw err;
8782
let today = new Date();
88-
await count_apps(today);
89-
await count_dataset(today);
90-
await count_project(today);
91-
await count_public_project(today);
92-
await count_private_project(today);
83+
84+
const writeApi = new influx.InfluxDB(config.influxdb.connection)
85+
.getWriteApi(config.influxdb.org, config.influxdb.bucket, 'ns')
86+
writeApi.useDefaultTags({location: config.influxdb.location})
87+
const point = new influx.Point("warehouse");
88+
point.timestamp(today);
89+
point.intField("app", await count_apps(today));
90+
point.intField("dataset", await count_dataset(today));
91+
point.intField("all_project", await count_project(today));
92+
point.intField("public_project", await count_public_project(today));
93+
point.intField("private_project", await count_private_project(today));
94+
writeApi.writePoint(point);
95+
writeApi.close();
96+
9397
db.disconnect();
9498
});
9599

bin/event_handler.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const rp = require('request-promise-native');
99
const redis = require('redis');
1010
const fs = require('fs');
1111
const child_process = require('child_process');
12+
const influx = require('@influxdata/influxdb-client');
1213

1314
const pkg = require('../package.json');
1415

@@ -19,6 +20,7 @@ const common = require('../api/common');
1920
// TODO Look for failed tasks and report to the user/dev?
2021
let acon;
2122

23+
2224
console.log("connected to mongo");
2325
db.init(async err=>{
2426
common.connectAMQP((err, conn)=>{
@@ -28,8 +30,9 @@ db.init(async err=>{
2830
});
2931

3032
await common.connectRedis();
31-
setInterval(emit_counts, 1000*config.metrics.counts.interval); //usually 24 hours?
32-
setInterval(emit_health_counts, 1000*config.metrics.health_counts.interval); //usually 5min
33+
34+
setInterval(emit_counts, config.influxdb.countInterval); //usually 24 hours?
35+
setInterval(emit_health_counts, config.influxdb.countInterval); //usually 5min
3336
});
3437

3538
function subscribe() {
@@ -129,7 +132,9 @@ function subscribe() {
129132
});
130133
}
131134

132-
let counts = {};
135+
let counts = {
136+
test: 10,
137+
};
133138
function inc_count(path) {
134139
if(counts[path] === undefined) counts[path] = 0;
135140
counts[path]++;
@@ -142,18 +147,31 @@ function isValidationTask(task) {
142147

143148
function emit_counts() {
144149
//emit graphite metrics
150+
/*
145151
let out = "";
146152
for(let key in counts) {
147153
out += config.metrics.counts.prefix+"."+key+" "+counts[key]+" "+new Date().getTime()/1000+"\n";
148154
}
149155
fs.writeFileSync(config.metrics.counts.path, out);
156+
*/
157+
const writeApi = new influx.InfluxDB(config.influxdb.connection)
158+
.getWriteApi(config.influxdb.org, config.influxdb.bucket, 'ns')
159+
160+
writeApi.useDefaultTags({location: config.influxdb.location})
161+
const point = new influx.Point("warehouse.counts");
162+
point.timestamp(new Date());
163+
for(let key in counts) {
164+
point.intField(key, counts[key]);
165+
}
166+
writeApi.writePoint(point);
167+
writeApi.close();
150168

151169
counts = {}; //reset all counters
152170
}
153171

154172
const health_counts = {
155173
tasks: 0,
156-
instanceS: 0,
174+
instances: 0,
157175
}
158176
async function emit_health_counts() {
159177
var report = {
@@ -176,12 +194,25 @@ async function emit_health_counts() {
176194
await common.redisClient.set("health.warehouse.event."+process.env.HOSTNAME+"-"+process.pid, JSON.stringify(report));
177195

178196
//emit graphite metrics
197+
/*
179198
const time = new Date().getTime()/1000;
180199
let out = `
181200
${config.metrics.health_counts.prefix}.health.tasks ${health_counts.tasks} ${time}
182201
${config.metrics.health_counts.prefix}.health.instances ${health_counts.instances} ${time}
183202
`;
184203
fs.writeFileSync(config.metrics.health_counts.path, out);
204+
*/
205+
206+
const writeApi = new influx.InfluxDB(config.influxdb.connection)
207+
.getWriteApi(config.influxdb.org, config.influxdb.bucket, 'ns')
208+
writeApi.useDefaultTags({location: config.influxdb.location})
209+
210+
const point = new influx.Point("warehouse.health");
211+
point.intField('tasks', health_counts.tasks);
212+
point.intField('instances', health_counts.instances);
213+
point.timestamp(new Date());
214+
writeApi.writePoint(point);
215+
writeApi.close();
185216

186217
health_counts.tasks = 0;
187218
health_counts.instances = 0;

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"homepage": "https://github.com/brain-life/warehouse#readme",
2222
"dependencies": {
2323
"@cdxoo/dbscan": "^1.0.2",
24+
"@influxdata/influxdb-client": "^1.29.0",
2425
"amqp": "^0.2.7",
2526
"archiver": "^5.3.1",
2627
"async": "^3.2.3",

0 commit comments

Comments
 (0)