import * as ecs from '@aws-cdk/aws-ecs' ; import * as iam from '@aws-cdk/aws-iam' ; import * as cdk from '@aws-cdk/core' ; import { Service } from '../service' ; import { ServiceExtension } from './extension-interfaces' ; import { Construct } from '@aws-cdk/core' ; const XRAY_DAEMON_IMAGE = 'amazon/aws-xray-daemon:latest' ; /**
 * This extension adds an X-Ray daemon inside the task definition for
 * capturing application trace spans and submitting them to the AWS
 * X-Ray service.
 */ export class XRayExtension extends ServiceExtension { constructor ( ) { super ( 'xray' ) ; } public prehook ( service , scope ) { this . parentService = service ; } public useTaskDefinition ( taskDefinition ) { this . container = taskDefinition . addContainer ( 'xray' , { image : ecs . ContainerImage . fromRegistry ( XRAY_DAEMON_IMAGE ) , essential : true , memoryReservationMiB : 256 , environment : { AWS_REGION : cdk . Stack . of ( this . parentService ) . region , } , healthCheck : { command : [ 'CMD-SHELL' , 'curl -s http://localhost:2000' , ] , startPeriod : cdk . Duration . seconds ( 10 ) , interval : cdk . Duration . seconds ( 5 ) , timeout : cdk . Duration . seconds ( 2 ) , retries : 3 , } , logging : new ecs . AwsLogDriver ( { streamPrefix : 'xray' } ) , user : '1337' , } ) ; taskDefinition . taskRole . addManagedPolicy ( iam . ManagedPolicy . fromAwsManagedPolicyName ( 'AWSXRayDaemonWriteAccess' ) ) ; } public resolveContainerDependencies ( ) { if ( ! this . container ) { throw new Error ( 'The container dependency hook was called before the container was created' ) ; } const appmeshextension = this . parentService . serviceDescription . get ( 'appmesh' ) ; if ( appmeshextension && appmeshextension . container ) { this . container . addContainerDependencies ( { container : appmeshextension . container , condition : ecs . ContainerDependencyCondition . HEALTHY , } ) ; } } }