use std::{
borrow::Cow,
collections::BTreeMap,
fmt::{self, Display, Write as _},
future::Future,
io,
sync::{Arc, Mutex},
};
use serde::{Serialize, Serializer, ser::SerializeMap};
use serde_json::Value;
use thiserror::Error;
use super::{
Diagnostic, SqlDiagnostic, SqlExecution,
bounded_json::{BoundedJsonValue, to_bounded_json_value},
};
const TRUNCATE_SUFFIX: &str = "…";
const DEFAULT_MAX_KEY_BYTES: usize = 256;
const DEFAULT_MAX_IDENTIFIER_BYTES: usize = 4 * 1024;
const DEFAULT_MAX_VALUE_BYTES: usize = 16 * 1024;
tokio::task_local! {
static CURRENT_REQUEST_LOG: RequestLogContext;
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum OperationKind {
Query,
Command,
}
#[derive(Clone, Debug, Serialize)]
#[non_exhaustive]
#[must_use = "操作信息需要写入请求日志上下文才会生效"]
pub struct RequestOperation {
pub name: Cow<'static, str>,
pub kind: OperationKind,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<Cow<'static, str>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub resource: Option<Cow<'static, str>>,
}
impl RequestOperation {
pub fn query(name: impl Into<String>) -> Self {
Self::new(name, OperationKind::Query)
}
pub fn command(name: impl Into<String>) -> Self {
Self::new(name, OperationKind::Command)
}
pub fn new(name: impl Into<String>, kind: OperationKind) -> Self {
Self {
name: Cow::Owned(truncate_owned(name.into(), DEFAULT_MAX_KEY_BYTES)),
kind,
description: None,
resource: None,
}
}
pub const fn query_static(name: &'static str) -> Self {
Self::new_static(name, OperationKind::Query)
}
pub const fn command_static(name: &'static str) -> Self {
Self::new_static(name, OperationKind::Command)
}
const fn new_static(name: &'static str, kind: OperationKind) -> Self {
Self {
name: Cow::Borrowed(name),
kind,
description: None,
resource: None,
}
}
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(Cow::Owned(truncate_owned(
description.into(),
DEFAULT_MAX_VALUE_BYTES,
)));
self
}
pub fn with_description_static(mut self, description: &'static str) -> Self {
self.description = Some(Cow::Borrowed(description));
self
}
pub fn with_resource(mut self, resource: impl Into<String>) -> Self {
self.resource = Some(Cow::Owned(truncate_owned(
resource.into(),
DEFAULT_MAX_KEY_BYTES,
)));
self
}
pub fn with_resource_static(mut self, resource: &'static str) -> Self {
self.resource = Some(Cow::Borrowed(resource));
self
}
}
#[derive(Clone, Debug, Serialize)]
#[non_exhaustive]
#[must_use = "主体信息需要写入请求日志上下文才会生效"]
pub struct RequestSubject {
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub kind: Option<String>,
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub attributes: BTreeMap<String, Value>,
}
impl RequestSubject {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: truncate_owned(id.into(), DEFAULT_MAX_IDENTIFIER_BYTES),
kind: None,
attributes: BTreeMap::new(),
}
}
pub fn with_kind(mut self, kind: impl Into<String>) -> Self {
self.kind = Some(truncate_owned(kind.into(), DEFAULT_MAX_KEY_BYTES));
self
}
pub fn with_attribute(mut self, key: impl Into<String>, value: impl Display) -> Self {
self.attributes.insert(
truncate_owned(key.into(), DEFAULT_MAX_KEY_BYTES),
Value::String(bounded_display(&value, DEFAULT_MAX_VALUE_BYTES)),
);
self
}
}
#[derive(Clone, Debug, Serialize)]
#[non_exhaustive]
#[must_use = "资源目标需要写入请求日志上下文才会生效"]
pub struct ResourceTarget {
pub kind: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub attributes: BTreeMap<String, Value>,
}
impl ResourceTarget {
pub fn new(kind: impl Into<String>) -> Self {
Self {
kind: truncate_owned(kind.into(), DEFAULT_MAX_KEY_BYTES),
id: None,
attributes: BTreeMap::new(),
}
}
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = Some(truncate_owned(id.into(), DEFAULT_MAX_IDENTIFIER_BYTES));
self
}
pub fn with_attribute(mut self, key: impl Into<String>, value: impl Display) -> Self {
self.attributes.insert(
truncate_owned(key.into(), DEFAULT_MAX_KEY_BYTES),
Value::String(bounded_display(&value, DEFAULT_MAX_VALUE_BYTES)),
);
self
}
}
#[derive(Clone, Debug, Serialize)]
#[non_exhaustive]
pub struct FieldChange {
pub field: String,
pub before: Value,
pub after: Value,
}
#[derive(Clone, Debug)]
#[must_use = "需要将限制传给 RequestLoggingConfig 才会生效"]
#[allow(clippy::struct_field_names)]
pub struct RequestLoggingLimits {
pub(crate) max_fields: usize,
pub(crate) max_targets: usize,
pub(crate) max_changes: usize,
pub(crate) max_diagnostics: usize,
pub(crate) max_sql_executions: usize,
pub(crate) max_value_bytes: usize,
pub(crate) max_collection_items: usize,
pub(crate) max_nesting_depth: usize,
pub(crate) max_header_value_bytes: usize,
pub(crate) max_uri_bytes: usize,
}
#[derive(Clone, Debug, Eq, Error, PartialEq)]
pub enum RequestLoggingLimitsError {
#[error("{name}必须大于 0")]
NonPositive { name: &'static str },
#[error("{name}不能小于 {minimum}")]
TooSmall { name: &'static str, minimum: usize },
}
impl Default for RequestLoggingLimits {
fn default() -> Self {
Self {
max_fields: 32,
max_targets: 32,
max_changes: 32,
max_diagnostics: 16,
max_sql_executions: 16,
max_value_bytes: DEFAULT_MAX_VALUE_BYTES,
max_collection_items: 64,
max_nesting_depth: 8,
max_header_value_bytes: 2 * 1024,
max_uri_bytes: 4 * 1024,
}
}
}
impl RequestLoggingLimits {
pub fn new() -> Self {
Self::default()
}
pub fn max_fields(mut self, value: usize) -> Self {
assert_positive(value, "请求日志字段数量上限");
self.max_fields = value;
self
}
pub fn try_max_fields(mut self, value: usize) -> Result<Self, RequestLoggingLimitsError> {
ensure_positive(value, "请求日志字段数量上限")?;
self.max_fields = value;
Ok(self)
}
pub fn max_targets(mut self, value: usize) -> Self {
assert_positive(value, "请求日志目标数量上限");
self.max_targets = value;
self
}
pub fn try_max_targets(mut self, value: usize) -> Result<Self, RequestLoggingLimitsError> {
ensure_positive(value, "请求日志目标数量上限")?;
self.max_targets = value;
Ok(self)
}
pub fn max_changes(mut self, value: usize) -> Self {
assert_positive(value, "请求日志变化数量上限");
self.max_changes = value;
self
}
pub fn try_max_changes(mut self, value: usize) -> Result<Self, RequestLoggingLimitsError> {
ensure_positive(value, "请求日志变化数量上限")?;
self.max_changes = value;
Ok(self)
}
pub fn max_diagnostics(mut self, value: usize) -> Self {
assert_positive(value, "请求日志诊断数量上限");
self.max_diagnostics = value;
self
}
pub fn try_max_diagnostics(mut self, value: usize) -> Result<Self, RequestLoggingLimitsError> {
ensure_positive(value, "请求日志诊断数量上限")?;
self.max_diagnostics = value;
Ok(self)
}
pub fn max_sql_executions(mut self, value: usize) -> Self {
assert_positive(value, "请求日志 SQL 执行记录数量上限");
self.max_sql_executions = value;
self
}
pub fn try_max_sql_executions(
mut self,
value: usize,
) -> Result<Self, RequestLoggingLimitsError> {
ensure_positive(value, "请求日志 SQL 执行记录数量上限")?;
self.max_sql_executions = value;
Ok(self)
}
pub fn max_value_bytes(mut self, value: usize) -> Self {
assert_minimum(value, 64, "请求日志单值字节上限");
self.max_value_bytes = value;
self
}
pub fn try_max_value_bytes(mut self, value: usize) -> Result<Self, RequestLoggingLimitsError> {
ensure_minimum(value, 64, "请求日志单值字节上限")?;
self.max_value_bytes = value;
Ok(self)
}
pub fn max_collection_items(mut self, value: usize) -> Self {
assert_positive(value, "请求日志 JSON 集合元素上限");
self.max_collection_items = value;
self
}
pub fn try_max_collection_items(
mut self,
value: usize,
) -> Result<Self, RequestLoggingLimitsError> {
ensure_positive(value, "请求日志 JSON 集合元素上限")?;
self.max_collection_items = value;
Ok(self)
}
pub fn max_nesting_depth(mut self, value: usize) -> Self {
assert_positive(value, "请求日志 JSON 嵌套深度上限");
self.max_nesting_depth = value;
self
}
pub fn try_max_nesting_depth(
mut self,
value: usize,
) -> Result<Self, RequestLoggingLimitsError> {
ensure_positive(value, "请求日志 JSON 嵌套深度上限")?;
self.max_nesting_depth = value;
Ok(self)
}
pub fn max_header_value_bytes(mut self, value: usize) -> Self {
assert_minimum(value, 4, "请求头日志值字节上限");
self.max_header_value_bytes = value;
self
}
pub fn try_max_header_value_bytes(
mut self,
value: usize,
) -> Result<Self, RequestLoggingLimitsError> {
ensure_minimum(value, 4, "请求头日志值字节上限")?;
self.max_header_value_bytes = value;
Ok(self)
}
pub fn max_uri_bytes(mut self, value: usize) -> Self {
assert_minimum(value, 4, "请求 URI 日志字节上限");
self.max_uri_bytes = value;
self
}
pub fn try_max_uri_bytes(mut self, value: usize) -> Result<Self, RequestLoggingLimitsError> {
ensure_minimum(value, 4, "请求 URI 日志字节上限")?;
self.max_uri_bytes = value;
Ok(self)
}
}
fn assert_positive(value: usize, name: &str) {
assert!(value > 0, "{name}必须大于 0");
}
fn assert_minimum(value: usize, minimum: usize, name: &str) {
assert!(value >= minimum, "{name}不能小于 {minimum}");
}
const fn ensure_positive(
value: usize,
name: &'static str,
) -> Result<(), RequestLoggingLimitsError> {
if value == 0 {
Err(RequestLoggingLimitsError::NonPositive { name })
} else {
Ok(())
}
}
const fn ensure_minimum(
value: usize,
minimum: usize,
name: &'static str,
) -> Result<(), RequestLoggingLimitsError> {
if value < minimum {
Err(RequestLoggingLimitsError::TooSmall { name, minimum })
} else {
Ok(())
}
}
#[derive(Clone, Debug, Default, Serialize)]
pub struct RequestLogTruncation {
#[serde(skip_serializing_if = "is_zero")]
pub dropped_fields: usize,
#[serde(skip_serializing_if = "is_zero")]
pub dropped_targets: usize,
#[serde(skip_serializing_if = "is_zero")]
pub dropped_changes: usize,
#[serde(skip_serializing_if = "is_zero")]
pub dropped_attempted_changes: usize,
#[serde(skip_serializing_if = "is_zero")]
pub dropped_diagnostics: usize,
#[serde(skip_serializing_if = "is_zero")]
pub dropped_sql_executions: usize,
#[serde(skip_serializing_if = "is_zero")]
pub truncated_values: usize,
}
impl RequestLogTruncation {
const fn is_empty(&self) -> bool {
self.dropped_fields == 0
&& self.dropped_targets == 0
&& self.dropped_changes == 0
&& self.dropped_attempted_changes == 0
&& self.dropped_diagnostics == 0
&& self.dropped_sql_executions == 0
&& self.truncated_values == 0
}
}
#[allow(clippy::trivially_copy_pass_by_ref)]
const fn is_zero(value: &usize) -> bool {
*value == 0
}
impl FieldChange {
pub fn new(field: impl Into<String>, before: impl Display, after: impl Display) -> Self {
Self {
field: truncate_owned(field.into(), DEFAULT_MAX_KEY_BYTES),
before: Value::String(bounded_display(&before, DEFAULT_MAX_VALUE_BYTES)),
after: Value::String(bounded_display(&after, DEFAULT_MAX_VALUE_BYTES)),
}
}
pub fn from_json<T, U>(
field: impl Into<String>,
before: T,
after: U,
) -> Result<Self, serde_json::Error>
where
T: Serialize,
U: Serialize,
{
let before = standalone_json_value(&before)?;
let after = standalone_json_value(&after)?;
Ok(Self {
field: truncate_owned(field.into(), DEFAULT_MAX_KEY_BYTES),
before,
after,
})
}
pub fn from_values(
field: impl Into<String>,
before: impl Into<Value>,
after: impl Into<Value>,
) -> Self {
Self {
field: truncate_owned(field.into(), DEFAULT_MAX_KEY_BYTES),
before: standalone_bounded_value(before.into()),
after: standalone_bounded_value(after.into()),
}
}
}
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct RequestLogSnapshot {
pub fields: BTreeMap<String, Value>,
pub operation: Option<RequestOperation>,
pub subject: Option<RequestSubject>,
pub targets: Vec<ResourceTarget>,
pub changes: Vec<FieldChange>,
pub attempted_changes: Vec<FieldChange>,
pub diagnostics: Vec<Diagnostic>,
pub sql_executions: Vec<SqlExecution>,
pub truncation: RequestLogTruncation,
}
impl RequestLogSnapshot {
#[must_use]
pub fn operation_kind(&self) -> Option<OperationKind> {
self.operation.as_ref().map(|operation| operation.kind)
}
}
impl Serialize for RequestLogSnapshot {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let structured_count = usize::from(self.operation.is_some())
+ usize::from(self.subject.is_some())
+ usize::from(!self.targets.is_empty())
+ usize::from(!self.changes.is_empty())
+ usize::from(!self.attempted_changes.is_empty())
+ usize::from(!self.diagnostics.is_empty())
+ usize::from(!self.sql_executions.is_empty())
+ usize::from(!self.truncation.is_empty());
let mut map = serializer.serialize_map(Some(self.fields.len() + structured_count))?;
for (key, value) in &self.fields {
let reserved = matches!(
key.as_str(),
"operation"
| "subject"
| "targets"
| "changes"
| "attempted_changes"
| "diagnostics"
| "sql_executions"
| "truncation"
);
if !reserved
|| (key == "operation" && self.operation.is_none())
|| (key == "subject" && self.subject.is_none())
|| (key == "targets" && self.targets.is_empty())
|| (key == "changes" && self.changes.is_empty())
|| (key == "attempted_changes" && self.attempted_changes.is_empty())
|| (key == "diagnostics" && self.diagnostics.is_empty())
|| (key == "sql_executions" && self.sql_executions.is_empty())
|| (key == "truncation" && self.truncation.is_empty())
{
map.serialize_entry(key, value)?;
}
}
if let Some(operation) = &self.operation {
map.serialize_entry("operation", operation)?;
}
if let Some(subject) = &self.subject {
map.serialize_entry("subject", subject)?;
}
if !self.targets.is_empty() {
map.serialize_entry("targets", &self.targets)?;
}
if !self.changes.is_empty() {
map.serialize_entry("changes", &self.changes)?;
}
if !self.attempted_changes.is_empty() {
map.serialize_entry("attempted_changes", &self.attempted_changes)?;
}
if !self.diagnostics.is_empty() {
map.serialize_entry("diagnostics", &self.diagnostics)?;
}
if !self.sql_executions.is_empty() {
map.serialize_entry("sql_executions", &self.sql_executions)?;
}
if !self.truncation.is_empty() {
map.serialize_entry("truncation", &self.truncation)?;
}
map.end()
}
}
#[derive(Clone, Default)]
#[must_use = "应将请求日志上下文写入请求扩展或继续传递"]
pub struct RequestLogContext {
state: Arc<Mutex<RequestLogState>>,
}
#[derive(Default)]
struct RequestLogState {
snapshot: RequestLogSnapshot,
limits: RequestLoggingLimits,
#[cfg(any(feature = "connection-mysql", feature = "connection-postgres"))]
next_sql_execution_id: u64,
}
#[derive(Clone, Copy)]
pub(super) struct RequestLogDecision {
pub operation_kind: Option<OperationKind>,
pub has_failures: bool,
pub is_empty: bool,
}
impl RequestLogContext {
pub fn new() -> Self {
Self::default()
}
pub(crate) fn with_limits(limits: RequestLoggingLimits) -> Self {
Self {
state: Arc::new(Mutex::new(RequestLogState {
snapshot: RequestLogSnapshot::default(),
limits,
#[cfg(any(feature = "connection-mysql", feature = "connection-postgres"))]
next_sql_execution_id: 0,
})),
}
}
pub fn try_current() -> Option<Self> {
CURRENT_REQUEST_LOG.try_with(Clone::clone).ok()
}
pub(crate) fn is_current() -> bool {
CURRENT_REQUEST_LOG.try_with(|_| ()).is_ok()
}
pub fn with_current<T>(action: impl FnOnce(&Self) -> T) -> Option<T> {
CURRENT_REQUEST_LOG.try_with(|context| action(context)).ok()
}
pub fn spawn_current<F>(future: F) -> tokio::task::JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
let context = Self::try_current();
tokio::spawn(async move {
if let Some(context) = context {
scope_request_log(context, future).await
} else {
future.await
}
})
}
pub fn insert(&self, key: impl Into<String>, value: impl Display) {
let Some((key, max_bytes)) = self.prepare_field_key(key.into()) else {
return;
};
let value = bounded_display(&value, max_bytes);
self.insert_prepared_value(key, Value::String(value), false);
}
fn prepare_field_key(&self, mut key: String) -> Option<(String, usize)> {
self.with_state(|state| {
bound_string(
&mut key,
key_limit(&state.limits),
&mut state.snapshot.truncation,
);
if state.snapshot.fields.contains_key(&key)
|| state.snapshot.fields.len() < state.limits.max_fields
{
Some((key, state.limits.max_value_bytes))
} else {
state.snapshot.truncation.dropped_fields += 1;
None
}
})
}
fn insert_prepared_value(&self, key: String, value: Value, was_truncated: bool) {
self.with_state(|state| {
if !state.snapshot.fields.contains_key(&key)
&& state.snapshot.fields.len() >= state.limits.max_fields
{
state.snapshot.truncation.dropped_fields += 1;
return;
}
if was_truncated {
state.snapshot.truncation.truncated_values += 1;
}
let value = bounded_value(value, &state.limits, &mut state.snapshot.truncation);
state.snapshot.fields.insert(key, value);
});
}
pub fn insert_json<T>(&self, key: impl Into<String>, value: T) -> Result<(), serde_json::Error>
where
T: Serialize,
{
let Some((key, max_bytes)) = self.prepare_field_key(key.into()) else {
return Ok(());
};
match to_bounded_json_value(&value, max_bytes)? {
BoundedJsonValue::Value(value) => self.insert_prepared_value(key, value, false),
BoundedJsonValue::TooLarge => {
self.insert_prepared_value(key, Value::String("内容过大,已省略".into()), true);
}
}
Ok(())
}
#[must_use]
pub fn remove(&self, key: &str) -> Option<String> {
self.with_state(|state| state.snapshot.fields.remove(key).map(value_to_string))
}
#[must_use]
pub fn snapshot(&self) -> BTreeMap<String, String> {
self.with_state(|state| {
state
.snapshot
.fields
.iter()
.map(|(key, value)| (key.clone(), value_to_string(value.clone())))
.collect()
})
}
#[must_use]
pub fn structured_snapshot(&self) -> RequestLogSnapshot {
self.with_state(|state| state.snapshot.clone())
}
#[must_use]
pub fn operation_kind(&self) -> Option<OperationKind> {
self.with_state(|state| state.snapshot.operation_kind())
}
#[must_use]
pub fn has_diagnostics(&self) -> bool {
self.with_state(|state| !state.snapshot.diagnostics.is_empty())
}
pub(super) fn decision(&self) -> RequestLogDecision {
self.with_state(|state| RequestLogDecision {
operation_kind: state.snapshot.operation_kind(),
has_failures: !state.snapshot.diagnostics.is_empty()
|| state
.snapshot
.sql_executions
.iter()
.any(SqlExecution::is_failure),
is_empty: snapshot_is_empty(&state.snapshot),
})
}
pub fn set_operation(&self, mut operation: RequestOperation) {
self.with_state(|state| {
bound_cow(
&mut operation.name,
key_limit(&state.limits),
&mut state.snapshot.truncation,
);
if let Some(description) = &mut operation.description {
bound_cow(
description,
state.limits.max_value_bytes,
&mut state.snapshot.truncation,
);
}
if let Some(resource) = &mut operation.resource {
bound_cow(
resource,
key_limit(&state.limits),
&mut state.snapshot.truncation,
);
}
state.snapshot.operation = Some(operation);
});
}
pub fn set_subject(&self, mut subject: RequestSubject) {
self.with_state(|state| {
bound_string(
&mut subject.id,
identifier_limit(&state.limits),
&mut state.snapshot.truncation,
);
if let Some(kind) = &mut subject.kind {
bound_string(
kind,
key_limit(&state.limits),
&mut state.snapshot.truncation,
);
}
bound_map(
&mut subject.attributes,
&state.limits,
&mut state.snapshot.truncation,
);
state.snapshot.subject = Some(subject);
});
}
pub fn add_target(&self, mut target: ResourceTarget) {
self.with_state(|state| {
if state.snapshot.targets.len() >= state.limits.max_targets {
state.snapshot.truncation.dropped_targets += 1;
return;
}
bound_string(
&mut target.kind,
key_limit(&state.limits),
&mut state.snapshot.truncation,
);
if let Some(id) = &mut target.id {
bound_string(
id,
identifier_limit(&state.limits),
&mut state.snapshot.truncation,
);
}
bound_map(
&mut target.attributes,
&state.limits,
&mut state.snapshot.truncation,
);
state.snapshot.targets.push(target);
});
}
pub fn add_change(&self, change: FieldChange) {
self.with_state(|state| {
if state.snapshot.changes.len() >= state.limits.max_changes {
state.snapshot.truncation.dropped_changes += 1;
return;
}
let change = bounded_change(change, &state.limits, &mut state.snapshot.truncation);
state.snapshot.changes.push(change);
});
}
pub fn add_attempted_change(&self, change: FieldChange) {
self.with_state(|state| {
if state.snapshot.attempted_changes.len() >= state.limits.max_changes {
state.snapshot.truncation.dropped_attempted_changes += 1;
return;
}
let change = bounded_change(change, &state.limits, &mut state.snapshot.truncation);
state.snapshot.attempted_changes.push(change);
});
}
pub fn record_diagnostic(&self, diagnostic: impl Into<Diagnostic>) {
let diagnostic = diagnostic.into();
self.with_state(|state| {
if state.snapshot.diagnostics.len() >= state.limits.max_diagnostics {
state.snapshot.truncation.dropped_diagnostics += 1;
return;
}
let mut diagnostic = diagnostic;
bound_string(
&mut diagnostic.name,
key_limit(&state.limits),
&mut state.snapshot.truncation,
);
bound_string(
&mut diagnostic.error,
state.limits.max_value_bytes,
&mut state.snapshot.truncation,
);
bound_map(
&mut diagnostic.details,
&state.limits,
&mut state.snapshot.truncation,
);
state.snapshot.diagnostics.push(diagnostic);
});
}
pub fn record_sql_error(&self, diagnostic: SqlDiagnostic) {
self.record_diagnostic(diagnostic);
}
#[must_use]
#[cfg(any(feature = "connection-mysql", feature = "connection-postgres"))]
pub(crate) fn record_sql_execution(&self, mut execution: SqlExecution) -> Option<u64> {
self.with_state(|state| {
if state.snapshot.sql_executions.len() >= state.limits.max_sql_executions {
state.snapshot.truncation.dropped_sql_executions += 1;
return None;
}
state.next_sql_execution_id = state.next_sql_execution_id.saturating_add(1);
execution.execution_id = state.next_sql_execution_id;
bound_sql_execution(
&mut execution,
&state.limits,
&mut state.snapshot.truncation,
);
let execution_id = execution.execution_id;
state.snapshot.sql_executions.push(execution);
Some(execution_id)
})
}
fn with_state<T>(&self, action: impl FnOnce(&mut RequestLogState) -> T) -> T {
let mut state = self
.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
action(&mut state)
}
}
impl Serialize for RequestLogContext {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let state = self
.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
state.snapshot.serialize(serializer)
}
}
fn snapshot_is_empty(snapshot: &RequestLogSnapshot) -> bool {
snapshot.fields.is_empty()
&& snapshot.operation.is_none()
&& snapshot.subject.is_none()
&& snapshot.targets.is_empty()
&& snapshot.changes.is_empty()
&& snapshot.attempted_changes.is_empty()
&& snapshot.diagnostics.is_empty()
&& snapshot.sql_executions.is_empty()
&& snapshot.truncation.is_empty()
}
#[cfg(any(feature = "connection-mysql", feature = "connection-postgres"))]
fn bound_sql_execution(
execution: &mut SqlExecution,
limits: &RequestLoggingLimits,
truncation: &mut RequestLogTruncation,
) {
bound_string(&mut execution.query_name, key_limit(limits), truncation);
bound_string(&mut execution.statement, limits.max_value_bytes, truncation);
bound_map(&mut execution.safe_parameters, limits, truncation);
if let Some(error) = &mut execution.error {
bound_string(&mut error.class, key_limit(limits), truncation);
bound_string(&mut error.message, limits.max_value_bytes, truncation);
if let Some(code) = &mut error.code {
bound_string(code, key_limit(limits), truncation);
}
if let Some(constraint) = &mut error.constraint {
bound_string(constraint, key_limit(limits), truncation);
}
}
}
fn bounded_change(
mut change: FieldChange,
limits: &RequestLoggingLimits,
truncation: &mut RequestLogTruncation,
) -> FieldChange {
bound_string(&mut change.field, key_limit(limits), truncation);
change.before = bounded_value(change.before, limits, truncation);
change.after = bounded_value(change.after, limits, truncation);
change
}
fn bound_map(
values: &mut BTreeMap<String, Value>,
limits: &RequestLoggingLimits,
truncation: &mut RequestLogTruncation,
) {
let original_len = values.len();
let mut bounded = BTreeMap::new();
for (mut key, value) in std::mem::take(values)
.into_iter()
.take(limits.max_collection_items)
{
bound_string(&mut key, key_limit(limits), truncation);
bounded.insert(key, bounded_value(value, limits, truncation));
}
if original_len > bounded.len() {
truncation.truncated_values += 1;
}
*values = bounded;
}
const fn key_limit(limits: &RequestLoggingLimits) -> usize {
if limits.max_value_bytes < DEFAULT_MAX_KEY_BYTES {
limits.max_value_bytes
} else {
DEFAULT_MAX_KEY_BYTES
}
}
const fn identifier_limit(limits: &RequestLoggingLimits) -> usize {
if limits.max_value_bytes < DEFAULT_MAX_IDENTIFIER_BYTES {
limits.max_value_bytes
} else {
DEFAULT_MAX_IDENTIFIER_BYTES
}
}
fn bound_cow(
value: &mut Cow<'static, str>,
max_bytes: usize,
truncation: &mut RequestLogTruncation,
) {
if value.len() <= max_bytes {
return;
}
let mut owned = value.to_string();
if truncate_utf8(&mut owned, max_bytes) {
truncation.truncated_values += 1;
}
*value = Cow::Owned(owned);
}
fn bound_string(value: &mut String, max_bytes: usize, truncation: &mut RequestLogTruncation) {
if truncate_utf8(value, max_bytes) {
truncation.truncated_values += 1;
}
}
fn bounded_value(
mut value: Value,
limits: &RequestLoggingLimits,
truncation: &mut RequestLogTruncation,
) -> Value {
truncate_value_structure(&mut value, limits, truncation, 0);
if json_fits_limit(&value, limits.max_value_bytes) {
value
} else {
truncation.truncated_values += 1;
Value::String("内容过大,已省略".into())
}
}
fn standalone_json_value<T>(value: &T) -> Result<Value, serde_json::Error>
where
T: Serialize + ?Sized,
{
Ok(
match to_bounded_json_value(value, DEFAULT_MAX_VALUE_BYTES)? {
BoundedJsonValue::Value(value) => standalone_bounded_value(value),
BoundedJsonValue::TooLarge => Value::String("内容过大,已省略".into()),
},
)
}
fn standalone_bounded_value(value: Value) -> Value {
let limits = RequestLoggingLimits::default();
let mut truncation = RequestLogTruncation::default();
bounded_value(value, &limits, &mut truncation)
}
fn json_fits_limit(value: &Value, max_bytes: usize) -> bool {
serde_json::to_writer(
SizeLimitedWriter {
written: 0,
max_bytes,
},
value,
)
.is_ok()
}
struct SizeLimitedWriter {
written: usize,
max_bytes: usize,
}
impl io::Write for SizeLimitedWriter {
fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
let Some(total) = self.written.checked_add(buffer.len()) else {
return Err(io::Error::other("JSON 字节数溢出"));
};
if total > self.max_bytes {
return Err(io::Error::other("JSON 超过日志字段限制"));
}
self.written = total;
Ok(buffer.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
fn truncate_value_structure(
value: &mut Value,
limits: &RequestLoggingLimits,
truncation: &mut RequestLogTruncation,
depth: usize,
) {
if depth >= limits.max_nesting_depth && matches!(value, Value::Array(_) | Value::Object(_)) {
*value = Value::String("嵌套过深,已省略".into());
truncation.truncated_values += 1;
return;
}
match value {
Value::String(text) => {
if truncate_utf8(text, limits.max_value_bytes) {
truncation.truncated_values += 1;
}
}
Value::Array(values) => {
if values.len() > limits.max_collection_items {
values.truncate(limits.max_collection_items);
truncation.truncated_values += 1;
}
for value in values {
truncate_value_structure(value, limits, truncation, depth + 1);
}
}
Value::Object(values) => {
if values.len() > limits.max_collection_items {
let retained = std::mem::take(values)
.into_iter()
.take(limits.max_collection_items)
.collect();
*values = retained;
truncation.truncated_values += 1;
}
for value in values.values_mut() {
truncate_value_structure(value, limits, truncation, depth + 1);
}
}
Value::Null | Value::Bool(_) | Value::Number(_) => {}
}
}
pub(super) fn truncate_utf8(value: &mut String, max_bytes: usize) -> bool {
if value.len() <= max_bytes {
return false;
}
let target = max_bytes.saturating_sub(TRUNCATE_SUFFIX.len());
let mut end = target.min(value.len());
while !value.is_char_boundary(end) {
end -= 1;
}
value.truncate(end);
if max_bytes >= TRUNCATE_SUFFIX.len() {
value.push_str(TRUNCATE_SUFFIX);
}
true
}
fn truncate_owned(mut value: String, max_bytes: usize) -> String {
truncate_utf8(&mut value, max_bytes);
value
}
pub(crate) async fn scope_request_log<F>(context: RequestLogContext, future: F) -> F::Output
where
F: Future,
{
CURRENT_REQUEST_LOG.scope(context, future).await
}
fn value_to_string(value: Value) -> String {
match value {
Value::String(value) => value,
value => value.to_string(),
}
}
fn bounded_display<T>(value: &T, max_bytes: usize) -> String
where
T: Display + ?Sized,
{
let mut writer = LimitedDisplayWriter {
output: String::with_capacity(max_bytes.min(256)),
max_bytes,
truncated: false,
};
let _ = write!(&mut writer, "{value}");
if writer.truncated {
let keep = max_bytes.saturating_sub(TRUNCATE_SUFFIX.len());
let mut boundary = keep.min(writer.output.len());
while boundary > 0 && !writer.output.is_char_boundary(boundary) {
boundary -= 1;
}
writer.output.truncate(boundary);
if max_bytes >= TRUNCATE_SUFFIX.len() {
writer.output.push_str(TRUNCATE_SUFFIX);
}
}
writer.output
}
struct LimitedDisplayWriter {
output: String,
max_bytes: usize,
truncated: bool,
}
impl fmt::Write for LimitedDisplayWriter {
fn write_str(&mut self, value: &str) -> fmt::Result {
let remaining = self.max_bytes.saturating_sub(self.output.len());
if value.len() <= remaining {
self.output.push_str(value);
return Ok(());
}
let mut boundary = remaining;
while boundary > 0 && !value.is_char_boundary(boundary) {
boundary -= 1;
}
self.output.push_str(&value[..boundary]);
self.truncated = true;
Err(fmt::Error)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn structured_context_preserves_native_json_values() {
let context = RequestLogContext::new();
context.insert_json("count", 2_u64).unwrap();
context.set_operation(RequestOperation::command("create_user"));
context.set_subject(RequestSubject::new("guid-1").with_kind("user"));
context.add_change(FieldChange::from_json("enabled", false, true).unwrap());
let value = serde_json::to_value(context.structured_snapshot()).unwrap();
assert_eq!(value["count"], 2);
assert_eq!(value["operation"]["kind"], "command");
assert_eq!(value["subject"]["id"], "guid-1");
assert_eq!(value["changes"][0]["before"], false);
}
#[test]
fn fallible_limit_builders_reject_invalid_configuration() {
assert!(RequestLoggingLimits::new().try_max_fields(0).is_err());
assert!(
RequestLoggingLimits::new()
.try_max_sql_executions(0)
.is_err()
);
assert!(RequestLoggingLimits::new().try_max_value_bytes(63).is_err());
assert!(RequestLoggingLimits::new().try_max_uri_bytes(3).is_err());
assert!(RequestLoggingLimits::new().try_max_fields(1).is_ok());
}
#[tokio::test]
async fn current_context_is_available_only_inside_scope() {
assert!(RequestLogContext::try_current().is_none());
assert!(!RequestLogContext::is_current());
let context = RequestLogContext::new();
scope_request_log(context.clone(), async {
assert!(RequestLogContext::is_current());
RequestLogContext::try_current()
.unwrap()
.insert("stage", "service");
})
.await;
assert_eq!(context.snapshot().get("stage").unwrap(), "service");
assert!(RequestLogContext::try_current().is_none());
assert!(!RequestLogContext::is_current());
}
#[test]
fn configured_limits_bound_context_and_report_truncation() {
let context = RequestLogContext::with_limits(
RequestLoggingLimits::new()
.max_fields(2)
.max_value_bytes(64)
.max_collection_items(2),
);
context.insert("first", "x".repeat(100));
context.insert("second", "保留");
context.insert("third", "丢弃");
context
.insert_json("second", serde_json::json!([1, 2, 3, 4]))
.unwrap();
let snapshot = context.structured_snapshot();
assert_eq!(snapshot.fields.len(), 2);
assert_eq!(snapshot.fields["second"], serde_json::json!([1, 2]));
assert_eq!(snapshot.truncation.dropped_fields, 1);
assert!(snapshot.truncation.truncated_values >= 2);
}
#[test]
fn field_names_keep_an_independent_hard_limit() {
let context = RequestLogContext::with_limits(
RequestLoggingLimits::new().max_value_bytes(DEFAULT_MAX_VALUE_BYTES * 2),
);
context.insert("k".repeat(DEFAULT_MAX_KEY_BYTES * 2), "值");
let snapshot = context.structured_snapshot();
assert_eq!(snapshot.fields.len(), 1);
assert!(snapshot.fields.keys().next().unwrap().len() <= DEFAULT_MAX_KEY_BYTES);
assert_eq!(snapshot.truncation.truncated_values, 1);
}
#[test]
fn oversized_json_is_omitted_without_building_an_unbounded_value() {
let context =
RequestLogContext::with_limits(RequestLoggingLimits::new().max_value_bytes(64));
context.insert_json("payload", "x".repeat(1024)).unwrap();
let snapshot = context.structured_snapshot();
assert_eq!(snapshot.fields["payload"], "内容过大,已省略");
assert_eq!(snapshot.truncation.truncated_values, 1);
}
#[test]
fn standalone_audit_values_apply_safe_default_bounds() {
let change =
FieldChange::from_json("field", "x".repeat(DEFAULT_MAX_VALUE_BYTES * 2), true).unwrap();
assert_eq!(change.before, "内容过大,已省略");
let subject = RequestSubject::new("id")
.with_attribute("source", "x".repeat(DEFAULT_MAX_VALUE_BYTES * 2));
assert!(
subject.attributes["source"]
.as_str()
.is_some_and(|value| value.len() <= DEFAULT_MAX_VALUE_BYTES)
);
}
#[tokio::test]
async fn spawn_current_propagates_task_local_context() {
let context = RequestLogContext::new();
scope_request_log(context.clone(), async {
RequestLogContext::spawn_current(async {
RequestLogContext::with_current(|current| current.insert("task", "完成"));
})
.await
.unwrap();
})
.await;
assert_eq!(context.snapshot()["task"], "完成");
}
}