|
| 1 | +import { getCurrentHub } from '@sentry/node'; |
| 2 | +import { Integration, Span, Transaction } from '@sentry/types'; |
| 3 | +import { fill } from '@sentry/utils'; |
| 4 | +// 'aws-sdk/global' import is expected to be type-only so it's erased in the final .js file. |
| 5 | +// When TypeScript compiler is upgraded, use `import type` syntax to explicitly assert that we don't want to load a module here. |
| 6 | +import * as AWS from 'aws-sdk/global'; |
| 7 | + |
| 8 | +type GenericParams = { [key: string]: any }; // eslint-disable-line @typescript-eslint/no-explicit-any |
| 9 | +type MakeRequestCallback<TResult> = (err: AWS.AWSError, data: TResult) => void; |
| 10 | +// This interace could be replaced with just type alias once the `strictBindCallApply` mode is enabled. |
| 11 | +interface MakeRequestFunction<TParams, TResult> extends CallableFunction { |
| 12 | + (operation: string, params?: TParams, callback?: MakeRequestCallback<TResult>): AWS.Request<TResult, AWS.AWSError>; |
| 13 | +} |
| 14 | +interface AWSService { |
| 15 | + serviceIdentifier: string; |
| 16 | +} |
| 17 | + |
| 18 | +/** AWS service requests tracking */ |
| 19 | +export class AWSServices implements Integration { |
| 20 | + /** |
| 21 | + * @inheritDoc |
| 22 | + */ |
| 23 | + public static id: string = 'AWSServices'; |
| 24 | + |
| 25 | + /** |
| 26 | + * @inheritDoc |
| 27 | + */ |
| 28 | + public name: string = AWSServices.id; |
| 29 | + |
| 30 | + /** |
| 31 | + * @inheritDoc |
| 32 | + */ |
| 33 | + public setupOnce(): void { |
| 34 | + const awsModule = require('aws-sdk/global') as typeof AWS; |
| 35 | + fill( |
| 36 | + awsModule.Service.prototype, |
| 37 | + 'makeRequest', |
| 38 | + <TService extends AWSService, TResult>( |
| 39 | + orig: MakeRequestFunction<GenericParams, TResult>, |
| 40 | + ): MakeRequestFunction<GenericParams, TResult> => |
| 41 | + function(this: TService, operation: string, params?: GenericParams, callback?: MakeRequestCallback<TResult>) { |
| 42 | + let transaction: Transaction | undefined; |
| 43 | + let span: Span | undefined; |
| 44 | + const scope = getCurrentHub().getScope(); |
| 45 | + if (scope) { |
| 46 | + transaction = scope.getTransaction(); |
| 47 | + } |
| 48 | + const req = orig.call(this, operation, params); |
| 49 | + req.on('afterBuild', () => { |
| 50 | + if (transaction) { |
| 51 | + span = transaction.startChild({ |
| 52 | + description: describe(this, operation, params), |
| 53 | + op: 'request', |
| 54 | + }); |
| 55 | + } |
| 56 | + }); |
| 57 | + req.on('complete', () => { |
| 58 | + if (span) { |
| 59 | + span.finish(); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + if (callback) { |
| 64 | + req.send(callback); |
| 65 | + } |
| 66 | + return req; |
| 67 | + }, |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** Describes an operation on generic AWS service */ |
| 73 | +function describe<TService extends AWSService>(service: TService, operation: string, params?: GenericParams): string { |
| 74 | + let ret = `aws.${service.serviceIdentifier}.${operation}`; |
| 75 | + if (params === undefined) { |
| 76 | + return ret; |
| 77 | + } |
| 78 | + switch (service.serviceIdentifier) { |
| 79 | + case 's3': |
| 80 | + ret += describeS3Operation(operation, params); |
| 81 | + break; |
| 82 | + case 'lambda': |
| 83 | + ret += describeLambdaOperation(operation, params); |
| 84 | + break; |
| 85 | + } |
| 86 | + return ret; |
| 87 | +} |
| 88 | + |
| 89 | +/** Describes an operation on AWS Lambda service */ |
| 90 | +function describeLambdaOperation(_operation: string, params: GenericParams): string { |
| 91 | + let ret = ''; |
| 92 | + if ('FunctionName' in params) { |
| 93 | + ret += ` ${params.FunctionName}`; |
| 94 | + } |
| 95 | + return ret; |
| 96 | +} |
| 97 | + |
| 98 | +/** Describes an operation on AWS S3 service */ |
| 99 | +function describeS3Operation(_operation: string, params: GenericParams): string { |
| 100 | + let ret = ''; |
| 101 | + if ('Bucket' in params) { |
| 102 | + ret += ` ${params.Bucket}`; |
| 103 | + } |
| 104 | + return ret; |
| 105 | +} |
0 commit comments