Skip to content

feat: reuse the dogstatd socket fd#1237

Draft
sky-mart wants to merge 1 commit into
mainfrom
vlad/dogd-unix-socket-fd
Draft

feat: reuse the dogstatd socket fd#1237
sky-mart wants to merge 1 commit into
mainfrom
vlad/dogd-unix-socket-fd

Conversation

@sky-mart
Copy link
Copy Markdown
Contributor

The motivation is to be able to write like this in orb-core: (the rest is collateral damage)

Parent process (orb-core main):

use std::os::fd::AsRawFd;
use std::time::Duration;
use orb_dogd::DogstatsdClient;

let client = DogstatsdClient::new();           // returns immediately;
                                               // bg thread starts retrying

// ... emit metrics any time; they buffer in the bounded channel
//     until the bg thread connects, then drain out
client.incr("orb.boot", ["stage:early"])?;

// Before forking a subprocess agent, get a shareable FD.
// Blocks until the bg thread has actually connected (or 5min timeout).
let fd = client
    .wait_for_connection(Duration::from_secs(300))
    .expect("dsd unreachable for 5 minutes");

// Hand `fd` to the child: clear CLOEXEC and stash its number in an env var.
// (This part lives in orb-core's ProcessInitializer; orb-dogd doesn't
// dictate how you pass the FD — it just gives you one.)
clear_descriptor_cloexec(&fd)?;
env::set_var("ORB_CORE_PROCESS_DOGSTATSD", fd.as_raw_fd().to_string());
// ... then fork+exec / agentwire spawn

Subprocess (forked agent, inside the sandbox netns):

use std::os::fd::FromRawFd;
use std::os::unix::net::UnixDatagram;
use orb_dogd::DogstatsdClient;

let raw_fd: i32 = env::var("ORB_CORE_PROCESS_DOGSTATSD")?.parse()?;
// SAFETY: the FD was inherited across exec; we own it.
let socket = unsafe { UnixDatagram::from_raw_fd(raw_fd) };

let client = DogstatsdClient::from_unix_datagram(socket);
// No retry, no connect — `socket` is already connected to the
// parent's dsd session.

client.gauge("orb.agent.ready", 1.0, ["agent:iris"])?;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant