Skip to content

Commit b09abfb

Browse files
authored
feat(telemetry)!: add process_tags to Application in telemetry (#1459)
# What does this PR do? This PR adds a `process_tags` field to the telemetry `Application` struct. This tag can be set during configuration or later (when the process_tags are acquired). # Motivation `dd-trace-php` needs to be send the process_tags for its telemetry according to this [RFC](https://docs.google.com/document/d/1AFdLUuVk70i0bJd5335-RxqsvwAV9ovAqcO2z5mEMbA/edit?tab=t.0#heading=h.s9l1lctqlg11). It is important to note that the tracer will set the configuration of its sidecar before we can retrieve the process_tags. That's why I created a function specifically to set the processs_tags. # How to test the change? The change is tested in dd-trace-php through this PR: DataDog/dd-trace-php#3627 Co-authored-by: louis.tricot <louis.tricot@datadoghq.com>
1 parent a183c05 commit b09abfb

File tree

11 files changed

+83
-0
lines changed

11 files changed

+83
-0
lines changed

datadog-sidecar-ffi/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ pub unsafe extern "C" fn ddog_sidecar_session_set_config(
588588
remote_config_capabilities_count: usize,
589589
remote_config_enabled: bool,
590590
is_fork: bool,
591+
process_tags: ffi::CharSlice,
591592
) -> MaybeError {
592593
#[cfg(unix)]
593594
let remote_config_notify_target = libc::getpid();
@@ -631,13 +632,31 @@ pub unsafe extern "C" fn ddog_sidecar_session_set_config(
631632
.as_slice()
632633
.to_vec(),
633634
remote_config_enabled,
635+
process_tags: process_tags.to_utf8_lossy().into(),
634636
},
635637
is_fork
636638
));
637639

638640
MaybeError::None
639641
}
640642

643+
/// Updates the process_tags for an existing session.
644+
#[no_mangle]
645+
#[allow(clippy::missing_safety_doc)]
646+
pub unsafe extern "C" fn ddog_sidecar_session_set_process_tags(
647+
transport: &mut Box<SidecarTransport>,
648+
session_id: ffi::CharSlice,
649+
process_tags: ffi::CharSlice,
650+
) -> MaybeError {
651+
try_c!(blocking::set_session_process_tags(
652+
transport,
653+
session_id.to_utf8_lossy().into(),
654+
process_tags.to_utf8_lossy().into(),
655+
));
656+
657+
MaybeError::None
658+
}
659+
641660
#[repr(C)]
642661
pub struct TracerHeaderTags<'a> {
643662
pub lang: ffi::CharSlice<'a>,

datadog-sidecar-ffi/tests/sidecar.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ fn test_ddog_sidecar_register_app() {
112112
0,
113113
false,
114114
false,
115+
"".into(),
115116
)
116117
.unwrap_none();
117118

@@ -162,6 +163,7 @@ fn test_ddog_sidecar_register_app() {
162163
0,
163164
false,
164165
false,
166+
"".into(),
165167
)
166168
.unwrap_none();
167169

datadog-sidecar/src/service/blocking.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,28 @@ pub fn set_session_config(
243243
})
244244
}
245245

246+
/// Updates the process tags for an existing session.
247+
///
248+
/// # Arguments
249+
///
250+
/// * `transport` - The transport used for communication.
251+
/// * `session_id` - The ID of the session.
252+
/// * `process_tags` - The process tags string to set.
253+
///
254+
/// # Returns
255+
///
256+
/// An `io::Result<()>` indicating the result of the operation.
257+
pub fn set_session_process_tags(
258+
transport: &mut SidecarTransport,
259+
session_id: String,
260+
process_tags: String,
261+
) -> io::Result<()> {
262+
transport.send(SidecarInterfaceRequest::SetSessionProcessTags {
263+
session_id,
264+
process_tags,
265+
})
266+
}
267+
246268
/// Sends a trace as bytes.
247269
///
248270
/// # Arguments

datadog-sidecar/src/service/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub struct SessionConfig {
6464
pub remote_config_products: Vec<RemoteConfigProduct>,
6565
pub remote_config_capabilities: Vec<RemoteConfigCapabilities>,
6666
pub remote_config_enabled: bool,
67+
pub process_tags: String,
6768
}
6869

6970
#[derive(Debug, Deserialize, Serialize)]

datadog-sidecar/src/service/session_info.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub(crate) struct SessionInfo {
4343
pub(crate) session_id: String,
4444
pub(crate) pid: Arc<AtomicI32>,
4545
pub(crate) remote_config_enabled: Arc<Mutex<bool>>,
46+
pub(crate) process_tags: Arc<Mutex<Option<String>>>,
4647
}
4748

4849
impl Clone for SessionInfo {
@@ -62,6 +63,7 @@ impl Clone for SessionInfo {
6263
session_id: self.session_id.clone(),
6364
pid: self.pid.clone(),
6465
remote_config_enabled: self.remote_config_enabled.clone(),
66+
process_tags: self.process_tags.clone(),
6567
}
6668
}
6769
}

