Skip to content

Commit ccc52ef

Browse files
authored
[SVLS-5649] Add HTTP Context Propagation to Azure Functions (#5839)
As drive-by improve readability
1 parent 93d59c4 commit ccc52ef

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

packages/datadog-plugin-azure-functions/src/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ class AzureFunctionsPlugin extends TracingPlugin {
1919
static get operation () { return 'invoke' }
2020
static get kind () { return 'server' }
2121
static get type () { return 'serverless' }
22-
2322
static get prefix () { return 'tracing:datadog:azure:functions:invoke' }
2423

2524
bindStart (ctx) {
26-
const { functionName, methodName } = ctx
25+
const { functionName, methodName, httpRequest } = ctx
2726
const store = storage('legacy').getStore()
28-
27+
// httpRequest.headers is a map
28+
const childOf = this._tracer.extract('http_headers', Object.fromEntries(httpRequest.headers))
2929
const span = this.startSpan(this.operationName(), {
30+
childOf,
3031
service: this.serviceName(),
3132
type: 'serverless',
3233
meta: {
@@ -52,7 +53,7 @@ class AzureFunctionsPlugin extends TracingPlugin {
5253
const path = (new URL(httpRequest.url)).pathname
5354
const req = {
5455
method: httpRequest.method,
55-
headers: Object.fromEntries(httpRequest.headers.entries()),
56+
headers: Object.fromEntries(httpRequest.headers),
5657
url: path
5758
}
5859

packages/datadog-plugin-azure-functions/test/integration-test/client.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ describe('esm', () => {
5050
assert.propertyVal(payload[0][0], 'name', 'azure.functions.invoke')
5151
})
5252
}).timeout(50000)
53+
54+
it('propagates context to child requests', async () => {
55+
const envArgs = {
56+
PATH: `${sandbox.folder}/node_modules/azure-functions-core-tools/bin:${process.env.PATH}`
57+
}
58+
proc = await spawnPluginIntegrationTestProc(sandbox.folder, 'func', ['start'], agent.port, undefined, envArgs)
59+
60+
return curlAndAssertMessage(agent, 'http://127.0.0.1:7071/api/httptest2', ({ headers, payload }) => {
61+
assert.strictEqual(payload.length, 2)
62+
assert.strictEqual(payload[0][0].parent_id, payload[1][1].span_id)
63+
})
64+
}).timeout(50000)
5365
})
5466
})
5567

packages/datadog-plugin-azure-functions/test/integration-test/fixtures/src/functions/server.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,15 @@ app.http('httptest', {
1313
authLevel: 'anonymous',
1414
handler: handlerFunction
1515
})
16+
17+
app.http('httptest2', {
18+
methods: ['GET'],
19+
authLevel: 'anonymous',
20+
handler: async (request, context) => {
21+
await fetch('http://127.0.0.1:7071/api/httptest')
22+
return {
23+
status: 200,
24+
body: 'Hello Datadog 2!'
25+
}
26+
}
27+
})

0 commit comments

Comments
 (0)