use fastly_shared::FastlyStatus;
use fastly_sys::fastly_compute_runtime::{get_heap_mib, get_vcpu_ms};
#[cfg(not(target_env = "p1"))]
use fastly_sys::service0_1_0::fastly::compute as wit;
use std::sync::OnceLock;
pub fn elapsed_vcpu_ms() -> Result<u64, FastlyStatus> {
let mut vcpu_time = 0u64;
let vcpu_time_result = unsafe { get_vcpu_ms(&mut vcpu_time) };
if vcpu_time_result != FastlyStatus::OK {
return Err(vcpu_time_result);
}
Ok(vcpu_time)
}
pub fn heap_memory_snapshot_mib() -> Result<u32, FastlyStatus> {
let mut memory_mib = 0u32;
let result = unsafe { get_heap_mib(&mut memory_mib) };
if result != FastlyStatus::OK {
return Err(result);
}
Ok(memory_mib)
}
pub fn hostname() -> &'static str {
static HOSTNAME: OnceLock<String> = OnceLock::new();
#[cfg(target_env = "p1")]
{
HOSTNAME.get_or_init(|| std::env::var("FASTLY_HOSTNAME").unwrap())
}
#[cfg(not(target_env = "p1"))]
{
HOSTNAME.get_or_init(wit::compute_runtime::get_hostname)
}
}
pub fn pop() -> &'static str {
static POP: OnceLock<String> = OnceLock::new();
#[cfg(target_env = "p1")]
{
POP.get_or_init(|| std::env::var("FASTLY_POP").unwrap())
}
#[cfg(not(target_env = "p1"))]
{
POP.get_or_init(wit::compute_runtime::get_pop)
}
}
pub fn region() -> &'static str {
static REGION: OnceLock<String> = OnceLock::new();
#[cfg(target_env = "p1")]
{
REGION.get_or_init(|| std::env::var("FASTLY_REGION").unwrap())
}
#[cfg(not(target_env = "p1"))]
{
REGION.get_or_init(wit::compute_runtime::get_region)
}
}
pub fn cache_generation() -> u64 {
static CACHE_GENERATION: OnceLock<u64> = OnceLock::new();
#[cfg(target_env = "p1")]
{
*CACHE_GENERATION.get_or_init(|| {
std::env::var("FASTLY_CACHE_GENERATION")
.unwrap()
.parse()
.unwrap()
})
}
#[cfg(not(target_env = "p1"))]
{
*CACHE_GENERATION.get_or_init(wit::compute_runtime::get_cache_generation)
}
}
pub fn customer_id() -> &'static str {
static CUSTOMER_ID: OnceLock<String> = OnceLock::new();
#[cfg(target_env = "p1")]
{
CUSTOMER_ID.get_or_init(|| std::env::var("FASTLY_CUSTOMER_ID").unwrap())
}
#[cfg(not(target_env = "p1"))]
{
CUSTOMER_ID.get_or_init(wit::compute_runtime::get_customer_id)
}
}
pub fn is_staging() -> bool {
static IS_STAGING: OnceLock<bool> = OnceLock::new();
#[cfg(target_env = "p1")]
{
*IS_STAGING.get_or_init(
|| match std::env::var("FASTLY_IS_STAGING").unwrap().as_str() {
"0" => false,
"1" => true,
_ => unreachable!(),
},
)
}
#[cfg(not(target_env = "p1"))]
{
*IS_STAGING.get_or_init(wit::compute_runtime::get_is_staging)
}
}
pub fn service_id() -> &'static str {
static SERVICE_ID: OnceLock<String> = OnceLock::new();
#[cfg(target_env = "p1")]
{
SERVICE_ID.get_or_init(|| std::env::var("FASTLY_SERVICE_ID").unwrap())
}
#[cfg(not(target_env = "p1"))]
{
SERVICE_ID.get_or_init(wit::compute_runtime::get_service_id)
}
}
pub fn service_version() -> u64 {
static SERVICE_VERSION: OnceLock<u64> = OnceLock::new();
#[cfg(target_env = "p1")]
{
*SERVICE_VERSION.get_or_init(|| {
std::env::var("FASTLY_SERVICE_VERSION")
.unwrap()
.parse()
.unwrap()
})
}
#[cfg(not(target_env = "p1"))]
{
*SERVICE_VERSION.get_or_init(wit::compute_runtime::get_service_version)
}
}
#[doc(hidden)]
pub fn namespace_id() -> &'static str {
static NAMESPACE_ID: OnceLock<String> = OnceLock::new();
#[cfg(target_env = "p1")]
{
NAMESPACE_ID.get_or_init(|| std::env::var("FASTLY_NAMESPACE_ID").unwrap())
}
#[cfg(not(target_env = "p1"))]
{
NAMESPACE_ID.get_or_init(wit::compute_runtime::get_hostname)
}
}
pub fn sandbox_id() -> &'static str {
static SANDBOX_ID: OnceLock<String> = OnceLock::new();
#[cfg(target_env = "p1")]
{
SANDBOX_ID.get_or_init(|| std::env::var("FASTLY_TRACE_ID").unwrap())
}
#[cfg(not(target_env = "p1"))]
{
SANDBOX_ID.get_or_init(wit::compute_runtime::get_sandbox_id)
}
}