forked from CloudNativeJS/appmetrics-zipkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappmetrics-zipkin.js
executable file
·139 lines (125 loc) · 3.74 KB
/
appmetrics-zipkin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*******************************************************************************
* Copyright 2017 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
'use strict';
var path = require('path');
var aspect = require('./lib/aspect.js');
var fs = require('fs');
var PropertyReader = require('properties-reader');
var properties = PropertyReader(__dirname + '/appmetrics-zipkin.properties');
var tcpp = require('tcp-ping');
const {
BatchRecorder
} = require('zipkin');
const {
HttpLogger
} = require('zipkin-transport-http');
// Load module probes into probes array by searching the probes directory.
var probes = [];
var dirPath = path.join(__dirname, 'probes');
var files = fs.readdirSync(dirPath);
var processName = '';
module.exports = function(options) {
options = options;
processName = path.basename(process.argv[1]);
if (processName.includes('.js')) {
processName = processName.substring(0, processName.length - 3);
}
files.forEach(function(fileName) {
var file = path.join(dirPath, fileName);
var probeModule = new (require(file))();
probes.push(probeModule);
});
start(options);
};
function start(options) {
// Set up the zipkin
var host, port, serviceName, sampleRate;
if (options) {
host = options['host'];
port = options['port'];
serviceName = options['serviceName'];
sampleRate = options['sampleRate'];
}
// Uses properties from file if present
if (properties){
if (properties.get('host')) {
host = properties.get('host');
}
if (properties.get('port')) {
port = properties.get('port');
}
if (properties.get('serviceName')) {
serviceName = properties.get('serviceName');
}
if (properties.get('sampleRate')) {
sampleRate = properties.get('sampleRate');
}
}
if (!serviceName) {
serviceName = processName;
}
if (!host) {
host = 'localhost';
}
if (!port) {
port = 9411;
}
if (!sampleRate) {
sampleRate = 1.0;
}
// Test if the host & port are valid
tcpp.probe(host, port, function(err, available) {
if (err) {
console.log('Unable to contact Zipkin at ' + host + ':' + port);
return;
}
if (!available) {
console.log('Unable to contact Zipkin at ' + host + ':' + port);
}
});
const zipkinUrl = `http://${host}:${port}`;
const recorder = new BatchRecorder({
logger: new HttpLogger({
endpoint: `${zipkinUrl}/api/v1/spans`
})
});
// Configure and start the probes
probes.forEach(function(probe) {
probe.setConfig(options);
probe.setRecorder(recorder);
probe.setServiceName(serviceName);
probe.start();
// probe.enableRequests();
});
}
/*
* Patch the module require function to run the probe attach function
* for any matching module. This loads the monitoring probes into the modules
*/
var data = {};
/* eslint no-proto:0 */
aspect.after(module.__proto__, 'require', data, function(obj, methodName, args, context, ret) {
if (ret == null || ret.__ddProbeAttached__) {
return ret;
} else {
for (var i = 0; i < probes.length; i++) {
if (probes[i].name === args[0]) {
ret = probes[i].attach(args[0], ret, module.exports);
}
}
return ret;
}
});