Skip to content

Commit ea8a355

Browse files
committed
Add support for file based configuration (library-config)
1 parent f157156 commit ea8a355

File tree

9 files changed

+253
-10
lines changed

9 files changed

+253
-10
lines changed

Diff for: Cargo.lock

+49-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Makefile

+6-3
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,9 @@ clang_format_fix:
406406
cbindgen: remove_cbindgen generate_cbindgen
407407

408408
remove_cbindgen:
409-
rm -f components-rs/ddtrace.h components-rs/live-debugger.h components-rs/telemetry.h components-rs/sidecar.h components-rs/common.h components-rs/crashtracker.h
409+
rm -f components-rs/ddtrace.h components-rs/live-debugger.h components-rs/telemetry.h components-rs/sidecar.h components-rs/common.h components-rs/crashtracker.h components-rs/library-config.h
410410

411-
generate_cbindgen: cbindgen_binary # Regenerate components-rs/ddtrace.h components-rs/live-debugger.h components-rs/telemetry.h components-rs/sidecar.h components-rs/common.h components-rs/crashtracker.h
411+
generate_cbindgen: cbindgen_binary # Regenerate components-rs/ddtrace.h components-rs/live-debugger.h components-rs/telemetry.h components-rs/sidecar.h components-rs/common.h components-rs/crashtracker.h components-rs/library-config.h
412412
( \
413413
$(command rustup && echo run nightly --) cbindgen --crate ddtrace-php \
414414
--config cbindgen.toml \
@@ -429,11 +429,14 @@ generate_cbindgen: cbindgen_binary # Regenerate components-rs/ddtrace.h componen
429429
$(command rustup && echo run nightly --) cbindgen --crate datadog-crashtracker-ffi \
430430
--config crashtracker-ffi/cbindgen.toml \
431431
--output $(PROJECT_ROOT)/components-rs/crashtracker.h; \
432+
$(command rustup && echo run nightly --) cbindgen --crate datadog-library-config-ffi \
433+
--config library-config-ffi/cbindgen.toml \
434+
--output $(PROJECT_ROOT)/components-rs/library-config.h; \
432435
if test -d $(PROJECT_ROOT)/tmp; then \
433436
mkdir -pv "$(BUILD_DIR)"; \
434437
export CARGO_TARGET_DIR="$(BUILD_DIR)/target"; \
435438
fi; \
436-
cargo run -p tools -- $(PROJECT_ROOT)/components-rs/common.h $(PROJECT_ROOT)/components-rs/ddtrace.h $(PROJECT_ROOT)/components-rs/live-debugger.h $(PROJECT_ROOT)/components-rs/telemetry.h $(PROJECT_ROOT)/components-rs/sidecar.h $(PROJECT_ROOT)/components-rs/crashtracker.h \
439+
cargo run -p tools -- $(PROJECT_ROOT)/components-rs/common.h $(PROJECT_ROOT)/components-rs/ddtrace.h $(PROJECT_ROOT)/components-rs/live-debugger.h $(PROJECT_ROOT)/components-rs/telemetry.h $(PROJECT_ROOT)/components-rs/sidecar.h $(PROJECT_ROOT)/components-rs/crashtracker.h $(PROJECT_ROOT)/components-rs/library-config.h \
437440
)
438441

439442
cbindgen_binary:

Diff for: components-rs/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ datadog-remote-config = { path = "../libdatadog/remote-config" }
2020
datadog-sidecar = { path = "../libdatadog/sidecar" }
2121
datadog-sidecar-ffi = { path = "../libdatadog/sidecar-ffi" }
2222
datadog-crashtracker-ffi = { path = "../libdatadog/crashtracker-ffi", default-features = false, features = ["collector"] }
23+
datadog-library-config-ffi = { path = "../libdatadog/library-config-ffi" }
2324
spawn_worker = { path = "../libdatadog/spawn_worker" }
2425
anyhow = { version = "1.0" }
2526
const-str = "0.5.6"

Diff for: components-rs/common.h

+115
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,121 @@ typedef struct ddog_crasht_Result_StringWrapper {
12891289
};
12901290
} ddog_crasht_Result_StringWrapper;
12911291

