Skip to content
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

v5.27.0 proposal #4925

Merged
merged 13 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/appsec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,12 @@ jobs:

next:
strategy:
fail-fast: false
matrix:
version:
- 18
- latest
range: ['9.5.0', '11.1.4', '13.2.0', '14.2.6']
range: ['9.5.0', '11.1.4', '13.2.0', '>=14.0.0 <=14.2.6', '>=14.2.7 <15']
runs-on: ubuntu-latest
env:
PLUGINS: next
Expand Down
22 changes: 21 additions & 1 deletion .github/workflows/plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,25 @@ jobs:
- uses: actions/checkout@v4
- uses: ./.github/actions/plugins/test-and-upstream

langchain:
runs-on: ubuntu-latest
env:
PLUGINS: langchain
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/testagent/start
- uses: ./.github/actions/node/setup
- uses: ./.github/actions/install
- uses: ./.github/actions/node/18 # langchain doesn't support Node 16
- run: yarn test:plugins:ci
shell: bash
- uses: ./.github/actions/node/latest
- run: yarn test:plugins:ci
shell: bash
- uses: codecov/codecov-action@v3
- if: always()
uses: ./.github/actions/testagent/logs

limitd-client:
runs-on: ubuntu-latest
services:
Expand Down Expand Up @@ -734,11 +753,12 @@ jobs:
# TODO: fix performance issues and test more Node versions
next:
strategy:
fail-fast: false
matrix:
version:
- 18
- latest
range: ['9.5.0', '11.1.4', '13.2.0', '14.2.6']
range: ['9.5.0', '11.1.4', '13.2.0', '>=14.0.0 <=14.2.6', '>=14.2.7 <15']
runs-on: ubuntu-latest
env:
PLUGINS: next
Expand Down
5 changes: 4 additions & 1 deletion .gitlab/requirements_block.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
{"name": "unsupported 2.x.x glibc x64","filepath": "/some/path", "args": [], "envars": [], "host": {"os": "linux", "arch": "arm64", "libc": "glibc:2.16.9"}},
{"name": "unsupported 2.x.x glibc x86","filepath": "/some/path", "args": [], "envars": [], "host": {"os": "linux", "arch": "x86", "libc": "glibc:2.17"}},
{"name": "npm","filepath": "/pathto/node", "args": ["/pathto/node", "/pathto/npm-cli.js"], "envars": [], "host": {"os": "linux", "arch": "x64", "libc": "glibc:2.40"}},
{"name": "npm-symlink","filepath": "/pathto/node", "args": ["/pathto/node", "/pathto/npm"], "envars": [], "host": {"os": "linux", "arch": "x64", "libc": "glibc:2.40"}},
{"name": "yarn","filepath": "/pathto/node", "args": ["/pathto/node", "/pathto/yarn.js"], "envars": [], "host": {"os": "linux", "arch": "x64", "libc": "glibc:2.40"}},
{"name": "pnpm","filepath": "/pathto/node", "args": ["/pathto/node", "/pathto/pnpm.cjs"], "envars": [], "host": {"os": "linux", "arch": "x64", "libc": "glibc:2.40"}}
{"name": "yarn-symlink","filepath": "/pathto/node", "args": ["/pathto/node", "/pathto/yarn"], "envars": [], "host": {"os": "linux", "arch": "x64", "libc": "glibc:2.40"}},
{"name": "pnpm","filepath": "/pathto/node", "args": ["/pathto/node", "/pathto/pnpm.cjs"], "envars": [], "host": {"os": "linux", "arch": "x64", "libc": "glibc:2.40"}},
{"name": "pnpm-symlink","filepath": "/pathto/node", "args": ["/pathto/node", "/pathto/pnpm"], "envars": [], "host": {"os": "linux", "arch": "x64", "libc": "glibc:2.40"}}
]
1 change: 1 addition & 0 deletions docs/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ tracer.use('kafkajs');
tracer.use('knex');
tracer.use('koa');
tracer.use('koa', httpServerOptions);
tracer.use('langchain');
tracer.use('mariadb', { service: () => `my-custom-mariadb` })
tracer.use('memcached');
tracer.use('microgateway-core');
Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ interface Plugins {
"kafkajs": tracer.plugins.kafkajs
"knex": tracer.plugins.knex;
"koa": tracer.plugins.koa;
"langchain": tracer.plugins.langchain;
"mariadb": tracer.plugins.mariadb;
"memcached": tracer.plugins.memcached;
"microgateway-core": tracer.plugins.microgateway_core;
Expand Down Expand Up @@ -1592,6 +1593,12 @@ declare namespace tracer {
*/
interface kafkajs extends Instrumentation {}

/**
* This plugin automatically instruments the
* [langchain](https://js.langchain.com/) module
*/
interface langchain extends Instrumentation {}

/**
* This plugin automatically instruments the
* [ldapjs](https://github.com/ldapjs/node-ldapjs/) module.
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/profiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ function busyWait (ms) {
})
}

setImmediate(async () => busyWait(500))
const durationMs = Number.parseInt(process.env.TEST_DURATION_MS ?? '500')
setImmediate(async () => busyWait(durationMs))
65 changes: 65 additions & 0 deletions integration-tests/profiler/profiler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,71 @@ describe('profiler', () => {
})
})

context('Profiler API telemetry', () => {
beforeEach(async () => {
agent = await new FakeAgent().start()
})

afterEach(async () => {
proc.kill()
await agent.stop()
})

it('sends profiler API telemetry', () => {
proc = fork(profilerTestFile, {
cwd,
env: {
DD_TRACE_AGENT_PORT: agent.port,
DD_PROFILING_ENABLED: 1,
DD_PROFILING_UPLOAD_PERIOD: 1,
TEST_DURATION_MS: 2500
}
})

let requestCount = 0
let pointsCount = 0

const checkMetrics = agent.assertTelemetryReceived(({ _, payload }) => {
const pp = payload.payload
assert.equal(pp.namespace, 'profilers')
const series = pp.series
assert.lengthOf(series, 2)
assert.equal(series[0].metric, 'profile_api.requests')
assert.equal(series[0].type, 'count')
// There's a race between metrics and on-shutdown profile, so metric
// value will be between 2 and 3
requestCount = series[0].points[0][1]
assert.isAtLeast(requestCount, 2)
assert.isAtMost(requestCount, 3)

assert.equal(series[1].metric, 'profile_api.responses')
assert.equal(series[1].type, 'count')
assert.include(series[1].tags, 'status_code:200')

// Same number of requests and responses
assert.equal(series[1].points[0][1], requestCount)
}, timeout, 'generate-metrics')

const checkDistributions = agent.assertTelemetryReceived(({ _, payload }) => {
const pp = payload.payload
assert.equal(pp.namespace, 'profilers')
const series = pp.series
assert.lengthOf(series, 2)
assert.equal(series[0].metric, 'profile_api.bytes')
assert.equal(series[1].metric, 'profile_api.ms')

// Same number of points
pointsCount = series[0].points.length
assert.equal(pointsCount, series[1].points.length)
}, timeout, 'distributions')

return Promise.all([checkProfiles(agent, proc, timeout), checkMetrics, checkDistributions]).then(() => {
// Same number of requests and points
assert.equal(requestCount, pointsCount)
})
})
})

function forkSsi (args, whichEnv) {
const profilerEnablingEnv = whichEnv ? { DD_PROFILING_ENABLED: 'auto' } : { DD_INJECTION_ENABLED: 'profiler' }
return fork(ssiTestFile, args, {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dd-trace",
"version": "5.26.0",
"version": "5.27.0",
"description": "Datadog APM tracing client for JavaScript",
"main": "index.js",
"typings": "index.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions packages/datadog-instrumentations/src/helpers/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module.exports = {
'@jest/test-sequencer': () => require('../jest'),
'@jest/transform': () => require('../jest'),
'@koa/router': () => require('../koa'),
'@langchain/core': () => require('../langchain'),
'@langchain/openai': () => require('../langchain'),
'@node-redis/client': () => require('../redis'),
'@opensearch-project/opensearch': () => require('../opensearch'),
'@opentelemetry/sdk-trace-node': () => require('../otel-sdk-trace'),
Expand Down Expand Up @@ -67,6 +69,7 @@ module.exports = {
koa: () => require('../koa'),
'koa-router': () => require('../koa'),
kafkajs: () => require('../kafkajs'),
langchain: () => require('../langchain'),
ldapjs: () => require('../ldapjs'),
'limitd-client': () => require('../limitd-client'),
lodash: () => require('../lodash'),
Expand Down
77 changes: 77 additions & 0 deletions packages/datadog-instrumentations/src/langchain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict'

const { addHook } = require('./helpers/instrument')
const shimmer = require('../../datadog-shimmer')

const tracingChannel = require('dc-polyfill').tracingChannel

const invokeTracingChannel = tracingChannel('apm:langchain:invoke')

function wrapLangChainPromise (fn, type, namespace = []) {
return function () {
if (!invokeTracingChannel.start.hasSubscribers) {
return fn.apply(this, arguments)
}

// Runnable interfaces have an `lc_namespace` property
const ns = this.lc_namespace || namespace
const resource = [...ns, this.constructor.name].join('.')

const ctx = {
args: arguments,
instance: this,
type,
resource
}

return invokeTracingChannel.tracePromise(fn, ctx, this, ...arguments)
}
}

// langchain compiles into ESM and CommonJS, with ESM being the default and landing in the `.js` files
// however, CommonJS ends up in `cjs` files, and are required under the hood with `.cjs` files
// we patch each separately and explicitly to match against exports only once, and not rely on file regex matching
const extensions = ['js', 'cjs']

for (const extension of extensions) {
addHook({ name: '@langchain/core', file: `dist/runnables/base.${extension}`, versions: ['>=0.1'] }, exports => {
const RunnableSequence = exports.RunnableSequence
shimmer.wrap(RunnableSequence.prototype, 'invoke', invoke => wrapLangChainPromise(invoke, 'chain'))
shimmer.wrap(RunnableSequence.prototype, 'batch', batch => wrapLangChainPromise(batch, 'chain'))
return exports
})

addHook({
name: '@langchain/core',
file: `dist/language_models/chat_models.${extension}`,
versions: ['>=0.1']
}, exports => {
const BaseChatModel = exports.BaseChatModel
shimmer.wrap(
BaseChatModel.prototype,
'generate',
generate => wrapLangChainPromise(generate, 'chat_model')
)
return exports
})

addHook({ name: '@langchain/core', file: `dist/language_models/llms.${extension}`, versions: ['>=0.1'] }, exports => {
const BaseLLM = exports.BaseLLM
shimmer.wrap(BaseLLM.prototype, 'generate', generate => wrapLangChainPromise(generate, 'llm'))
return exports
})

addHook({ name: '@langchain/openai', file: `dist/embeddings.${extension}`, versions: ['>=0.1'] }, exports => {
const OpenAIEmbeddings = exports.OpenAIEmbeddings

// OpenAI (and Embeddings in general) do not define an lc_namespace
const namespace = ['langchain', 'embeddings', 'openai']
shimmer.wrap(OpenAIEmbeddings.prototype, 'embedDocuments', embedDocuments =>
wrapLangChainPromise(embedDocuments, 'embedding', namespace)
)
shimmer.wrap(OpenAIEmbeddings.prototype, 'embedQuery', embedQuery =>
wrapLangChainPromise(embedQuery, 'embedding', namespace)
)
return exports
})
}
26 changes: 19 additions & 7 deletions packages/datadog-instrumentations/src/next.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ const queryParsedChannel = channel('apm:next:query-parsed')
const requests = new WeakSet()
const nodeNextRequestsToNextRequests = new WeakMap()

// Next.js <= 14.2.6
const MIDDLEWARE_HEADER = 'x-middleware-invoke'

// Next.js >= 14.2.7
const NEXT_REQUEST_META = Symbol.for('NextInternalRequestMeta')
const META_IS_MIDDLEWARE = 'middlewareInvoke'
const encounteredMiddleware = new WeakSet()

function wrapHandleRequest (handleRequest) {
return function (req, res, pathname, query) {
return instrument(req, res, () => handleRequest.apply(this, arguments))
Expand Down Expand Up @@ -111,6 +117,11 @@ function getPageFromPath (page, dynamicRoutes = []) {
return getPagePath(page)
}

function getRequestMeta (req, key) {
const meta = req[NEXT_REQUEST_META] || {}
return typeof key === 'string' ? meta[key] : meta
}

function instrument (req, res, error, handler) {
if (typeof error === 'function') {
handler = error
Expand All @@ -121,8 +132,9 @@ function instrument (req, res, error, handler) {
res = res.originalResponse || res

// TODO support middleware properly in the future?
const isMiddleware = req.headers[MIDDLEWARE_HEADER]
if (isMiddleware || requests.has(req)) {
const isMiddleware = req.headers[MIDDLEWARE_HEADER] || getRequestMeta(req, META_IS_MIDDLEWARE)
if ((isMiddleware && !encounteredMiddleware.has(req)) || requests.has(req)) {
encounteredMiddleware.add(req)
if (error) {
errorChannel.publish({ error })
}
Expand Down Expand Up @@ -188,7 +200,7 @@ function finish (ctx, result, err) {
// however, it is not provided as a class function or exported property
addHook({
name: 'next',
versions: ['>=13.3.0 <14.2.7'],
versions: ['>=13.3.0 <15'],
file: 'dist/server/web/spec-extension/adapters/next-request.js'
}, NextRequestAdapter => {
shimmer.wrap(NextRequestAdapter.NextRequestAdapter, 'fromNodeNextRequest', fromNodeNextRequest => {
Expand All @@ -203,7 +215,7 @@ addHook({

addHook({
name: 'next',
versions: ['>=11.1 <14.2.7'],
versions: ['>=11.1 <15'],
file: 'dist/server/serve-static.js'
}, serveStatic => shimmer.wrap(serveStatic, 'serveStatic', wrapServeStatic))

Expand All @@ -213,7 +225,7 @@ addHook({
file: 'dist/next-server/server/serve-static.js'
}, serveStatic => shimmer.wrap(serveStatic, 'serveStatic', wrapServeStatic))

addHook({ name: 'next', versions: ['>=11.1 <14.2.7'], file: 'dist/server/next-server.js' }, nextServer => {
addHook({ name: 'next', versions: ['>=11.1 <15'], file: 'dist/server/next-server.js' }, nextServer => {
const Server = nextServer.default

shimmer.wrap(Server.prototype, 'handleRequest', wrapHandleRequest)
Expand All @@ -230,7 +242,7 @@ addHook({ name: 'next', versions: ['>=11.1 <14.2.7'], file: 'dist/server/next-se
})

// `handleApiRequest` changes parameters/implementation at 13.2.0
addHook({ name: 'next', versions: ['>=13.2 <14.2.7'], file: 'dist/server/next-server.js' }, nextServer => {
addHook({ name: 'next', versions: ['>=13.2 <15'], file: 'dist/server/next-server.js' }, nextServer => {
const Server = nextServer.default
shimmer.wrap(Server.prototype, 'handleApiRequest', wrapHandleApiRequestWithMatch)
return nextServer
Expand Down Expand Up @@ -264,7 +276,7 @@ addHook({

addHook({
name: 'next',
versions: ['>=13 <14.2.7'],
versions: ['>=13 <15'],
file: 'dist/server/web/spec-extension/request.js'
}, request => {
const nextUrlDescriptor = Object.getOwnPropertyDescriptor(request.NextRequest.prototype, 'nextUrl')
Expand Down
3 changes: 1 addition & 2 deletions packages/datadog-plugin-amqplib/src/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class AmqplibConsumerPlugin extends ConsumerPlugin {
'amqp.consumerTag': fields.consumerTag,
'amqp.source': fields.source,
'amqp.destination': fields.destination
},
extractedLinks: childOf?._links
}
})

if (
Expand Down
3 changes: 1 addition & 2 deletions packages/datadog-plugin-aws-sdk/src/services/kinesis.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class Kinesis extends BaseAwsSdkPlugin {
{},
this.requestTags.get(request) || {},
{ 'span.kind': 'server' }
),
extractedLinks: responseExtraction.maybeChildOf._links
)
}
span = plugin.tracer.startSpan('aws.response', options)
this.enter(span, store)
Expand Down
3 changes: 1 addition & 2 deletions packages/datadog-plugin-aws-sdk/src/services/sqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ class Sqs extends BaseAwsSdkPlugin {
{},
this.requestTags.get(request) || {},
{ 'span.kind': 'server' }
),
extractedLinks: contextExtraction.datadogContext._links
)
}
parsedMessageAttributes = contextExtraction.parsedAttributes
span = plugin.tracer.startSpan('aws.response', options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class GoogleCloudPubsubConsumerPlugin extends ConsumerPlugin {
},
metrics: {
'pubsub.ack': 0
},
extractedLinks: childOf?._links
}
})
if (this.config.dsmEnabled && message?.attributes) {
const payloadSize = getMessageSize(message)
Expand Down
Loading
Loading