-
Notifications
You must be signed in to change notification settings - Fork 318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lazy load dsm only when needed #5305
Open
rochdev
wants to merge
3
commits into
master
Choose a base branch
from
lazy-load-dsm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
2 changes: 1 addition & 1 deletion
2
packages/dd-trace/src/data_streams.js → .../dd-trace/src/datastreams/checkpointer.js
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
4 changes: 2 additions & 2 deletions
4
...ages/dd-trace/src/data_streams_context.js → packages/dd-trace/src/datastreams/context.js
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,104 @@ | ||
'use strict' | ||
|
||
const { | ||
getAmqpMessageSize, | ||
getHeadersSize, | ||
getMessageSize, | ||
getSizeOrZero | ||
} = require('./size') | ||
|
||
// This is only needed because DSM code is spread across existing tracing | ||
// plugins instead of having dedicated DSM plugins that are themselves | ||
// lazy loaded. | ||
// | ||
// TODO: Remove this when DSM has been moved to dedicaed plugins. | ||
function lazyClass (classGetter, methods = [], staticMethods = []) { | ||
let constructorArgs | ||
let ActiveClass | ||
|
||
const LazyClass = function (...args) { | ||
constructorArgs = args | ||
} | ||
|
||
const activate = () => { | ||
return (ActiveClass = ActiveClass || classGetter()) | ||
} | ||
|
||
for (const method of methods) { | ||
LazyClass.prototype[method] = function (...args) { | ||
const instance = activate() && new ActiveClass(...constructorArgs) | ||
|
||
// Replace the whole prototype instead of only the method itself whenever | ||
// any individual method is called to avoid running through this code | ||
// again every time another method is called. This is not only more | ||
// efficient but it also means that the class instance does not need to be | ||
// stored for future calls to other methods. | ||
Object.setPrototypeOf(this, instance) | ||
|
||
return this[method](...args) | ||
} | ||
} | ||
|
||
for (const method of staticMethods) { | ||
LazyClass[method] = function (...args) { | ||
LazyClass[method] = activate() && ActiveClass[method] | ||
|
||
return LazyClass[method](...args) | ||
} | ||
} | ||
|
||
return LazyClass | ||
} | ||
|
||
const DsmPathwayCodec = lazyClass(() => require('./pathway').DsmPathwayCodec, [], [ | ||
'encode', | ||
'decode' | ||
]) | ||
|
||
const DataStreamsCheckpointer = lazyClass(() => require('./checkpointer').DataStreamsCheckpointer, [ | ||
'setProduceCheckpoint', | ||
'setConsumeCheckpoint' | ||
]) | ||
|
||
const DataStreamsManager = lazyClass(() => require('./manager').DataStreamsManager, [ | ||
'setCheckpoint', | ||
'decodeDataStreamsContext' | ||
]) | ||
|
||
// TODO: Are all those methods actually public? | ||
const DataStreamsProcessor = lazyClass(() => require('./processor').DataStreamsProcessor, [ | ||
'onInterval', | ||
'bucketFromTimestamp', | ||
'recordCheckpoint', | ||
'setCheckpoint', | ||
'recordOffset', | ||
'setOffset', | ||
'setUrl', | ||
'trySampleSchema', | ||
'canSampleSchema', | ||
'getSchema' | ||
]) | ||
|
||
const SchemaBuilder = lazyClass(() => require('./schemas/schema_builder').SchemaBuilder, [ | ||
'build', | ||
'addProperty', | ||
'shouldExtractSchema' | ||
], [ | ||
'getCache', | ||
'getSchemaDefinition', | ||
'getSchema' | ||
]) | ||
|
||
module.exports = { | ||
DsmPathwayCodec, | ||
DataStreamsCheckpointer, | ||
DataStreamsManager, | ||
DataStreamsProcessor, | ||
SchemaBuilder, | ||
|
||
// These are small functions so they are exposed directly and not lazy loaded. | ||
getAmqpMessageSize, | ||
getHeadersSize, | ||
getMessageSize, | ||
getSizeOrZero | ||
} |
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,27 @@ | ||
'use strict' | ||
|
||
const { DsmPathwayCodec } = require('./pathway') | ||
const DataStreamsContext = require('./context') | ||
|
||
class DataStreamsManager { | ||
constructor (processor) { | ||
this._dataStreamsProcessor = processor | ||
} | ||
|
||
setCheckpoint (edgeTags, span, payloadSize = 0) { | ||
const ctx = this._dataStreamsProcessor.setCheckpoint( | ||
edgeTags, span, DataStreamsContext.getDataStreamsContext(), payloadSize | ||
) | ||
DataStreamsContext.setDataStreamsContext(ctx) | ||
return ctx | ||
} | ||
|
||
decodeDataStreamsContext (carrier) { | ||
const ctx = DsmPathwayCodec.decode(carrier) | ||
// we erase the previous context everytime we decode a new one | ||
DataStreamsContext.setDataStreamsContext(ctx) | ||
return ctx | ||
} | ||
} | ||
|
||
module.exports = { DataStreamsManager } |
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,53 @@ | ||
'use strict' | ||
|
||
const { types } = require('util') | ||
|
||
function getSizeOrZero (obj) { | ||
if (typeof obj === 'string') { | ||
return Buffer.from(obj, 'utf-8').length | ||
} | ||
if (types.isArrayBuffer(obj)) { | ||
return obj.byteLength | ||
} | ||
if (Buffer.isBuffer(obj)) { | ||
return obj.length | ||
} | ||
if (Array.isArray(obj) && obj.length > 0) { | ||
if (typeof obj[0] === 'number') return Buffer.from(obj).length | ||
let payloadSize = 0 | ||
obj.forEach(item => { | ||
payloadSize += getSizeOrZero(item) | ||
}) | ||
return payloadSize | ||
} | ||
if (obj !== null && typeof obj === 'object') { | ||
try { | ||
return getHeadersSize(obj) | ||
} catch { | ||
// pass | ||
} | ||
} | ||
return 0 | ||
} | ||
|
||
function getHeadersSize (headers) { | ||
if (headers === undefined) return 0 | ||
return Object.entries(headers).reduce((prev, [key, val]) => getSizeOrZero(key) + getSizeOrZero(val) + prev, 0) | ||
} | ||
|
||
function getMessageSize (message) { | ||
const { key, value, headers } = message | ||
return getSizeOrZero(key) + getSizeOrZero(value) + getHeadersSize(headers) | ||
} | ||
|
||
function getAmqpMessageSize (message) { | ||
const { headers, content } = message | ||
return getSizeOrZero(content) + getHeadersSize(headers) | ||
} | ||
|
||
module.exports = { | ||
getMessageSize, | ||
getHeadersSize, | ||
getSizeOrZero, | ||
getAmqpMessageSize | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The helpers in this file were copied from
processor.js
untouched.