1292+
typedef enum ddog_LibraryConfigName {
1293+
DDOG_LIBRARY_CONFIG_NAME_DD_TRACE_DEBUG = 0,
1294+
DDOG_LIBRARY_CONFIG_NAME_DD_SERVICE = 1,
1295+
DDOG_LIBRARY_CONFIG_NAME_DD_ENV = 2,
1296+
DDOG_LIBRARY_CONFIG_NAME_DD_VERSION = 3,
1297+
DDOG_LIBRARY_CONFIG_NAME_DD_PROFILING_ENABLED = 4,
1298+
} ddog_LibraryConfigName;
1299+
1300+
typedef struct ddog_Configurator ddog_Configurator;
1301+
1302+
/**
1303+
* Ffi safe type representing an owned null-terminated C array
1304+
* Equivalent to a std::ffi::CString
1305+
*/
1306+
typedef struct ddog_CString {
1307+
/**
1308+
* Null terminated char array
1309+
*/
1310+
char *ptr;
1311+
/**
1312+
* Length of the array, not counting the null-terminator
1313+
*/
1314+
uintptr_t length;
1315+
} ddog_CString;
1316+
1317+
typedef struct ddog_LibraryConfig {
1318+
enum ddog_LibraryConfigName name;
1319+
struct ddog_CString value;
1320+
} ddog_LibraryConfig;
1321+
1322+
/**
1323+
* Holds the raw parts of a Rust Vec; it should only be created from Rust,
1324+
* never from C.
1325+
*/
1326+
typedef struct ddog_Vec_LibraryConfig {
1327+
const struct ddog_LibraryConfig *ptr;
1328+
uintptr_t len;
1329+
uintptr_t capacity;
1330+
} ddog_Vec_LibraryConfig;
1331+
1332+
/**
1333+
* A generic result type for when an operation may fail,
1334+
* or may return <T> in case of success.
1335+
*/
1336+
typedef enum ddog_Result_VecLibraryConfig_Tag {
1337+
DDOG_RESULT_VEC_LIBRARY_CONFIG_OK_VEC_LIBRARY_CONFIG,
1338+
DDOG_RESULT_VEC_LIBRARY_CONFIG_ERR_VEC_LIBRARY_CONFIG,
1339+
} ddog_Result_VecLibraryConfig_Tag;
1340+
1341+
typedef struct ddog_Result_VecLibraryConfig {
1342+
ddog_Result_VecLibraryConfig_Tag tag;
1343+
union {
1344+
struct {
1345+
struct ddog_Vec_LibraryConfig ok;
1346+
};
1347+
struct {
1348+
struct ddog_Error err;
1349+
};
1350+
};
1351+
} ddog_Result_VecLibraryConfig;
1352+
1353+
typedef struct ddog_Slice_CharSlice {
1354+
/**
1355+
* Should be non-null and suitably aligned for the underlying type. It is
1356+
* allowed but not recommended for the pointer to be null when the len is
1357+
* zero.
1358+
*/
1359+
const ddog_CharSlice *ptr;
1360+
/**
1361+
* The number of elements (not bytes) that `.ptr` points to. Must be less
1362+
* than or equal to [isize::MAX].
1363+
*/
1364+
uintptr_t len;
1365+
} ddog_Slice_CharSlice;
1366+
1367+
typedef struct ddog_ProcessInfo {
1368+
struct ddog_Slice_CharSlice args;
1369+
struct ddog_Slice_CharSlice envp;
1370+
ddog_CharSlice language;
1371+
} ddog_ProcessInfo;
1372+
1373+
typedef struct ddog_Slice_U8 {
1374+
/**
1375+
* Should be non-null and suitably aligned for the underlying type. It is
1376+
* allowed but not recommended for the pointer to be null when the len is
1377+
* zero.
1378+
*/
1379+
const uint8_t *ptr;
1380+
/**
1381+
* The number of elements (not bytes) that `.ptr` points to. Must be less
1382+
* than or equal to [isize::MAX].
1383+
*/
1384+
uintptr_t len;
1385+
} ddog_Slice_U8;
1386+
1387+
/**
1388+
* Use to represent bytes -- does not need to be valid UTF-8.
1389+
*/
1390+
typedef struct ddog_Slice_U8 ddog_ByteSlice;
1391+
1392+
/**
1393+
* Ffi safe type representing a borrowed null-terminated C array
1394+
* Equivalent to a std::ffi::CStr
1395+
*/
1396+
typedef struct ddog_CStr {
1397+
/**
1398+
* Null terminated char array
1399+
*/
1400+
char *ptr;
1401+
/**
1402+
* Length of the array, not counting the null-terminator
1403+
*/
1404+
uintptr_t length;
1405+
} ddog_CStr;
1406+
12921407
#ifdef __cplusplus
12931408
extern "C" {
12941409
#endif // __cplusplus

Diff for: components-rs/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use ddcommon::{parse_uri, Endpoint};
2121
use ddcommon_ffi::slice::AsBytes;
2222
pub use ddcommon_ffi::*;
2323
pub use ddtelemetry_ffi::*;
24+
pub use datadog_library_config_ffi::*;
2425

2526
#[no_mangle]
2627
#[allow(non_upper_case_globals)]

Diff for: components-rs/library-config.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
5+
#ifndef DDOG_LIBRARY_CONFIG_H
6+
#define DDOG_LIBRARY_CONFIG_H
7+
8+
#pragma once
9+
10+
#include "common.h"
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif // __cplusplus
15+
16+
struct ddog_Configurator *ddog_library_configurator_new(bool debug_logs);
17+
18+
void ddog_library_configurator_drop(struct ddog_Configurator*);
19+
20+
struct ddog_Result_VecLibraryConfig ddog_library_configurator_get_path(const struct ddog_Configurator *configurator,
21+
struct ddog_ProcessInfo process_info,
22+
ddog_CharSlice path);
23+
24+
struct ddog_Result_VecLibraryConfig ddog_library_configurator_get(const struct ddog_Configurator *configurator,
25+
struct ddog_ProcessInfo process_info);
26+
27+
struct ddog_Result_VecLibraryConfig ddog_library_configurator_get_from_bytes(const struct ddog_Configurator *configurator,
28+
struct ddog_ProcessInfo process_info,
29+
ddog_ByteSlice config_bytes);
30+
31+
struct ddog_CStr ddog_library_config_name_to_env(enum ddog_LibraryConfigName name);
32+
33+
void ddog_library_config_drop(struct ddog_Vec_LibraryConfig);
34+
35+
#ifdef __cplusplus
36+
} // extern "C"
37+
#endif // __cplusplus
38+
39+
#endif /* DDOG_LIBRARY_CONFIG_H */

