Skip to content

Commit

Permalink
Merge branch 'master' into watson/DEBUG-3228/batch-requests
Browse files Browse the repository at this point in the history
  • Loading branch information
watson authored Jan 7, 2025
2 parents 39ba330 + 1e76223 commit ccdbe34
Show file tree
Hide file tree
Showing 13 changed files with 499 additions and 228 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ jobs:
range: '>=5.12.1'
aerospike-image: ce-6.4.0.3
test-image: ubuntu-latest
- node-version: 22
range: '>=6.0.0'
aerospike-image: ce-6.4.0.3
test-image: ubuntu-latest
runs-on: ${{ matrix.test-image }}
services:
aerospike:
Expand Down
30 changes: 15 additions & 15 deletions integration-tests/debugger/snapshot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ describe('Dynamic Instrumentation', function () {
assert.deepEqual(Object.keys(captures.lines), [String(t.breakpoint.line)])

const { locals } = captures.lines[t.breakpoint.line]
const { request, fastify, getSomeData } = locals
const { request, fastify, getUndefined } = locals
delete locals.request
delete locals.fastify
delete locals.getSomeData
delete locals.getUndefined

// from block scope
assert.deepEqual(locals, {
Expand Down Expand Up @@ -67,19 +67,19 @@ describe('Dynamic Instrumentation', function () {
}
},
emptyObj: { type: 'Object', fields: {} },
fn: {
type: 'Function',
fields: {
length: { type: 'number', value: '0' },
name: { type: 'string', value: 'fn' }
}
},
p: {
type: 'Promise',
fields: {
'[[PromiseState]]': { type: 'string', value: 'fulfilled' },
'[[PromiseResult]]': { type: 'undefined' }
}
},
arrowFn: {
type: 'Function',
fields: {
length: { type: 'number', value: '0' },
name: { type: 'string', value: 'arrowFn' }
}
}
})

Expand All @@ -99,11 +99,11 @@ describe('Dynamic Instrumentation', function () {
assert.equal(fastify.type, 'Object')
assert.typeOf(fastify.fields, 'Object')

assert.deepEqual(getSomeData, {
assert.deepEqual(getUndefined, {
type: 'Function',
fields: {
length: { type: 'number', value: '0' },
name: { type: 'string', value: 'getSomeData' }
name: { type: 'string', value: 'getUndefined' }
}
})

Expand All @@ -118,7 +118,7 @@ describe('Dynamic Instrumentation', function () {
const { locals } = captures.lines[t.breakpoint.line]
delete locals.request
delete locals.fastify
delete locals.getSomeData
delete locals.getUndefined

assert.deepEqual(locals, {
nil: { type: 'null', isNull: true },
Expand All @@ -139,8 +139,8 @@ describe('Dynamic Instrumentation', function () {
arr: { type: 'Array', notCapturedReason: 'depth' },
obj: { type: 'Object', notCapturedReason: 'depth' },
emptyObj: { type: 'Object', notCapturedReason: 'depth' },
fn: { type: 'Function', notCapturedReason: 'depth' },
p: { type: 'Promise', notCapturedReason: 'depth' }
p: { type: 'Promise', notCapturedReason: 'depth' },
arrowFn: { type: 'Function', notCapturedReason: 'depth' }
})

done()
Expand Down Expand Up @@ -212,7 +212,7 @@ describe('Dynamic Instrumentation', function () {
// Up to 3 properties from the local scope
'request', 'nil', 'undef',
// Up to 3 properties from the closure scope
'fastify', 'getSomeData'
'fastify', 'getUndefined'
])

assert.strictEqual(locals.request.type, 'Request')
Expand Down
59 changes: 27 additions & 32 deletions integration-tests/debugger/target-app/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,33 @@ const Fastify = require('fastify')

const fastify = Fastify()

// Since line probes have hardcoded line numbers, we want to try and keep the line numbers from changing within the
// `handler` function below when making changes to this file. This is achieved by calling `getSomeData` and keeping all
// variable names on the same line as much as possible.
fastify.get('/:name', function handler (request) {
// eslint-disable-next-line no-unused-vars
const { nil, undef, bool, num, bigint, str, lstr, sym, regex, arr, obj, emptyObj, fn, p } = getSomeData()
/* eslint-disable no-unused-vars */
const nil = null
const undef = getUndefined()
const bool = true
const num = 42
const bigint = 42n
const str = 'foo'
// eslint-disable-next-line @stylistic/js/max-len
const lstr = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
const sym = Symbol('foo')
const regex = /bar/i
const arr = [1, 2, 3, 4, 5]
const obj = {
foo: {
baz: 42,
nil: null,
undef: undefined,
deep: { nested: { obj: { that: { goes: { on: { forever: true } } } } } }
},
bar: true
}
const emptyObj = {}
const p = Promise.resolve()
const arrowFn = () => {}
/* eslint-enable no-unused-vars */

return { hello: request.params.name } // BREAKPOINT: /foo
})

Expand All @@ -22,30 +43,4 @@ fastify.listen({ port: process.env.APP_PORT }, (err) => {
process.send({ port: process.env.APP_PORT })
})

function getSomeData () {
return {
nil: null,
undef: undefined,
bool: true,
num: 42,
bigint: 42n,
str: 'foo',
// eslint-disable-next-line @stylistic/js/max-len
lstr: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
sym: Symbol('foo'),
regex: /bar/i,
arr: [1, 2, 3, 4, 5],
obj: {
foo: {
baz: 42,
nil: null,
undef: undefined,
deep: { nested: { obj: { that: { goes: { on: { forever: true } } } } } }
},
bar: true
},
emptyObj: {},
fn: () => {},
p: Promise.resolve()
}
}
function getUndefined () {}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
},
"dependencies": {
"@datadog/libdatadog": "^0.3.0",
"@datadog/native-appsec": "8.3.0",
"@datadog/native-appsec": "8.4.0",
"@datadog/native-iast-rewriter": "2.6.1",
"@datadog/native-iast-taint-tracking": "3.2.0",
"@datadog/native-metrics": "^3.1.0",
Expand Down Expand Up @@ -145,7 +145,7 @@
"jszip": "^3.5.0",
"knex": "^2.4.2",
"mkdirp": "^3.0.1",
"mocha": "^9",
"mocha": "^10",
"msgpack-lite": "^0.1.26",
"multer": "^1.4.5-lts.1",
"nock": "^11.3.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-instrumentations/src/aerospike.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function wrapProcess (process) {
addHook({
name: 'aerospike',
file: 'lib/commands/command.js',
versions: ['4', '5']
versions: ['4', '5', '6']
},
commandFactory => {
return shimmer.wrapFunction(commandFactory, f => wrapCreateCommand(f))
Expand Down
12 changes: 11 additions & 1 deletion packages/dd-trace/src/appsec/remote_config/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const log = require('../../log')
const { getExtraServices } = require('../../service-naming/extra-services')
const { UNACKNOWLEDGED, ACKNOWLEDGED, ERROR } = require('./apply_states')
const Scheduler = require('./scheduler')
const { GIT_REPOSITORY_URL, GIT_COMMIT_SHA } = require('../../plugins/util/tags')

const clientId = uuid()

Expand All @@ -33,6 +34,14 @@ class RemoteConfigManager extends EventEmitter {
port: config.port
}))

const tags = config.repositoryUrl
? {
...config.tags,
[GIT_REPOSITORY_URL]: config.repositoryUrl,
[GIT_COMMIT_SHA]: config.commitSHA
}
: config.tags

this._handlers = new Map()
const appliedConfigs = this.appliedConfigs = new Map()

Expand Down Expand Up @@ -67,7 +76,8 @@ class RemoteConfigManager extends EventEmitter {
service: config.service,
env: config.env,
app_version: config.version,
extra_services: []
extra_services: [],
tags: Object.entries(tags).map((pair) => pair.join(':'))
},
capabilities: DEFAULT_CAPABILITY // updated by `updateCapabilities()`
},
Expand Down
2 changes: 2 additions & 0 deletions packages/dd-trace/src/debugger/devtools_client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ function highestOrUndefined (num, max) {
}

async function getDD (callFrameId) {
// TODO: Consider if an `objectGroup` should be used, so it can be explicitly released using
// `Runtime.releaseObjectGroup`
const { result } = await session.post('Debugger.evaluateOnCallFrame', {
callFrameId,
expression,
Expand Down
116 changes: 90 additions & 26 deletions packages/dd-trace/src/llmobs/sdk.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

const { SPAN_KIND, OUTPUT_VALUE } = require('./constants/tags')
const { SPAN_KIND, OUTPUT_VALUE, INPUT_VALUE } = require('./constants/tags')

const {
getFunctionArguments,
validateKind
} = require('./util')
const { isTrue } = require('../util')
const { isTrue, isError } = require('../util')

const { storage } = require('./storage')

Expand Down Expand Up @@ -134,29 +134,63 @@ class LLMObs extends NoopLLMObs {

function wrapped () {
const span = llmobs._tracer.scope().active()

const result = llmobs._activate(span, { kind, options: llmobsOptions }, () => {
if (!['llm', 'embedding'].includes(kind)) {
llmobs.annotate(span, { inputData: getFunctionArguments(fn, arguments) })
const fnArgs = arguments

const lastArgId = fnArgs.length - 1
const cb = fnArgs[lastArgId]
const hasCallback = typeof cb === 'function'

if (hasCallback) {
const scopeBoundCb = llmobs._bind(cb)
fnArgs[lastArgId] = function () {
// it is standard practice to follow the callback signature (err, result)
// however, we try to parse the arguments to determine if the first argument is an error
// if it is not, and is not undefined, we will use that for the output value
const maybeError = arguments[0]
const maybeResult = arguments[1]

llmobs._autoAnnotate(
span,
kind,
getFunctionArguments(fn, fnArgs),
isError(maybeError) || maybeError == null ? maybeResult : maybeError
)

return scopeBoundCb.apply(this, arguments)
}
}

return fn.apply(this, arguments)
})
try {
const result = llmobs._activate(span, { kind, options: llmobsOptions }, () => fn.apply(this, fnArgs))

if (result && typeof result.then === 'function') {
return result.then(
value => {
if (!hasCallback) {
llmobs._autoAnnotate(span, kind, getFunctionArguments(fn, fnArgs), value)
}
return value
},
err => {
llmobs._autoAnnotate(span, kind, getFunctionArguments(fn, fnArgs))
throw err
}
)
}

if (result && typeof result.then === 'function') {
return result.then(value => {
if (value && !['llm', 'retrieval'].includes(kind) && !LLMObsTagger.tagMap.get(span)?.[OUTPUT_VALUE]) {
llmobs.annotate(span, { outputData: value })
}
return value
})
}
// it is possible to return a value and have a callback
// however, since the span finishes when the callback is called, it is possible that
// the callback is called before the function returns (although unlikely)
// we do not want to throw for "annotating a finished span" in this case
if (!hasCallback) {
llmobs._autoAnnotate(span, kind, getFunctionArguments(fn, fnArgs), result)
}

if (result && !['llm', 'retrieval'].includes(kind) && !LLMObsTagger.tagMap.get(span)?.[OUTPUT_VALUE]) {
llmobs.annotate(span, { outputData: result })
return result
} catch (e) {
llmobs._autoAnnotate(span, kind, getFunctionArguments(fn, fnArgs))
throw e
}

return result
}

return this._tracer.wrap(name, spanOptions, wrapped)
Expand Down Expand Up @@ -333,20 +367,34 @@ class LLMObs extends NoopLLMObs {
flushCh.publish()
}

_autoAnnotate (span, kind, input, output) {
const annotations = {}
if (input && !['llm', 'embedding'].includes(kind) && !LLMObsTagger.tagMap.get(span)?.[INPUT_VALUE]) {
annotations.inputData = input
}

if (output && !['llm', 'retrieval'].includes(kind) && !LLMObsTagger.tagMap.get(span)?.[OUTPUT_VALUE]) {
annotations.outputData = output
}

this.annotate(span, annotations)
}

_active () {
const store = storage.getStore()
return store?.span
}

_activate (span, { kind, options } = {}, fn) {
_activate (span, options, fn) {
const parent = this._active()
if (this.enabled) storage.enterWith({ span })

this._tagger.registerLLMObsSpan(span, {
...options,
parent,
kind
})
if (options) {
this._tagger.registerLLMObsSpan(span, {
...options,
parent
})
}

try {
return fn()
Expand All @@ -355,6 +403,22 @@ class LLMObs extends NoopLLMObs {
}
}

// bind function to active LLMObs span
_bind (fn) {
if (typeof fn !== 'function') return fn

const llmobs = this
const activeSpan = llmobs._active()

const bound = function () {
return llmobs._activate(activeSpan, null, () => {
return fn.apply(this, arguments)
})
}

return bound
}

_extractOptions (options) {
const {
modelName,
Expand Down
Loading

0 comments on commit ccdbe34

Please sign in to comment.