Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 21c85af

Browse files
committedOct 10, 2024··
simplify serverless util
1 parent 6967f7d commit 21c85af

File tree

3 files changed

+9
-127
lines changed

3 files changed

+9
-127
lines changed
 

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class AzureFunctionsPlugin extends TracingPlugin {
5959
const context = web.patch(req)
6060
context.config = this.config
6161
context.paths = [path]
62-
context.res = result
62+
context.res = { statusCode: result.status }
6363
context.span = ctx.currentStore.span
6464

6565
serverless.finishSpan(context)
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,7 @@
1-
const tags = require('../../../../../ext/tags')
21
const types = require('../../../../../ext/types')
3-
const kinds = require('../../../../../ext/kinds')
42
const web = require('./web')
53

6-
const SERVERLESS = types.SERVERLESS
7-
const SERVER = kinds.SERVER
8-
const RESOURCE_NAME = tags.RESOURCE_NAME
9-
const SPAN_TYPE = tags.SPAN_TYPE
10-
const SPAN_KIND = tags.SPAN_KIND
11-
const HTTP_METHOD = tags.HTTP_METHOD
12-
const HTTP_STATUS_CODE = tags.HTTP_STATUS_CODE
13-
const HTTP_ROUTE = tags.HTTP_ROUTE
14-
const HTTP_REQUEST_HEADERS = tags.HTTP_REQUEST_HEADERS
15-
const HTTP_RESPONSE_HEADERS = tags.HTTP_RESPONSE_HEADERS
16-
const HTTP_URL = tags.HTTP_URL
17-
const HTTP_USERAGENT = tags.HTTP_USERAGENT
18-
const HTTP_CLIENT_IP = tags.HTTP_CLIENT_IP
19-
20-
const HTTP2_HEADER_AUTHORITY = ':authority'
21-
const HTTP2_HEADER_SCHEME = ':scheme'
22-
const HTTP2_HEADER_PATH = ':path'
23-
24-
const serverless = {
25-
finishSpan (context) {
26-
const { req, res } = context
27-
28-
if (context.finished && !req.stream) return
29-
30-
addRequestTags(context)
31-
addResponseTags(context)
32-
33-
context.config.hooks.request(context.span, req, res)
34-
addResourceTag(context)
35-
36-
context.span.finish()
37-
context.finished = true
38-
}
39-
}
40-
41-
function addRequestTags (context) {
42-
const { req, span, config } = context
43-
const url = extractURL(req)
44-
45-
span.addTags({
46-
[HTTP_URL]: web.obfuscateQs(config, url),
47-
[HTTP_METHOD]: req.method,
48-
[SPAN_KIND]: SERVER,
49-
[SPAN_TYPE]: SERVERLESS,
50-
[HTTP_USERAGENT]: req.headers['user-agent']
51-
})
52-
53-
// if client ip has already been set by appsec, no need to run it again
54-
if (config.clientIpEnabled && !span.context()._tags.hasOwnProperty(HTTP_CLIENT_IP)) {
55-
const clientIp = web.extractIp(config, req)
56-
57-
if (clientIp) {
58-
span.setTag(HTTP_CLIENT_IP, clientIp)
59-
}
60-
}
61-
62-
addHeaders(context)
63-
}
64-
65-
function addResponseTags (context) {
66-
const { req, res, paths, span } = context
67-
68-
if (paths.length > 0) {
69-
span.setTag(HTTP_ROUTE, paths.join(''))
70-
}
71-
72-
span.addTags({
73-
[HTTP_STATUS_CODE]: res.status
74-
})
75-
76-
web.addStatusError(req, res.status)
77-
}
78-
79-
function extractURL (req) {
80-
const headers = req.headers
81-
82-
if (req.stream) {
83-
return `${headers[HTTP2_HEADER_SCHEME]}://${headers[HTTP2_HEADER_AUTHORITY]}${headers[HTTP2_HEADER_PATH]}`
84-
} else {
85-
const protocol = getProtocol(req)
86-
return `${protocol}://${req.headers.host}${req.originalUrl || req.url}`
87-
}
88-
}
89-
90-
function getProtocol (req) {
91-
if (req.socket && req.socket.encrypted) return 'https'
92-
if (req.connection && req.connection.encrypted) return 'https'
93-
94-
return 'http'
95-
}
96-
97-
function addHeaders (context) {
98-
const { req, res, config, span } = context
99-
100-
config.headers.forEach(([key, tag]) => {
101-
const reqHeader = req.headers[key]
102-
const resHeader = res.getHeader(key)
103-
104-
if (reqHeader) {
105-
span.setTag(tag || `${HTTP_REQUEST_HEADERS}.${key}`, reqHeader)
106-
}
107-
108-
if (resHeader) {
109-
span.setTag(tag || `${HTTP_RESPONSE_HEADERS}.${key}`, resHeader)
110-
}
111-
})
112-
}
113-
114-
function addResourceTag (context) {
115-
const { req, span } = context
116-
const tags = span.context()._tags
117-
118-
if (tags['resource.name']) return
119-
120-
const resource = [req.method, tags[HTTP_ROUTE]]
121-
.filter(val => val)
122-
.join(' ')
123-
124-
span.setTag(RESOURCE_NAME, resource)
125-
}
4+
const serverless = { ...web }
5+
serverless.TYPE = types.SERVERLESS
1266

1277
module.exports = serverless

‎packages/dd-trace/src/plugins/util/web.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const contexts = new WeakMap()
3636
const ends = new WeakMap()
3737

3838
const web = {
39+
TYPE: WEB,
40+
3941
// Ensure the configuration has the correct structure and defaults.
4042
normalizeConfig (config) {
4143
const headers = getHeadersToRecord(config)
@@ -103,7 +105,7 @@ const web = {
103105
context.res = res
104106

105107
this.setConfig(req, config)
106-
addRequestTags(context)
108+
addRequestTags(context, this.TYPE)
107109

108110
return span
109111
},
@@ -296,7 +298,7 @@ const web = {
296298

297299
if (context.finished && !req.stream) return
298300

299-
addRequestTags(context)
301+
addRequestTags(context, this.TYPE)
300302
addResponseTags(context)
301303

302304
context.config.hooks.request(context.span, req, res)
@@ -423,15 +425,15 @@ function reactivate (req, fn) {
423425
: fn()
424426
}
425427

426-
function addRequestTags (context) {
428+
function addRequestTags (context, spanType) {
427429
const { req, span, config } = context
428430
const url = extractURL(req)
429431

430432
span.addTags({
431433
[HTTP_URL]: web.obfuscateQs(config, url),
432434
[HTTP_METHOD]: req.method,
433435
[SPAN_KIND]: SERVER,
434-
[SPAN_TYPE]: WEB,
436+
[SPAN_TYPE]: spanType,
435437
[HTTP_USERAGENT]: req.headers['user-agent']
436438
})
437439

0 commit comments

Comments
 (0)
Please sign in to comment.