-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathinit.go
More file actions
551 lines (460 loc) · 16.8 KB
/
Copy pathinit.go
File metadata and controls
551 lines (460 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build python
package python
import (
"errors"
"expvar"
"fmt"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"sync"
"time"
"unsafe"
telemetryimpl "github.com/DataDog/datadog-agent/comp/core/telemetry/impl"
"github.com/DataDog/datadog-agent/pkg/aggregator"
coreaggregator "github.com/DataDog/datadog-agent/pkg/collector/aggregator"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
configutils "github.com/DataDog/datadog-agent/pkg/config/utils"
"github.com/DataDog/datadog-agent/pkg/fips"
"github.com/DataDog/datadog-agent/pkg/metrics"
"github.com/DataDog/datadog-agent/pkg/tagset"
"github.com/DataDog/datadog-agent/pkg/util/cache"
"github.com/DataDog/datadog-agent/pkg/util/executable"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/version"
)
/*
// On AIX, Go's CGO requires shared libraries to be wrapped in .a archives.
// libdatadog-agent-rtloader.a is built from the .so file using "ar -X64 -r".
#cgo aix LDFLAGS: -L${SRCDIR}/../../../rtloader/build/rtloader -ldatadog-agent-rtloader -ldl
#cgo !aix,!windows LDFLAGS: -L${SRCDIR}/../../../rtloader/build/rtloader -ldatadog-agent-rtloader -ldl
#cgo windows LDFLAGS: -L${SRCDIR}/../../../rtloader/build/rtloader -ldatadog-agent-rtloader -lstdc++ -static
#cgo CFLAGS: -I "${SRCDIR}/../../../rtloader/include" -I "${SRCDIR}/../../../rtloader/common"
#include "datadog_agent_rtloader.h"
#include "rtloader_mem.h"
#include <stdlib.h>
// helpers
char *getStringAddr(char **array, unsigned int idx) {
return array[idx];
}
//
// init free method
//
// On windows we need to free memory in the same DLL where it was allocated.
// This allows rtloader to free memory returned by Go callbacks.
//
void initCgoFree(rtloader_t *rtloader) {
set_cgo_free_cb(rtloader, _free);
}
//
// init log method
//
void LogMessage(char *, int);
void initLogger(rtloader_t *rtloader) {
set_log_cb(rtloader, LogMessage);
}
//
// datadog_agent module
//
// This also init "util" module who expose the same "headers" function
//
void GetClusterName(char **);
void GetConfig(char*, char **);
void GetHostname(char **);
void GetHostTags(char **);
void GetVersion(char **);
void Headers(char **);
char * ReadPersistentCache(char *);
void SendLog(char *, char *);
void SetCheckMetadata(char *, char *, char *);
void SetExternalTags(char *, char *, char **);
void WritePersistentCache(char *, char *);
bool TracemallocEnabled();
char* ObfuscateSQL(char *, char *, char **);
char* ObfuscateSQLExecPlan(char *, bool, char **);
double getProcessStartTime();
char* ObfuscateMongoDBString(char *, char **);
void EmitAgentTelemetry(char *, char *, double, char *);
void ReportIssue(char *, char *, char **);
void ResolveIssue(char *, char **);
char* ParsePrometheusMetrics(char *, char *, char **);
char* ProcessPrometheusMetrics(char *, char *, char *, char **);
void initDatadogAgentModule(rtloader_t *rtloader) {
set_get_clustername_cb(rtloader, GetClusterName);
set_get_config_cb(rtloader, GetConfig);
set_get_hostname_cb(rtloader, GetHostname);
set_get_host_tags_cb(rtloader, GetHostTags);
set_get_version_cb(rtloader, GetVersion);
set_headers_cb(rtloader, Headers);
set_send_log_cb(rtloader, SendLog);
set_set_check_metadata_cb(rtloader, SetCheckMetadata);
set_set_external_tags_cb(rtloader, SetExternalTags);
set_write_persistent_cache_cb(rtloader, WritePersistentCache);
set_read_persistent_cache_cb(rtloader, ReadPersistentCache);
set_tracemalloc_enabled_cb(rtloader, TracemallocEnabled);
set_obfuscate_sql_cb(rtloader, ObfuscateSQL);
set_obfuscate_sql_exec_plan_cb(rtloader, ObfuscateSQLExecPlan);
set_get_process_start_time_cb(rtloader, getProcessStartTime);
set_obfuscate_mongodb_string_cb(rtloader, ObfuscateMongoDBString);
set_emit_agent_telemetry_cb(rtloader, EmitAgentTelemetry);
set_report_issue_cb(rtloader, ReportIssue);
set_resolve_issue_cb(rtloader, ResolveIssue);
set_parse_prometheus_metrics_cb(rtloader, ParsePrometheusMetrics);
set_process_prometheus_metrics_cb(rtloader, ProcessPrometheusMetrics);
}
//
// aggregator module
//
// The submit callbacks are owned by the collector aggregator package; their
// addresses are received here as opaque pointers and registered with rtloader.
// Referencing those exported symbols directly would fail this package's cgo
// link on the MinGW/Windows linker.
void initAggregatorModule(rtloader_t *rtloader, void *m, void *sc, void *e, void *h, void *ep) {
set_submit_metric_cb(rtloader, (cb_submit_metric_t)m);
set_submit_service_check_cb(rtloader, (cb_submit_service_check_t)sc);
set_submit_event_cb(rtloader, (cb_submit_event_t)e);
set_submit_histogram_bucket_cb(rtloader, (cb_submit_histogram_bucket_t)h);
set_submit_event_platform_event_cb(rtloader, (cb_submit_event_platform_event_t)ep);
}
//
// _util module
//
void GetSubprocessOutput(char **, char **, char **, char **, int*, char **);
void initUtilModule(rtloader_t *rtloader) {
set_get_subprocess_output_cb(rtloader, GetSubprocessOutput);
}
//
// tagger module
//
char **Tags(char *, int);
void initTaggerModule(rtloader_t *rtloader) {
set_tags_cb(rtloader, Tags);
}
//
// containers module
//
int IsContainerExcluded(char *, char *, char *);
void initContainersModule(rtloader_t *rtloader) {
set_is_excluded_cb(rtloader, IsContainerExcluded);
}
//
// kubeutil module
//
void GetKubeletConnectionInfo(char **);
void initkubeutilModule(rtloader_t *rtloader) {
set_get_connection_info_cb(rtloader, GetKubeletConnectionInfo);
}
//
// Wrapper to call _free function pointer from CGO
//
static inline void call_free(void* ptr) {
_free(ptr);
}
*/
import "C"
// InterpreterResolutionError is our custom error for when our interpreter
// path resolution fails
type InterpreterResolutionError struct {
IsFatal bool
Err error
}
func (ire InterpreterResolutionError) Error() string {
if ire.IsFatal {
return fmt.Sprintf("Error trying to resolve interpreter path: '%v'."+
" You can set 'allow_python_path_heuristics_failure' to ignore this error.", ire.Err)
}
return fmt.Sprintf("Error trying to resolve interpreter path: '%v'."+
" Python's 'multiprocessing' library may fail to work.", ire.Err)
}
//nolint:revive
const PythonWinExeBasename = "python.exe"
var (
// PythonVersion contains the interpreter version string provided by
// `sys.version`. It's empty if the interpreter was not initialized.
PythonVersion = ""
// The pythonHome variable typically comes from -ldflags
// it's needed in case the agent was built using embedded libs
pythonHome3 = ""
// PythonHome contains the computed value of the Python Home path once the
// intepreter is created. It might be empty in case the interpreter wasn't
// initialized, or the Agent was built using system libs and the env var
// PYTHONHOME is empty. It's expected to always contain a value when the
// Agent is built using embedded libs.
PythonHome = ""
pythonBinPath = ""
// PythonPath contains the string representation of the Python list returned
// by `sys.path`. It's empty if the interpreter was not initialized.
PythonPath = ""
rtloader *C.rtloader_t
expvarPyInit *expvar.Map
pyInitLock sync.RWMutex
pyDestroyLock sync.RWMutex
pyInitErrors []string
// ErrNotInitialized is returned when rtloader is not initialized yet
ErrNotInitialized = errors.New("rtloader is not initialized")
)
func init() {
pyInitErrors = []string{}
expvarPyInit = expvar.NewMap("pythonInit")
expvarPyInit.Set("Errors", expvar.Func(expvarPythonInitErrors))
// Setting environment variables must happen as early as possible in the process lifetime to avoid data race with
// `getenv`. Ideally before we start any goroutines that call native code or open network connections.
initFIPS()
}
func expvarPythonInitErrors() interface{} {
pyInitLock.RLock()
defer pyInitLock.RUnlock()
return slices.Clone(pyInitErrors)
}
func addExpvarPythonInitErrors(msg string) error {
pyInitLock.Lock()
defer pyInitLock.Unlock()
pyInitErrors = append(pyInitErrors, msg)
return errors.New(msg)
}
func sendTelemetry() {
tags := []string{
"python_version:3",
}
if agentVersion, err := version.Agent(); err == nil {
tags = append(tags,
fmt.Sprintf("agent_version_major:%d", agentVersion.Major),
fmt.Sprintf("agent_version_minor:%d", agentVersion.Minor),
fmt.Sprintf("agent_version_patch:%d", agentVersion.Patch),
)
}
aggregator.AddRecurrentSeries(&metrics.Serie{
Name: "datadog.agent.python.version",
Points: []metrics.Point{{Value: 1.0}},
Tags: tagset.CompositeTagsFromSlice(tags),
MType: metrics.APIGaugeType,
})
}
func pathToBinary(name string, ignoreErrors bool) (string, error) {
absPath, err := executable.ResolvePath(name)
if err != nil {
resolutionError := InterpreterResolutionError{
IsFatal: !ignoreErrors,
Err: err,
}
log.Error(resolutionError)
if ignoreErrors {
return name, nil
}
return "", resolutionError
}
return absPath, nil
}
func resolvePythonHome() {
// Allow to relatively import python
_here, err := executable.Folder()
if err != nil {
log.Warnf("Error getting executable folder: %v", err)
log.Warnf("Trying again allowing symlink resolution to fail")
_here, err = executable.FolderAllowSymlinkFailure()
if err != nil {
log.Warnf("Error getting executable folder w/o symlinks: %v", err)
}
}
log.Debugf("Executable folder is %v", _here)
var embeddedPythonHome3 string
if runtime.GOOS == "windows" {
embeddedPythonHome3 = filepath.Join(_here, "..", "embedded3")
} else { // Both macOS and Linux have the same relative paths
embeddedPythonHome3 = filepath.Join(_here, "../..", "embedded")
}
// We want to use the path-relative embedded2/3 directories above by default.
// They will be correct for normal installation on Windows. However, if they
// are not present for cases like running unit tests, fall back to the compile
// time values.
if _, err := os.Stat(embeddedPythonHome3); os.IsNotExist(err) {
log.Warnf("Relative embedded directory not found for Python 3. Using default: %s", pythonHome3)
} else {
pythonHome3 = embeddedPythonHome3
}
PythonHome = pythonHome3
log.Infof("Using '%s' as Python home", PythonHome)
}
func resolvePythonExecPath(ignoreErrors bool) (string, error) {
resolvePythonHome()
// For Windows, the binary should be in our path already and have a
// consistent name
if runtime.GOOS == "windows" {
// If we are in a development environment, PythonHome will not be set so we
// use the absolute path to the python.exe in our path.
if PythonHome == "" {
log.Warnf("Python home is empty. Inferring interpreter path from binary in path.")
return pathToBinary(PythonWinExeBasename, ignoreErrors)
}
return filepath.Join(PythonHome, PythonWinExeBasename), nil
}
// On *nix both Python versions are installed in the same embedded directory. We
// don't want to use the default version (aka "python") but rather "python2" or
// "python3" based on the configuration. Also on some Python3 platforms there
// are no "python" aliases either.
interpreterBasename := "python3"
// If we are in a development env or just the ldflags haven't been set, the PythonHome
// variable won't be set so what we do here is to just find out where our current
// default in-path "python2"/"python3" command is located and get its absolute path.
if PythonHome == "" {
log.Warnf("Python home is empty. Inferring interpreter path from binary in path.")
return pathToBinary(interpreterBasename, ignoreErrors)
}
// If we're here, the ldflags have been used so we key off of those to get the
// absolute path of the interpreter executable
return filepath.Join(PythonHome, "bin", interpreterBasename), nil
}
// Initialize initializes the Python interpreter
func Initialize(paths ...string) error {
allowPathHeuristicsFailure := pkgconfigsetup.Datadog().GetBool("allow_python_path_heuristics_failure")
// Memory related RTLoader-global initialization
if pkgconfigsetup.Datadog().GetBool("memtrack_enabled") {
InitMemoryTracker()
}
// Any platform-specific initialization
// should be done before rtloader initialization
if initializePlatform() != nil {
log.Warnf("Unable to complete platform-specific initialization - should be non-fatal")
}
// Note: pythonBinPath is a module-level var
pythonBinPath, err := resolvePythonExecPath(allowPathHeuristicsFailure)
if err != nil {
return err
}
log.Debugf("Using '%s' as Python interpreter path", pythonBinPath)
var pyErr *C.char
csPythonHome := TrackedCString(PythonHome)
defer C.call_free(unsafe.Pointer(csPythonHome))
csPythonExecPath := TrackedCString(pythonBinPath)
defer C.call_free(unsafe.Pointer(csPythonExecPath))
log.Infof("Initializing rtloader with Python 3 %s", PythonHome)
rtloader = C.make3(csPythonHome, csPythonExecPath, &pyErr)
if rtloader == nil {
err := addExpvarPythonInitErrors(
"could not load runtime python for version 3: " + C.GoString(pyErr),
)
if pyErr != nil {
// pyErr tracked when created in rtloader
C.call_free(unsafe.Pointer(pyErr))
}
return err
}
// Should we track python memory?
if pkgconfigsetup.Datadog().GetBool("telemetry.python_memory") {
var interval time.Duration
if pkgconfigsetup.Datadog().GetBool("telemetry.enabled") {
// detailed telemetry is enabled
interval = 1 * time.Second
} else if configutils.IsAgentTelemetryEnabled(pkgconfigsetup.Datadog()) {
// default telemetry is enabled (emitted every 15 minute)
interval = 15 * time.Minute
}
// interval is 0 if telemetry is disabled
if interval > 0 {
initPymemTelemetry(interval)
}
}
// Set the PYTHONPATH if needed.
for _, p := range paths {
// bounded but never released allocations with CString
C.add_python_path(rtloader, TrackedCString(p))
}
// Setup custom builtin before RtLoader initialization
C.initCgoFree(rtloader)
C.initLogger(rtloader)
C.initDatadogAgentModule(rtloader)
aggCb := coreaggregator.GetCallbacks()
C.initAggregatorModule(rtloader, aggCb.Metric, aggCb.ServiceCheck, aggCb.Event, aggCb.HistogramBucket, aggCb.EventPlatformEvent)
C.initUtilModule(rtloader)
C.initTaggerModule(rtloader)
C.initContainersModule(rtloader)
C.initkubeutilModule(rtloader)
// Init RtLoader machinery
if C.init(rtloader) == 0 {
err := "could not initialize rtloader: " + C.GoString(C.get_error(rtloader))
return addExpvarPythonInitErrors(err)
}
// Lock the GIL
glock, err := newStickyLock()
if err != nil {
return err
}
pyInfo := C.get_py_info(rtloader)
glock.unlock()
// store the Python version after killing \n chars within the string
if pyInfo != nil {
PythonVersion = strings.ReplaceAll(C.GoString(pyInfo.version), "\n", "")
// Set python version in the cache
cache.Cache.Set(pythonInfoCacheKey, PythonVersion, cache.NoExpiration)
PythonPath = C.GoString(pyInfo.path)
C.free_py_info(rtloader, pyInfo)
} else {
log.Errorf("Could not query python information: %s", C.GoString(C.get_error(rtloader)))
}
sendTelemetry()
return nil
}
// GetRtLoader returns the underlying rtloader_t struct. This is meant for testing and
// tooling, use the rtloader_t struct at your own risk
func GetRtLoader() *C.rtloader_t {
return rtloader
}
func initPymemTelemetry(d time.Duration) {
C.init_pymem_stats(rtloader)
// "alloc" for consistency with go memstats and mallochook metrics.
alloc := telemetryimpl.GetCompatComponent().NewSimpleCounter("pymem", "alloc", "Total number of bytes allocated by the python interpreter since the start of the agent.")
inuse := telemetryimpl.GetCompatComponent().NewSimpleGauge("pymem", "inuse", "Number of bytes currently allocated by the python interpreter.")
go func() {
t := time.NewTicker(d)
var prevAlloc C.size_t
for range t.C {
var s C.pymem_stats_t
C.get_pymem_stats(rtloader, &s)
inuse.Set(float64(s.inuse))
alloc.Add(float64(s.alloc - prevAlloc))
prevAlloc = s.alloc
}
}()
}
func initFIPS() {
fipsEnabled, err := fips.Enabled()
if err != nil {
log.Warnf("could not check FIPS mode: %v", err)
return
}
resolvePythonHome()
if PythonHome == "" {
log.Warnf("Python home is empty. FIPS mode could not be enabled.")
return
}
if fipsEnabled {
err := enableFIPS(PythonHome)
if err != nil {
log.Warnf("could not initialize FIPS mode: %v", err)
}
}
}
// enableFIPS sets the OPENSSL_CONF and OPENSSL_MODULES environment variables
func enableFIPS(embeddedPath string) error {
envVars := map[string][]string{
"OPENSSL_CONF": {embeddedPath, "ssl", "openssl.cnf"},
"OPENSSL_MODULES": {embeddedPath, "lib", "ossl-modules"},
}
for envVar, pathParts := range envVars {
if v := os.Getenv(envVar); v != "" {
continue
}
path := filepath.Join(pathParts...)
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("path %q does not exist", path)
}
os.Setenv(envVar, path)
}
return nil
}