|
1 | 1 | # Python RTI Example |
2 | 2 |
|
3 | | -[](https://codecov.io/gh/codecov/python-rti-example) |
| 3 | +[](https://codecov.io/gh/codecov/impact-analysis-example-python) |
4 | 4 |
|
5 | | -A repository demonstrating how to use Codecov's [Runtime Insights](https://about.codecov.io/product/feature/runtime-insights/) feature with the Flask framework. This example repository leverages the [codecov/opentelem-python](https://github.com/codecov/opentelem-python) package to send information to Codecov's Runtime Insights API. It is recommended to view the README for that package to learn more about Runtime Insights. |
| 5 | +This repository demonstrating how to use Codecov's [Impact Analysis](https://about.codecov.io/product/feature/impact-analysis/) feature with python. It runs with the [Flask](https://flask.palletsprojects.com/en/2.1.x/) framework and leverages the [codecov/opentelem-python](https://github.com/codecov/opentelem-python) package to send information to Codecov's Runtime Insights API. |
6 | 6 |
|
7 | | -This repository is not intended to be used directly, but rather referred to as a reference for how to integrate Runtime Insights into your own python projects. |
| 7 | +This repository is set up to do to be used as |
| 8 | +1. a working sandbox to explore Impact Analysis |
| 9 | +1. a reference for adding Impact Analysis to your own repositories |
8 | 10 |
|
9 | | -## Requirements and Pre-requisites |
| 11 | +## Getting Started |
10 | 12 |
|
11 | | -1. A repository that is active on [Codecov](https://codecov.io) |
12 | | -2. A profiling token obtainable from Codecov. |
13 | | -3. python version >=3 |
| 13 | +The following section details how to get started with Impact Analysis. Before getting started, you will need |
| 14 | +1. `python` version 3+ |
| 15 | +2. An account on [Codecov](https://about.codecov.com) |
14 | 16 |
|
15 | | -A profiling token can be obtained by applying to and being selected for our [Runtime Insights Early Access Program](https://about.codecov.io/product/feature/runtime-insights/). |
| 17 | +<br /> |
16 | 18 |
|
17 | | -### Codecov.yml Configuration |
| 19 | +**Step 1: Fork and clone this repository** |
| 20 | +------------- |
18 | 21 |
|
19 | | -Some configuration is required in the `codecov.yml` to see Runtime Insights results in Pull Request comments. The full specification can be [found in our public documentation](https://docs.codecov.com/docs/runtime-insights#codecovyml-configuration), but the minimum is as follows: |
| 22 | +[Fork](https://docs.github.com/en/get-started/quickstart/fork-a-repo#forking-a-repository) this repository from GitHub. It is strongly recommended that you fork the repository to your personal GitHub account. |
| 23 | +Clone your new repository to your local machine. |
20 | 24 |
|
| 25 | +<br /> |
| 26 | + |
| 27 | +**Step 2: Set the `profiling token`** |
| 28 | +------------- |
| 29 | + |
| 30 | +Go to [Codecov](https://app.codecov.io/gh) and find the fork in the list of repositories. Note that it may be under `Not yet setup` in the right-hand section. |
| 31 | + |
| 32 | +In the `settings` page, grab the `Impact analysis token`, and set the token locally in a terminal. |
| 33 | +``` |
| 34 | +export CODECOV_OPENTELEMETRY_TOKEN='***' |
| 35 | +``` |
| 36 | + |
| 37 | +<br /> |
| 38 | + |
| 39 | +**Step 3: Install the dependencies** |
| 40 | +------------- |
| 41 | + |
| 42 | +Install all dependencies for this project. It is highly recommended to do this in a virtual environment. |
| 43 | +``` |
| 44 | +pip install -r requirements.txt |
| 45 | +``` |
| 46 | + |
| 47 | +<br /> |
| 48 | + |
| 49 | +**Step 4: Run the server locally and generate profiling data** |
| 50 | +------------- |
| 51 | + |
| 52 | +Run the server from your machine using the command |
| 53 | +``` |
| 54 | +python example-app/app.py |
| 55 | +``` |
| 56 | +If the token has been set properly, you should see the server running with the following logs |
| 57 | +``` |
| 58 | + * Serving Flask app 'app' (lazy loading) |
| 59 | + * Environment: production |
| 60 | + WARNING: This is a development server. Do not use it in a production deployment. |
| 61 | + Use a production WSGI server instead. |
| 62 | + * Debug mode: off |
| 63 | + * Running on all addresses (0.0.0.0) |
| 64 | + WARNING: This is a development server. Do not use it in a production deployment. |
| 65 | + * Running on http://127.0.0.1:8080 |
| 66 | + * Running on http://192.168.1.184:8080 (Press CTRL+C to quit) |
| 67 | +``` |
| 68 | + |
| 69 | +You can view the app by going to `http://127.0.0.1:8080`. |
| 70 | +The app has two pages, the main page that has a button to `Get the time`, while the other page displays the time. |
| 71 | + |
| 72 | +<br /> |
| 73 | + |
| 74 | +**Step 5: Overloading the `/time` endpoint** |
| 75 | +------------- |
| 76 | + |
| 77 | +In order for us to see what happens when we change a critical (frequently hit) line, we will need to hit the `/time` endpoint. In a python shell, run the following with the server running |
21 | 78 | ``` |
22 | | -comment: |
23 | | - layout: "reach,diff,flags,tree,betaprofiling" |
24 | | - show_critical_paths: true |
| 79 | +import requests |
| 80 | +import time |
| 81 | +
|
| 82 | +for i in range(100): |
| 83 | + print(i) |
| 84 | + requests.get('http://127.0.0.1:8080/time') |
| 85 | + time.sleep(0.1) |
| 86 | +``` |
| 87 | +This should hit our `/time` endpoint 100 times and upload the telemetry data to Codecov. |
| 88 | + |
| 89 | +<br /> |
| 90 | + |
| 91 | +**Step 6: Making a change to critical code** |
| 92 | +------------- |
| 93 | + |
| 94 | +Let's now make a change in our code to see if what we are changing is critical. |
| 95 | + |
| 96 | +In `example-app/utils/time.py`, update line 4 from |
| 97 | +``` |
| 98 | + return datetime.strftime(time, '%Y-%m-%d %H:%M:%S') |
| 99 | +``` |
| 100 | +to |
| 101 | +``` |
| 102 | + return datetime.strftime(time, '%Y-%m-%d:%H:%M:%S') |
| 103 | +``` |
| 104 | +. |
| 105 | +You will also need to update the tests. Change line 7 of `example-app/utils/tests/test_time.py` |
| 106 | +``` |
| 107 | + assert(format_time(current_time) == datetime.strftime(current_time, '%Y-%m-%d %H:%M:%S')) |
25 | 108 | ``` |
| 109 | +to |
| 110 | +``` |
| 111 | + assert(format_time(current_time) == datetime.strftime(current_time, '%Y-%m-%d:%H:%M:%S')) |
| 112 | +``` |
| 113 | + |
| 114 | +Save the changes, create a new branch, and push to GitHub. |
| 115 | +``` |
| 116 | +git checkout -b 'test-codecov' |
| 117 | +git add example-app/ |
| 118 | +git commit -m 'fix: update time display with colon' |
| 119 | +git push origin test-codecov |
| 120 | +``` |
| 121 | +Open a new pull request. Be sure to set the base branch to your fork. |
| 122 | + |
| 123 | +<br /> |
| 124 | + |
| 125 | +**Step 7: Seeing Impact Analysis in the UI** |
| 126 | +------------- |
| 127 | + |
| 128 | +After CI/CD has completed, you should see a comment from Codecov. The comment will now show 2 new elements |
| 129 | + |
| 130 | +1. Under `impacted files`, you should see a `Critical` label next to `example-app/utils/time.py`. This means that the PR has a change in that file that is frequently hit in production. |
| 131 | +2. Under `related entrypoints`, you should see `/time`. This means that the PR has a change that touches that endpoint. |
| 132 | + |
| 133 | +You should now be able to see how Impact Analysis can give crucial information on how a code change can affect critical code in your system. |
26 | 134 |
|
27 | | -Providing these settings in the `codecov.yml` will ensure that impacted files are marked as critical and impacted entrypoints are also shown in the Pull Request comment. |
| 135 | +### Using your own repositories |
| 136 | +To get started with Impact Analysis on your own repositories, check out our getting started [guide](https://docs.codecov.com/docs/impact-analysis-python). |
0 commit comments