|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and other contributors where applicable. |
| 3 | + * Licensed under the BSD 2-Clause License; you may not use this file except in |
| 4 | + * compliance with the BSD 2-Clause License. |
| 5 | + */ |
| 6 | + |
| 7 | +'use strict' |
| 8 | + |
| 9 | +const constants = require('../../../constants') |
| 10 | +const NAME = 'S3' |
| 11 | +const TYPE = 'storage' |
| 12 | +const SUBTYPE = 's3' |
| 13 | +const elasticAPMStash = Symbol('elasticAPMStash') |
| 14 | + |
| 15 | +/** |
| 16 | + * Gets the region from the ARN |
| 17 | + * |
| 18 | + * @param {String} s3Arn |
| 19 | + * @returns {String} |
| 20 | + */ |
| 21 | +function regionFromS3Arn (s3Arn) { |
| 22 | + return s3Arn.split(':')[3] |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Return an APM "resource" string for the bucket, Access Point ARN, or Outpost |
| 27 | + * ARN. ARNs are normalized to a shorter resource name. |
| 28 | + * Known ARN patterns: |
| 29 | + * - arn:aws:s3:<region>:<account-id>:accesspoint/<accesspoint-name> |
| 30 | + * - arn:aws:s3-outposts:<region>:<account>:outpost/<outpost-id>/bucket/<bucket-name> |
| 31 | + * - arn:aws:s3-outposts:<region>:<account>:outpost/<outpost-id>/accesspoint/<accesspoint-name> |
| 32 | + * |
| 33 | + * In general that is: |
| 34 | + * arn:$partition:$service:$region:$accountId:$resource |
| 35 | + * |
| 36 | + * This parses using the same "split on colon" used by the JavaScript AWS SDK v3. |
| 37 | + * https://github.com/aws/aws-sdk-js-v3/blob/v3.18.0/packages/util-arn-parser/src/index.ts#L14-L37 |
| 38 | + * |
| 39 | + * @param {String} bucket The bucket string |
| 40 | + * @returns {String | null} |
| 41 | + */ |
| 42 | +function resourceFromBucket (bucket) { |
| 43 | + let resource = null |
| 44 | + if (bucket) { |
| 45 | + resource = bucket |
| 46 | + if (resource.startsWith('arn:')) { |
| 47 | + resource = bucket.split(':').slice(5).join(':') |
| 48 | + } |
| 49 | + } |
| 50 | + return resource |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Returns middlewares to instrument an S3Client instance |
| 55 | + * |
| 56 | + * @param {import('@aws-sdk/client-s3').S3Client} client |
| 57 | + * @param {any} agent |
| 58 | + * @returns {import('./smithy-client').AWSMiddlewareEntry[]} |
| 59 | + */ |
| 60 | +function s3MiddlewareFactory (client, agent) { |
| 61 | + return [ |
| 62 | + { |
| 63 | + middleware: (next, context) => async (args) => { |
| 64 | + const input = args.input |
| 65 | + const bucket = input && input.Bucket |
| 66 | + const resource = resourceFromBucket(bucket) |
| 67 | + const span = agent._instrumentation.currSpan() |
| 68 | + |
| 69 | + if (!span) { |
| 70 | + return await next(args) |
| 71 | + } |
| 72 | + // The given span comes with the operation name and we need to |
| 73 | + // add the resource if applies |
| 74 | + if (resource) { |
| 75 | + span.name += ' ' + resource |
| 76 | + span.setServiceTarget('s3', resource) |
| 77 | + } |
| 78 | + |
| 79 | + // As for now OTel spec defines attributes for operations that require a Bucket |
| 80 | + // if that changes we should review this guard |
| 81 | + // https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/semantic_conventions/trace/instrumentation/aws-sdk.yml#L435 |
| 82 | + if (bucket) { |
| 83 | + const otelAttrs = span._getOTelAttributes() |
| 84 | + |
| 85 | + otelAttrs['aws.s3.bucket'] = bucket |
| 86 | + |
| 87 | + if (input.Key) { |
| 88 | + otelAttrs['aws.s3.key'] = input.Key |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + let err |
| 93 | + let result |
| 94 | + let response |
| 95 | + let statusCode |
| 96 | + try { |
| 97 | + result = await next(args) |
| 98 | + response = result && result.response |
| 99 | + statusCode = response && response.statusCode |
| 100 | + } catch (ex) { |
| 101 | + // Save the error for use in `finally` below, but re-throw it to |
| 102 | + // not impact code flow. |
| 103 | + err = ex |
| 104 | + |
| 105 | + // This code path happens with a GetObject conditional request |
| 106 | + // that returns a 304 Not Modified. |
| 107 | + statusCode = err && err.$metadata && err.$metadata.httpStatusCode |
| 108 | + throw ex |
| 109 | + } finally { |
| 110 | + if (statusCode) { |
| 111 | + span._setOutcomeFromHttpStatusCode(statusCode) |
| 112 | + } else { |
| 113 | + span._setOutcomeFromErrorCapture(constants.OUTCOME_FAILURE) |
| 114 | + } |
| 115 | + if (err && (!statusCode || statusCode >= 400)) { |
| 116 | + agent.captureError(err, { skipOutcome: true }) |
| 117 | + } |
| 118 | + |
| 119 | + // Set the httpContext |
| 120 | + if (statusCode) { |
| 121 | + const httpContext = { |
| 122 | + status_code: statusCode |
| 123 | + } |
| 124 | + |
| 125 | + if (response && response.headers && response.headers['content-length']) { |
| 126 | + const encodedBodySize = Number(response.headers['content-length']) |
| 127 | + if (!isNaN(encodedBodySize)) { |
| 128 | + httpContext.response = { encoded_body_size: encodedBodySize } |
| 129 | + } |
| 130 | + } |
| 131 | + span.setHttpContext(httpContext) |
| 132 | + } |
| 133 | + |
| 134 | + // Configuring `new S3Client({useArnRegion:true})` allows one to |
| 135 | + // use an Access Point bucket ARN for a region *other* than the |
| 136 | + // one for which the client is configured. Therefore, we attempt |
| 137 | + // to get the bucket region from the ARN first. |
| 138 | + const config = client.config |
| 139 | + let useArnRegion |
| 140 | + if (typeof config.useArnRegion === 'boolean') { |
| 141 | + useArnRegion = config.useArnRegion |
| 142 | + } else { |
| 143 | + useArnRegion = await config.useArnRegion() |
| 144 | + } |
| 145 | + |
| 146 | + let region |
| 147 | + if (useArnRegion && bucket && bucket.startsWith('arn:')) { |
| 148 | + region = regionFromS3Arn(args.input.Bucket) |
| 149 | + } else { |
| 150 | + region = typeof config.region === 'boolean' ? region : await config.region() |
| 151 | + } |
| 152 | + |
| 153 | + // Destination context. |
| 154 | + const destContext = { |
| 155 | + address: context[elasticAPMStash].hostname, |
| 156 | + port: context[elasticAPMStash].port, |
| 157 | + service: { |
| 158 | + name: SUBTYPE, |
| 159 | + type: TYPE |
| 160 | + } |
| 161 | + } |
| 162 | + if (resource) { |
| 163 | + destContext.service.resource = resource |
| 164 | + } |
| 165 | + |
| 166 | + if (region) { |
| 167 | + destContext.cloud = { region } |
| 168 | + } |
| 169 | + span._setDestinationContext(destContext) |
| 170 | + |
| 171 | + span.end() |
| 172 | + } |
| 173 | + |
| 174 | + return result |
| 175 | + }, |
| 176 | + options: { step: 'initialize', priority: 'high', name: 'elasticAPMSpan' } |
| 177 | + }, |
| 178 | + { |
| 179 | + middleware: (next, context) => async (args) => { |
| 180 | + const req = args.request |
| 181 | + let port = req.port |
| 182 | + |
| 183 | + // Resolve port for HTTP(S) protocols |
| 184 | + if (port === undefined) { |
| 185 | + if (req.protocol === 'https:') { |
| 186 | + port = 443 |
| 187 | + } else if (req.protocol === 'http:') { |
| 188 | + port = 80 |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + context[elasticAPMStash] = { |
| 193 | + protocol: req.protocol, |
| 194 | + hostname: req.hostname, |
| 195 | + port: port |
| 196 | + } |
| 197 | + return next(args) |
| 198 | + }, |
| 199 | + options: { step: 'finalizeRequest', name: 'elasticAPMHTTPInfo' } |
| 200 | + } |
| 201 | + ] |
| 202 | +} |
| 203 | + |
| 204 | +module.exports = { |
| 205 | + S3_NAME: NAME, |
| 206 | + S3_TYPE: TYPE, |
| 207 | + S3_SUBTYPE: SUBTYPE, |
| 208 | + s3MiddlewareFactory |
| 209 | +} |
0 commit comments