Skip to content

Commit 87a7b32

Browse files
authored
Merge pull request #6 from codecov/prepare_package
Use GHA and Add Coverage
2 parents df1e241 + 19358df commit 87a7b32

File tree

5 files changed

+220
-57
lines changed

5 files changed

+220
-57
lines changed

.circleci/config.yml

-54
This file was deleted.

.github/workflows/run.yml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Python package
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
python-version: [3.6, 3.7, 3.8]
11+
steps:
12+
- uses: actions/checkout@v2
13+
- name: Set up Python ${{ matrix.python-version }}
14+
uses: actions/setup-python@v2
15+
with:
16+
python-version: ${{ matrix.python-version }}
17+
- name: Install dependencies
18+
run: |
19+
make testsuite.install
20+
- name: Test with pytest
21+
run: |
22+
make testsuite.run
23+
- uses: codecov/codecov-action@v2
24+
with:
25+
token: ${{ secrets.CODECOV_TOKEN }}
26+
flags: Python-v${{ matrix.python-version }}
27+
files: ./coverage.xml
28+
package:
29+
needs: build
30+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/checkout@v2
34+
- name: Set up Python
35+
uses: actions/setup-python@v2
36+
with:
37+
python-version: 3.8
38+
- name: Install Packaging Tools
39+
run: |
40+
make package.install
41+
- name: Build Package
42+
run: |
43+
make package.build
44+
- name: Publish package to TestPyPi
45+
uses: pypa/gh-action-pypi-publish@release/v1
46+
with:
47+
user: __token__
48+
password: ${{ secrets.PYPI_API_TOKEN}}

Makefile

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
testsuite.install:
2-
pip install pytest pytest-mock responses
2+
pip install pytest pytest-mock responses pytest-cov
33
python setup.py develop
44

55
testsuite.run:
6-
python -m pytest .
6+
python -m pytest --cov=./ --cov-report=xml:coverage.xml .
7+
8+
package.install:
9+
pip install --upgrade build
10+
python setup.py develop
11+
12+
package.build:
13+
python -m build

README.md

+161-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,163 @@
11
# opentelem-python
22

3-
Open Telemetry Codecov Python Prototype
3+
_Note: This package is part of the [Runtime Insights Early Access Program](https://about.codecov.io/product/feature/runtime-insights/)._
4+
5+
## Purpose
6+
7+
This package allows Python projects to leverage Codecov's [Runtime Insights](https://about.codecov.io/product/feature/runtime-insights/) feature.
8+
9+
More information about Runtime Insights can be found [in Codecov's public documentation](https://docs.codecov.com/docs/runtime-insights).
10+
11+
## Requirements and Pre-requisites
12+
13+
1. A repository that is active on [Codecov](https://codecov.io)
14+
2. A profiling token for that repository, obtainable from Codecov.
15+
3. Python version >=3.6
16+
17+
## Installation
18+
19+
First, install the package:
20+
21+
```
22+
pip install codecovopentelem
23+
```
24+
25+
Second, include the following snippet in your application. Where this is snippet is placed varies depending on the application, see _Integration Examples_ below.
26+
27+
The snippet:
28+
29+
```python
30+
from opentelemetry.sdk.trace import TracerProvider
31+
from opentelemetry.sdk.trace.export import (
32+
ConsoleSpanExporter,
33+
BatchSpanProcessor,
34+
SimpleSpanProcessor,
35+
)
36+
from opentelemetry import trace
37+
38+
from codecovopentelem import (
39+
CoverageSpanFilter,
40+
get_codecov_opentelemetry_instances,
41+
)
42+
from utils.time import format_time
43+
44+
provider = TracerProvider()
45+
trace.set_tracer_provider(provider)
46+
47+
"""
48+
CONFIGURATION
49+
"""
50+
current_version = "your-application-version"
51+
current_env = "your-application-envrionment"
52+
code=f"{current_version}:{current_env}"
53+
export_rate = 0.01
54+
repository_token="your-repository-profiling-token"
55+
untracked_export_rate = 0
56+
57+
generator, exporter = get_codecov_opentelemetry_instances(
58+
repository_token=repository_token,
59+
version_identifier=current_version,
60+
sample_rate=export_rate,
61+
filters={
62+
CoverageSpanFilter.span_kind_filter: [
63+
trace.SpanKind.SERVER,
64+
trace.SpanKind.CONSUMER,
65+
],
66+
},
67+
code=code,
68+
untracked_export_rate=untracked_export_rate,
69+
environment=current_env,
70+
)
71+
provider.add_span_processor(generator)
72+
provider.add_span_processor(BatchSpanProcessor(exporter))
73+
```
74+
75+
### Integration Examples
76+
77+
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.
78+
79+
Note that these examples demonstrate _possible_ ways to incorporate this package into your project. As always, your specific needs may vary.
80+
81+
#### Flask
82+
83+
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:
84+
85+
```python
86+
87+
from opentelemetry.instrumentation.flask import FlaskInstrumentor
88+
89+
# Snippet Code ...
90+
# Other Startup Code ...
91+
92+
app = Flask(
93+
__name__,
94+
static_url_path='',
95+
static_folder='',
96+
template_folder='templates',
97+
)
98+
99+
FlaskInstrumentor().instrument_app(app)
100+
101+
# app.route(...)
102+
# ...
103+
104+
app.run(host='0.0.0.0', port=8080)
105+
```
106+
107+
#### Django
108+
109+
In Django, you can place this snippet in your application's `<application_name>/wsgi.py` file:
110+
111+
```python
112+
113+
import os
114+
115+
from django.core.wsgi import get_wsgi_application
116+
117+
from utils.config import get_settings_module
118+
119+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", get_settings_module())
120+
121+
#... Other Startup Code
122+
123+
from opentelemetry.instrumentation.django import DjangoInstrumentor
124+
try:
125+
# Snippet Code
126+
except UnableToStartProcessorException:
127+
# Handle the Exception...
128+
129+
DjangoInstrumentor().instrument()
130+
131+
132+
application = get_wsgi_application()
133+
```
134+
135+
Note that this example also demonstrates how to integrate using a `try/except`.
136+
137+
#### General Integration
138+
139+
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.
140+
141+
## Configuration
142+
143+
- `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.
144+
- `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.)
145+
- `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.
146+
- `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).
147+
- `repository_token` -- _(Required)_ The identifying token for this repository. Currently only obtainable by being selected for Codecov's [Runtime Insights Early Access Program](https://about.codecov.io/product/feature/runtime-insights/). It should be treated as a sensitive credential (e.g., not committed to source control, etc.)
148+
- `untracked_export_rate` -- Currently unused, should remain at 0.
149+
150+
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).
151+
152+
## Codecov.yml Changes
153+
154+
You will need to update your `codecov.yml` as follows:
155+
156+
```
157+
comment:
158+
layout: 'reach,diff,flags,tree,betaprofiling'
159+
show_critical_paths: true
160+
161+
```
162+
163+
[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.

codecov.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ignore:
2+
- '**tests**/test_*.py'

0 commit comments

Comments
 (0)