@@ -27,48 +27,60 @@ export class AWSServices implements Integration {
27
27
*/
28
28
public name : string = AWSServices . id ;
29
29
30
+ private readonly _optional : boolean ;
31
+
32
+ public constructor ( options : { optional ?: boolean } = { } ) {
33
+ this . _optional = options . optional || false ;
34
+ }
35
+
30
36
/**
31
37
* @inheritDoc
32
38
*/
33
39
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 : 'aws.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
- ) ;
40
+ try {
41
+ const awsModule = require ( 'aws-sdk/global' ) as typeof AWS ;
42
+ fill ( awsModule . Service . prototype , 'makeRequest' , wrapMakeRequest ) ;
43
+ } catch ( e ) {
44
+ if ( ! this . _optional ) {
45
+ throw e ;
46
+ }
47
+ }
69
48
}
70
49
}
71
50
51
+ /** */
52
+ function wrapMakeRequest < TService extends AWSService , TResult > (
53
+ orig : MakeRequestFunction < GenericParams , TResult > ,
54
+ ) : MakeRequestFunction < GenericParams , TResult > {
55
+ return function ( this : TService , operation : string , params ?: GenericParams , callback ?: MakeRequestCallback < TResult > ) {
56
+ let transaction : Transaction | undefined ;
57
+ let span : Span | undefined ;
58
+ const scope = getCurrentHub ( ) . getScope ( ) ;
59
+ if ( scope ) {
60
+ transaction = scope . getTransaction ( ) ;
61
+ }
62
+ const req = orig . call ( this , operation , params ) ;
63
+ req . on ( 'afterBuild' , ( ) => {
64
+ if ( transaction ) {
65
+ span = transaction . startChild ( {
66
+ description : describe ( this , operation , params ) ,
67
+ op : 'aws.request' ,
68
+ } ) ;
69
+ }
70
+ } ) ;
71
+ req . on ( 'complete' , ( ) => {
72
+ if ( span ) {
73
+ span . finish ( ) ;
74
+ }
75
+ } ) ;
76
+
77
+ if ( callback ) {
78
+ req . send ( callback ) ;
79
+ }
80
+ return req ;
81
+ } ;
82
+ }
83
+
72
84
/** Describes an operation on generic AWS service */
73
85
function describe < TService extends AWSService > ( service : TService , operation : string , params ?: GenericParams ) : string {
74
86
let ret = `aws.${ service . serviceIdentifier } .${ operation } ` ;
0 commit comments