Skip to content

Commit fbbccfc

Browse files
authored
feat(tracer): implement process discovery feature (#5782)
* feat(tracer): implement process discovery feature Resolves [APMAPI-1071]
1 parent edc2ada commit fbbccfc

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

integration-tests/startup.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const {
99
const path = require('path')
1010
const { assert } = require('chai')
1111
const semver = require('semver')
12+
const { inspect } = require('util')
13+
const fs = require('fs')
1214

1315
const execArgvs = [
1416
{
@@ -72,6 +74,39 @@ execArgvs.forEach(({ execArgv, skip }) => {
7274
})
7375
})
7476

77+
it('saves tracer configuration on disk', async () => {
78+
if (process.platform !== 'linux') {
79+
return
80+
}
81+
82+
proc = await spawnProc(startupTestFile, {
83+
cwd,
84+
execArgv,
85+
env: {
86+
AGENT_PORT: agent.port
87+
}
88+
})
89+
90+
const containsDatadogMemfd = (fds) => {
91+
for (const fd of fds) {
92+
try {
93+
const fdName = fs.readlinkSync(`/proc/${proc.pid}/fd/${fd}`)
94+
if (fdName.includes('datadog-tracer-info-')) {
95+
return true
96+
}
97+
} catch {}
98+
}
99+
return false
100+
}
101+
102+
const fds = fs.readdirSync(`/proc/${proc.pid}/fd`)
103+
104+
assert(
105+
containsDatadogMemfd(fds),
106+
`FDs ${inspect(fds)} of PID ${proc.pid} did not contain the datadog tracer configuration in memfd`
107+
)
108+
})
109+
75110
it('works for options.url', async () => {
76111
proc = await spawnProc(startupTestFile, {
77112
cwd,

packages/dd-trace/src/tracer.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ class DatadogTracer extends Tracer {
2424
this._scope = new Scope()
2525
setStartupLogConfig(config)
2626
flushStartupLogs(log)
27+
28+
if (!config._isInServerlessEnvironment()) {
29+
const storeConfig = require('./tracer_metadata')
30+
// Keep a reference to the handle, to keep the memfd alive in memory.
31+
// It is read by the service discovery feature.
32+
const metadata = storeConfig(config)
33+
if (metadata === undefined) {
34+
log.warn('Could not store tracer configuration for service discovery')
35+
}
36+
this._inmem_cfg = metadata
37+
}
2738
}
2839

2940
configure (config) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
// Load binding first to not import other modules if it throws
4+
const libdatadog = require('@datadog/libdatadog')
5+
const tracerVersion = require('../../../version').VERSION
6+
7+
function storeConfig (config) {
8+
const processDiscovery = libdatadog.maybeLoad('process-discovery')
9+
if (processDiscovery === undefined) {
10+
return
11+
}
12+
13+
const metadata = new processDiscovery.TracerMetadata(
14+
config.tags['runtime-id'],
15+
tracerVersion,
16+
config.hostname,
17+
config.server || null,
18+
config.env || null,
19+
config.version || null
20+
)
21+
22+
return processDiscovery.storeMetadata(metadata)
23+
}
24+
25+
module.exports = storeConfig

version.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
/* eslint-disable no-var */
44
/* eslint-disable unicorn/prefer-number-properties */
55

6-
var ddMatches = require('./package.json').version.match(/^(\d+)\.(\d+)\.(\d+)/)
6+
var version = require('./package.json').version
7+
var ddMatches = version.match(/^(\d+)\.(\d+)\.(\d+)/)
78
var nodeMatches = process.versions.node.match(/^(\d+)\.(\d+)\.(\d+)/)
89

910
module.exports = {
11+
VERSION: version,
1012
DD_MAJOR: parseInt(ddMatches[1]),
1113
DD_MINOR: parseInt(ddMatches[2]),
1214
DD_PATCH: parseInt(ddMatches[3]),

0 commit comments

Comments
 (0)