Skip to content

Commit

Permalink
[Serverless Mini Agent] Add Azure App Service Runtime Tags (#512)
Browse files Browse the repository at this point in the history
* adds azure runtime tags for serverless traces

* apply formatting

* update unit test for new fields

* handle empty strings for azure app service tags

* adds unit test for get_value_or_unknown

* filter empty strings during construction
  • Loading branch information
duncanpharvey authored Jul 2, 2024
1 parent 8a5d447 commit c00ab02
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
55 changes: 54 additions & 1 deletion ddcommon/src/azure_app_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const INSTANCE_NAME: &str = "COMPUTERNAME";
const INSTANCE_ID: &str = "WEBSITE_INSTANCE_ID";
const SERVICE_CONTEXT: &str = "DD_AZURE_APP_SERVICES";
const FUNCTIONS_WORKER_RUNTIME: &str = "FUNCTIONS_WORKER_RUNTIME";
const FUNCTIONS_WORKER_RUNTIME_VERSION: &str = "FUNCTIONS_WORKER_RUNTIME_VERSION";
const FUNCTIONS_EXTENSION_VERSION: &str = "FUNCTIONS_EXTENSION_VERSION";

const UNKNOWN_VALUE: &str = "unknown";
Expand All @@ -25,7 +26,10 @@ enum AzureContext {

macro_rules! get_trimmed_env_var {
($name:expr) => {
env::var($name).ok().map(|v| v.trim().to_string())
env::var($name)
.ok()
.map(|v| v.trim().to_string())
.filter(|s| !s.is_empty())
};
}

Expand Down Expand Up @@ -72,6 +76,9 @@ pub struct AzureMetadata {
instance_id: Option<String>,
site_kind: String,
site_type: String,
runtime: Option<String>,
runtime_version: Option<String>,
function_runtime_version: Option<String>,
}

impl AzureMetadata {
Expand Down Expand Up @@ -146,6 +153,10 @@ impl AzureMetadata {
let instance_name = query.get_var(INSTANCE_NAME);
let instance_id = query.get_var(INSTANCE_ID);

let runtime = query.get_var(FUNCTIONS_WORKER_RUNTIME);
let runtime_version = query.get_var(FUNCTIONS_WORKER_RUNTIME_VERSION);
let function_runtime_version = query.get_var(FUNCTIONS_EXTENSION_VERSION);

Some(AzureMetadata {
resource_id,
subscription_id,
Expand All @@ -157,6 +168,9 @@ impl AzureMetadata {
instance_id,
site_kind,
site_type,
runtime,
runtime_version,
function_runtime_version,
})
}

Expand Down Expand Up @@ -222,6 +236,18 @@ impl AzureMetadata {
pub fn get_site_kind(&self) -> &str {
self.site_kind.as_str()
}

pub fn get_runtime(&self) -> &str {
get_value_or_unknown!(self.runtime)
}

pub fn get_runtime_version(&self) -> &str {
get_value_or_unknown!(self.runtime_version)
}

pub fn get_function_runtime_version(&self) -> &str {
get_value_or_unknown!(self.function_runtime_version)
}
}

pub fn get_metadata() -> &'static Option<AzureMetadata> {
Expand Down Expand Up @@ -583,6 +609,9 @@ mod tests {
let expected_operating_system = "FreeBSD".to_owned();
let expected_instance_name = "my_instance_name".to_owned();
let expected_instance_id = "my_instance_id".to_owned();
let expected_function_extension_version = "~4".to_owned();
let expected_runtime = "node".to_owned();
let expected_runtime_version = "18".to_owned();

let mocked_env = MockEnv::new(&[
(WEBSITE_SITE_NAME, expected_site_name.as_str()),
Expand All @@ -592,6 +621,15 @@ mod tests {
(INSTANCE_NAME, expected_instance_name.as_str()),
(INSTANCE_ID, expected_instance_id.as_str()),
(SERVICE_CONTEXT, "1"),
(
FUNCTIONS_EXTENSION_VERSION,
expected_function_extension_version.as_str(),
),
(FUNCTIONS_WORKER_RUNTIME, expected_runtime.as_str()),
(
FUNCTIONS_WORKER_RUNTIME_VERSION,
expected_runtime_version.as_str(),
),
]);

let metadata = AzureMetadata::new(mocked_env).unwrap();
Expand All @@ -602,5 +640,20 @@ mod tests {
assert_eq!(expected_operating_system, metadata.get_operating_system());
assert_eq!(expected_instance_name, metadata.get_instance_name());
assert_eq!(expected_instance_id, metadata.get_instance_id());
assert_eq!(
expected_function_extension_version,
metadata.get_function_runtime_version()
);
assert_eq!(expected_runtime, metadata.get_runtime());
assert_eq!(expected_runtime_version, metadata.get_runtime_version());
}

#[test]
fn test_get_trimmed_env_var_empty_string() {
env::remove_var("TEST_VAR_NONE");
assert_eq!(get_trimmed_env_var!("TEST_VAR_NONE"), None);

env::set_var("TEST_VAR_EMPTY_STRING", "");
assert_eq!(get_trimmed_env_var!("TEST_VAR_EMPTY_STRING"), None);
}
}
9 changes: 9 additions & 0 deletions trace-utils/src/trace_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,15 @@ pub fn enrich_span_with_azure_metadata(span: &mut Span, mini_agent_version: &str
("aas.subscription.id", aas_metadata.get_subscription_id()),
("aas.environment.mini_agent_version", mini_agent_version),
("aas.environment.os", aas_metadata.get_operating_system()),
("aas.environment.runtime", aas_metadata.get_runtime()),
(
"aas.environment.runtime_version",
aas_metadata.get_runtime_version(),
),
(
"aas.environment.function_runtime",
aas_metadata.get_function_runtime_version(),
),
("aas.resource.group", aas_metadata.get_resource_group()),
("aas.site.name", aas_metadata.get_site_name()),
("aas.site.kind", aas_metadata.get_site_kind()),
Expand Down

0 comments on commit c00ab02

Please sign in to comment.