Introduce X-Ray for primary-replica

Bug: Issue 14897
Change-Id: Iea9568d57ee3831d1f6d2c2d7bc1c1350493fead
diff --git a/primary-replica/Makefile b/primary-replica/Makefile
index fce2cb8..1bd3d25 100644
--- a/primary-replica/Makefile
+++ b/primary-replica/Makefile
@@ -58,7 +58,8 @@
 		$(GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_CAPACITY) \
 		$(GERRIT_OPTIONAL_PARAMS_REPLICA_CAPACITY_PROVIDER)
 
-service-primary: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-ldap-account-pattern set-optional-gerrit-ulimits set-optional-jgit-conf
+service-primary: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-ldap-account-pattern set-optional-gerrit-ulimits set-optional-jgit-conf \
+					set-optional-x-ray
 ifdef LOAD_BALANCER_SCHEME
 		$(eval PRIMARY_SERVICE_OPTIONAL_PARAMS := $(PRIMARY_SERVICE_OPTIONAL_PARAMS) ParameterKey=LoadBalancerScheme,ParameterValue=$(LOAD_BALANCER_SCHEME))
 endif
@@ -99,14 +100,16 @@
 		$(PRIMARY_SERVICE_OPTIONAL_PARAMS) \
 		$(METRICS_CW_OPTIONAL_PARAMS) \
 		$(SMTP_OPTIONAL_PARAMS) \
-		$(GERRIT_ULIMITS)
+		$(GERRIT_ULIMITS) \
+		$(XRAY_OPTIONAL_PARAMS)
 
 
 service-replica: set-optional-params-metrics-cloudwatch \
 					set-ldap-account-pattern \
 					set-optional-gerrit-ulimits set-optional-jgit-conf \
 					set-optional-params-for-replica-auto-scaling-capacity \
-					set-optional-params-for-replica-auto-scaling-policy
+					set-optional-params-for-replica-auto-scaling-policy \
+					set-optional-x-ray
 
 ifdef LOAD_BALANCER_SCHEME
 		$(eval REPLICA_SERVICE_OPTIONAL_PARAMS := $(REPLICA_SERVICE_OPTIONAL_PARAMS) ParameterKey=LoadBalancerScheme,ParameterValue=$(LOAD_BALANCER_SCHEME))
@@ -144,7 +147,8 @@
 		$(METRICS_CW_OPTIONAL_PARAMS) \
 		$(GERRIT_ULIMITS) \
 		$(GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_CAPACITY) \
-		$(GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_POLICY)
+		$(GERRIT_OPTIONAL_PARAMS_REPLICA_AUTO_SCALING_POLICY) \
+		$(XRAY_OPTIONAL_PARAMS)
 
 dns-routing:
 	$(AWS_FC_COMMAND) create-stack \
diff --git a/primary-replica/cf-service-primary.yml b/primary-replica/cf-service-primary.yml
index 6dc2a74..d1fb22c 100644
--- a/primary-replica/cf-service-primary.yml
+++ b/primary-replica/cf-service-primary.yml
@@ -176,8 +176,17 @@
     Description: Comma separated list of regex patterns to exclude metrics reported to CloudWatch
     Type: CommaDelimitedList
     Default: ''
+  EnableXray:
+    Description: Whether to enable X-Ray tracing for Gerrit
+    Type: String
+    Default: false
+    AllowedValues: [true, false]
 
 Mappings:
+  XRay:
+    Info:
+      ContainerName: xray-daemon
+      Port: 2000
   Gerrit:
     Volume:
       Git: gerrit-git
@@ -187,6 +196,9 @@
       Db: gerrit-db
       Logs: gerrit-logs
 
+Conditions:
+  ShouldEnableXRay: !Equals [!Ref EnableXray, true]
+
 Resources:
     Service:
         Type: AWS::ECS::Service
@@ -222,6 +234,12 @@
                   Essential: true
                   Image: !Sub '${DockerRegistryUrl}/${DockerImage}'
                   Environment:
+                    - Name: XRAY_ENABLED
+                      Value: !Ref EnableXray
+                    - Name: AWS_XRAY_TRACING_NAME
+                      Value: !Ref InstanceId
+                    - Name: AWS_XRAY_DAEMON_ADDRESS
+                      Value: !Join [':', [!FindInMap ['XRay', 'Info', 'ContainerName'], !FindInMap ['XRay', 'Info', 'Port']]]
                     - Name: CANONICAL_WEB_URL
                       Value: !Sub 'https://${HttpSubdomain}.${HostedZoneName}'
                     - Name: SSHD_ADVERTISED_ADDRESS
