Skip to content

Commit 14c44cb

Browse files
committed
feat: add elastic apm and logstash components
1 parent 1dba538 commit 14c44cb

File tree

6 files changed

+65
-3
lines changed

6 files changed

+65
-3
lines changed

.env.example

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,16 @@ STORAGE_LOCATION=files
44
SYSTEM_SHUTDOWN_DELAY=0
55
MONGODB_URL='mongodb://localhost:27017/example?maxPoolSize=10&socketTimeoutMS=10000&connectTimeoutMS=10000&useUnifiedTopology=true'
66
AUTH_BASIC_USERNAME=
7-
AUTH_BASIC_PASSWORD=
7+
AUTH_BASIC_PASSWORD=
8+
9+
LOGSTASH_IS_ENABLE=false
10+
LOGSTASH_BASE_URL=
11+
LOGSTASH_PORT_URL=
12+
LOGSTASH_SSL_ENABLE=
13+
LOGSTASH_MAX_CONNECT_RETRIES=
14+
15+
ELASTIC_APM_IS_ENABLE=false
16+
ELASTIC_APM_SERVER_URL=
17+
ELASTIC_APM_SECRET_TOKEN=
18+
ELASTIC_APM_API_KEY=
19+
ELASTIC_APM_OPTIONS='{}'

bin/app/server.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const project = require('../../package.json');
22
const configs = require('../helpers/configs/global_config');
3+
const apm = require('../helpers/components/elastic-apm/apm');
34
const { GracefulShutdown, livenessProbe, readinessProbe } = require('../helpers/components/system/graceful_shutdown');
45
const observers = require('./observers');
56
const multer = require("multer");
@@ -37,7 +38,10 @@ class AppServer {
3738
async init() {
3839
// Initiation
3940
try {
40-
await Promise.all([observers.init()]);
41+
await Promise.all([
42+
observers.init(),
43+
apm.init()
44+
]);
4145

4246
const gs = new GracefulShutdown(configs.get('/system/shutdownDelay'));
4347
gs.enable(this.server);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const apm = require('elastic-apm-node');
2+
const project = require('../../../../package.json');
3+
const configs = require('../../../infra/configs/global_config');
4+
5+
const init = () => {
6+
if(configs.get('/elasticApm/isEnable')) {
7+
const options = {
8+
serviceName: project.name,
9+
serviceVersion: project.version,
10+
serverUrl: configs.get('/elasticApm/serverUrl'),
11+
secretToken: configs.get('/elasticApm/secretToken'),
12+
apiKey: configs.get('/elasticApm/apiKey'),
13+
};
14+
options = { ...options, ...configs.get('/elasticApm/options') };
15+
apm.start(options);
16+
}
17+
};
18+
19+
module.exports = {
20+
init
21+
};

bin/helpers/configs/global_config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ const config = {
1919
mongodb: {
2020
url: process.env.MONGODB_URL,
2121
},
22+
logstash: {
23+
isEnable: process.env.LOGSTASH_IS_ENABLE === 'true',
24+
host: process.env.LOGSTASH_BASE_URL,
25+
port: process.env.LOGSTASH_PORT_URL,
26+
ssl_enable: process.env.LOGSTASH_SSL_ENABLE === 'true',
27+
max_connect_retries: process.env.LOGSTASH_MAX_CONNECT_RETRIES,
28+
},
29+
elasticApm: {
30+
isEnable: process.env.ELASTIC_APM_IS_ENABLE === 'true',
31+
serverUrl: process.env.ELASTIC_APM_SERVER_URL,
32+
secretToken: process.env.ELASTIC_APM_SECRET_TOKEN,
33+
apiKey: process.env.ELASTIC_APM_API_KEY,
34+
options: (process.env.ELASTIC_APM_OPTIONS ? JSON.parse(process.env.ELASTIC_APM_OPTIONS) : {}),
35+
},
2236
};
2337

2438
const store = new confidence.Store(config);

bin/helpers/utils/logger.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const project = require('../../../package.json');
12
const winston = require('winston');
23

34
let transports = [
@@ -10,6 +11,14 @@ let transports = [
1011
}),
1112
];
1213

14+
if(config.get('/logstash/isEnable')) {
15+
const logstashConfig = config.get('/logstash');
16+
logstashConfig.node_name = project.name;
17+
transports.push(
18+
new winston.transports.Logstash(logstashConfig)
19+
);
20+
}
21+
1322
let logger = winston.createLogger({
1423
transports: transports,
1524
exitOnError: false

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"body-parser": "^1.20.2",
1616
"confidence": "^5.0.1",
1717
"dotenv": "^16.4.0",
18+
"elastic-apm-node": "^4.4.0",
1819
"express": "^4.18.2",
1920
"http-errors": "^2.0.0",
2021
"http-status-codes": "^2.3.0",
@@ -27,6 +28,7 @@
2728
"passport-http": "^0.3.0",
2829
"uuid": "^9.0.1",
2930
"validate.js": "^0.13.1",
30-
"winston": "^3.11.0"
31+
"winston": "^3.11.0",
32+
"winston-logstash": "^1.2.1"
3133
}
3234
}

0 commit comments

Comments
 (0)