Skip to content

Commit b1a1212

Browse files
authored
fix(profiler): support for latest protobuf versions [backport #5664 to 1.13] (#5668)
Backport of #5664 to 1.13 Version 4.21 of the Python protobuf package introduced the [upb C backend](https://github.com/protocolbuffers/protobuf/tree/main/python#implementation-backends). The protobuf sources where generated by the 3.19 version of the compiler, so there are good chances that the latest protobuf releases are not well-supported by the profiler. In this change we re-compile the pprof protobuf spec with the latest 21 protoc compiler. ## Testing strategy No issues have been reported by CI that can be related to a lack of support for latest versions of protobuf. Therefore it is unclear how to test for possible incompatibility issues. ## Risk Support for different protobuf versions remain too coarse and prone to potential future incompatibilities. This problem will be resolved by the introduction of libdatadog which will delegate pprof generation to a native layer that allows us to drop the dependency on the Python protobuf package. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines) are followed. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] PR description includes explicit acknowledgement/acceptance of the performance implications of this PR as reported in the benchmarks PR comment. ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment.
1 parent 651a2c5 commit b1a1212

File tree

9 files changed

+69
-15
lines changed

9 files changed

+69
-15
lines changed

ddtrace/profiling/exporter/pprof.pyx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,18 @@ def _protobuf_version():
9494
return parse_version(google.protobuf.__version__)
9595

9696

97+
# Load the appropriate pprof_pb2 module
9798
_pb_version = _protobuf_version()
98-
99-
if _pb_version >= (3, 19, 0):
100-
from ddtrace.profiling.exporter import pprof_pb2
101-
elif _pb_version >= (3, 12, 0):
102-
from ddtrace.profiling.exporter import pprof_pre319_pb2 as pprof_pb2 # type: ignore[no-redef]
99+
for v in [(4, 21), (3, 19), (3, 12)]:
100+
if _pb_version >= v:
101+
import sys
102+
103+
pprof_module = "ddtrace.profiling.exporter.pprof_%s%s_pb2" % v
104+
__import__(pprof_module)
105+
pprof_pb2 = sys.modules[pprof_module]
106+
break
103107
else:
104-
from ddtrace.profiling.exporter import pprof_pre312_pb2 as pprof_pb2 # type: ignore[no-redef]
108+
from ddtrace.profiling.exporter import pprof_3_pb2 as pprof_pb2 # type: ignore[no-redef]
105109

106110

107111
_ITEMGETTER_ZERO = operator.itemgetter(0)

ddtrace/profiling/exporter/pprof_421_pb2.py

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
profiler: Fix support for latest versions of protobuf.

mypy.ini

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ ignore_errors = true
1818
[mypy-ddtrace.vendor.*]
1919
ignore_errors = true
2020

21-
[mypy-ddtrace.profiling.exporter.pprof_pb2]
21+
[mypy-ddtrace.profiling.exporter.pprof_3_pb2]
2222
ignore_errors = true
2323

24-
[mypy-ddtrace.profiling.exporter.pprof_pre312_pb2]
24+
[mypy-ddtrace.profiling.exporter.pprof_312_pb2]
2525
ignore_errors = true
2626

27-
[mypy-ddtrace.profiling.exporter.pprof_pre319_pb2]
27+
[mypy-ddtrace.profiling.exporter.pprof_319_pb2]
28+
ignore_errors = true
29+
30+
[mypy-ddtrace.profiling.exporter.pprof_421_pb2]
2831
ignore_errors = true

pyproject.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ exclude = '''
3131
| ddtrace/profiling/collector/_task.pyx$
3232
| ddtrace/profiling/_threading.pyx$
3333
| ddtrace/profiling/collector/stack.pyx$
34-
| ddtrace/profiling/exporter/pprof_.*pb2.py$
34+
| ddtrace/profiling/exporter/pprof_.*_pb2.py$
3535
| ddtrace/profiling/exporter/pprof.pyx$
3636
| ddtrace/vendor/
3737
| \.eggs
@@ -69,8 +69,10 @@ exclude-modules = '''
6969
| ddtrace.profiling.bootstrap.sitecustomize
7070
| ddtrace.profiling.auto
7171
# protobuf file fails to import
72-
| ddtrace.profiling.exporter.pprof_pre312_pb2
73-
| ddtrace.profiling.exporter.pprof_pre319_pb2
72+
| ddtrace.profiling.exporter.pprof_3_pb2
73+
| ddtrace.profiling.exporter.pprof_312_pb2
74+
| ddtrace.profiling.exporter.pprof_319_pb2
75+
| ddtrace.profiling.exporter.pprof_421_pb2
7476
# TODO: resolve slot inheritance issues with profiling
7577
| ddtrace.profiling.collector
7678
| ddtrace.appsec.ddwaf.ddwaf_types

setup.cfg

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ exclude=
1515
ddtrace/__init__.py,
1616
# We shouldn't lint our vendored dependencies
1717
ddtrace/vendor/*
18-
ddtrace/profiling/exporter/pprof_pb2.py
19-
ddtrace/profiling/exporter/pprof_pre319_pb2.py
20-
ddtrace/profiling/exporter/pprof_pre312_pb2.py
18+
ddtrace/profiling/exporter/pprof_3_pb2.py
19+
ddtrace/profiling/exporter/pprof_312_pb2.py
20+
ddtrace/profiling/exporter/pprof_319_pb2.py
21+
ddtrace/profiling/exporter/pprof_421_pb2.py
2122
tests/profiling/simple_program_gevent.py
2223
tests/contrib/grpc/hello_pb2.py
2324

0 commit comments

Comments
 (0)