-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tracing): implement protobufjs DSM schema support (#4701)
* add protobufjs schemas support for DSM
- Loading branch information
Showing
20 changed files
with
1,147 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ declare -a plugins=( | |
"pg" | ||
"promise" | ||
"promise_js" | ||
"protobufjs" | ||
"q" | ||
"redis" | ||
"restify" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
const shimmer = require('../../datadog-shimmer') | ||
const { addHook } = require('./helpers/instrument') | ||
|
||
const dc = require('dc-polyfill') | ||
const serializeChannel = dc.channel('apm:protobufjs:serialize-start') | ||
const deserializeChannel = dc.channel('apm:protobufjs:deserialize-end') | ||
|
||
function wrapSerialization (messageClass) { | ||
if (messageClass?.encode) { | ||
shimmer.wrap(messageClass, 'encode', original => function () { | ||
if (!serializeChannel.hasSubscribers) { | ||
return original.apply(this, arguments) | ||
} | ||
serializeChannel.publish({ messageClass: this }) | ||
return original.apply(this, arguments) | ||
}) | ||
} | ||
} | ||
|
||
function wrapDeserialization (messageClass) { | ||
if (messageClass?.decode) { | ||
shimmer.wrap(messageClass, 'decode', original => function () { | ||
if (!deserializeChannel.hasSubscribers) { | ||
return original.apply(this, arguments) | ||
} | ||
const result = original.apply(this, arguments) | ||
deserializeChannel.publish({ messageClass: result }) | ||
return result | ||
}) | ||
} | ||
} | ||
|
||
function wrapSetup (messageClass) { | ||
if (messageClass?.setup) { | ||
shimmer.wrap(messageClass, 'setup', original => function () { | ||
const result = original.apply(this, arguments) | ||
|
||
wrapSerialization(messageClass) | ||
wrapDeserialization(messageClass) | ||
|
||
return result | ||
}) | ||
} | ||
} | ||
|
||
function wrapProtobufClasses (root) { | ||
if (!root) { | ||
return | ||
} | ||
|
||
if (root.decode) { | ||
wrapSetup(root) | ||
} | ||
|
||
if (root.nestedArray) { | ||
for (const subRoot of root.nestedArray) { | ||
wrapProtobufClasses(subRoot) | ||
} | ||
} | ||
} | ||
|
||
function wrapReflection (protobuf) { | ||
const reflectionMethods = [ | ||
{ | ||
target: protobuf.Root, | ||
name: 'fromJSON' | ||
}, | ||
{ | ||
target: protobuf.Type.prototype, | ||
name: 'fromObject' | ||
} | ||
] | ||
|
||
reflectionMethods.forEach(method => { | ||
shimmer.wrap(method.target, method.name, original => function () { | ||
const result = original.apply(this, arguments) | ||
if (result.nested) { | ||
for (const type in result.nested) { | ||
wrapSetup(result.nested[type]) | ||
} | ||
} | ||
if (result.$type) { | ||
wrapSetup(result.$type) | ||
} | ||
return result | ||
}) | ||
}) | ||
} | ||
|
||
function isPromise (obj) { | ||
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function' | ||
} | ||
|
||
addHook({ | ||
name: 'protobufjs', | ||
versions: ['>=6.8.0'] | ||
}, protobuf => { | ||
shimmer.wrap(protobuf.Root.prototype, 'load', original => function () { | ||
const result = original.apply(this, arguments) | ||
if (isPromise(result)) { | ||
return result.then(root => { | ||
wrapProtobufClasses(root) | ||
return root | ||
}) | ||
} else { | ||
// If result is not a promise, directly wrap the protobuf classes | ||
wrapProtobufClasses(result) | ||
return result | ||
} | ||
}) | ||
|
||
shimmer.wrap(protobuf.Root.prototype, 'loadSync', original => function () { | ||
const root = original.apply(this, arguments) | ||
wrapProtobufClasses(root) | ||
return root | ||
}) | ||
|
||
shimmer.wrap(protobuf, 'Type', Original => function () { | ||
const typeInstance = new Original(...arguments) | ||
wrapSetup(typeInstance) | ||
return typeInstance | ||
}) | ||
|
||
wrapReflection(protobuf) | ||
|
||
return protobuf | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const SchemaPlugin = require('../../dd-trace/src/plugins/schema') | ||
const SchemaExtractor = require('./schema_iterator') | ||
|
||
class ProtobufjsPlugin extends SchemaPlugin { | ||
static get id () { | ||
return 'protobufjs' | ||
} | ||
|
||
static get schemaExtractor () { | ||
return SchemaExtractor | ||
} | ||
} | ||
|
||
module.exports = ProtobufjsPlugin |
Oops, something went wrong.