Skip to content
Draft
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
10 changes: 10 additions & 0 deletions .github/workflows/instrumentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@ jobs:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
- uses: ./.github/actions/instrumentations/test

instrumentation-otel-api:
runs-on: ubuntu-latest
permissions:
id-token: write
env:
PLUGINS: otel-api
steps:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
- uses: ./.github/actions/instrumentations/test

instrumentation-otel-sdk-trace:
runs-on: ubuntu-latest
permissions:
Expand Down
10 changes: 10 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,16 @@ const tracerProvider = new tracer.TracerProvider()
tracerProvider.register()
```

dd-trace captures application copies of `@opentelemetry/api` from 1.4.1 through 1.9.x and
`@opentelemetry/api-logs` from 0.33.0 through 0.x for bridge operations. Provider registration stays
on dd-trace's optional compatibility-max copies so every supported application copy can consume the
providers through OpenTelemetry's global API.

The `dd-trace/esbuild` and `dd-trace/webpack` plugins keep dd-trace's API copies inside relocated
bundles even when application imports are configured as external. Application imports otherwise
follow the bundler's normal external configuration and must remain resolvable after deployment when
externalized.

The following attributes are available to override Datadog-specific options:

* `service.name`: The service name to be used for this span. The service name from the tracer will be used if this is not provided.
Expand Down
46 changes: 46 additions & 0 deletions integration-tests/esbuild/build-and-test-otel-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env node
'use strict'

const esbuild = require('esbuild')

const ddPlugin = require('../../esbuild')
const {
EXTERNALS,
runOtelApiBundleScenario,
} = require('../helpers/otel-api-bundle')

/**
* @param {{ entry: string, outfile: string, workingDirectory: string }} paths
* @param {'cjs' | 'esm'} format
*/
function build (paths, format) {
return esbuild.build({
absWorkingDir: paths.workingDirectory,
bundle: true,
entryPoints: [paths.entry],
external: [...EXTERNALS, '@opentelemetry/*'],
format,
outfile: paths.outfile,
platform: 'node',
plugins: [ddPlugin],
target: 'node18',
})
}

async function main () {
for (const [format, extension] of [['cjs', 'js'], ['esm', 'mjs']]) {
for (const applicationOwnsApi of [false, true]) {
await runOtelApiBundleScenario({
applicationOwnsApi,
build: paths => build(paths, /** @type {'cjs' | 'esm'} */ (format)),
extension,
})
}
}
}

main().catch((error) => {
// eslint-disable-next-line no-console
console.error(error)
process.exitCode = 1
})
6 changes: 6 additions & 0 deletions integration-tests/esbuild/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ esbuildVersions.forEach((version) => {
})
})

it('preserves application externals and bundles API fallbacks after relocation', () => {
execSync('node ./build-and-test-otel-api.js', {
timeout,
})
})

it('injects Git metadata into bundled applications', () => {
execSync('node ./build-and-test-git-tags.js', {
timeout,
Expand Down
169 changes: 169 additions & 0 deletions integration-tests/helpers/otel-api-bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
'use strict'

const assert = require('node:assert/strict')
const { execFileSync } = require('node:child_process')
const fs = require('node:fs')
const os = require('node:os')
const path = require('node:path')

const DD_TRACE_PATH = path.join(__dirname, '..', '..')
const OTEL_API_DIRECTORY = findPackageDirectory(require.resolve('@opentelemetry/api'))
const OTEL_API_LOGS_DIRECTORY = findPackageDirectory(require.resolve('@opentelemetry/api-logs'))

const EXTERNALS = [
'diagnostics_channel',
'pg',
'mysql2',
'better-sqlite3',
'sqlite3',
'mysql',
'oracledb',
'pg-query-stream',
'tedious',
'@yaacovcr/transform',
'@datadog/native-appsec',
'@datadog/native-iast-taint-tracking',
'@datadog/native-metrics',
'@datadog/pprof',
'@datadog/libdatadog',
]

/**
* @param {object} options
* @param {boolean} options.applicationOwnsApi
* @param {(paths: { entry: string, outfile: string, workingDirectory: string }) => Promise<void>} options.build
* @param {string} options.extension
*/
async function runOtelApiBundleScenario ({ applicationOwnsApi, build, extension }) {
const buildDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'dd-otel-bundle-build-'))
const runtimeDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'dd-otel-bundle-runtime-'))
const entry = path.join(buildDirectory, 'app.js')
const outfile = path.join(buildDirectory, `out.${extension}`)

try {
fs.writeFileSync(path.join(buildDirectory, 'package.json'), JSON.stringify({
name: 'otel-api-bundle-app',
private: true,
dependencies: applicationOwnsApi
? { '@opentelemetry/api': '*', '@opentelemetry/api-logs': '*' }
: {},
}))
linkOtelApiPackages(buildDirectory)
fs.writeFileSync(entry, applicationSource(applicationOwnsApi))

await build({ entry, outfile, workingDirectory: buildDirectory })

const relocated = path.join(runtimeDirectory, `out.${extension}`)
fs.copyFileSync(outfile, relocated)
fs.writeFileSync(path.join(runtimeDirectory, 'package.json'), JSON.stringify({
name: 'relocated-otel-api-bundle',
private: true,
type: extension === 'mjs' ? 'module' : 'commonjs',
}))
if (applicationOwnsApi) linkOtelApiPackages(runtimeDirectory)

let output
try {
const specifier = `./${path.basename(relocated)}`
const preload = applicationOwnsApi
? "globalThis.__ddRuntimeApi = require('@opentelemetry/api'); " +
"globalThis.__ddRuntimeApiLogs = require('@opentelemetry/api-logs'); "
: ''
const runner = `${preload}(async () => { try { await import(${JSON.stringify(specifier)}) } ` +
'catch (error) { console.error(error.stack || error); process.exitCode = 1 } })()'
output = execFileSync(process.execPath, ['-e', runner], {
cwd: runtimeDirectory,
encoding: 'utf8',
maxBuffer: 16 * 1024 * 1024,
stdio: ['ignore', 'pipe', 'pipe'],
env: {
...process.env,
DD_INSTRUMENTATION_TELEMETRY_ENABLED: 'false',
DD_REMOTE_CONFIG_ENABLED: 'false',
DD_TRACE_ENABLED: 'false',
OTEL_LOGS_EXPORTER: '',
OTEL_METRICS_EXPORTER: '',
OTEL_TRACES_EXPORTER: '',
},
})
} catch (error) {
const stderr = String(error.stderr)
const diagnostic = stderr
.split('\n')
.map(line => line.length < 1_000 ? line : `${line.slice(0, 100)}…${line.slice(-200)}`)
.slice(-20)
.join('\n')
throw new Error(`${extension} applicationOwnsApi=${applicationOwnsApi}\n${diagnostic}`)
}
assert.match(output, /OTEL_API_BUNDLE_OK/)
} finally {
fs.rmSync(buildDirectory, { recursive: true, force: true })
fs.rmSync(runtimeDirectory, { recursive: true, force: true })
}
}

/**
* @param {boolean} applicationOwnsApi
* @returns {string}
*/
function applicationSource (applicationOwnsApi) {
const applicationApi = applicationOwnsApi
? `const api = require('@opentelemetry/api')
const apiLogs = require('@opentelemetry/api-logs')`
: `const api = holder.getApi()
const apiLogs = holder.getApiLogs()`
const runtimeCopyAssertion = applicationOwnsApi
? `if (api.trace !== globalThis.__ddRuntimeApi.trace || apiLogs.logs !== globalThis.__ddRuntimeApiLogs.logs) {
throw new Error('Application OpenTelemetry APIs were bundled instead of loaded at runtime')
}
if (api.trace !== holder.getApi().trace || apiLogs.logs !== holder.getApiLogs().logs) {
throw new Error('The bridge did not capture the application OpenTelemetry APIs')
}
`
: ''

return `'use strict'
const holder = require(${JSON.stringify(path.join(DD_TRACE_PATH, 'packages/dd-trace/src/opentelemetry/api'))})
const tracer = require(${JSON.stringify(DD_TRACE_PATH)}).init({ startupLogs: false })
const provider = new tracer.TracerProvider()
provider.register()
${applicationApi}
${runtimeCopyAssertion}if (!apiLogs.SeverityNumber) {
throw new Error('OpenTelemetry Logs API did not load')
}
const span = api.trace.getTracer('bundle-test').startSpan('bundle-test')
const traceId = span.spanContext().traceId
span.end()
if (!/^[0-9a-f]{32}$/.test(traceId) || /^0+$/.test(traceId)) {
throw new Error('OpenTelemetry bridge returned an invalid trace ID: ' + traceId)
}
console.log('OTEL_API_BUNDLE_OK')
process.exit(0)
`
}

/**
* @param {string} directory
*/
function linkOtelApiPackages (directory) {
const scopeDirectory = path.join(directory, 'node_modules', '@opentelemetry')
fs.mkdirSync(scopeDirectory, { recursive: true })
fs.symlinkSync(OTEL_API_DIRECTORY, path.join(scopeDirectory, 'api'), 'dir')
fs.symlinkSync(OTEL_API_LOGS_DIRECTORY, path.join(scopeDirectory, 'api-logs'), 'dir')
}

/**
* @param {string} entry
* @returns {string}
*/
function findPackageDirectory (entry) {
let directory = path.dirname(entry)
const { root } = path.parse(directory)
while (directory !== root) {
if (fs.existsSync(path.join(directory, 'package.json'))) return directory
directory = path.dirname(directory)
}
throw new Error(`Unable to find package.json for ${entry}`)
}

module.exports = { EXTERNALS, runOtelApiBundleScenario }
71 changes: 71 additions & 0 deletions integration-tests/webpack/build-and-test-otel-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env node
'use strict'

const path = require('node:path')

const webpack = require('webpack')

const {
EXTERNALS,
runOtelApiBundleScenario,
} = require('../helpers/otel-api-bundle')
const DatadogWebpackPlugin = require('../../webpack')

/**
* @param {{ entry: string, outfile: string, workingDirectory: string }} paths
* @param {boolean} outputModule
* @returns {Promise<void>}
*/
function build (paths, outputModule) {
return new Promise((resolve, reject) => {
const externalType = outputModule ? 'node-commonjs' : 'commonjs'
const externals = {
'@opentelemetry/api': `${externalType} @opentelemetry/api`,
'@opentelemetry/api-logs': `${externalType} @opentelemetry/api-logs`,
}
for (const name of EXTERNALS) {
externals[name] = `${externalType} ${name}`
}

webpack({
context: paths.workingDirectory,
devtool: false,
entry: paths.entry,
experiments: { outputModule },
externals,
externalsType: outputModule ? 'module' : 'commonjs',
mode: 'development',
optimization: { minimize: false },
output: {
filename: path.basename(paths.outfile),
hashFunction: 'sha256',
module: outputModule,
path: path.dirname(paths.outfile),
},
plugins: [new DatadogWebpackPlugin()],
target: 'node18',
}, (error, stats) => {
if (error) return reject(error)
if (stats.hasErrors()) return reject(new Error(stats.toString({ errors: true })))
resolve()
})
})
}

async function main () {
for (const [outputModule, extension] of [[false, 'js'], [true, 'mjs']]) {
for (const applicationOwnsApi of [false, true]) {
await runOtelApiBundleScenario({
applicationOwnsApi,
build: paths => build(paths, outputModule),
extension,
})
}
}
}

main().catch((error) => {
// eslint-disable-next-line no-console
console.error(error)
process.exitCode = 1
})
4 changes: 4 additions & 0 deletions integration-tests/webpack/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ webpackVersions.forEach((version) => {
execSync('node ./build-and-test-openfeature.js', { timeout })
})

it('preserves application externals and bundles API fallbacks after relocation', () => {
execSync('node ./build-and-test-otel-api.js', { timeout })
})

it('injects Git metadata into bundled applications', () => {
execSync('node ./build-and-test-git-tags.js', { timeout })
})
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@
"@datadog/openfeature-node-server": "2.0.0",
"@datadog/pprof": "5.15.1",
"@datadog/wasm-js-rewriter": "5.0.1",
"@opentelemetry/api": ">=1.0.0 <1.10.0",
"@opentelemetry/api-logs": "<1.0.0",
"@opentelemetry/api": "1.9.1",
"@opentelemetry/api-logs": "0.212.0",
"oxc-parser": "^0.132.0"
},
"devDependencies": {
Expand Down Expand Up @@ -230,6 +230,9 @@
"node-preload": "^0.2.1",
"nyc": "^18.0.0",
"octokit": "^5.0.3",
"otel-api-logs-v033": "https://registry.npmjs.org/@opentelemetry/api-logs/-/api-logs-0.33.0.tgz",
"otel-api-logs-v034": "https://registry.npmjs.org/@opentelemetry/api-logs/-/api-logs-0.34.0.tgz",
"otel-api-v14": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.4.1.tgz",
"p-limit": "^7.2.0",
"proxyquire": "^2.1.3",
"retry": "^0.13.1",
Expand Down
Loading
Loading