|
| 1 | +/* |
| 2 | + * Copyright 2025 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +const constants = require('../constants') |
| 7 | +const createMapper = require('./utils') |
| 8 | +const { DESTINATIONS } = require('../../config/attribute-filter') |
| 9 | + |
| 10 | +// These mappings are compliant with v1.23.0 and have mappings with v1.20.0 of semantic conventions |
| 11 | +// https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md |
| 12 | + |
| 13 | +const attrMappings = { |
| 14 | + clientHost: { |
| 15 | + attrs: [constants.ATTR_SERVER_ADDRESS, constants.ATTR_NET_PEER_NAME], |
| 16 | + mapping() { |
| 17 | + return () => {} |
| 18 | + } |
| 19 | + }, |
| 20 | + clientPort: { |
| 21 | + attrs: [constants.ATTR_SERVER_PORT, constants.ATTR_NETWORK_PEER_PORT, constants.ATTR_NET_PEER_PORT], |
| 22 | + mapping() { |
| 23 | + return () => {} |
| 24 | + } |
| 25 | + }, |
| 26 | + clientUrl: { |
| 27 | + attrs: [constants.ATTR_FULL_URL, constants.ATTR_HTTP_URL], |
| 28 | + mapping() { |
| 29 | + return () => {} |
| 30 | + } |
| 31 | + }, |
| 32 | + grpcStatusCode: { |
| 33 | + attrs: [constants.ATTR_GRPC_STATUS_CODE], |
| 34 | + mapping({ segment, transaction }) { |
| 35 | + return (value) => { |
| 36 | + transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_COMMON, 'response.status', value) |
| 37 | + segment.addAttribute(constants.ATTR_GRPC_STATUS_CODE, value) |
| 38 | + } |
| 39 | + } |
| 40 | + }, |
| 41 | + rpcMethod: { |
| 42 | + attrs: [constants.ATTR_RPC_METHOD], |
| 43 | + mapping({ transaction }) { |
| 44 | + return (value) => transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_COMMON, 'request.method', value) |
| 45 | + } |
| 46 | + }, |
| 47 | + rpcSystem: { |
| 48 | + attrs: [constants.ATTR_RPC_SYSTEM], |
| 49 | + mapping({ segment }) { |
| 50 | + return (value) => segment.addAttribute('component', value) |
| 51 | + } |
| 52 | + }, |
| 53 | + rpcService: { |
| 54 | + attrs: [constants.ATTR_RPC_SERVICE], |
| 55 | + }, |
| 56 | + host: { |
| 57 | + attrs: [constants.ATTR_SERVER_ADDRESS, constants.ATTR_NET_HOST_NAME], |
| 58 | + mapping({ segment }) { |
| 59 | + return (value) => { |
| 60 | + segment.addAttribute('host', value) |
| 61 | + } |
| 62 | + } |
| 63 | + }, |
| 64 | + method: { |
| 65 | + attrs: [constants.ATTR_HTTP_REQUEST_METHOD, constants.ATTR_HTTP_METHOD], |
| 66 | + mapping({ transaction }) { |
| 67 | + return (value) => { |
| 68 | + if (transaction) { |
| 69 | + transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_COMMON, 'request.method', value) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + }, |
| 74 | + port: { |
| 75 | + attrs: [constants.ATTR_SERVER_PORT, constants.ATTR_NET_HOST_PORT], |
| 76 | + mapping({ segment }) { |
| 77 | + return (value) => { |
| 78 | + segment.addAttribute('port', value) |
| 79 | + } |
| 80 | + } |
| 81 | + }, |
| 82 | + route: { |
| 83 | + attrs: [constants.ATTR_HTTP_ROUTE], |
| 84 | + mapping({ segment, transaction }) { |
| 85 | + return (value) => { |
| 86 | + transaction.nameState.appendPath(value) |
| 87 | + segment.addAttribute('http.route', value) |
| 88 | + } |
| 89 | + } |
| 90 | + }, |
| 91 | + statusCode: { |
| 92 | + attrs: [constants.ATTR_HTTP_RES_STATUS_CODE, constants.ATTR_HTTP_STATUS_CODE], |
| 93 | + mapping({ segment, transaction }) { |
| 94 | + return (value) => { |
| 95 | + if (segment) { |
| 96 | + segment.addAttribute('http.statusCode', value) |
| 97 | + } else { |
| 98 | + transaction.statusCode = value |
| 99 | + transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_COMMON, 'http.statusCode', value) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + }, |
| 104 | + statusText: { |
| 105 | + attrs: [constants.ATTR_HTTP_STATUS_TEXT], |
| 106 | + mapping({ segment, transaction }) { |
| 107 | + return (value) => { |
| 108 | + if (segment) { |
| 109 | + segment.addAttribute('http.statusText', value) |
| 110 | + } else { |
| 111 | + transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_COMMON, 'http.statusText', value) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + }, |
| 116 | + url: { |
| 117 | + attrs({ span }) { |
| 118 | + let value |
| 119 | + if (span.attributes[constants.ATTR_HTTP_URL]) { |
| 120 | + value = span.attributes[constants.ATTR_HTTP_URL] |
| 121 | + } else { |
| 122 | + const protocol = span.attributes[constants.ATTR_URL_SCHEME] ?? 'https' |
| 123 | + const host = span.attributes[constants.ATTR_SERVER_ADDRESS] ?? 'unknown' |
| 124 | + const port = span.attributes[constants.ATTR_SERVER_PORT] |
| 125 | + const path = span.attributes[constants.ATTR_URL_PATH] ?? '/unknown' |
| 126 | + const qp = span.attributes[constants.ATTR_URL_QUERY] |
| 127 | + value = `${protocol}://${host}` |
| 128 | + if (port) { |
| 129 | + value += `:${port}` |
| 130 | + } |
| 131 | + value += path |
| 132 | + if (qp) { |
| 133 | + value += qp |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return value |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +const { getAttr: httpAttr, attributesMapper } = createMapper(attrMappings) |
| 143 | + |
| 144 | +function rpcMapper({ segment, transaction }) { |
| 145 | + const statusMapping = attributesMapper({ key: 'grpcStatusCode', segment, transaction }) |
| 146 | + const systemMapping = attributesMapper({ key: 'rpcSystem', segment }) |
| 147 | + const rpcMethodMapping = attributesMapper({ key: 'rpcMethod', transaction }) |
| 148 | + return { |
| 149 | + ...rpcMethodMapping, |
| 150 | + ...statusMapping, |
| 151 | + ...systemMapping |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +function serverMapper({ segment, transaction }) { |
| 156 | + // TODO: if route params are available, assign them as well |
| 157 | + const methodMapping = attributesMapper({ key: 'method', transaction }) |
| 158 | + const statusCodeMapping = attributesMapper({ key: 'statusCode', transaction }) |
| 159 | + const statusTextMapping = attributesMapper({ key: 'statusText', transaction }) |
| 160 | + const serverPortMapping = attributesMapper({ key: 'port', segment }) |
| 161 | + const httpRouteMapping = attributesMapper({ key: 'route', segment, transaction }) |
| 162 | + const httpHostMapping = attributesMapper({ key: 'host', segment, transaction }) |
| 163 | + return { |
| 164 | + ...httpHostMapping, |
| 165 | + ...httpRouteMapping, |
| 166 | + ...methodMapping, |
| 167 | + ...serverPortMapping, |
| 168 | + ...statusCodeMapping, |
| 169 | + ...statusTextMapping |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +function clientMapper({ segment }) { |
| 174 | + const statusCodeMapping = attributesMapper({ key: 'statusCode', segment }) |
| 175 | + const statusTextMapping = attributesMapper({ key: 'statusText', segment }) |
| 176 | + const clientHostMapping = attributesMapper({ key: 'clientHost', segment }) |
| 177 | + const clientUrlMapping = attributesMapper({ key: 'clientUrl', segment }) |
| 178 | + const clientPortMapping = attributesMapper({ key: 'clientPort', segment }) |
| 179 | + return { |
| 180 | + ...clientHostMapping, |
| 181 | + ...clientPortMapping, |
| 182 | + ...clientUrlMapping, |
| 183 | + ...statusCodeMapping, |
| 184 | + ...statusTextMapping |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +module.exports = { |
| 189 | + clientMapper, |
| 190 | + httpAttr, |
| 191 | + rpcMapper, |
| 192 | + serverMapper, |
| 193 | +} |
0 commit comments