feat: harden community production foundation through story 1.5
This commit is contained in:
1 parent
c30461cc92
commit
f6fc2e5c9b
161 files changed
+16758
-2515
No files matched your search
@@ -1,10 +1,8 @@
|
||||
use std::env;
|
||||
|
||||
use thiserror::Error;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
|
||||
use crate::RedactionLimits;
|
||||
|
||||
const DEFAULT_ENVIRONMENT: &str = "development";
|
||||
const MAX_IDENTITY_LABEL_BYTES: usize = 64;
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
@@ -52,6 +50,20 @@ pub struct ObservabilityConfig {
|
||||
}
|
||||
|
||||
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>,
|
||||
@@ -64,26 +76,6 @@ impl ObservabilityConfig {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_env(
|
||||
service: &'static str,
|
||||
version: &'static str,
|
||||
default_filter: &'static str,
|
||||
) -> Result<Self, ObservabilityConfigError> {
|
||||
let environment = env_value_or_default(
|
||||
"CRANK_ENVIRONMENT",
|
||||
env::var("CRANK_ENVIRONMENT"),
|
||||
DEFAULT_ENVIRONMENT,
|
||||
)?;
|
||||
let filter = env_value_or_default(
|
||||
"CRANK_LOG_LEVEL",
|
||||
env::var("CRANK_LOG_LEVEL"),
|
||||
default_filter,
|
||||
)?;
|
||||
let identity = ServiceIdentity::try_new(service, version, environment)?;
|
||||
|
||||
Ok(Self::new(identity, filter, RedactionLimits::default()))
|
||||
}
|
||||
|
||||
pub(crate) fn into_parts(self) -> (ServiceIdentity, String, RedactionLimits) {
|
||||
(self.identity, self.filter, self.redaction_limits)
|
||||
}
|
||||
@@ -101,22 +93,8 @@ impl ObservabilityConfig {
|
||||
pub enum ObservabilityConfigError {
|
||||
#[error("invalid observability identity field: {field}")]
|
||||
InvalidIdentity { field: &'static str },
|
||||
#[error("observability environment variable is not valid UTF-8: {field}")]
|
||||
InvalidEnvironmentEncoding { field: &'static str },
|
||||
}
|
||||
|
||||
fn env_value_or_default(
|
||||
field: &'static str,
|
||||
value: Result<String, env::VarError>,
|
||||
default: &'static str,
|
||||
) -> Result<String, ObservabilityConfigError> {
|
||||
match value {
|
||||
Ok(value) => Ok(value),
|
||||
Err(env::VarError::NotPresent) => Ok(default.to_owned()),
|
||||
Err(env::VarError::NotUnicode(_)) => {
|
||||
Err(ObservabilityConfigError::InvalidEnvironmentEncoding { field })
|
||||
}
|
||||
}
|
||||
#[error("invalid observability log filter")]
|
||||
InvalidFilter,
|
||||
}
|
||||
|
||||
fn validate_label(field: &'static str, value: &str) -> Result<(), ObservabilityConfigError> {
|
||||
@@ -135,9 +113,7 @@ fn validate_label(field: &'static str, value: &str) -> Result<(), ObservabilityC
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::ffi::OsString;
|
||||
|
||||
use super::{ObservabilityConfigError, ServiceIdentity, env_value_or_default};
|
||||
use super::ServiceIdentity;
|
||||
|
||||
#[test]
|
||||
fn accepts_release_and_environment_labels() {
|
||||
@@ -148,23 +124,4 @@ mod tests {
|
||||
assert_eq!(identity.version(), "0.3.1+build.7");
|
||||
assert_eq!(identity.environment(), "production");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_non_utf8_environment_values() {
|
||||
let error = env_value_or_default(
|
||||
"CRANK_ENVIRONMENT",
|
||||
Err(std::env::VarError::NotUnicode(OsString::from(
|
||||
"invalid-environment",
|
||||
))),
|
||||
"development",
|
||||
)
|
||||
.expect_err("non-UTF-8 values must not be replaced with defaults");
|
||||
|
||||
assert!(matches!(
|
||||
error,
|
||||
ObservabilityConfigError::InvalidEnvironmentEncoding {
|
||||
field: "CRANK_ENVIRONMENT"
|
||||
}
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user