-
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 support for exit spans in Code Origin for Spans (#4772)
- Loading branch information
Showing
10 changed files
with
194 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict' | ||
|
||
const digitRegex = /^\d+$/ | ||
|
||
/** | ||
* Converts a flat object of tags into a nested object. For example: | ||
* { 'a.b.c': 'value' } -> { a: { b: { c: 'value' } } } | ||
* Also supports array-keys. For example: | ||
* { 'a.0.b': 'value' } -> { a: [{ b: 'value' }] } | ||
* | ||
* @param {Object} tags - Key/value pairs of tags | ||
* @returns Object - Parsed tags | ||
*/ | ||
module.exports = tags => { | ||
const parsedTags = {} | ||
for (const [tag, value] of Object.entries(tags)) { | ||
const keys = tag.split('.') | ||
let current = parsedTags | ||
let depth = 0 | ||
for (const key of keys) { | ||
if (!current[key]) { | ||
if (depth === keys.length - 1) { | ||
current[key] = value | ||
break | ||
} | ||
current[key] = keys[depth + 1]?.match(digitRegex) ? [] : {} | ||
} | ||
current = current[key] | ||
depth++ | ||
} | ||
} | ||
return parsedTags | ||
} |
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,23 @@ | ||
'use strict' | ||
|
||
require('../../../../dd-trace/test/setup/tap') | ||
|
||
const parseTags = require('../../../src/utils/src/parse-tags') | ||
|
||
describe('parseTags', () => { | ||
it('should parse tags to object', () => { | ||
const obj = { | ||
'a.0.a': 'foo', | ||
'a.0.b': 'bar', | ||
'a.1.a': 'baz' | ||
} | ||
|
||
expect(parseTags(obj)).to.deep.equal({ | ||
a: [{ a: 'foo', b: 'bar' }, { a: 'baz' }] | ||
}) | ||
}) | ||
|
||
it('should work with empty object', () => { | ||
expect(parseTags({})).to.deep.equal({}) | ||
}) | ||
}) |
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,63 @@ | ||
'use strict' | ||
|
||
const agent = require('../../dd-trace/test/plugins/agent') | ||
|
||
describe('Plugin', () => { | ||
describe('http', () => { | ||
describe('Code Origin for Spans', () => { | ||
before(() => { | ||
// Needed when this spec file run together with other spec files, in which case the agent config is not | ||
// re-loaded unless the existing agent is wiped first. And we need the agent config to be re-loaded in order to | ||
// enable Code Origin for Spans. | ||
agent.wipe() | ||
}) | ||
|
||
beforeEach(async () => { | ||
return agent.load('http', { server: false }, { codeOriginForSpans: { enabled: true } }) | ||
}) | ||
|
||
afterEach(() => { | ||
return agent.close({ ritmReset: false }) | ||
}) | ||
|
||
it('should add code_origin tags for outbound requests', done => { | ||
server((port) => { | ||
const http = require('http') | ||
|
||
agent | ||
.use(traces => { | ||
const span = traces[0][0] | ||
expect(span.meta).to.have.property('_dd.code_origin.type', 'exit') | ||
|
||
// Just validate that frame 0 tags are present. The detailed validation is performed in a different test. | ||
expect(span.meta).to.have.property('_dd.code_origin.frames.0.file') | ||
expect(span.meta).to.have.property('_dd.code_origin.frames.0.line') | ||
expect(span.meta).to.have.property('_dd.code_origin.frames.0.column') | ||
expect(span.meta).to.have.property('_dd.code_origin.frames.0.method') | ||
expect(span.meta).to.have.property('_dd.code_origin.frames.0.type') | ||
}) | ||
.then(done) | ||
.catch(done) | ||
|
||
const req = http.request(`http://localhost:${port}/`, res => { | ||
res.resume() | ||
}) | ||
|
||
req.end() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
function server (callback) { | ||
const http = require('http') | ||
|
||
const server = http.createServer((req, res) => { | ||
res.end() | ||
}) | ||
|
||
server.listen(() => { | ||
callback(server.address().port) | ||
}) | ||
} |
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