Diff for: ext/configuration.c

+39
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "configuration.h"
22

33
#include <assert.h>
4+
#include <SAPI.h>
45

56
#include "ip_extraction.h"
67
#include "logging.h"
78
#include "json/json.h"
89
#include "sidecar.h"
910
#include <components/log/log.h>
11+
#include <components-rs/library-config.h>
1012
#include <zai_string/string.h>
1113
#include "sidecar.h"
1214

@@ -236,6 +238,43 @@ void ddtrace_config_first_rinit() {
236238
runtime_config_first_init = true;
237239
}
238240

241+
// FIXME: where should it be done? minit? rinit? first rinit???
242+
void ddtrace_config_rinit() {
243+
// FIXME: should be done directly in the library-config crate
244+
ddog_CharSlice args[] = {
245+
DDOG_CHARSLICE_C("/usr/bin/php"),
246+
};
247+
248+
ddog_CharSlice envp[] = {
249+
DDOG_CHARSLICE_C("FOO=BAR"),
250+
};
251+
ddog_ProcessInfo process_info = {
252+
.args = args,
253+
.envp = envp,
254+
.language = DDOG_CHARSLICE_C("php")
255+
};
256+
257+
ddog_Configurator *configurator = ddog_library_configurator_new(true);
258+
ddog_Result_VecLibraryConfig config_result = ddog_library_configurator_get_path(configurator, process_info, DDOG_CHARSLICE_C("./config.yaml"));
259+
// ddog_Result_VecLibraryConfig config_result = ddog_library_configurator_get(configurator, process_info);
260+
261+
if (config_result.tag != DDOG_RESULT_VEC_LIBRARY_CONFIG_ERR_VEC_LIBRARY_CONFIG) {
262+
ddog_Vec_LibraryConfig configs = config_result.ok;
263+
for (int i = 0; i < configs.len; i++) {
264+
const ddog_LibraryConfig *cfg = &configs.ptr[i];
265+
ddog_CStr name = ddog_library_config_name_to_env(cfg->name);
266+
267+
printf("Setting env variable: %s=%s\n", name.ptr, cfg->value.ptr);
268+
setenv(name.ptr, cfg->value.ptr, 1);
269+
}
270+
} else {
271+
// ddog_Error err = config_result.err;
272+
// fprintf(stderr, "%.*s", (int)err.message.len, err.message.ptr);
273+
// ddog_Error_drop(&err);
274+
}
275+
}
276+
277+
239278
// note: only call this if get_DD_TRACE_ENABLED() returns true
240279
bool ddtrace_config_integration_enabled(ddtrace_integration_name integration_name) {
241280
ddtrace_integration *integration = &ddtrace_integrations[integration_name];

0 commit comments

Comments
 (0)