-
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.
Add filesystem events to the timeline (#4965)
- Loading branch information
Showing
6 changed files
with
263 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const fs = require('fs') | ||
const os = require('os') | ||
const path = require('path') | ||
|
||
const tracer = require('dd-trace').init() | ||
tracer.profilerStarted().then(() => { | ||
tracer.trace('x', (_, done) => { | ||
setImmediate(() => { | ||
// Generate 1MB of random data | ||
const buffer = Buffer.alloc(1024 * 1024) | ||
for (let i = 0; i < buffer.length; i++) { | ||
buffer[i] = Math.floor(Math.random() * 256) | ||
} | ||
|
||
// Create a temporary file | ||
const tempFilePath = path.join(os.tmpdir(), 'tempfile.txt') | ||
|
||
fs.writeFile(tempFilePath, buffer, (err) => { | ||
if (err) throw err | ||
|
||
// Read the data back | ||
setImmediate(() => { | ||
fs.readFile(tempFilePath, (err, readData) => { | ||
setImmediate(() => { | ||
// Delete the temporary file | ||
fs.unlink(tempFilePath, (err) => { | ||
if (err) throw err | ||
}) | ||
done() | ||
}) | ||
if (err) throw err | ||
if (Buffer.compare(buffer, readData) !== 0) { | ||
throw new Error('Data read from file is different from data written to file') | ||
} | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
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
49 changes: 49 additions & 0 deletions
49
packages/dd-trace/src/profiling/profilers/event_plugins/fs.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const EventPlugin = require('./event') | ||
|
||
// Values taken from parameter names in datadog-instrumentations/src/fs.js. | ||
// Known param names that are disallowed because they can be strings and have arbitrary sizes: | ||
// 'data' | ||
// Known param names that are disallowed because they are never a string or number: | ||
// 'buffer', 'buffers', 'listener' | ||
const allowedParams = new Set([ | ||
'atime', 'dest', | ||
'existingPath', 'fd', 'file', | ||
'flag', 'gid', 'len', | ||
'length', 'mode', 'mtime', | ||
'newPath', 'offset', 'oldPath', | ||
'operation', 'options', 'path', | ||
'position', 'prefix', 'src', | ||
'target', 'type', 'uid' | ||
]) | ||
|
||
class FilesystemPlugin extends EventPlugin { | ||
static get id () { | ||
return 'fs' | ||
} | ||
|
||
static get operation () { | ||
return 'operation' | ||
} | ||
|
||
static get entryType () { | ||
return 'fs' | ||
} | ||
|
||
ignoreEvent (event) { | ||
// Don't care about sync events, they show up in the event loop samples anyway | ||
return event.operation?.endsWith('Sync') | ||
} | ||
|
||
extendEvent (event, detail) { | ||
const d = { ...detail } | ||
Object.entries(d).forEach(([k, v]) => { | ||
if (!(allowedParams.has(k) && (typeof v === 'string' || typeof v === 'number'))) { | ||
delete d[k] | ||
} | ||
}) | ||
event.detail = d | ||
|
||
return event | ||
} | ||
} | ||
module.exports = FilesystemPlugin |
Oops, something went wrong.