99 callback | 1 | 2
1010 */
1111import { BasePlugin } from "@opentelemetry/core" ;
12- import { Span } from "@opentelemetry/api" ;
12+ import { Span , CanonicalCode } from "@opentelemetry/api" ;
1313import * as shimmer from "shimmer" ;
1414import AWS from "aws-sdk" ;
1515import { AttributeNames } from "./enums" ;
1616import {
1717 getRequestServiceAttributes ,
1818 getResponseServiceAttributes ,
1919} from "./services" ;
20+ import { AwsSdkPluginConfig } from "./types" ;
2021
2122const VERSION = "0.0.3" ;
2223
2324class AwsPlugin extends BasePlugin < typeof AWS > {
2425 readonly component : string ;
26+ protected _config : AwsSdkPluginConfig ;
2527 private activeRequests : Set < AWS . Request < any , any > > = new Set ( ) ;
2628
2729 constructor ( readonly moduleName : string ) {
@@ -60,13 +62,12 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
6062
6163 private _getPatchedRequestMethod = ( original : Function ) => {
6264 const thisPlugin = this ;
63-
6465 return function ( ) {
6566 let span : Span | null = null ;
6667 /*
67- if the span was already started, we don't want to start a new one
68- when Request.promise() is called
69- */
68+ if the span was already started, we don't want to start a new one
69+ when Request.promise() is called
70+ */
7071
7172 if (
7273 this . _asm . currentState !== "complete" &&
@@ -78,7 +79,6 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
7879 attributes : {
7980 [ AttributeNames . COMPONENT ] : thisPlugin . moduleName ,
8081 [ AttributeNames . AWS_OPERATION ] : this . operation ,
81- [ AttributeNames . AWS_PARAMS ] : JSON . stringify ( this . params ) ,
8282 [ AttributeNames . AWS_SIGNATURE_VERSION ] : this . service ?. config
8383 ?. signatureVersion ,
8484 [ AttributeNames . AWS_REGION ] : this . service ?. config ?. region ,
@@ -90,6 +90,14 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
9090 } ,
9191 } ) ;
9292
93+ if ( thisPlugin . _config ?. preRequestHook ) {
94+ thisPlugin . _safeExecute (
95+ span ,
96+ ( ) => thisPlugin . _config . preRequestHook ( span , this ) ,
97+ false
98+ ) ;
99+ }
100+
93101 ( this as AWS . Request < any , any > ) . on ( "complete" , ( response ) => {
94102 if ( thisPlugin . activeRequests . has ( this ) ) {
95103 thisPlugin . activeRequests . delete ( this ) ;
@@ -121,6 +129,33 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
121129 request . operation
122130 } `;
123131 } ;
132+
133+ private _safeExecute <
134+ T extends ( ...args : unknown [ ] ) => ReturnType < T > ,
135+ K extends boolean
136+ > (
137+ span : Span ,
138+ execute : T ,
139+ rethrow : K
140+ ) : K extends true ? ReturnType < T > : ReturnType < T > | void ;
141+ private _safeExecute < T extends ( ...args : unknown [ ] ) => ReturnType < T > > (
142+ span : Span ,
143+ execute : T ,
144+ rethrow : boolean
145+ ) : ReturnType < T > | void {
146+ try {
147+ return execute ( ) ;
148+ } catch ( error ) {
149+ if ( rethrow ) {
150+ span . setStatus ( {
151+ code : CanonicalCode . UNKNOWN ,
152+ } ) ;
153+ span . end ( ) ;
154+ throw error ;
155+ }
156+ this . _logger . error ( "caught error " , error ) ;
157+ }
158+ }
124159}
125160
126161export const plugin = new AwsPlugin ( "aws-sdk" ) ;
0 commit comments