Skip to content

Commit 4b8b2b5

Browse files
authored
rtloader: drop python2 support (DataDog#30089)
1 parent 7ad2605 commit 4b8b2b5

31 files changed

+21
-1724
lines changed

pkg/collector/python/init.go

+5-19
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ var (
205205
PythonVersion = ""
206206
// The pythonHome variable typically comes from -ldflags
207207
// it's needed in case the agent was built using embedded libs
208-
pythonHome2 = ""
209208
pythonHome3 = ""
210209
// PythonHome contains the computed value of the Python Home path once the
211210
// intepreter is created. It might be empty in case the interpreter wasn't
@@ -303,7 +302,7 @@ func pathToBinary(name string, ignoreErrors bool) (string, error) {
303302
return absPath, nil
304303
}
305304

306-
func resolvePythonExecPath(pythonVersion string, ignoreErrors bool) (string, error) {
305+
func resolvePythonExecPath(ignoreErrors bool) (string, error) {
307306
// Since the install location can be set by the user on Windows we use relative import
308307
if runtime.GOOS == "windows" {
309308
_here, err := executable.Folder()
@@ -317,30 +316,20 @@ func resolvePythonExecPath(pythonVersion string, ignoreErrors bool) (string, err
317316
}
318317
log.Debugf("Executable folder is %v", _here)
319318

320-
embeddedPythonHome2 := filepath.Join(_here, "..", "embedded2")
321319
embeddedPythonHome3 := filepath.Join(_here, "..", "embedded3")
322320

323321
// We want to use the path-relative embedded2/3 directories above by default.
324322
// They will be correct for normal installation on Windows. However, if they
325323
// are not present for cases like running unit tests, fall back to the compile
326324
// time values.
327-
if _, err := os.Stat(embeddedPythonHome2); os.IsNotExist(err) {
328-
log.Warnf("Relative embedded directory not found for Python 2. Using default: %s", pythonHome2)
329-
} else {
330-
pythonHome2 = embeddedPythonHome2
331-
}
332325
if _, err := os.Stat(embeddedPythonHome3); os.IsNotExist(err) {
333326
log.Warnf("Relative embedded directory not found for Python 3. Using default: %s", pythonHome3)
334327
} else {
335328
pythonHome3 = embeddedPythonHome3
336329
}
337330
}
338331

339-
if pythonVersion == "2" {
340-
PythonHome = pythonHome2
341-
} else if pythonVersion == "3" {
342-
PythonHome = pythonHome3
343-
}
332+
PythonHome = pythonHome3
344333

345334
log.Infof("Using '%s' as Python home", PythonHome)
346335

@@ -361,7 +350,7 @@ func resolvePythonExecPath(pythonVersion string, ignoreErrors bool) (string, err
361350
// don't want to use the default version (aka "python") but rather "python2" or
362351
// "python3" based on the configuration. Also on some Python3 platforms there
363352
// are no "python" aliases either.
364-
interpreterBasename := "python" + pythonVersion
353+
interpreterBasename := "python3"
365354

366355
// If we are in a development env or just the ldflags haven't been set, the PythonHome
367356
// variable won't be set so what we do here is to just find out where our current
@@ -393,7 +382,7 @@ func Initialize(paths ...string) error {
393382
}
394383

395384
// Note: pythonBinPath is a module-level var
396-
pythonBinPath, err := resolvePythonExecPath(pythonVersion, allowPathHeuristicsFailure)
385+
pythonBinPath, err := resolvePythonExecPath(allowPathHeuristicsFailure)
397386
if err != nil {
398387
return err
399388
}
@@ -407,10 +396,7 @@ func Initialize(paths ...string) error {
407396
csPythonExecPath := TrackedCString(pythonBinPath)
408397
defer C._free(unsafe.Pointer(csPythonExecPath))
409398

410-
if pythonVersion == "2" {
411-
log.Infof("Initializing rtloader with Python 2 %s", PythonHome)
412-
rtloader = C.make2(csPythonHome, csPythonExecPath, &pyErr)
413-
} else if pythonVersion == "3" {
399+
if pythonVersion == "3" {
414400
log.Infof("Initializing rtloader with Python 3 %s", PythonHome)
415401
rtloader = C.make3(csPythonHome, csPythonExecPath, &pyErr)
416402
} else {

rtloader/CMakeLists.txt

+1-8
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,10 @@ if(ARCH_I386)
2424
endif()
2525

2626
## Config options
27-
option(DISABLE_PYTHON2 "Do not build Python2 support")
28-
option(DISABLE_PYTHON3 "Do not build Python3 support")
2927
option(BUILD_DEMO "Build the demo app" ON)
3028

3129
## Add Build Targets
32-
if (NOT DISABLE_PYTHON2)
33-
add_subdirectory(two)
34-
endif()
35-
if (NOT DISABLE_PYTHON3)
36-
add_subdirectory(three)
37-
endif()
30+
add_subdirectory(three)
3831
add_subdirectory(rtloader)
3932
add_subdirectory(test)
4033
if (BUILD_DEMO)

rtloader/README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ RtLoader will `dlopen` the proper backend libraries accordingly.
1515
### libdatadog-agent-rtloader
1616

1717
RtLoader exposes its C89-compatible API through `include/datadog_agent_rtloader.h`. By
18-
using the `make2` and `make3` functions, the corresponding Python backend will
18+
using the `make3` function, the corresponding Python backend will
1919
be loaded at runtime. Under the hood the library provides `RtLoader`, a C++ interface
2020
that must be implemented by any supported backend, see `include/rtloader.h` for details.
2121

2222
### Two and Three
2323

24-
`libdatadog-agent-three` and `libdatadog-agent-two` libraries provide Python support
25-
for extending and embedding by linking different versions of the CPython library.
24+
`libdatadog-agent-three` library provides Python3 support. Python2 isn't supported anymore.
2625

2726
### Common
2827

@@ -33,7 +32,6 @@ Most of the code used to extend the embedded interpreter is there.
3332
## Requirements
3433

3534
* C/C++ compiler
36-
* Python 2.7.x development packages
3735
* Python 3.12.x development packages
3836
* Cmake version 3.12 or above
3937
* Go compiler with `cgo` capabilities to run the tests

rtloader/common/builtins/_util.c

-11
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ static PyMethodDef methods[] = {
3434
{ NULL, NULL } // guards
3535
};
3636

37-
#ifdef DATADOG_AGENT_THREE
3837
static struct PyModuleDef module_def = { PyModuleDef_HEAD_INIT, _UTIL_MODULE_NAME, NULL, -1, methods };
3938

4039
PyMODINIT_FUNC PyInit__util(void)
@@ -43,16 +42,6 @@ PyMODINIT_FUNC PyInit__util(void)
4342
addSubprocessException(m);
4443
return m;
4544
}
46-
#elif defined(DATADOG_AGENT_TWO)
47-
// in Python2 keep the object alive for the program lifetime
48-
static PyObject *module;
49-
50-
void Py2_init__util()
51-
{
52-
module = Py_InitModule(_UTIL_MODULE_NAME, methods);
53-
addSubprocessException(module);
54-
}
55-
#endif
5645

5746
void _set_get_subprocess_output_cb(cb_get_subprocess_output_t cb)
5847
{

rtloader/common/builtins/_util.h

+1-9
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,13 @@
4646
// The keyword-only arguments separator ($) for PyArg_ParseTupleAndKeywords()
4747
// has been introduced in Python 3.3
4848
// https://docs.python.org/3/c-api/arg.html#other-objects
49-
#ifdef DATADOG_AGENT_THREE
50-
# define PY_ARG_PARSE_TUPLE_KEYWORD_ONLY "$"
51-
#elif defined(DATADOG_AGENT_TWO)
52-
# define PY_ARG_PARSE_TUPLE_KEYWORD_ONLY ""
53-
#endif
49+
#define PY_ARG_PARSE_TUPLE_KEYWORD_ONLY "$"
5450

5551
#ifdef __cplusplus
5652
extern "C" {
5753
#endif
5854

59-
#ifdef DATADOG_AGENT_THREE
6055
PyMODINIT_FUNC PyInit__util(void);
61-
#elif defined(DATADOG_AGENT_TWO)
62-
void Py2_init__util();
63-
#endif
6456

6557
void _set_get_subprocess_output_cb(cb_get_subprocess_output_t);
6658
#ifdef __cplusplus

rtloader/common/builtins/aggregator.c

-11
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ static void add_constants(PyObject *m)
4848
PyModule_AddIntConstant(m, "HISTORATE", DATADOG_AGENT_RTLOADER_HISTORATE);
4949
}
5050

51-
#ifdef DATADOG_AGENT_THREE
5251
static struct PyModuleDef module_def = { PyModuleDef_HEAD_INIT, AGGREGATOR_MODULE_NAME, NULL, -1, methods };
5352

5453
PyMODINIT_FUNC PyInit_aggregator(void)
@@ -57,16 +56,6 @@ PyMODINIT_FUNC PyInit_aggregator(void)
5756
add_constants(m);
5857
return m;
5958
}
60-
#elif defined(DATADOG_AGENT_TWO)
61-
// module object storage
62-
static PyObject *module;
63-
64-
void Py2_init_aggregator()
65-
{
66-
module = Py_InitModule(AGGREGATOR_MODULE_NAME, methods);
67-
add_constants(module);
68-
}
69-
#endif
7059

7160
void _set_submit_metric_cb(cb_submit_metric_t cb)
7261
{

rtloader/common/builtins/aggregator.h

-4
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,8 @@
7474
extern "C" {
7575
#endif
7676

77-
#ifdef DATADOG_AGENT_THREE
7877
// PyMODINIT_FUNC macro already specifies extern "C", nesting these is legal
7978
PyMODINIT_FUNC PyInit_aggregator(void);
80-
#elif defined(DATADOG_AGENT_TWO)
81-
void Py2_init_aggregator();
82-
#endif
8379

8480
void _set_submit_metric_cb(cb_submit_metric_t cb);
8581
void _set_submit_service_check_cb(cb_submit_service_check_t cb);

rtloader/common/builtins/containers.c

-10
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,12 @@ static PyMethodDef methods[] = {
1818
{ NULL, NULL } // guards
1919
};
2020

21-
#ifdef DATADOG_AGENT_THREE
2221
static struct PyModuleDef module_def = { PyModuleDef_HEAD_INIT, CONTAINERS_MODULE_NAME, NULL, -1, methods };
2322

2423
PyMODINIT_FUNC PyInit_containers(void)
2524
{
2625
return PyModule_Create(&module_def);
2726
}
28-
#elif defined(DATADOG_AGENT_TWO)
29-
// in Python2 keep the object alive for the program lifetime
30-
static PyObject *module;
31-
32-
void Py2_init_containers()
33-
{
34-
module = Py_InitModule(CONTAINERS_MODULE_NAME, methods);
35-
}
36-
#endif
3727

3828
void _set_is_excluded_cb(cb_is_excluded_t cb)
3929
{

rtloader/common/builtins/containers.h

-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ PyMODINIT_FUNC PyInit_containers(void);
5050
extern "C" {
5151
#endif
5252

53-
#ifdef DATADOG_AGENT_TWO
54-
void Py2_init_containers();
55-
#endif
56-
5753
void _set_is_excluded_cb(cb_is_excluded_t);
5854

5955
#ifdef __cplusplus

rtloader/common/builtins/datadog_agent.c

-10
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,12 @@ static PyMethodDef methods[] = {
7070
{ NULL, NULL } // guards
7171
};
7272

73-
#ifdef DATADOG_AGENT_THREE
7473
static struct PyModuleDef module_def = { PyModuleDef_HEAD_INIT, DATADOG_AGENT_MODULE_NAME, NULL, -1, methods };
7574

7675
PyMODINIT_FUNC PyInit_datadog_agent(void)
7776
{
7877
return PyModule_Create(&module_def);
7978
}
80-
#elif defined(DATADOG_AGENT_TWO)
81-
// in Python2 keep the object alive for the program lifetime
82-
static PyObject *module;
83-
84-
void Py2_init_datadog_agent()
85-
{
86-
module = Py_InitModule(DATADOG_AGENT_MODULE_NAME, methods);
87-
}
88-
#endif
8979

9080
void _set_get_version_cb(cb_get_version_t cb)
9181
{

rtloader/common/builtins/datadog_agent.h

-4
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,7 @@
145145
extern "C" {
146146
#endif
147147

148-
#ifdef DATADOG_AGENT_THREE
149148
PyMODINIT_FUNC PyInit_datadog_agent(void);
150-
#elif defined(DATADOG_AGENT_TWO)
151-
void Py2_init_datadog_agent();
152-
#endif
153149

154150
void _set_get_clustername_cb(cb_get_clustername_t);
155151
void _set_get_config_cb(cb_get_config_t);

rtloader/common/builtins/kubeutil.c

-10
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,12 @@ static PyMethodDef methods[] = {
1919
{ NULL, NULL } // guards
2020
};
2121

22-
#ifdef DATADOG_AGENT_THREE
2322
static struct PyModuleDef module_def = { PyModuleDef_HEAD_INIT, KUBEUTIL_MODULE_NAME, NULL, -1, methods };
2423

2524
PyMODINIT_FUNC PyInit_kubeutil(void)
2625
{
2726
return PyModule_Create(&module_def);
2827
}
29-
#elif defined(DATADOG_AGENT_TWO)
30-
// in Python2 keep the object alive for the program lifetime
31-
static PyObject *module;
32-
33-
void Py2_init_kubeutil()
34-
{
35-
module = Py_InitModule(KUBEUTIL_MODULE_NAME, methods);
36-
}
37-
#endif
3828

3929
void _set_get_connection_info_cb(cb_get_connection_info_t cb)
4030
{

rtloader/common/builtins/kubeutil.h

-4
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,8 @@
4545
extern "C" {
4646
#endif
4747

48-
#ifdef DATADOG_AGENT_THREE
4948
// PyMODINIT_FUNC macro already specifies extern "C", nesting these is legal
5049
PyMODINIT_FUNC PyInit_kubeutil(void);
51-
#elif defined(DATADOG_AGENT_TWO)
52-
void Py2_init_kubeutil();
53-
#endif
5450

5551
void _set_get_connection_info_cb(cb_get_connection_info_t);
5652

rtloader/common/builtins/tagger.c

-11
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ static void add_constants(PyObject *m)
172172
PyModule_AddIntConstant(m, "HIGH", DATADOG_AGENT_RTLOADER_TAGGER_HIGH);
173173
}
174174

175-
#ifdef DATADOG_AGENT_THREE
176175
static struct PyModuleDef module_def = { PyModuleDef_HEAD_INIT, TAGGER_MODULE_NAME, NULL, -1, methods };
177176

178177
PyMODINIT_FUNC PyInit_tagger(void)
@@ -181,13 +180,3 @@ PyMODINIT_FUNC PyInit_tagger(void)
181180
add_constants(module);
182181
return module;
183182
}
184-
#elif defined(DATADOG_AGENT_TWO)
185-
// in Python2 keep the object alive for the program lifetime
186-
static PyObject *module;
187-
188-
void Py2_init_tagger()
189-
{
190-
module = Py_InitModule(TAGGER_MODULE_NAME, methods);
191-
add_constants(module);
192-
}
193-
#endif

rtloader/common/builtins/tagger.h

-6
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,12 @@
4646

4747
#define TAGGER_MODULE_NAME "tagger"
4848

49-
#ifdef DATADOG_AGENT_THREE
5049
PyMODINIT_FUNC PyInit_tagger(void);
51-
#endif
5250

5351
#ifdef __cplusplus
5452
extern "C" {
5553
#endif
5654

57-
#ifdef DATADOG_AGENT_TWO
58-
void Py2_init_tagger();
59-
#endif
60-
6155
void _set_tags_cb(cb_tags_t);
6256

6357
#ifdef __cplusplus

rtloader/common/builtins/util.c

-10
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,12 @@ static PyMethodDef methods[] = {
1919
{ NULL, NULL } // guards
2020
};
2121

22-
#ifdef DATADOG_AGENT_THREE
2322
static struct PyModuleDef module_def = { PyModuleDef_HEAD_INIT, UTIL_MODULE_NAME, NULL, -1, methods };
2423

2524
PyMODINIT_FUNC PyInit_util(void)
2625
{
2726
return PyModule_Create(&module_def);
2827
}
29-
#elif defined(DATADOG_AGENT_TWO)
30-
// in Python2 keep the object alive for the program lifetime
31-
static PyObject *module;
32-
33-
void Py2_init_util()
34-
{
35-
module = Py_InitModule(UTIL_MODULE_NAME, methods);
36-
}
37-
#endif
3828

3929
/*! \fn PyObject *headers(PyObject *self, PyObject *args, PyObject *kwargs)
4030
\brief This function provides a standard set of HTTP headers the caller might want to

0 commit comments

Comments
 (0)