> For the complete documentation index, see [llms.txt](https://gwen-leigh.gitbook.io/free-aws-cloud-project-bookcamp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gwen-leigh.gitbook.io/free-aws-cloud-project-bookcamp/week-2-distributed-tracing/2.1.0-instrument-x-ray/2.1.1-x-ray-subsegment-trial.md).

# 2.1.1 X-Ray Subsegment Trial

What types of data would you include in your spans for tracing and observability? I tried to send the name of the current git branch which will change over the coming weeks of Cruddur development.

## **0. Learning Materials**

* ~~Video~~: personal micro-project branching out of the original Cruddur
* ~~Andrew's repo~~
* My branch repo: [02-01-01-xray-subsegment-trial](https://github.com/mariachiinajar/aws-bootcamp-cruddur-2023-again/tree/02-01-01-xray-subsegment-trial)

### &#x20;Task List

* [ ] **1.** Create a code to get the name of the current git branch
* [ ] **2.** Customise the spans of the tracing tools
  * [ ] **2.1. Honeycomb**: customise the spans by setting attributes
  * [ ] **2.2 X-Ray**: send segments and subsegments to AWS X-Ray

***

## 1. Workflow

### 1. Create a code to get the name of the current git branch

The below code is what I got from my dear friend ChatGPT. I'm so happy to chat with ChatGPT because it never gets mad at me for having infinite questions :innocent:

{% code title="branch.py" lineNumbers="true" %}

```python
import subprocess

def get_current_git_branch():
    try:
        result = subprocess.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], capture_output=True, text=True, check=True)

        # Extract and return the branch name
        branch_name = result.stdout.strip()
        return branch_name
    except subprocess.CalledProcessError as e:
        print(f"Error: {e}")
        return None
```

{% endcode %}

* **line 1**: *the* [*subprocess*](https://docs.python.org/3/library/subprocess.html) *module allows you to spawn new processes, connect to their input/output/error pipes, and **obtain their return codes**.*
  * Meaning, I can run bash script using Python and store the return value in a Python variable!
* **line 5**: the function `subprocess.run` runs terminal commands.&#x20;
  * `git rev-parse --abbrev-ref HEAD`: this command grabs the name of the current branch.
    * As this is a bash command, it returns the branch name only as a string:&#x20;
      * `02-01-01-xray-subsegment-trial`
* **line 8**: however, we are grabbing the current branch name in bash using python, so we need some string processing.

  <figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FXPufNrWuAlfaCBcsTMzO%2Fimage.png?alt=media&amp;token=86a9e650-5d7c-4fbf-bccd-afa09e4d92d2" alt=""><figcaption><p>Python subprocess in action, extracting the Bash-returned value.</p></figcaption></figure>

### 2.1. Honeycomb: customise the spans by setting attributes

### 2.2. X-Ray: send segments and subsegments to AWS X-Ray

The codes for these tasks don't work and isn't important for the discussion. You can still look here:

* Honeycomb and X-Ray in:
  * [home\_activities.py](https://github.com/mariachiinajar/aws-bootcamp-cruddur-2023-again/compare/main...mariachiinajar:aws-bootcamp-cruddur-2023-again:02-01-01-xray-subsegment-trial#diff-e7fc4f0f2b4e4510d81bbc953fe4e4198587359967fc005d49cb23f39e7f3130)
  * [notifications\_activities.py](https://github.com/mariachiinajar/aws-bootcamp-cruddur-2023-again/compare/main...mariachiinajar:aws-bootcamp-cruddur-2023-again:02-01-01-xray-subsegment-trial#diff-86937d0e77c06cacef0fda4b93c8b34c40f93962cd8962babbc54ed5aa105f1b)

## 2. Discussion

### Dockerisation, Dev & Prod containers

#### Honeycomb

<figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FV0x1YsTrk7HJp4vpVec4%2Fimage.png?alt=media&amp;token=214276a5-ce07-47c8-be14-23f61ad78599" alt=""><figcaption><p>The recent value for app.branch shows the name of the branch 02-01-01-xray-subsegment-trial. This value is hard-coded :(</p></figcaption></figure>

I tried customising the Honeycomb span by adding a new attribute `app.branch`. This attribute would have the name of the current git branch.&#x20;

In my mind, as I will move through the weeks, I will continue to create more branches, and the value of the `app.branch` will be dynamically updated!

So it would be really cool to have spans coming in on Honeycomb which clearly marks which phases of the development they were captured at.

* **However, it failed because the backend is containerised**. This means that the backend-container has to:
  * have `git` installed
  * should be in sync with this repository all the time.&#x20;
    * Generally speaking, docker containers used in development and production should never be the same. Dev containers usually contain many more dependencies than prod containers for troubleshooting and testing purposes.
      * Speaking of which, installing git in the prod container would be a bad idea, as it can become a security vulnerability. In order to identify the branch name, Git has to be in sync with the remote repo. Furthermore, I'll have to provide a Git credential.&#x20;
      * Even for a dev container, this seems to be a waste of compute power. Packge Git, include my Git credentials as env variables in the docker container... (why would you install a tool that can control the versions of your production source code, even if the Github credential you are providing doesn't have permissions to change the repo?)
* It is too costly for an experiment and it will never be adopted in the prod container down the road. Sso I discarded the idea.&#x20;

#### X-Ray

I also failed sending segments and subsegments to `X-Ray`. It does not pick up the subsegments. I might as well retry this after completing the episode "X-Ray Subsegments Solved"

#### Conclusion

So this trial ended up being  more of a thought experiment than coding. However, it still was a valuable experience. As the docker dev and prod containerisation, we will learn more about this consideration again in the coming weeks.

## 3. Resources

* Python: [subprocess](https://docs.python.org/3/library/subprocess.html)
