Skip to content

Commit 432c83e

Browse files
committed
feat: add API for process discovery
Resolves [APMAPI-1063]
1 parent d62c468 commit 432c83e

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//use std::os::raw::c_void;
5+
use std::ffi::c_void;
6+
use ddcommon::{TracerMetadata, AnonymousFileHandle, store_tracer_metadata};
7+
8+
impl AnonymousFileHandle {
9+
fn as_raw_pointer(&self) -> *mut c_void {
10+
match self {
11+
#[cfg(target_os = "linux")]
12+
AnonymousFileHandle::Linux(memfd) => Box::as_ptr(memfd) as *mut c_void,
13+
#[cfg(not(target_os = "linux"))]
14+
AnonymousFileHandle::Other(()) => std::ptr::null() as *mut c_void,
15+
}
16+
}
17+
}
18+
19+
#[no_mangle]
20+
#[must_use]
21+
pub extern "C" fn ddog_store_tracer_metadata(tracer_metadata: TracerMetadata) -> *mut c_void {
22+
let handle = store_tracer_metadata(tracer_metadata).unwrap();
23+
return handle.as_raw_pointer();
24+
}

ddcommon/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ hyper-util = "0.1.3"
3232
lazy_static = "1.4"
3333
log = { version = "0.4" }
3434
pin-project = "1"
35+
rand = "0.8.3"
3536
regex = "1.5"
37+
rmp = "0.8.14"
38+
rmp-serde = "1.3.0"
3639
rustls = { version = "0.23", default-features = false }
3740
rustls-native-certs = { version = "0.7" }
3841
tokio = { version = "1.23", features = ["rt", "macros"] }
@@ -55,6 +58,7 @@ hyper-rustls = { version = "0.27", default-features = false, features = [
5558
"tls12",
5659
"aws-lc-rs",
5760
] }
61+
memfd = { version = "0.6" }
5862

5963
[target.'cfg(not(unix))'.dependencies]
6064
hyper-rustls = { version = "0.27", default-features = false, features = [

ddcommon/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod cstr;
1818
pub mod config;
1919
pub mod rate_limiter;
2020
pub mod tag;
21+
pub mod tracer_metadata;
2122

2223
pub mod header {
2324
#![allow(clippy::declare_interior_mutable_const)]

ddcommon/src/tracer_metadata.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use serde::Serialize;
5+
#[cfg(target_os = "linux")]
6+
use memfd::{Memfd, MemfdOptions};
7+
8+
/// This struct MUST be backward compatible.
9+
#[derive(Serialize, Debug)]
10+
#[repr(C)]
11+
pub struct TracerMetadata {
12+
/// Version of the schema.
13+
pub schema_version: u8,
14+
/// Runtime UUID.
15+
#[serde(skip_serializing_if = "Option::is_none")]
16+
pub runtime_id: Option<String>,
17+
/// Programming language of the tracer library (e.g., “python”). Refers to telemetry for valid values.
18+
pub tracer_language: String,
19+
/// Version of the tracer (e.g., "1.0.0").
20+
pub tracer_version: String,
21+
/// Identifier of the machine running the process.
22+
pub hostname: String,
23+
/// Name of the service being instrumented.
24+
#[serde(skip_serializing_if = "Option::is_none")]
25+
pub service_name: Option<String>,
26+
/// Environment of the service being instrumented.
27+
#[serde(skip_serializing_if = "Option::is_none")]
28+
pub service_env: Option<String>,
29+
/// Version of the service being instrumented.
30+
#[serde(skip_serializing_if = "Option::is_none")]
31+
pub service_version: Option<String>,
32+
}
33+
34+
pub enum AnonymousFileHandle {
35+
#[cfg(target_os = "linux")]
36+
Linux(Box<Memfd>),
37+
#[cfg(not(target_os = "linux"))]
38+
Other(()),
39+
}
40+
41+
/// Create the anonymous file storing the tracer metadata.
42+
#[cfg(target_os = "linux")]
43+
pub fn store_tracer_metadata(data: TracerMetadata) -> Result<AnonymousFileHandle, String> {
44+
let uid: String = rand::thread_rng().sample_iter(&Alphanumeric).take(8).map(char::from).collect();
45+
let mfd_name: String = format!("{}-{}", "datadog-tracer-info", uid);
46+
47+
let mfd = MemfdOptions::default()
48+
.close_on_exec(true)
49+
.allow_sealing(true)
50+
.create::<&str>(mfd_name.as_ref())
51+
.map_err(|e| format!("Unable to create memfd: {}", e))?;
52+
53+
let mut buf = Vec::new();
54+
data.serialize(&mut Serializer::new(&mut buf).with_struct_map()).unwrap();
55+
56+
mfd.as_file().write_all(&buf).unwrap();
57+
mfd.add_seals(&[
58+
memfd::FileSeal::SealShrink,
59+
memfd::FileSeal::SealGrow,
60+
memfd::FileSeal::SealSeal,
61+
]).map_error(|e| format!("Unable to seal: {}", e))?;
62+
63+
return Ok(AnonymousFileHandle::Linux(Box::new(mfd)));
64+
}
65+
66+
#[cfg(not(target_os = "linux"))]
67+
pub fn store_tracer_metadata(_data: TracerMetadata) -> Result<AnonymousFileHandle, String> {
68+
return Ok(AnonymousFileHandle::Other(()));
69+
}

0 commit comments

Comments
 (0)