Skip to content

Commit 0d9d364

Browse files
committed
Adds process tags to remote config payload
1 parent 1f7f4ca commit 0d9d364

6 files changed

Lines changed: 104 additions & 2 deletions

File tree

components-rs/ddtrace.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ bool ddog_remote_configs_service_env_change(struct ddog_RemoteConfigState *remot
8080
ddog_CharSlice service,
8181
ddog_CharSlice env,
8282
ddog_CharSlice version,
83-
const struct ddog_Vec_Tag *tags);
83+
const struct ddog_Vec_Tag *tags,
84+
const struct ddog_Vec_Tag *process_tags);
8485

8586
bool ddog_remote_config_alter_dynamic_config(struct ddog_RemoteConfigState *remote_config,
8687
ddog_CharSlice config,

components-rs/remote_config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,14 @@ pub extern "C" fn ddog_remote_configs_service_env_change(
494494
env: CharSlice,
495495
version: CharSlice,
496496
tags: &libdd_common_ffi::Vec<Tag>,
497+
process_tags: &libdd_common_ffi::Vec<Tag>,
497498
) -> bool {
498499
let new_target = Target {
499500
service: service.to_utf8_lossy().to_string(),
500501
env: env.to_utf8_lossy().to_string(),
501502
app_version: version.to_utf8_lossy().to_string(),
502503
tags: tags.as_slice().to_vec(),
504+
process_tags: process_tags.as_slice().to_vec(),
503505
};
504506

505507
if let Some(target) = remote_config.manager.get_target() {

ext/process_tags.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef struct {
3232
size_t count;
3333
size_t capacity;
3434
zend_string *serialized;
35+
ddog_Vec_Tag vec;
3536
} process_tags_t;
3637

3738
static process_tags_t process_tags = {0};
@@ -190,12 +191,35 @@ static void serialize_process_tags(void) {
190191
}
191192

192193
smart_str_free(&buf);
194+
195+
process_tags.vec = ddog_Vec_Tag_new();
196+
for (size_t i = 0; i < process_tags.count; i++) {
197+
const char* key = process_tags.tag_list[i].key;
198+
const char* value = process_tags.tag_list[i].value;
199+
200+
UNUSED(ddog_Vec_Tag_push(&process_tags.vec,
201+
(ddog_CharSlice) {.ptr = key, .len = strlen(key)},
202+
(ddog_CharSlice) {.ptr = value, .len = strlen(value)}
203+
));
204+
}
193205
}
194206

195207
zend_string *ddtrace_process_tags_get_serialized(void) {
196208
return (ddtrace_process_tags_enabled() && process_tags.serialized) ? process_tags.serialized : ZSTR_EMPTY_ALLOC();
197209
}
198210

211+
const ddog_Vec_Tag *ddtrace_process_tags_get_vec(void) {
212+
if (ddtrace_process_tags_enabled()) {
213+
return &process_tags.vec;
214+
}
215+
216+
static ddog_Vec_Tag empty_vec;
217+
if (!empty_vec.ptr) {
218+
empty_vec = ddog_Vec_Tag_new();
219+
}
220+
return &empty_vec;
221+
}
222+
199223
bool ddtrace_process_tags_enabled(void){
200224
return get_global_DD_EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED();
201225
}
@@ -224,5 +248,10 @@ void ddtrace_process_tags_mshutdown(void) {
224248
if (process_tags.serialized) {
225249
zend_string_release(process_tags.serialized);
226250
}
251+
252+
if (process_tags.vec.ptr) {
253+
ddog_Vec_Tag_drop(process_tags.vec);
254+
}
255+
227256
memset(&process_tags, 0, sizeof(process_tags));
228257
}

ext/process_tags.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <stdbool.h>
55
#include "Zend/zend_types.h"
66
#include "ddtrace_export.h"
7+
#include "components-rs/common.h"
8+
79

810
// Called at first RINIT to collect process tags
911
void ddtrace_process_tags_first_rinit(void);
@@ -18,4 +20,8 @@ bool ddtrace_process_tags_enabled(void);
1820
// Returns NULL if disabled or not yet collected
1921
DDTRACE_PUBLIC zend_string *ddtrace_process_tags_get_serialized(void);
2022

23+
// Get a pointer to the process tags Vec<Tag>
24+
// Returns a pointer to an empty Vec if disabled or not yet collected
25+
const ddog_Vec_Tag *ddtrace_process_tags_get_vec(void);
26+
2127
#endif // DD_PROCESS_TAGS_H

ext/sidecar.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,11 @@ void ddtrace_sidecar_submit_root_span_data_direct(ddog_SidecarTransport **transp
528528
}
529529
ddog_CharSlice version_slice = dd_zend_string_to_CharSlice(version_string);
530530

531+
const ddog_Vec_Tag *process_tags = ddtrace_process_tags_get_vec();
532+
531533
bool changed = true;
532534
if (DDTRACE_G(remote_config_state)) {
533-
changed = ddog_remote_configs_service_env_change(DDTRACE_G(remote_config_state), service_slice, env_slice, version_slice, &DDTRACE_G(active_global_tags));
535+
changed = ddog_remote_configs_service_env_change(DDTRACE_G(remote_config_state), service_slice, env_slice, version_slice, &DDTRACE_G(active_global_tags), process_tags);
534536
if (!changed && root) {
535537
// ddog_remote_configs_service_env_change() generally only processes configs if they changed. However, upon request initialization it may be identical to the previous request.
536538
// However, at request shutdown some configs are unloaded. Explicitly forcing a processing step ensures these are re-loaded.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
--TEST--
2+
Test remote config request payload
3+
--SKIPIF--
4+
<?php include __DIR__ . '/../includes/skipif_no_dev_env.inc'; ?>
5+
--ENV--
6+
DD_AGENT_HOST=request-replayer
7+
DD_TRACE_AGENT_PORT=80
8+
DD_TRACE_GENERATE_ROOT_SPAN=0
9+
DD_REMOTE_CONFIG_POLL_INTERVAL_SECONDS=0.01
10+
DD_EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED=1
11+
--INI--
12+
datadog.trace.agent_test_session_token=remote-config/check_payload
13+
--FILE--
14+
<?php
15+
16+
require __DIR__ . "/remote_config.inc";
17+
include __DIR__ . '/../includes/request_replayer.inc';
18+
19+
reset_request_replayer();
20+
$rr = new RequestReplayer();
21+
22+
// Start a span to trigger RC
23+
\DDTrace\start_span();
24+
25+
$path = put_dynamic_config_file([
26+
"log_injection_enabled" => true,
27+
]);
28+
29+
try {
30+
$request = $rr->waitForRequest(function($req) {
31+
return strpos($req["uri"], '/v0.7/config') !== false;
32+
});
33+
$body = json_decode($request["body"], true);
34+
} catch (Exception $e) {
35+
echo "ERROR: No RC request found\n";
36+
exit(1);
37+
}
38+
39+
if (!isset($body["client"]["client_tracer"]["process_tags"])) {
40+
echo "ERROR: Missing 'process_tags' field\n";
41+
exit(1);
42+
}
43+
44+
$process_tags = $body["client"]["client_tracer"]["process_tags"];
45+
foreach ($process_tags as $tag) {
46+
echo $tag . PHP_EOL;
47+
}
48+
49+
del_rc_file($path);
50+
51+
?>
52+
--CLEAN--
53+
<?php
54+
require __DIR__ . "/remote_config.inc";
55+
reset_request_replayer();
56+
?>
57+
--EXPECTF--
58+
entrypoint.basedir:remote_config
59+
entrypoint.name:process_tags
60+
entrypoint.type:script
61+
entrypoint.workdir:%s
62+
runtime.sapi:cli

0 commit comments

Comments
 (0)