-
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 Fastify entry spans for Code Origin for Spans (#4449)
This commit does two things: - It lays the groundwork for an upcoming feature called "Code Origin for Spans". - To showcase this feature, it adds limited support for just Fastify entry-spans. To enable, set `DD_CODE_ORIGIN_FOR_SPANS_ENABLED=true`.
- Loading branch information
Showing
14 changed files
with
511 additions
and
37 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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,38 @@ | ||
'use strict' | ||
|
||
const { getUserLandFrames } = require('../dd-trace/src/plugins/util/stacktrace') | ||
|
||
const limit = Number(process.env._DD_CODE_ORIGIN_MAX_USER_FRAMES) || 8 | ||
|
||
module.exports = { | ||
entryTag, | ||
exitTag | ||
} | ||
|
||
function entryTag (topOfStackFunc) { | ||
return tag('entry', topOfStackFunc) | ||
} | ||
|
||
function exitTag (topOfStackFunc) { | ||
return tag('exit', topOfStackFunc) | ||
} | ||
|
||
function tag (type, topOfStackFunc) { | ||
const frames = getUserLandFrames(topOfStackFunc, limit) | ||
const tags = { | ||
'_dd.code_origin.type': type | ||
} | ||
for (let i = 0; i < frames.length; i++) { | ||
const frame = frames[i] | ||
tags[`_dd.code_origin.frames.${i}.file`] = frame.file | ||
tags[`_dd.code_origin.frames.${i}.line`] = String(frame.line) | ||
tags[`_dd.code_origin.frames.${i}.column`] = String(frame.column) | ||
if (frame.method) { | ||
tags[`_dd.code_origin.frames.${i}.method`] = frame.method | ||
} | ||
if (frame.type) { | ||
tags[`_dd.code_origin.frames.${i}.type`] = frame.type | ||
} | ||
} | ||
return tags | ||
} |
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,31 @@ | ||
'use strict' | ||
|
||
const { entryTag } = require('../../datadog-code-origin') | ||
const Plugin = require('../../dd-trace/src/plugins/plugin') | ||
const web = require('../../dd-trace/src/plugins/util/web') | ||
|
||
const kCodeOriginForSpansTagsSym = Symbol('datadog.codeOriginForSpansTags') | ||
|
||
class FastifyCodeOriginForSpansPlugin extends Plugin { | ||
static get id () { | ||
return 'fastify' | ||
} | ||
|
||
constructor (...args) { | ||
super(...args) | ||
|
||
this.addSub('apm:fastify:request:handle', ({ req, routeConfig }) => { | ||
const tags = routeConfig?.[kCodeOriginForSpansTagsSym] | ||
if (!tags) return | ||
const context = web.getContext(req) | ||
context.span?.addTags(tags) | ||
}) | ||
|
||
this.addSub('apm:fastify:route:added', ({ routeOptions, onRoute }) => { | ||
if (!routeOptions.config) routeOptions.config = {} | ||
routeOptions.config[kCodeOriginForSpansTagsSym] = entryTag(onRoute) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = FastifyCodeOriginForSpansPlugin |
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,19 @@ | ||
'use strict' | ||
|
||
const RouterPlugin = require('../../datadog-plugin-router/src') | ||
|
||
class FastifyTracingPlugin extends RouterPlugin { | ||
static get id () { | ||
return 'fastify' | ||
} | ||
|
||
constructor (...args) { | ||
super(...args) | ||
|
||
this.addSub('apm:fastify:request:handle', ({ req }) => { | ||
this.setFramework(req, 'fastify', this.config) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = FastifyTracingPlugin |
Oops, something went wrong.