|
1 | 1 | # opentelem-python
|
2 | 2 |
|
3 |
| - |
4 | 3 | ## Purpose
|
5 | 4 |
|
6 | 5 | This package allows Python projects to leverage Codecov's [Impact Analysis]([https://about.codecov.io/product/feature/runtime-insights/](https://docs.codecov.com/docs/impact-analysis)) feature.
|
7 | 6 |
|
8 | 7 | More information about Runtime Insights can be found [in Codecov's public documentation]([https://docs.codecov.com/docs/runtime-insights](https://docs.codecov.com/docs/impact-analysis)).
|
9 | 8 |
|
10 |
| -## Requirements and Pre-requisites |
11 |
| - |
12 |
| -1. A repository that is active on [Codecov](https://codecov.io) |
13 |
| -2. A profiling token for that repository, obtainable from Codecov. |
14 |
| -3. Python version >=3.6 |
15 |
| - |
16 |
| -## Installation |
17 |
| - |
18 |
| -First, install the package: |
19 |
| - |
20 |
| -``` |
21 |
| -pip install codecovopentelem |
22 |
| -``` |
23 |
| - |
24 |
| -Second, include the following snippet in your application. Where this is snippet is placed varies depending on the application, see _Integration Examples_ below. |
25 |
| - |
26 |
| -The snippet: |
27 |
| - |
28 |
| -```python |
29 |
| -from opentelemetry.sdk.trace import TracerProvider |
30 |
| -from opentelemetry.sdk.trace.export import ( |
31 |
| - ConsoleSpanExporter, |
32 |
| - BatchSpanProcessor, |
33 |
| - SimpleSpanProcessor, |
34 |
| -) |
35 |
| -from opentelemetry import trace |
36 |
| - |
37 |
| -from codecovopentelem import ( |
38 |
| - CoverageSpanFilter, |
39 |
| - get_codecov_opentelemetry_instances, |
40 |
| -) |
41 |
| -from utils.time import format_time |
42 |
| - |
43 |
| -provider = TracerProvider() |
44 |
| -trace.set_tracer_provider(provider) |
45 |
| - |
46 |
| -""" |
47 |
| -CONFIGURATION |
48 |
| -""" |
49 |
| -current_version = "your-application-version" |
50 |
| -current_env = "your-application-envrionment" |
51 |
| -code=f"{current_version}:{current_env}" |
52 |
| -export_rate = 0.01 |
53 |
| -repository_token="your-repository-profiling-token" |
54 |
| -untracked_export_rate = 0 |
55 |
| - |
56 |
| -generator, exporter = get_codecov_opentelemetry_instances( |
57 |
| - repository_token=repository_token, |
58 |
| - version_identifier=current_version, |
59 |
| - sample_rate=export_rate, |
60 |
| - filters={ |
61 |
| - CoverageSpanFilter.span_kind_filter: [ |
62 |
| - trace.SpanKind.SERVER, |
63 |
| - trace.SpanKind.CONSUMER, |
64 |
| - ], |
65 |
| - }, |
66 |
| - code=code, |
67 |
| - untracked_export_rate=untracked_export_rate, |
68 |
| - environment=current_env, |
69 |
| -) |
70 |
| -provider.add_span_processor(generator) |
71 |
| -provider.add_span_processor(BatchSpanProcessor(exporter)) |
72 |
| -``` |
73 |
| - |
74 |
| -### Integration Examples |
75 |
| - |
76 |
| -The specifics of how this library is integrated into your project depends on the project itself. This section contains a few common, framework specific, integration approaches along with the general integration approach at the end. |
77 |
| - |
78 |
| -Note that these examples demonstrate _possible_ ways to incorporate this package into your project. As always, your specific needs may vary. |
79 |
| - |
80 |
| -#### Flask |
81 |
| - |
82 |
| -In a Flask application, you could place the code snippet in your application's `app.py` file _before_ the call to initialize your Flask app, like so: |
83 |
| - |
84 |
| -```python |
85 |
| - |
86 |
| -from opentelemetry.instrumentation.flask import FlaskInstrumentor |
87 |
| - |
88 |
| -# Snippet Code ... |
89 |
| -# Other Startup Code ... |
90 |
| - |
91 |
| -app = Flask( |
92 |
| - __name__, |
93 |
| - static_url_path='', |
94 |
| - static_folder='', |
95 |
| - template_folder='templates', |
96 |
| -) |
97 |
| - |
98 |
| -FlaskInstrumentor().instrument_app(app) |
99 |
| - |
100 |
| -# app.route(...) |
101 |
| -# ... |
102 |
| - |
103 |
| -app.run(host='0.0.0.0', port=8080) |
104 |
| -``` |
105 |
| - |
106 |
| -#### Django |
107 |
| - |
108 |
| -In Django, you can place this snippet in your application's `<application_name>/wsgi.py` file: |
109 |
| - |
110 |
| -```python |
111 |
| - |
112 |
| -import os |
113 |
| - |
114 |
| -from django.core.wsgi import get_wsgi_application |
115 |
| - |
116 |
| -from utils.config import get_settings_module |
117 |
| - |
118 |
| -os.environ.setdefault("DJANGO_SETTINGS_MODULE", get_settings_module()) |
119 |
| - |
120 |
| -#... Other Startup Code |
121 |
| - |
122 |
| -from opentelemetry.instrumentation.django import DjangoInstrumentor |
123 |
| -try: |
124 |
| - # Snippet Code |
125 |
| -except UnableToStartProcessorException: |
126 |
| - # Handle the Exception... |
127 |
| - |
128 |
| -DjangoInstrumentor().instrument() |
129 |
| - |
130 |
| - |
131 |
| -application = get_wsgi_application() |
132 |
| -``` |
133 |
| - |
134 |
| -Note that this example also demonstrates how to integrate using a `try/except`. |
135 |
| - |
136 |
| -#### General Integration |
137 |
| - |
138 |
| -If you are not using Django or Flask integration is still possible using the above code snippet. How to do this may vary greatly depending on your use case. In general, though, the code snippet should be placed wherever you would put your application's OpenTelemetry startup code. In lieu of that, this code should be incorporated in such a way that it is part of your application's startup process. |
139 |
| - |
140 |
| -## Configuration |
141 |
| - |
142 |
| -- `current_version` -- _(Required)_ The current version of the application. This can be semver, a commit SHA, or whatever is meaningful to you, but it should uniquely identify the particular version of the code. |
143 |
| -- `current_env` -- _(Required)_ The environment in which the application is currently running. Typically "production", but can be other values as well (e.g., "local" / "dev" for testing during setup of the package, "test" for instrumenting in your test environment, etc.) |
144 |
| -- `code` -- A unique identifier for the current deployment across all environments where it may be deployed. Conventionally, this is a combination of version number and environment name, but can be anything as long as it is unique in each environment for the version being deployed. |
145 |
| -- `export_rate` -- _(Required. Min: 0, Max: 1)_ The percentage of your application's calls that are instrumented using this package. Using this package does incur some performance overhead, and instrumenting 100% of calls is not required. Therefore, for most applications, it is recommended to use 0.01 to 0.05 as the default value. However, low traffic applications may want to use a larger number (such as 0.1 or more). |
146 |
| -- `repository_token` -- _(Required)_ The identifying token for this repository. It is accessible from the repository's settings page in the Codecov application. It should be treated as a sensitive credential (e.g., not committed to source control, etc.) |
147 |
| -- `untracked_export_rate` -- Currently unused, should remain at 0. |
148 |
| - |
149 |
| -If desired, the `filters` parameter can also be changed to provide different filtering on any valid OpenTelemetry `SpanKind` as [defined by the specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#spankind). |
150 |
| - |
151 |
| -## Codecov.yml Changes |
152 |
| - |
153 |
| -You will need to update your `codecov.yml` as follows: |
154 |
| - |
155 |
| -``` |
156 |
| -comment: |
157 |
| - layout: 'reach,diff,flags,tree,betaprofiling' |
158 |
| - show_critical_paths: true |
159 |
| -
|
160 |
| -``` |
161 |
| - |
162 |
| -[You can read more about the codecov.yml in Codecov's public documentation](https://docs.codecov.com/docs/codecov-yaml). If you do not have a `codecov.yml` in your project, you can create the file in the root of your project and add the above configuration. |
| 9 | +For details on how to get started, please see our [Quickstart guide](https://docs.codecov.com/docs/impact-analysis-quickstart-python). You can also fork our [example repository](https://github.com/codecov/impact-analysis-example-python) to try it out for yourself first. |
0 commit comments