# 2.1.0 Instrument X-Ray

## 0. **Learning Materials**

* Video: [Week 2 Instrument XRay](https://www.youtube.com/watch?v=n2DTsuBrD_A\&list=PLBfufR7vyJJ7k25byhRXJldB5AiwgNnWv\&index=30\&ab_channel=ExamPro)
* Andrew's repo: [week-2-xray](https://github.com/omenking/aws-bootcamp-cruddur-2023/tree/week-2-xray)
* My branch repo: [02-01-instrument-xray](https://github.com/mariachiinajar/aws-bootcamp-cruddur-2023-again/tree/02-01-instrument-xray)
  * My commits:
    * [#9](https://github.com/mariachiinajar/aws-bootcamp-cruddur-2023-again/issues/9) [Instrumented X-ray (and container)](https://github.com/mariachiinajar/aws-bootcamp-cruddur-2023-again/commit/1b98ea63c3238ab5a0016b99174028e305edc01c)
    * [#9](https://github.com/mariachiinajar/aws-bootcamp-cruddur-2023-again/issues/9) [Implemented custom x-ray segment](https://github.com/mariachiinajar/aws-bootcamp-cruddur-2023-again/commit/def86e4f46123ebe9189d0010d785c3429fb024f)

### Task List

* [ ] Install AWS SDK
* [ ] Add X-ray daemon to Docker Compose
  * [ ] Add backend variables to Docker Compose

### Env variables

<table><thead><tr><th width="168">Micro service</th><th>Variables</th></tr></thead><tbody><tr><td>backend</td><td><ul><li><code>HONEYCOMB_API_KEY</code></li><li><code>OTEL_SERVICE_NAME</code></li><li><code>OTEL_EXPORTER_OTLP_ENDPOINT</code></li><li><code>OTEL_EXPORTER_OTLP_HEADERS</code></li><li><code>AWS_XRAY_URL</code></li><li><code>AWS_XRAY_DAEMON_ADDRESS</code></li></ul></td></tr><tr><td>x-ray daemon</td><td><ul><li><strong><code>AWS_ACCESS_KEY_ID</code></strong></li><li><strong><code>AWS_SECRET_ACCESS_KEY</code></strong></li><li><strong><code>AWS_REGION</code></strong></li></ul></td></tr></tbody></table>

***

## 1. Workflow

### 1.1 Install AWS SDK

* Install AWS SDK.

<pre class="language-diff" data-title="requirements.txt"><code class="lang-diff"><strong>+ aws-xray-sdk
</strong></code></pre>

* Then run:
  * `pip install -r requirements.txt`
* Create a json file for samling rules.
  * This is necessary because the X-Ray tracing can become expensive very quickly so we are reducing the amount of tgracing analysis by sampling only a limited amount.

```
cd aws
code xray.json
```



{% code title="xray.json" %}
```json
{
    "SamplingRule": {
        "RuleName": "Cruddur",
        "ResourceARN": "*",
        "Priority": 9000,
        "FixedRate": 0.1,
        "ReservoirSize": 5,
        "ServiceName": "backend-flask",
        "ServiceType": "*", 
        "Host": "*",
        "HTTPMethod": "*",
        "URLPath": "*",
        "Version": 1
    }
}
```
{% endcode %}

<figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FHoEeulKU6NSCuObN248g%2Fimage.png?alt=media&#x26;token=7b234d05-7f85-4463-ba81-d5265f36257e" alt=""><figcaption></figcaption></figure>







```json
aws xray create-group \
    --group-name "Cruddur" \ 
    --filter-expression "service(\"$FLASK_ADDRESS\" {fault OR error}"
```

<div data-full-width="true"><figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FpzwHa83Snoc7H4bdr2Ia%2Fimage.png?alt=media&#x26;token=00f96141-1955-4769-a7d7-155488e51626" alt=""><figcaption></figcaption></figure></div>

<div data-full-width="true"><figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2F7x2BQZ5HKPOGCMv81yJi%2Fimage.png?alt=media&#x26;token=5dc514a8-3f25-4926-a761-d1da18549e5f" alt=""><figcaption></figcaption></figure></div>



### 1.2 Add X-ray daemon to DC (Docker Compose)

{% code title="docker-compose.yml" lineNumbers="true" %}
```yaml
...
  xray-daemon:
    image: "amazon/aws-xray-daemon"
    environment:
      AWS_ACCESS_KEY_ID: "${AWS_ACCESS_KEY_ID}"
      AWS_SECRET_ACCESS_KEY: "${AWS_SECRET_ACCESS_KEY}"
      AWS_REGION: "us-east-1"
    command:
      - "xray -o -b xray-daemon:2000"

...
```
{% endcode %}

* **line 9**: run `xray` on port 2000.&#x20;
  * `-o`: local mode. :maple\_leaf: _By default, X-Ray thinks that it's running on an AWS EC2 instance and tries to grab the metadata from the instance_. The `-o` tag instructs X-ray to not check for EC2 instance metadata.
  * `-b`: Overrides default UDP address (127.0.0.1:2000).

### 1.2.1 Add backend variables in DC

Every time we add a container definition to the `docker-compose` file or we use env variables in the code base, it is important to add the env variables in the `docker-compose` file.

{% code title="docker-compose.yml" lineNumbers="true" fullWidth="true" %}
```
        AWS_XRAY_URL: "*4567-${GITPOD_WORKSPACE_ID}.${GITPOD_WORKSPACE_CLUSTER_HOST}"
        AWS_XRAY_DAEMON_ADDRESS: "xray-daemon:2000"
```
{% endcode %}

<div data-full-width="true"><figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2Fq0kCjWvBwruXwe1MLE0W%2Fimage.png?alt=media&#x26;token=45014694-0027-4bae-bed7-e96a1fe4490f" alt=""><figcaption></figcaption></figure></div>

* We can [commit](https://github.com/omenking/aws-bootcamp-cruddur-2023/commit/1b98ea63c3238ab5a0016b99174028e305edc01c) the implementation at this point.&#x20;

## 1.2 Troubleshooting



<div data-full-width="true"><figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FMGRXq21YcoOdl066lgEm%2Fimage.png?alt=media&#x26;token=1ecbb62a-3c7b-460d-a385-cd8d7b9a5be5" alt=""><figcaption><p>The Docker container seems to be running. Inside the contianer, I find that the AWS credentials are not correctly injected into the container. </p></figcaption></figure></div>

<div data-full-width="true"><figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FZnta1ErOlaarm4joHGWE%2Fimage.png?alt=media&#x26;token=083c2044-02d4-453a-867c-46d136fb25ba" alt=""><figcaption><p>I get no data on X-Ray console. Something must be wrong. </p></figcaption></figure></div>

<figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FmyZVUGko4XHVkxI98hS1%2Fimage.png?alt=media&#x26;token=5e69b358-d375-459b-a4cf-ae9b4acd7cad" alt=""><figcaption><p>No credentials provided</p></figcaption></figure>

<div data-full-width="true"><figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FZtPueVdFvWGkbZltez3n%2Fimage.png?alt=media&#x26;token=3faff17f-48fb-47d0-a404-5bd024dcc3af" alt=""><figcaption><p>Hardcoded the credential values into the docker-compose file did the magic <br>(This is a very poor thing to do in terms of security. I'm doing it this way only because I'm testing things out!).</p></figcaption></figure></div>

<figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FUqB2cnAkrgOO6N2lErZX%2Fimage.png?alt=media&#x26;token=08c84bd5-4510-4dfe-b08b-d207020c9a05" alt=""><figcaption><p>The span data are coming through to X-ray on AWS Cloud.</p></figcaption></figure>







##

## 2. Discussion&#x20;

### 2.1 AWS X-Ray

<figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FGoCGCDxsPiEnYi66GsZt%2Fimage.png?alt=media&#x26;token=b65ec1e1-7576-42b6-badb-1d3565893980" alt=""><figcaption></figcaption></figure>

* In our Bootcamp context, this X-ray is another container running alongside our application (the other containers - `backend-flask`, `frontend-react-js`, `dynamodb-local`, and `db`).
* **X-Ray** collects the data from our local machine, and send them in batches to AWS using the AWS APIs.&#x20;

### 2.2 Middleware

* _Middleware is a type of_ [_computer software_](https://en.wikipedia.org/wiki/Computer_software) _programme that provides services to software applications beyond those available from the_ [_operating system_](https://en.wikipedia.org/wiki/Operating_system)_. It can be described as "software glue"_ ([Wikipedia](https://en.wikipedia.org/wiki/Middleware))_._
*

