Skip to content

Commit

Permalink
Use a SIDECAR_VERSION by default for the reported version
Browse files Browse the repository at this point in the history
Signed-off-by: Bob Weinand <[email protected]>
  • Loading branch information
bwoebi committed May 28, 2024
1 parent d1a9934 commit 45f7e90
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
28 changes: 27 additions & 1 deletion sidecar/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::FnArg::Typed;
use syn::__private::Span;
use syn::{parse_quote, Arm, Ident, ItemTrait, Pat, TraitItem};
use syn::parse::{Parse, ParseStream};
use syn::{parse_macro_input, parse_quote, Arm, Ident, ItemTrait, Pat, TraitItem};

fn snake_to_camel(ident_str: &str) -> String {
let mut camel_ty = String::with_capacity(ident_str.len());
Expand Down Expand Up @@ -69,3 +70,28 @@ pub fn extract_request_id(_attr: TokenStream, mut input: TokenStream) -> TokenSt
}));
input
}

struct EnvOrDefault {
name: syn::LitStr,
default: syn::Expr,
}

impl Parse for EnvOrDefault {
fn parse(input: ParseStream) -> syn::Result<Self> {
let name: syn::LitStr = input.parse()?;
input.parse::<syn::Token![,]>()?;
let default = input.parse()?;
Ok(Self { name, default })
}
}

#[proc_macro]
pub fn env_or_default(input: TokenStream) -> TokenStream {
let env = parse_macro_input!(input as EnvOrDefault);
let default = env.default;

TokenStream::from(match std::env::var(env.name.value()) {
Ok(var) => quote! { #var },
Err(_) => quote! { #default },
})
}
7 changes: 7 additions & 0 deletions sidecar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ mod windows;

#[cfg(windows)]
pub use self::windows::*;

macro_rules! sidecar_version {
() => {
datadog_sidecar_macros::env_or_default!("SIDECAR_VERSION", env!("CARGO_PKG_VERSION"))
};
}
pub(crate) use sidecar_version;
2 changes: 1 addition & 1 deletion sidecar/src/self_telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl SelfTelemetry {
"datadog-ipc-helper".to_string(),
"php".to_string(),
"SIDECAR".to_string(),
env!("CARGO_PKG_VERSION").to_string(),
crate::sidecar_version!().to_string(),
)
.spawn_with_config(self.config.clone())
.await
Expand Down
6 changes: 3 additions & 3 deletions sidecar/src/setup/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Liaison for SharedDirLiaison {

impl SharedDirLiaison {
pub fn new<P: AsRef<Path>>(base_dir: P) -> Self {
let versioned_socket_basename = concat!("libdd.", env!("CARGO_PKG_VERSION"), ".sock");
let versioned_socket_basename = concat!("libdd.", crate::sidecar_version!(), ".sock");
let base_dir = base_dir.as_ref();
let socket_path = base_dir
.join(versioned_socket_basename)
Expand Down Expand Up @@ -146,13 +146,13 @@ mod linux {
}

fn ipc_shared() -> AbstractUnixSocketLiaison {
let path = PathBuf::from(concat!("libdatadog/", env!("CARGO_PKG_VERSION"), ".sock"));
let path = PathBuf::from(concat!("libdatadog/", crate::sidecar_version!(), ".sock"));
Self { path }
}

fn ipc_per_process() -> AbstractUnixSocketLiaison {
let path = PathBuf::from(format!(
concat!("libdatadog/", env!("CARGO_PKG_VERSION"), ".{}.sock"),
concat!("libdatadog/", crate::sidecar_version!(), ".{}.sock"),
getpid()
));
Self { path }
Expand Down
2 changes: 1 addition & 1 deletion sidecar/src/setup/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl NamedPipeLiaison {
PIPE_PATH,
prefix.as_ref(),
session_id,
env!("CARGO_PKG_VERSION")
crate::sidecar_version!()
))
.unwrap(),
}
Expand Down

0 comments on commit 45f7e90

Please sign in to comment.