Skip to content

Commit

Permalink
Add datadog-container-id header to profile (#1628)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qard authored and rochdev committed Nov 2, 2021
1 parent 0ca7182 commit 9cebdab
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
},
"dependencies": {
"@datadog/native-metrics": "^1.0.0",
"@datadog/pprof": "^0.1.3",
"@datadog/pprof": "^0.2.0",
"@datadog/sketches-js": "^1.0.4",
"@types/node": "^10.12.18",
"crypto-randomuuid": "^1.0.0",
Expand Down
8 changes: 8 additions & 0 deletions packages/dd-trace/src/profiling/exporters/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ const retry = require('retry')
const { request } = require('http')
const FormData = require('form-data')

// TODO: avoid using dd-trace internals. Make this a separate module?
const docker = require('../../exporters/agent/docker')
const version = require('../../../lib/version')

const containerId = docker.id()

function sendRequest (options, body, callback) {
const req = request(options, res => {
if (res.statusCode >= 400) {
Expand Down Expand Up @@ -104,6 +108,10 @@ class AgentExporter {
headers: form.getHeaders()
}

if (containerId) {
options.headers['Datadog-Container-ID'] = containerId
}

if (this._url.protocol === 'unix:') {
options.socketPath = this._url.pathname
} else {
Expand Down
10 changes: 5 additions & 5 deletions packages/dd-trace/test/asserts/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ module.exports = ({ Assertion, expect }) => {
expect(obj.timeNanos).to.be.a.long
expect(obj.period).to.be.a.long
expect(obj.periodType).to.be.a.valueType
expect(obj.sampleType).to.be.an('array').and.have.length.at.least(1)
expect(obj.sample).to.be.an('array').and.have.length.at.least(1)
expect(obj.location).to.be.an('array').and.have.length.at.least(1)
expect(obj.function).to.be.an('array').and.have.length.at.least(1)
expect(obj.stringTable).to.be.an('array')
expect(obj.sampleType).to.be.an('array').and.have.length(2)
expect(obj.sample).to.be.an('array')
expect(obj.location).to.be.an('array')
expect(obj.function).to.be.an('array')
expect(obj.stringTable).to.be.an('array').and.have.length.at.least(1)
expect(obj.stringTable[0]).to.equal('')

for (const sampleType of obj.sampleType) {
Expand Down
12 changes: 11 additions & 1 deletion packages/dd-trace/test/profiling/exporters/agent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const upload = require('multer')()
const os = require('os')
const path = require('path')
const getPort = require('get-port')
const proxyquire = require('proxyquire')
const { gunzipSync } = require('zlib')
const CpuProfiler = require('../../../src/profiling/profilers/cpu')
const HeapProfiler = require('../../../src/profiling/profilers/heap')
Expand Down Expand Up @@ -53,9 +54,17 @@ describe('exporters/agent', () => {
let url
let listener
let app
let docker

beforeEach(() => {
AgentExporter = require('../../../src/profiling/exporters/agent').AgentExporter
docker = {
id () {
return 'container-id'
}
}
AgentExporter = proxyquire('../../../src/profiling/exporters/agent', {
'../../exporters/agent/docker': docker
}).AgentExporter
sockets = []
app = express()
})
Expand Down Expand Up @@ -96,6 +105,7 @@ describe('exporters/agent', () => {
await new Promise((resolve, reject) => {
app.post('/profiling/v1/input', upload.any(), (req, res) => {
try {
expect(req.headers).to.have.property('datadog-container-id', docker.id())
expect(req.body).to.have.property('language', 'javascript')
expect(req.body).to.have.property('runtime', 'nodejs')
expect(req.body).to.have.property('runtime_version', process.version)
Expand Down

0 comments on commit 9cebdab

Please sign in to comment.