use serde::{Deserialize, Serialize};
use crate::error::WyrdError;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct InstrumentationScope {
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(default)]
pub attributes: serde_json::Map<String, serde_json::Value>,
}
impl InstrumentationScope {
pub fn validate(&self) -> Result<(), WyrdError> {
if self.name.is_empty() {
return Err(WyrdError::Validation {
message: "instrumentation_scope.name must be non-empty".to_string(),
details: serde_json::Value::Null,
});
}
if self.name.len() > 256 {
return Err(WyrdError::Validation {
message: format!(
"instrumentation_scope.name length must be <= 256, got {}",
self.name.len()
),
details: serde_json::Value::Null,
});
}
if let Some(v) = &self.version
&& v.len() > 64
{
return Err(WyrdError::Validation {
message: format!(
"instrumentation_scope.version length must be <= 64, got {}",
v.len()
),
details: serde_json::Value::Null,
});
}
if self.attributes.len() > 32 {
return Err(WyrdError::Validation {
message: format!(
"instrumentation_scope.attributes count must be <= 32, got {}",
self.attributes.len()
),
details: serde_json::Value::Null,
});
}
Ok(())
}
}