From cdb353840bcbd9f30b2d74ebb9968cfa99351b41 Mon Sep 17 00:00:00 2001 From: Brett Langdon Date: Fri, 13 Jan 2023 21:03:30 -0500 Subject: [PATCH] fix(internal): properly convert PEP 440 version to SemVer for RCM client (#4910) ## Description The fix for ensuring RCM used a SemVer compliant version string only took into account ddtrace versions that match `x.y.zrc2` or `x.y.zrc2.dev3+gf344hg`, but not `x.y.z.dev3+gf344hg`. This fix ensures that if we have a `.dev` without `rc` that we properly convert `.dev` -> `-dev` which will make it SemVer compliant. Since the tests rely heavily on the version of `ddtrace.__version__` writing a test is challenging. We should revisit this in the future so we can mock the version and parametrize the test to ensure all variations are tested. ## Checklist - [ ] Followed the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines) when writing a release note. - [ ] Add additional sections for `feat` and `fix` pull requests. - [ ] [Library documentation](https://github.com/DataDog/dd-trace-py/tree/1.x/docs) and/or [Datadog's documentation site](https://github.com/DataDog/documentation/) is updated. Link to doc PR in description. ## Reviewer Checklist - [ ] Title is accurate. - [ ] Description motivates each change. - [ ] No unnecessary changes were introduced in this PR. - [ ] Avoid breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [ ] Tests provided or description of manual testing performed is included in the code or PR. - [ ] Release note has been added and follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines), or else `changelog/no-changelog` label added. - [ ] All relevant GitHub issues are correctly linked. - [ ] Backports are identified and tagged with Mergifyio. --- ddtrace/internal/remoteconfig/client.py | 30 ++++++++++++++++++++----- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/ddtrace/internal/remoteconfig/client.py b/ddtrace/internal/remoteconfig/client.py index f622a367ac5..d01da373a72 100644 --- a/ddtrace/internal/remoteconfig/client.py +++ b/ddtrace/internal/remoteconfig/client.py @@ -199,15 +199,33 @@ def __init__(self): if container_id is not None: self._headers["Datadog-Container-Id"] = container_id + # The library uses a PEP 440-compliant versioning scheme, but the + # RCM spec requires that we use a SemVer-compliant version. + # + # However, we may have versions like: + # + # - 1.7.1.dev3+gf258c7d9 + # - 1.7.1rc2.dev3+gf258c7d9 + # + # Which are not Semver-compliant. + # + # The easiest fix is to replace the first occurrence of "rc" or + # ".dev" with "-rc" or "-dev" to make them compliant. + # + # Other than X.Y.Z, we are allowed `-+` + # https://semver.org/#backusnaur-form-grammar-for-valid-semver-versions + # + # e.g. 1.7.1-rc2.dev3+gf258c7d9 is valid + tracer_version = ddtrace.__version__ + if "rc" in tracer_version: + tracer_version = tracer_version.replace("rc", "-rc", 1) + elif ".dev" in tracer_version: + tracer_version = tracer_version.replace(".dev", "-dev", 1) + self._client_tracer = dict( runtime_id=runtime.get_runtime_id(), language="python", - # The library uses a PEP 440-compliant versioning scheme, but the - # RCM spec requires that we use a SemVer-compliant version. We only - # expect that the first occurrence of "rc" in the version string to - # break the SemVer format, so we replace it with "-rc" for - # simplicity. - tracer_version=ddtrace.__version__.replace("rc", "-rc", 1), + tracer_version=tracer_version, service=ddtrace.config.service, env=ddtrace.config.env, app_version=ddtrace.config.version,