datadog-sidecar/src/service/sidecar_interface.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ pub trait SidecarInterface {
6767
is_fork: bool,
6868
);
6969

70+
/// Updates the process tags for an existing session.
71+
///
72+
/// # Arguments
73+
///
74+
/// * `session_id` - The ID of the session.
75+
/// * `process_tags` - The process tags string.
76+
async fn set_session_process_tags(session_id: String, process_tags: String);
77+
7078
/// Shuts down a runtime.
7179
///
7280
/// # Arguments

datadog-sidecar/src/service/sidecar_server.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ impl SidecarInterface for SidecarServer {
421421
.unwrap_or("unknown-service");
422422
let env = entry.get().env.as_deref().unwrap_or("none");
423423

424+
let process_tags = session.process_tags.lock_or_panic().clone();
425+
424426
// Lock telemetry client
425427
let telemetry_mutex = self.telemetry_clients.get_or_create(
426428
service,
@@ -438,6 +440,7 @@ impl SidecarInterface for SidecarServer {
438440
Config::default()
439441
})
440442
},
443+
process_tags,
441444
);
442445
let mut telemetry = telemetry_mutex.lock_or_panic();
443446

@@ -565,6 +568,8 @@ impl SidecarInterface for SidecarServer {
565568
*session.remote_config_notify_function.lock().unwrap() = remote_config_notify_function;
566569
}
567570
*session.remote_config_enabled.lock_or_panic() = config.remote_config_enabled;
571+
*session.process_tags.lock_or_panic() =
572+
(!config.process_tags.is_empty()).then_some(config.process_tags.clone());
568573
session.modify_telemetry_config(|cfg| {
569574
cfg.telemetry_heartbeat_interval = config.telemetry_heartbeat_interval;
570575
let endpoint = get_product_endpoint(
@@ -658,6 +663,19 @@ impl SidecarInterface for SidecarServer {
658663
})
659664
}
660665

666+
type SetSessionProcessTagsFut = NoResponse;
667+
668+
fn set_session_process_tags(
669+
self,
670+
_: Context,
671+
session_id: String,
672+
process_tags: String,
673+
) -> Self::SetSessionProcessTagsFut {
674+
let session = self.get_session(&session_id);
675+
*session.process_tags.lock_or_panic() = (!process_tags.is_empty()).then_some(process_tags);
676+
no_response()
677+
}
678+
661679
type ShutdownRuntimeFut = NoResponse;
662680

663681
fn shutdown_runtime(self, _: Context, instance_id: InstanceId) -> Self::ShutdownRuntimeFut {

datadog-sidecar/src/service/telemetry.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ impl TelemetryCachedClient {
7777
instance_id: &InstanceId,
7878
runtime_meta: &RuntimeMetadata,
7979
get_config: impl FnOnce() -> libdd_telemetry::config::Config,
80+
process_tags: Option<String>,
8081
) -> Self {
8182
let mut builder = TelemetryWorkerBuilder::new_fetch_host(
8283
service.to_string(),
@@ -87,6 +88,7 @@ impl TelemetryCachedClient {
8788

8889
builder.runtime_id = Some(instance_id.runtime_id.clone());
8990
builder.application.env = Some(env.to_string());
91+
builder.application.process_tags = process_tags;
9092
let config = get_config();
9193
builder.config = config.clone();
9294

@@ -298,6 +300,7 @@ impl TelemetryCachedClientSet {
298300
instance_id: &InstanceId,
299301
runtime_meta: &RuntimeMetadata,
300302
get_config: F,
303+
process_tags: Option<String>,
301304
) -> Arc<Mutex<TelemetryCachedClient>>
302305
where
303306
F: FnOnce() -> libdd_telemetry::config::Config,
@@ -330,6 +333,7 @@ impl TelemetryCachedClientSet {
330333
instance_id,
331334
runtime_meta,
332335
get_config,
336+
process_tags,
333337
))),
334338
};
335339

@@ -487,12 +491,15 @@ fn get_telemetry_client(
487491
})
488492
};
489493

494+
let process_tags = session.process_tags.lock_or_panic().clone();
495+
490496
TelemetryCachedClientSet::get_or_create(
491497
&sidecar.telemetry_clients,
492498
service_name,
493499
env_name,
494500
instance_id,
495501
&runtime_meta,
496502
get_config,
503+
process_tags,
497504
)
498505
}

libdd-telemetry/examples/tm-ping.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
7575
runtime_name: None,
7676
runtime_version: None,
7777
runtime_patches: None,
78+
process_tags: None,
7879
};
7980
let host = build_host();
8081
let payload = data::payload::Payload::AppStarted(build_app_started_payload());

libdd-telemetry/examples/tm-send-sketch.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ async fn async_main() {
7878
runtime_name: None,
7979
runtime_version: None,
8080
runtime_patches: None,
81+
process_tags: None,
8182
};
8283
let host = build_host();
8384

0 commit comments

Comments
 (0)