forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.js
76 lines (64 loc) · 2.93 KB
/
redis.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
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const semver = require('semver')
const shimmer = require('shimmer')
const ServiceTypeCode = require('../../constant/service-type').ServiceTypeCode
const log = require('../../utils/logger')
const IdGenerator = require('../../context/id-generator')
const { addressStringOf } = require('../../utils/convert-utils')
module.exports = function (agent, version, redis) {
if (!semver.satisfies(version, '>2.5.3')) {
log.debug('redis version %s not supported - aborting...', version)
return redis
}
const proto = redis.RedisClient && redis.RedisClient.prototype
log.debug('shimming redis.RedisClient.prototype.internal_send_command')
shimmer.wrap(proto, 'internal_send_command', wrapInternalSendCommand)
return redis
function makeWrappedCallback (cb, command, asyncTrace, asyncEventRecorder) {
return agent.spanEndCallbackWrapper(asyncTrace, asyncEventRecorder, function wrappedCallback () {
log.debug(`wrappedCallback command: ${command}`)
if (cb) {
const result = cb.apply(this, arguments)
if (asyncTrace) asyncTrace.traceAsyncEnd(asyncEventRecorder)
return result
}
})
}
// >2.5.3
function wrapInternalSendCommand (original) {
return function wrappedInternalSendCommand (commandObj) {
const trace = agent.traceContext.currentTraceObject()
const isPinpointCallbackWrapper = commandObj &&
typeof commandObj.callback === 'function' &&
commandObj.callback.name === "pinpointCallbackWrapper"
if (!isPinpointCallbackWrapper && trace) {
const command = commandObj && commandObj.command
const spanEventRecorder = trace.traceBlockBegin()
spanEventRecorder.recordServiceType(ServiceTypeCode.redis)
spanEventRecorder.recordApiDesc('redis.'+ String(command).toLowerCase() + '.call')
spanEventRecorder.recordDestinationId('Redis')
if (this.connection_options) {
spanEventRecorder.recordEndPoint(addressStringOf(this.connection_options.host, this.connection_options.port))
}
trace.traceBlockEnd(spanEventRecorder)
const asyncTrace = trace.newAsyncTrace(spanEventRecorder)
const nextSpanId = IdGenerator.next
const asyncEventRecorder = asyncTrace.traceAsyncBegin()
asyncEventRecorder.recordServiceType(ServiceTypeCode.redis)
asyncEventRecorder.recordApiDesc('redis.'+ String(command).toLowerCase() + '.end')
asyncEventRecorder.recordNextSpanId(nextSpanId)
asyncEventRecorder.recordDestinationId('Redis')
if (this.connection_options) {
asyncEventRecorder.recordEndPoint(addressStringOf(this.connection_options.host, this.connection_options.port))
}
commandObj.callback = makeWrappedCallback(commandObj.callback, command, asyncTrace, asyncEventRecorder)
}
return original.apply(this, arguments)
}
}
}