@@ -38,7 +38,7 @@ import { Segment, Subsegment } from 'aws-xray-sdk-core';
3838 *
3939 * const tracer = new Tracer({ serviceName: 'serverlessAirline' });
4040 *
41- * const lambdaHandler = async (_event: any, _context: any) => {
41+ * const lambdaHandler = async (_event: any, _context: any) => {
4242 * ...
4343 * };
4444 *
@@ -153,20 +153,26 @@ class Tracer extends Utility implements TracerInterface {
153153 * @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-errors
154154 *
155155 * @param error - Error to serialize as metadata
156+ * @param [remote] - Whether the error was thrown by a remote service. Defaults to `false`
156157 */
157- public addErrorAsMetadata ( error : Error ) : void {
158+ public addErrorAsMetadata ( error : Error , remote ?: boolean ) : void {
158159 if ( ! this . isTracingEnabled ( ) ) {
159160 return ;
160161 }
161162
162163 const subsegment = this . getSegment ( ) ;
164+ if ( subsegment === undefined ) {
165+
166+ return ;
167+ }
168+
163169 if ( ! this . captureError ) {
164170 subsegment . addErrorFlag ( ) ;
165171
166172 return ;
167173 }
168174
169- subsegment . addError ( error , false ) ;
175+ subsegment . addError ( error , remote || false ) ;
170176 }
171177
172178 /**
@@ -506,7 +512,7 @@ class Tracer extends Utility implements TracerInterface {
506512 }
507513
508514 /**
509- * Get the active segment or subsegment in the current scope.
515+ * Get the active segment or subsegment (if any) in the current scope.
510516 *
511517 * Usually you won't need to call this method unless you are creating custom subsegments or using manual mode.
512518 *
@@ -525,15 +531,17 @@ class Tracer extends Utility implements TracerInterface {
525531 * }
526532 * ```
527533 *
528- * @returns segment - The active segment or subsegment in the current scope.
534+ * @returns The active segment or subsegment in the current scope. Will log a warning and return `undefined` if no segment is found .
529535 */
530- public getSegment ( ) : Segment | Subsegment {
536+ public getSegment ( ) : Segment | Subsegment | undefined {
531537 if ( ! this . isTracingEnabled ( ) ) {
532538 return new Subsegment ( '## Dummy segment' ) ;
533539 }
534540 const segment = this . provider . getSegment ( ) ;
535541 if ( segment === undefined ) {
536- throw new Error ( 'Failed to get the current sub/segment from the context.' ) ;
542+ console . warn (
543+ 'Failed to get the current sub/segment from the context, this is likely because you are not using the Tracer in a Lambda function.'
544+ ) ;
537545 }
538546
539547 return segment ;
@@ -573,13 +581,7 @@ class Tracer extends Utility implements TracerInterface {
573581 public putAnnotation ( key : string , value : string | number | boolean ) : void {
574582 if ( ! this . isTracingEnabled ( ) ) return ;
575583
576- const document = this . getSegment ( ) ;
577- if ( document instanceof Segment ) {
578- console . warn ( 'You cannot annotate the main segment in a Lambda execution environment' ) ;
579-
580- return ;
581- }
582- document . addAnnotation ( key , value ) ;
584+ this . provider . putAnnotation ( key , value ) ;
583585 }
584586
585587 /**
@@ -606,15 +608,7 @@ class Tracer extends Utility implements TracerInterface {
606608 public putMetadata ( key : string , value : unknown , namespace ?: string | undefined ) : void {
607609 if ( ! this . isTracingEnabled ( ) ) return ;
608610
609- const document = this . getSegment ( ) ;
610- if ( document instanceof Segment ) {
611- console . warn ( 'You cannot add metadata to the main segment in a Lambda execution environment' ) ;
612-
613- return ;
614- }
615-
616- namespace = namespace || this . serviceName ;
617- document . addMetadata ( key , value , namespace ) ;
611+ this . provider . putMetadata ( key , value , namespace || this . serviceName ) ;
618612 }
619613
620614 /**
0 commit comments