libdd_profiling/exporter/
config.rs

1// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
2// SPDX-License-Identifier: Apache-2.0
3
4#[cfg(unix)]
5use libdd_common::connector::uds;
6use libdd_common::Endpoint;
7
8#[cfg(windows)]
9use libdd_common::connector::named_pipe;
10
11use http::Uri;
12use std::borrow::Cow;
13use std::str::FromStr;
14
15/// Creates an Endpoint for talking to the Datadog agent.
16///
17/// # Arguments
18/// * `base_url` - has protocol, host, and port e.g. http://localhost:8126/
19pub fn agent(base_url: Uri) -> anyhow::Result<Endpoint> {
20    let mut parts = base_url.into_parts();
21    let p_q = match parts.path_and_query {
22        None => None,
23        Some(pq) => {
24            let path = pq.path();
25            let path = path.strip_suffix('/').unwrap_or(path);
26            Some(format!("{path}/profiling/v1/input").parse()?)
27        }
28    };
29    parts.path_and_query = p_q;
30    let url = Uri::from_parts(parts)?;
31    Ok(Endpoint::from_url(url))
32}
33
34/// Creates an Endpoint for talking to the Datadog agent though a unix socket.
35///
36/// # Arguments
37/// * `socket_path` - file system path to the socket
38#[cfg(unix)]
39pub fn agent_uds(path: &std::path::Path) -> anyhow::Result<Endpoint> {
40    let base_url = uds::socket_path_to_uri(path)?;
41    agent(base_url)
42}
43
44/// Creates an Endpoint for talking to the Datadog agent though a windows named pipe.
45///
46/// # Arguments
47/// * `path` - file system path to the named pipe
48#[cfg(windows)]
49pub fn agent_named_pipe(path: &std::path::Path) -> anyhow::Result<Endpoint> {
50    let base_url = named_pipe::named_pipe_path_to_uri(path)?;
51    agent(base_url)
52}
53
54/// Creates an Endpoint for talking to Datadog intake without using the agent.
55/// This is an experimental feature.
56///
57/// # Arguments
58/// * `site` - e.g. "datadoghq.com".
59/// * `api_key`
60pub fn agentless<AsStrRef: AsRef<str>, IntoCow: Into<Cow<'static, str>>>(
61    site: AsStrRef,
62    api_key: IntoCow,
63) -> anyhow::Result<Endpoint> {
64    let intake_url: String = format!("https://intake.profile.{}/api/v2/profile", site.as_ref());
65
66    Ok(Endpoint {
67        url: Uri::from_str(intake_url.as_str())?,
68        api_key: Some(api_key.into()),
69        ..Default::default()
70    })
71}
72
73pub fn file(path: impl AsRef<str>) -> anyhow::Result<Endpoint> {
74    let url: String = format!("file://{}", path.as_ref());
75    Ok(Endpoint::from_slice(&url))
76}