|
1 |
| -const tags = require('../../../../../ext/tags') |
2 | 1 | const types = require('../../../../../ext/types')
|
3 |
| -const kinds = require('../../../../../ext/kinds') |
4 | 2 | const web = require('./web')
|
5 | 3 |
|
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 |
126 | 6 |
|
127 | 7 | module.exports = serverless
|
0 commit comments