Skip to content

Commit 313abad

Browse files
committed
Implementing code
We are adding a `code` field to the profiling. Its meant to be a unique (per repo) string that can identify which profiling is being made on this system without needing to pass env, version or ids on the individual profiling uploads Also adding a `needs_version_creation` in case the customer doesn't want that API call to be made on startup (they will need to do at least themselves once). The call itself is idempotent, so they should not need to worry about it unless they particularly need it to go
1 parent a0e0840 commit 313abad

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

codecovopentelem/__init__.py

+15-24
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ def __init__(
8585
self,
8686
cov_storage: CodecovCoverageStorageManager,
8787
repository_token: str,
88-
profiling_id: str,
88+
code: str,
8989
codecov_endpoint: str,
9090
untracked_export_rate: float,
9191
):
9292
self._cov_storage = cov_storage
9393
self._repository_token = repository_token
94-
self._profiling_id = profiling_id
94+
self._code = code
9595
self._codecov_endpoint = codecov_endpoint
9696
self._untracked_export_rate = untracked_export_rate
9797

@@ -128,7 +128,7 @@ def export(self, spans):
128128
res = requests.post(
129129
url,
130130
headers={"Authorization": f"repotoken {self._repository_token}"},
131-
json={"profiling": self._profiling_id},
131+
json={"profiling": self._code},
132132
)
133133
res.raise_for_status()
134134
except requests.RequestException:
@@ -149,63 +149,54 @@ def get_codecov_opentelemetry_instances(
149149
repository_token: str,
150150
sample_rate: float,
151151
untracked_export_rate: float,
152+
code: str,
152153
filters: Optional[Dict] = None,
153-
profiling_identifier: Optional[str] = None,
154+
version_identifier: Optional[str] = None,
154155
environment: Optional[str] = None,
155-
profiling_id: Optional[str] = None,
156+
needs_version_creation: bool = True,
156157
codecov_endpoint: str = None,
157158
writeable_folder: str = None,
158159
) -> Tuple[CodecovCoverageGenerator, CoverageExporter]:
159160
"""
160161
Entrypoint for getting a span processor/span exporter
161162
pair for getting profiling data into codecov
162163
163-
Notice that either `profiling_id` or `profiling_identifier` and `environment` need to be set.
164-
If `profiling_id` is set, we just use it directly on the exporter. If not, we will use
165-
`profiling_identifier` and `environment` to generate fetch a `profiling_id` from the
166-
database
167-
168164
Args:
169165
repository_token (str): The profiling-capable authentication token
170166
sample_rate (float): The sampling rate for codecov
171167
untracked_export_rate (float): Description
172168
filters (Optional[Dict], optional): A dictionary of filters for determining which
173169
spans should have its coverage tracked
174-
profiling_identifier (Optional[str], optional): The identifier for what profiling one is doing
170+
version_identifier (Optional[str], optional): The identifier for what
171+
software version is being profiled
175172
environment (Optional[str], optional): Which environment this profiling is running on
176-
profiling_id (Optional[str], optional): Description
173+
code (str): The code of this profiling
177174
codecov_endpoint (str, optional): For configuring the endpoint in case
178175
the user is in enterprise (not supported yet). Default is "https://api.codecov.io/"
179176
writeable_folder (str, optional): A folder that is guaranteed to be write-able
180177
in the system. It's only used for temporary files, and nothing is expected
181178
to live very long in there.
182179
"""
183180
codecov_endpoint = codecov_endpoint or "https://api.codecov.io"
184-
if profiling_id is None:
185-
if profiling_identifier is None or environment is None:
186-
raise UnableToStartProcessorException(
187-
"Codecov profiling needs either the id or identifier + environment"
188-
)
181+
if code is None:
182+
raise UnableToStartProcessorException("Codecov profiling needs a code set")
183+
if needs_version_creation and version_identifier and environment:
189184
response = requests.post(
190185
urllib.parse.urljoin(codecov_endpoint, "/profiling/versions"),
191186
json={
192-
"version_identifier": profiling_identifier,
187+
"version_identifier": version_identifier,
193188
"environment": environment,
189+
"code": code,
194190
},
195191
headers={"Authorization": f"repotoken {repository_token}"},
196192
)
197193
try:
198194
response.raise_for_status()
199195
except requests.HTTPError:
200196
raise UnableToStartProcessorException()
201-
profiling_id = response.json()["external_id"]
202197
manager = CodecovCoverageStorageManager(writeable_folder, filters or {})
203198
generator = CodecovCoverageGenerator(manager, sample_rate)
204199
exporter = CoverageExporter(
205-
manager,
206-
repository_token,
207-
profiling_id,
208-
codecov_endpoint,
209-
untracked_export_rate,
200+
manager, repository_token, code, codecov_endpoint, untracked_export_rate,
210201
)
211202
return (generator, exporter)

tests/test_interface.py

+26-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import json
21
from uuid import uuid4
32

43
import pytest
54
import responses
6-
from coverage import Coverage
7-
from coverage.xmlreport import XmlReporter
85

96
from codecovopentelem import (
107
CodecovCoverageGenerator,
118
CoverageExporter,
12-
get_codecov_opentelemetry_instances,
139
UnableToStartProcessorException,
10+
get_codecov_opentelemetry_instances,
1411
)
1512

1613

@@ -26,15 +23,12 @@ def test_get_codecov_opentelemetry_instances_nothing_set(mocker, mocked_response
2623
repository_token="repository_token",
2724
sample_rate=0.1,
2825
untracked_export_rate=0.1,
26+
code=None,
2927
)
30-
assert exc.value.args == (
31-
"Codecov profiling needs either the id or identifier + environment",
32-
)
28+
assert exc.value.args == ("Codecov profiling needs a code set",)
3329

3430

35-
def test_get_codecov_opentelemetry_instances_nothing_set_env_and_version(
36-
mocker, mocked_responses
37-
):
31+
def test_get_codecov_opentelemetry_instances_with_call_made(mocker, mocked_responses):
3832
uuid = uuid4().hex
3933
mocked_responses.add(
4034
responses.POST,
@@ -45,8 +39,9 @@ def test_get_codecov_opentelemetry_instances_nothing_set_env_and_version(
4539
match=[
4640
responses.matchers.json_params_matcher(
4741
{
48-
"version_identifier": "profiling_identifier",
42+
"version_identifier": "version_identifier",
4943
"environment": "production",
44+
"code": "code",
5045
}
5146
)
5247
],
@@ -55,8 +50,27 @@ def test_get_codecov_opentelemetry_instances_nothing_set_env_and_version(
5550
repository_token="repository_token",
5651
sample_rate=0.1,
5752
untracked_export_rate=0.1,
58-
profiling_identifier="profiling_identifier",
53+
code="code",
54+
version_identifier="version_identifier",
55+
environment="production",
56+
)
57+
assert len(res) == 2
58+
generator, exporter = res
59+
assert isinstance(generator, CodecovCoverageGenerator)
60+
assert isinstance(exporter, CoverageExporter)
61+
62+
63+
def test_get_codecov_opentelemetry_instances_with_call_not_made(
64+
mocker, mocked_responses
65+
):
66+
res = get_codecov_opentelemetry_instances(
67+
repository_token="repository_token",
68+
sample_rate=0.1,
69+
untracked_export_rate=0.1,
70+
code="code",
71+
version_identifier="version_identifier",
5972
environment="production",
73+
needs_version_creation=False,
6074
)
6175
assert len(res) == 2
6276
generator, exporter = res

0 commit comments

Comments
 (0)