@@ -320,6 +338,20 @@
                         awslogs-group: !Ref ClusterStackName
                         awslogs-region: !Ref AWS::Region
                         awslogs-stream-prefix: !Ref EnvironmentName
+                  Links:
+                    - !FindInMap ['XRay', 'Info', 'ContainerName']
+                - Fn::If:
+                    - ShouldEnableXRay
+                    - Name: !FindInMap ['XRay', 'Info', 'ContainerName']
+                      Essential: false
+                      Image: "amazon/aws-xray-daemon"
+                      Cpu: 32
+                      MemoryReservation: 256
+                      PortMappings:
+                        - HostPort: 2000
+                          ContainerPort: 2000
+                          Protocol: "udp"
+                    - AWS::NoValue
             Volumes:
               - Name: !FindInMap ['Gerrit', 'Volume', 'Db']
                 Host:
diff --git a/primary-replica/cf-service-replica.yml b/primary-replica/cf-service-replica.yml
index 281db8b..a57270d 100644
--- a/primary-replica/cf-service-replica.yml
+++ b/primary-replica/cf-service-replica.yml
@@ -213,6 +213,20 @@
     Type: Number
     Description: Aggregate CPU utilization target for auto-scaling
     Default: 75.0
+  EnableXray:
+    Description: Whether to enable X-Ray tracing for replicas
+    Type: String
+    Default: false
+    AllowedValues: [true, false]
+
+Mappings:
+  XRay:
+    Info:
+      ContainerName: xray-daemon
+      Port: 2000
+
+Conditions:
+  ShouldEnableXRay: !Equals [!Ref EnableXray, true]
 
 Resources:
     GerritService:
@@ -260,6 +274,12 @@
                   Essential: true
                   Image: !Sub '${DockerRegistryUrl}/${GerritDockerImage}'
                   Environment:
+                    - Name: XRAY_ENABLED
+                      Value: !Ref EnableXray
+                    - Name: AWS_XRAY_TRACING_NAME
+                      Value: !Ref InstanceId
+                    - Name: AWS_XRAY_DAEMON_ADDRESS
+                      Value: !Join [':', [!FindInMap ['XRay', 'Info', 'ContainerName'], !FindInMap ['XRay', 'Info', 'Port']]]
                     - Name: CANONICAL_WEB_URL
                       Value: !Sub 'https://${HttpSubdomain}.${HostedZoneName}'
                     - Name: SSHD_ADVERTISED_ADDRESS
@@ -334,6 +354,8 @@
                         awslogs-group: !Ref ClusterStackName
                         awslogs-region: !Ref AWS::Region
                         awslogs-stream-prefix: !Ref EnvironmentName
+                  Links:
+                    - !FindInMap ['XRay', 'Info', 'ContainerName']
                 - Name: !Ref GitDaemonServiceName
                   Essential: true
                   Image: !Sub '${DockerRegistryUrl}/${GitDaemonDockerImage}'
@@ -379,6 +401,18 @@
                         awslogs-group: !Ref ClusterStackName
                         awslogs-region: !Ref AWS::Region
                         awslogs-stream-prefix: !Ref EnvironmentName
+                - Fn::If:
+                    - ShouldEnableXRay
+                    - Name: !FindInMap ['XRay', 'Info', 'ContainerName']
+                      Essential: false
+                      Image: "amazon/aws-xray-daemon"
+                      Cpu: 32
+                      MemoryReservation: 256
+                      PortMappings:
+                        - HostPort: 2000
+                          ContainerPort: 2000
+                          Protocol: "udp"
+                    - AWS::NoValue
             Volumes:
               - Name: !Ref 'GerritDbVolume'
                 DockerVolumeConfiguration:
diff --git a/primary-replica/setup.env.template b/primary-replica/setup.env.template
index 4aade05..7862813 100644
--- a/primary-replica/setup.env.template
+++ b/primary-replica/setup.env.template
@@ -80,4 +80,6 @@
 # Existing SUBNET2 settings
 SUBNET2_CIDR=
 SUBNET2_ID=
-SUBNET2_AZ=
\ No newline at end of file
+SUBNET2_AZ=
+
+XRAY_ENABLED=false
\ No newline at end of file