128 lines
3.3 KiB
Rust
128 lines
3.3 KiB
Rust
use thiserror::Error;
|
|
use tracing_subscriber::EnvFilter;
|
|
|
|
use crate::RedactionLimits;
|
|
|
|
const MAX_IDENTITY_LABEL_BYTES: usize = 64;
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct ServiceIdentity {
|
|
service: String,
|
|
version: String,
|
|
environment: String,
|
|
}
|
|
|
|
impl ServiceIdentity {
|
|
pub fn try_new(
|
|
service: impl Into<String>,
|
|
version: impl Into<String>,
|
|
environment: impl Into<String>,
|
|
) -> Result<Self, ObservabilityConfigError> {
|
|
let identity = Self {
|
|
service: service.into(),
|
|
version: version.into(),
|
|
environment: environment.into(),
|
|
};
|
|
validate_label("service", &identity.service)?;
|
|
validate_label("version", &identity.version)?;
|
|
validate_label("environment", &identity.environment)?;
|
|
Ok(identity)
|
|
}
|
|
|
|
pub fn service(&self) -> &str {
|
|
&self.service
|
|
}
|
|
|
|
pub fn version(&self) -> &str {
|
|
&self.version
|
|
}
|
|
|
|
pub fn environment(&self) -> &str {
|
|
&self.environment
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct ObservabilityConfig {
|
|
identity: ServiceIdentity,
|
|
filter: String,
|
|
redaction_limits: RedactionLimits,
|
|
}
|
|
|
|
impl ObservabilityConfig {
|
|
pub fn try_new(
|
|
identity: ServiceIdentity,
|
|
filter: impl Into<String>,
|
|
redaction_limits: RedactionLimits,
|
|
) -> Result<Self, ObservabilityConfigError> {
|
|
let filter = filter.into();
|
|
EnvFilter::try_new(&filter).map_err(|_| ObservabilityConfigError::InvalidFilter)?;
|
|
Ok(Self {
|
|
identity,
|
|
filter,
|
|
redaction_limits,
|
|
})
|
|
}
|
|
|
|
pub fn new(
|
|
identity: ServiceIdentity,
|
|
filter: impl Into<String>,
|
|
redaction_limits: RedactionLimits,
|
|
) -> Self {
|
|
Self {
|
|
identity,
|
|
filter: filter.into(),
|
|
redaction_limits,
|
|
}
|
|
}
|
|
|
|
pub(crate) fn into_parts(self) -> (ServiceIdentity, String, RedactionLimits) {
|
|
(self.identity, self.filter, self.redaction_limits)
|
|
}
|
|
|
|
pub(crate) fn identity(&self) -> &ServiceIdentity {
|
|
&self.identity
|
|
}
|
|
|
|
pub(crate) fn redaction_limits(&self) -> RedactionLimits {
|
|
self.redaction_limits
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Error)]
|
|
pub enum ObservabilityConfigError {
|
|
#[error("invalid observability identity field: {field}")]
|
|
InvalidIdentity { field: &'static str },
|
|
#[error("invalid observability log filter")]
|
|
InvalidFilter,
|
|
}
|
|
|
|
fn validate_label(field: &'static str, value: &str) -> Result<(), ObservabilityConfigError> {
|
|
let valid = !value.is_empty()
|
|
&& value.len() <= MAX_IDENTITY_LABEL_BYTES
|
|
&& value
|
|
.bytes()
|
|
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b'+'));
|
|
|
|
if valid {
|
|
Ok(())
|
|
} else {
|
|
Err(ObservabilityConfigError::InvalidIdentity { field })
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::ServiceIdentity;
|
|
|
|
#[test]
|
|
fn accepts_release_and_environment_labels() {
|
|
let identity = ServiceIdentity::try_new("admin-api", "0.3.1+build.7", "production")
|
|
.expect("identity must be valid");
|
|
|
|
assert_eq!(identity.service(), "admin-api");
|
|
assert_eq!(identity.version(), "0.3.1+build.7");
|
|
assert_eq!(identity.environment(), "production");
|
|
}
|
|
}
|