#![cfg(feature = "profiling")]
use std::error::Error;
use std::sync::OnceLock;
#[cfg(feature = "profiling-bridge-pyroscope-rs")]
use opentelemetry::trace::TraceContextExt;
fn validate_pyroscope_endpoint(endpoint: &str) -> Result<(), Box<dyn Error>> {
use url::Url;
if endpoint.starts_with("unix://") {
return Ok(());
}
if endpoint.starts_with("http://") || endpoint.starts_with("https://") {
let url = Url::parse(endpoint)?;
if !url.username().is_empty() || url.password().is_some() {
return Err(format!(
"pyroscope endpoint must not contain userinfo; got: {endpoint} (ADR platform/0203 AC1)"
).into());
}
let host = url.host_str().unwrap_or("");
match host {
"127.0.0.1" | "::1" | "[::1]" | "localhost" => Ok(()),
_ => Err(format!(
"pyroscope endpoint must target loopback (127.0.0.1, ::1, localhost, or unix socket); \
got: {endpoint} (ADR platform/0203 AC1)"
).into()),
}
} else {
Err(
format!("pyroscope endpoint must be http://, https://, or unix://; got: {endpoint}")
.into(),
)
}
}
pub struct ProfilingHandle {
#[cfg(feature = "profiling-bridge-pyroscope-rs")]
agent: Option<pyroscope::PyroscopeAgent<pyroscope::pyroscope::PyroscopeAgentRunning>>,
}
#[cfg(feature = "profiling-bridge-pyroscope-rs")]
impl Drop for ProfilingHandle {
fn drop(&mut self) {
if let Some(agent) = self.agent.take() {
let _ = agent.stop();
}
}
}
#[cfg(feature = "profiling-bridge-pyroscope-rs")]
type BoxedTagFn = Box<dyn Fn(String, String) -> pyroscope::Result<()> + Send + Sync>;
#[cfg(feature = "profiling-bridge-pyroscope-rs")]
static PROFILING_TAG_FNS: OnceLock<(BoxedTagFn, BoxedTagFn)> = OnceLock::new();
#[cfg(feature = "profiling-bridge-pyroscope-rs")]
static PROFILING_STARTED: OnceLock<()> = OnceLock::new();
#[cfg(feature = "profiling-bridge-pyroscope-rs")]
pub(crate) fn start_pyroscope_bridge(
service_name: &str,
pyroscope_endpoint: &str,
) -> Result<Option<ProfilingHandle>, Box<dyn Error>> {
use pyroscope_pprofrs::{PprofConfig, pprof_backend};
validate_pyroscope_endpoint(pyroscope_endpoint)?;
if PROFILING_STARTED.set(()).is_err() {
return Ok(None);
}
let agent = pyroscope::PyroscopeAgent::builder(pyroscope_endpoint, service_name)
.backend(pprof_backend(PprofConfig::new().sample_rate(100)))
.build()?
.start()?;
let (add_tag, remove_tag) = agent.tag_wrapper();
PROFILING_TAG_FNS
.set((Box::new(add_tag), Box::new(remove_tag)))
.ok();
Ok(Some(ProfilingHandle { agent: Some(agent) }))
}
#[cfg(all(feature = "profiling", not(feature = "profiling-bridge-pyroscope-rs")))]
pub(crate) fn start_pyroscope_bridge(
_service_name: &str,
_pyroscope_endpoint: &str,
) -> Result<Option<ProfilingHandle>, Box<dyn Error>> {
Ok(None)
}
#[cfg(feature = "profiling-bridge-pyroscope-rs")]
pub struct ProfilingTagLayer;
#[cfg(feature = "profiling-bridge-pyroscope-rs")]
impl<S> tracing_subscriber::Layer<S> for ProfilingTagLayer
where
S: tracing::Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
{
fn on_enter(&self, _id: &tracing::span::Id, _ctx: tracing_subscriber::layer::Context<'_, S>) {
if let Some((add_tag, _)) = PROFILING_TAG_FNS.get() {
let cx = opentelemetry::Context::current();
let span_ref = cx.span();
let span_context = span_ref.span_context();
if span_context.is_valid() {
let trace_id = span_context.trace_id();
let span_id = span_context.span_id();
let _ = add_tag("trace_id".to_string(), format!("{trace_id:x}"));
let _ = add_tag("span_id".to_string(), format!("{span_id:x}"));
}
}
}
fn on_exit(&self, _id: &tracing::span::Id, _ctx: tracing_subscriber::layer::Context<'_, S>) {
if let Some((_, remove_tag)) = PROFILING_TAG_FNS.get() {
let cx = opentelemetry::Context::current();
let span_ref = cx.span();
let span_context = span_ref.span_context();
if span_context.is_valid() {
let trace_id = span_context.trace_id();
let span_id = span_context.span_id();
let _ = remove_tag("trace_id".to_string(), format!("{trace_id:x}"));
let _ = remove_tag("span_id".to_string(), format!("{span_id:x}"));
}
}
}
}
#[cfg(all(test, feature = "profiling-bridge-pyroscope-rs"))]
mod tests {
use super::*;
#[test]
fn start_bridge_with_nonexistent_server() {
let result = start_pyroscope_bridge("test-svc", "http://localhost:4040");
assert!(
result.is_ok(),
"pyroscope agent start() is lazy and does not eagerly connect"
);
if let Ok(Some(_handle)) = result {
}
}
#[test]
fn start_bridge_multiple_times_ignores_second() {
let result1 = start_pyroscope_bridge("test-svc-1", "http://localhost:4040");
assert!(result1.is_ok());
let result2 = start_pyroscope_bridge("test-svc-2", "http://localhost:4041");
assert!(result2.is_ok());
assert!(result2.unwrap().is_none());
}
#[test]
fn validate_endpoint_accepts_loopback_ipv4() {
assert!(validate_pyroscope_endpoint("http://127.0.0.1:4040").is_ok());
}
#[test]
fn validate_endpoint_accepts_loopback_ipv6() {
assert!(validate_pyroscope_endpoint("http://[::1]:4040").is_ok());
}
#[test]
fn validate_endpoint_accepts_localhost() {
assert!(validate_pyroscope_endpoint("http://localhost:4040").is_ok());
}
#[test]
fn validate_endpoint_accepts_https_loopback() {
assert!(validate_pyroscope_endpoint("https://127.0.0.1:4040").is_ok());
}
#[test]
fn validate_endpoint_rejects_routable_ipv4() {
assert!(validate_pyroscope_endpoint("http://10.0.0.1:4040").is_err());
}
#[test]
fn validate_endpoint_rejects_userinfo_bypass() {
assert!(validate_pyroscope_endpoint("http://127.0.0.1:4040@evil.com/").is_err());
}
#[test]
fn validate_endpoint_rejects_userinfo_with_password() {
assert!(validate_pyroscope_endpoint("http://user:pass@localhost:4040").is_err());
}
#[test]
fn validate_endpoint_rejects_unix_socket_check() {
assert!(validate_pyroscope_endpoint("unix:///var/run/profiling.sock").is_ok());
}
}
#[cfg(all(
test,
feature = "profiling",
not(feature = "profiling-bridge-pyroscope-rs")
))]
mod tests_no_bridge {
use super::*;
#[test]
fn start_bridge_returns_none() {
let result = start_pyroscope_bridge("test-svc", "http://localhost:4040");
assert!(result.is_ok());
if let Ok(handle) = result {
assert!(handle.is_none());
}
}
}