Skip to content

Commit 1cce4a3

Browse files
committed
format
1 parent b573c10 commit 1cce4a3

File tree

8 files changed

+523
-486
lines changed

8 files changed

+523
-486
lines changed

.eslintrc

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
"SharedArrayBuffer": "readonly"
1111
},
1212
"parserOptions": {
13-
"ecmaVersion": 2018
13+
"ecmaVersion": 2020
1414
},
1515
"rules": {
1616
"semi": "off",
1717
"indent": ["warn", 2],
18-
"quotes": ["warn", "double"],
18+
"quotes": ["warn", "single"],
1919
"no-console": "warn",
20-
"space-before-function-paren": "off"
20+
"space-before-function-paren": "off",
21+
"camelcase":"off"
2122
}
2223
}

context.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const asyncHooks = require("async_hooks");
1+
const asyncHooks = require('async_hooks');
22
const contexts = {};
33
asyncHooks
44
.createHook({

helper.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
"use strict";
1+
'use strict';
22

33
const sensitive_keys = ['password', 'passwd', 'pwd', 'secret', 'token',
4-
'card', 'authorization','pw', 'pass']
4+
'card', 'authorization','pw', 'pass']
55

66
const tagPrimitive = (span, key, value) => {
77
span.setTag(key, value);
@@ -45,7 +45,7 @@ const isArray = input => {
4545

4646
const isExcludedPath = (method,requestPath, options) => {
4747
const { excludedPath = [] } = options;
48-
if (requestPath.includes("health") || requestPath.includes("public")) return true;
48+
if (requestPath.includes('health') || requestPath.includes('public')) return true;
4949
for (let index = 0; index < excludedPath.length; index++) {
5050
const path = excludedPath[index];
5151
if(requestPath.includes(path.url) && path.method.toLowerCase()===method.toLowerCase()) return true;

index.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { stringify } = require("flatted/cjs");
2-
const { FORMAT_HTTP_HEADERS, FORMAT_TEXT_MAP } = require("opentracing");
3-
const { tagObject, isExcludedPath } = require("./helper");
4-
const { getContext, setContext } = require("./context");
1+
const { stringify } = require('flatted/cjs');
2+
const { FORMAT_HTTP_HEADERS, FORMAT_TEXT_MAP } = require('opentracing');
3+
const { tagObject, isExcludedPath } = require('./helper');
4+
const { getContext, setContext } = require('./context');
55
const {registration,signUpEvent} = require('./instruments')
66

77
const buildSpanName = req => {
@@ -20,10 +20,10 @@ const buildSpanName = req => {
2020
return baseSpanName;
2121
};
2222

23-
process.on("unhandledRejection", error => {
24-
const errorSpan = globalTracer.startSpan("unhandledRejection");
25-
errorSpan.setTag("error", true);
26-
errorSpan.setTag("http.status_code", error.statusCode || 500);
23+
process.on('unhandledRejection', error => {
24+
const errorSpan = global.tracer.startSpan('unhandledRejection');
25+
errorSpan.setTag('error', true);
26+
errorSpan.setTag('http.status_code', error.statusCode || 500);
2727
errorSpan.log({
2828
error: JSON.stringify(error)
2929
});
@@ -61,8 +61,8 @@ class JaegerMiddleware {
6161
// let spanName = buildSpanName(req);
6262
const span = parentSpan
6363
? this._jaeger.startSpan(`${req.originalUrl}`, {
64-
childOf: parentSpan
65-
})
64+
childOf: parentSpan
65+
})
6666
: this._jaeger.startSpan(`${req.originalUrl}`);
6767
req.span = span;
6868
setContext(span);
@@ -98,16 +98,16 @@ class JaegerMiddleware {
9898
const parentSpan = this._jaeger.extract(FORMAT_TEXT_MAP, headers);
9999
const span = parentSpan
100100
? this._jaeger.startSpan(spanName, {
101-
childOf: parentSpan
102-
})
101+
childOf: parentSpan
102+
})
103103
: this._jaeger.startSpan(spanName);
104104
const spanContext = span;
105105
tagObject(span,data)
106106
setContext(spanContext);
107107
return span;
108108
}
109109

110-
spawnNewSpanWithData(spanName = "children-span",data={}) {
110+
spawnNewSpanWithData(spanName = 'children-span',data={}) {
111111
const context = getContext()
112112
const newSpan = !isEmpty(context) ? this._jaeger.startSpan(spanName, {
113113
childOf: context
@@ -167,7 +167,7 @@ class JaegerMiddleware {
167167
try {
168168
const span = req.span
169169
if (res.statusCode !== 200) {
170-
span.setTag("error", true);
170+
span.setTag('error', true);
171171
}
172172
tagObject(span, req.query);
173173
tagObject(span, req.params);
@@ -182,7 +182,7 @@ class JaegerMiddleware {
182182
const info = {
183183
timestamp: Date.now(),
184184
request: JSON.stringify(requestInfo),
185-
"response.body": stringify(res.resBody)
185+
'response.body': stringify(res.resBody)
186186
};
187187
span.log(info);
188188
} catch (error) {

instruments.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@ const emitter = require('./emitter')
55

66
log4js.levels = process.env.LOG_LEVEL || 'info'
77
const logger = log4js.getLogger('jaeger-client-nodejs-instruments')
8-
const modules = ["mongodb"]
8+
const modules = ['mongodb']
99

1010

1111

1212
const registration = ()=>{
13-
for (const module of modules) {
14-
try {
15-
require(`./instruments_modules/${module}`).start()
16-
} catch (error) {
17-
logger.info(`db module ${module} not found`)
18-
}
13+
for (const module of modules) {
14+
try {
15+
require(`./instruments_modules/${module}`).start()
16+
} catch (error) {
17+
logger.info(`db module ${module} not found`)
1918
}
19+
}
2020
}
2121

2222
const signUpEvent = span =>{
23-
for (const module of modules) {
24-
emitter.on(module,(...agrs)=>{
25-
const eventData = agrs[0]
26-
const {db_type,operation_name,data}=eventData
27-
const child_span = global.tracer.startSpan(operation_name,{
28-
childOf: span
29-
})
30-
child_span.setTag("db_type",db_type)
31-
helper.tagObject(child_span,data)
32-
child_span.log(data)
33-
child_span.finish()
34-
})
35-
}
23+
for (const module of modules) {
24+
emitter.on(module,(...agrs)=>{
25+
const eventData = agrs[0]
26+
const {db_type,operation_name,data}=eventData
27+
const child_span = global.tracer.startSpan(operation_name,{
28+
childOf: span
29+
})
30+
child_span.setTag('db_type',db_type)
31+
helper.tagObject(child_span,data)
32+
child_span.log(data)
33+
child_span.finish()
34+
})
35+
}
3636
}
3737

3838
module.exports = {
39-
registration,
40-
signUpEvent
39+
registration,
40+
signUpEvent
4141
}

instruments_modules/mongodb.js

+68-68
Original file line numberDiff line numberDiff line change
@@ -4,82 +4,82 @@ const emitter = require('../emitter')
44
const requestIdMap = {}
55

66
const onStart = (event) => {
7-
// {
8-
// address: '10.60.3.8:27017',
9-
// connectionId: 2,
10-
// requestId: 5,
11-
// databaseName: 'user_profiler',
12-
// commandName: 'createIndexes',
13-
// command: {
14-
// createIndexes: 'reasons',
15-
// indexes: [ [Object] ],
16-
// lsid: { id: [Binary] },
17-
// '$db': 'user_profiler'
18-
// }
19-
// }
20-
const {
21-
databaseName,
22-
commandName,
23-
command,
24-
requestId
25-
} = event;
26-
const {
27-
filter = {},
28-
projection = {},
7+
// {
8+
// address: '10.60.3.8:27017',
9+
// connectionId: 2,
10+
// requestId: 5,
11+
// databaseName: 'user_profiler',
12+
// commandName: 'createIndexes',
13+
// command: {
14+
// createIndexes: 'reasons',
15+
// indexes: [ [Object] ],
16+
// lsid: { id: [Binary] },
17+
// '$db': 'user_profiler'
18+
// }
19+
// }
20+
const {
21+
databaseName,
22+
commandName,
23+
command,
24+
requestId
25+
} = event;
26+
const {
27+
filter = {},
28+
projection = {},
29+
limit,
30+
query = {}
31+
} = command
32+
const collection = command[commandName]
33+
const operation_name = `${databaseName}.${collection}.${commandName}`
34+
// set operation name
35+
requestIdMap[requestId] = `${databaseName}.${collection}.${commandName}`
36+
if (!operation_name.includes('Index')) {
37+
emitter.emit('mongodb', {
38+
db_type: 'mongodb',
39+
operation_name,
40+
data: {
41+
filter,
42+
projection,
2943
limit,
30-
query = {}
31-
} = command
32-
const collection = command[commandName]
33-
const operation_name = `${databaseName}.${collection}.${commandName}`
34-
// set operation name
35-
requestIdMap[requestId] = `${databaseName}.${collection}.${commandName}`
36-
if (!operation_name.includes('Index')) {
37-
emitter.emit('mongodb', {
38-
db_type: 'mongodb',
39-
operation_name,
40-
data: {
41-
filter,
42-
projection,
43-
limit,
44-
query,
45-
}
46-
})
47-
}
44+
query,
45+
}
46+
})
47+
}
4848
}
4949

5050
const onEnd = (event) => {
51-
const {
52-
requestId,
53-
reply = {},
54-
duration = 0
55-
} = event;
56-
const {
57-
cursor = {},
58-
value = {}
59-
} = reply
60-
const operation_name = requestIdMap[requestId] + `_response`
61-
const firstBatch = cursor ?.firstBatch || []
62-
if (!operation_name.includes('Index')) {
63-
emitter.emit('mongodb', {
64-
db_type: 'mongodb',
65-
operation_name,
66-
data: {
67-
command_duration_ms: duration,
68-
write_operation_response: value,
69-
read_operation_response: firstBatch
70-
}
71-
})
72-
}
73-
delete requestIdMap[requestId]
51+
const {
52+
requestId,
53+
reply = {},
54+
duration = 0
55+
} = event;
56+
const {
57+
cursor = {},
58+
value = {}
59+
} = reply
60+
const operation_name = requestIdMap[requestId] + '_response'
61+
const firstBatch = cursor ?.firstBatch || []
62+
if (!operation_name.includes('Index')) {
63+
emitter.emit('mongodb', {
64+
db_type: 'mongodb',
65+
operation_name,
66+
data: {
67+
command_duration_ms: duration,
68+
write_operation_response: value,
69+
read_operation_response: firstBatch
70+
}
71+
})
72+
}
73+
delete requestIdMap[requestId]
7474
}
7575

7676
const start = () => {
77-
const path = process.cwd()
78-
const listener = require(path + '/node_modules/mongodb').instrument()
79-
listener.on("started", onStart)
80-
listener.on("succeeded", onEnd)
77+
const path = process.cwd()
78+
const listener = require(path + '/node_modules/mongodb').instrument()
79+
listener.on('started', onStart)
80+
listener.on('succeeded', onEnd)
8181
}
8282

8383
module.exports = {
84-
start
84+
start
8585
}

0 commit comments

Comments
 (0)