Skip to content

Commit b573c10

Browse files
committed
fix: event emitter
1 parent ac429b6 commit b573c10

File tree

4 files changed

+59
-108
lines changed

4 files changed

+59
-108
lines changed

emitter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const EventEmitter = require('events')
2+
const emitter = new EventEmitter()
3+
4+
module.exports = emitter;

index.js

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,6 @@ const { FORMAT_HTTP_HEADERS, FORMAT_TEXT_MAP } = require("opentracing");
33
const { tagObject, isExcludedPath } = require("./helper");
44
const { getContext, setContext } = require("./context");
55
const {registration,signUpEvent} = require('./instruments')
6-
const EventEmitter = require('events')
7-
8-
global.tracer=null;
9-
global.emitter = new EventEmitter()
10-
11-
const {
12-
PerformanceObserver,
13-
performance
14-
} = require('perf_hooks');
15-
16-
const obs = new PerformanceObserver((items) => {
17-
if (global.tracer){
18-
const span = globalTracer.startSpan("performance_hooks")
19-
span.setTag("operation_name",items.getEntries()[0].name)
20-
span.log({
21-
originTime: performance.timeOrigin,
22-
data: items.getEntries()[0],
23-
message: "origin time in MS, duration in MS"
24-
})
25-
span.finish()
26-
27-
performance.clearMarks();
28-
}
29-
30-
});
31-
obs.observe({
32-
entryTypes: ['measure']
33-
});
346

357
const buildSpanName = req => {
368
const params = req.pathParams || req.params;
@@ -213,8 +185,6 @@ class JaegerMiddleware {
213185
"response.body": stringify(res.resBody)
214186
};
215187
span.log(info);
216-
217-
span.finish();
218188
} catch (error) {
219189
console.log(error);
220190
} finally {

instruments.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22
const log4js = require('log4js')
33
const helper = require('./helper')
4+
const emitter = require('./emitter')
45

56
log4js.levels = process.env.LOG_LEVEL || 'info'
67
const logger = log4js.getLogger('jaeger-client-nodejs-instruments')
@@ -20,7 +21,9 @@ const registration = ()=>{
2021

2122
const signUpEvent = span =>{
2223
for (const module of modules) {
23-
emitter.on(module,(db_type,operation_name,data)=>{
24+
emitter.on(module,(...agrs)=>{
25+
const eventData = agrs[0]
26+
const {db_type,operation_name,data}=eventData
2427
const child_span = global.tracer.startSpan(operation_name,{
2528
childOf: span
2629
})

instruments_modules/mongodb.js

Lines changed: 51 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
11
'use strict'
2-
const { stringify } = require("flatted/cjs");
3-
const opentracing = require("opentracing");
4-
const context = require('../context')
5-
const helper = require('../helper')
62

3+
const emitter = require('../emitter')
74
const requestIdMap = {}
85

9-
const onStart = (event)=>{
10-
const {
11-
databaseName,
12-
commandName,
13-
command,
14-
requestId
15-
}= event;
16-
const {
17-
filter={},
18-
projection={},
19-
limit,
20-
query={}
21-
} = command
22-
const collection = command[commandName]
23-
const operation_name = `${databaseName}.${collection}.${commandName}`
24-
// set operation name
25-
requestIdMap[requestId]=`${databaseName}.${collection}.${commandName}`
26-
emitter.emit('mongodb','mongodb',operation_name,{
27-
filter={},
28-
projection={},
29-
limit,
30-
query={}
31-
})
6+
const onStart = (event) => {
327
// {
338
// address: '10.60.3.8:27017',
349
// connectionId: 2,
@@ -42,68 +17,67 @@ const onStart = (event)=>{
4217
// '$db': 'user_profiler'
4318
// }
4419
// }
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,
43+
limit,
44+
query,
45+
}
46+
})
47+
}
4548
}
4649

47-
const onEnd = (event)=>{
50+
const onEnd = (event) => {
4851
const {
4952
requestId,
50-
reply={},
51-
duration=0
52-
}= event;
53+
reply = {},
54+
duration = 0
55+
} = event;
5356
const {
54-
cursor={},
55-
value={}
57+
cursor = {},
58+
value = {}
5659
} = reply
57-
const operation_name = requestIdMap[requestId]+`_response`
58-
const firstBatch = cursor?.firstBatch || []
59-
emitter.emit('mongodb','mongodb',operation_name,{
60-
command_duration_ms: duration,
61-
write_operation_response: value,
62-
read_operation_response: firstBatch
63-
})
64-
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+
}
6573
delete requestIdMap[requestId]
66-
67-
// {
68-
// address: '10.60.3.8:27017',
69-
// connectionId: 2,
70-
// requestId: 3,
71-
// commandName: 'find',
72-
// duration: 16,
73-
// reply: {
74-
// cursor: { firstBatch: [Array], id: 0, ns: 'user_profiler.reasons' },
75-
// ok: 1
76-
// }
77-
// }
78-
79-
// {
80-
// address: '10.60.3.8:27017',
81-
// connectionId: 1,
82-
// requestId: 6,
83-
// commandName: 'findAndModify',
84-
// duration: 11,
85-
// reply: {
86-
// lastErrorObject: { n: 1, updatedExisting: true },
87-
// value: {
88-
// _id: 60caf63a9d634c2eb2a8b2ed,
89-
// is_deleted: false,
90-
// name: 'abccc',
91-
// created_at: 2021-06-17T07:14:02.702Z,
92-
// updated_at: 2021-06-17T07:27:40.084Z,
93-
// __v: 0
94-
// },
95-
// ok: 1
96-
// }
97-
// }
98-
9974
}
10075

101-
const start = ()=>{
76+
const start = () => {
10277
const path = process.cwd()
103-
const listener = require(path+'/node_modules/mongodb').instrument()
78+
const listener = require(path + '/node_modules/mongodb').instrument()
10479
listener.on("started", onStart)
10580
listener.on("succeeded", onEnd)
106-
// listener.on("failed", onEnd)
10781
}
10882

10983
module.exports = {

0 commit comments

Comments
 (0)