mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-09 14:49:25 +00:00
6db4c3538d
* fix(allocator): restore validated jemalloc target gating Restrict the global allocator and jemalloc profiling paths to linux-gnu-x86_64 so Linux ARM64 builds fall back to mimalloc again. Update the runtime profiling, allocator reclaim, and admin profiling handlers to use the same target gating and avoid exposing jemalloc-only code on unsupported targets. Verification: - cargo fmt --all - cargo fmt --all --check - cargo check -p rustfs - make pre-commit * fix(profiling): restore non-jemalloc platform behavior Restore CPU profiling support on macOS while keeping jemalloc-backed memory profiling restricted to validated linux-gnu-x86_64 targets. Also restore Unix log directory permission hardening so the allocator regression fix does not roll back unrelated observability behavior on other supported platforms. Verification in progress: - cargo fmt --all - cargo check -p rustfs - make pre-commit (running) * fix(profiling): qualify periodic memory log macros Use explicit tracing macro paths in periodic jemalloc memory profiling logging so builds do not fail when the local target configuration excludes those branches from the current import set. Verification: - cargo fmt --all - cargo check -p rustfs * fix(profiling): gate unsupported memory helper Restrict the unsupported memory profiling helper to non-linux-gnu-x86_64 targets so Linux builds do not emit dead_code warnings for an unreachable fallback. Verification: - cargo fmt --all - cargo check -p rustfs
134 lines
5.9 KiB
Rust
134 lines
5.9 KiB
Rust
// Copyright 2024 RustFS Team
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
//! RAII guard for OpenTelemetry provider lifecycle management.
|
|
//!
|
|
//! [`OtelGuard`] holds all runtime resources created during telemetry
|
|
//! initialisation. Dropping it triggers an ordered shutdown:
|
|
//!
|
|
//! 1. Tracer provider — flushes pending spans.
|
|
//! 2. Meter provider — flushes pending metrics.
|
|
//! 3. Logger provider — flushes pending log records.
|
|
//! 4. Profiling agent — flushes pending profiles.
|
|
//! 5. Cleanup task — aborted to prevent lingering background work.
|
|
//! 6. Tracing worker guard — flushes buffered log lines written by
|
|
//! `tracing_appender`.
|
|
//! 7. Stdout worker guard — flushes buffered log lines written to stdout.
|
|
|
|
use opentelemetry_sdk::{logs::SdkLoggerProvider, metrics::SdkMeterProvider, trace::SdkTracerProvider};
|
|
|
|
/// RAII guard that owns all active OpenTelemetry providers and the
|
|
/// `tracing_appender` worker guard.
|
|
///
|
|
/// Construct this via the `init_*` functions in [`crate::telemetry`] rather
|
|
/// than directly. The guard must be kept alive for the entire duration of the
|
|
/// application — once dropped, all telemetry pipelines are shut down.
|
|
pub struct OtelGuard {
|
|
/// Optional tracer provider for distributed tracing.
|
|
pub(crate) tracer_provider: Option<SdkTracerProvider>,
|
|
/// Optional meter provider for metrics collection.
|
|
pub(crate) meter_provider: Option<SdkMeterProvider>,
|
|
/// Optional logger provider for OTLP log export.
|
|
pub(crate) logger_provider: Option<SdkLoggerProvider>,
|
|
#[cfg(any(target_os = "macos", all(target_os = "linux", target_env = "gnu", target_arch = "x86_64")))]
|
|
pub(crate) profiling_agent: Option<pyroscope::PyroscopeAgent<pyroscope::pyroscope::PyroscopeAgentRunning>>,
|
|
#[cfg(all(target_os = "linux", target_env = "gnu", target_arch = "x86_64"))]
|
|
pub(crate) memory_profiling_agent: Option<pyroscope::PyroscopeAgent<pyroscope::pyroscope::PyroscopeAgentRunning>>,
|
|
/// Handle to the background log-cleanup task; aborted on drop.
|
|
pub(crate) cleanup_handle: Option<tokio::task::JoinHandle<()>>,
|
|
/// Worker guard that keeps the non-blocking `tracing_appender` thread
|
|
/// alive. Dropping it blocks until all buffered records are flushed.
|
|
pub(crate) tracing_guard: Option<tracing_appender::non_blocking::WorkerGuard>,
|
|
/// Optional guard for stdout logging; kept separate to allow independent flushing and shutdown.
|
|
pub(crate) stdout_guard: Option<tracing_appender::non_blocking::WorkerGuard>,
|
|
}
|
|
|
|
impl std::fmt::Debug for OtelGuard {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
let mut s = f.debug_struct("OtelGuard");
|
|
s.field("tracer_provider", &self.tracer_provider.is_some())
|
|
.field("meter_provider", &self.meter_provider.is_some())
|
|
.field("logger_provider", &self.logger_provider.is_some());
|
|
#[cfg(any(target_os = "macos", all(target_os = "linux", target_env = "gnu", target_arch = "x86_64")))]
|
|
s.field("profiling_agent", &self.profiling_agent.is_some());
|
|
#[cfg(all(target_os = "linux", target_env = "gnu", target_arch = "x86_64"))]
|
|
s.field("memory_profiling_agent", &self.memory_profiling_agent.is_some());
|
|
s.field("cleanup_handle", &self.cleanup_handle.is_some())
|
|
.field("tracing_guard", &self.tracing_guard.is_some())
|
|
.field("stdout_guard", &self.stdout_guard.is_some())
|
|
.finish()
|
|
}
|
|
}
|
|
|
|
impl Drop for OtelGuard {
|
|
/// Shut down all telemetry providers in order.
|
|
///
|
|
/// Errors during shutdown are printed to `stderr` so they are visible even
|
|
/// after the tracing subscriber has been torn down.
|
|
fn drop(&mut self) {
|
|
if let Some(provider) = self.tracer_provider.take()
|
|
&& let Err(err) = provider.shutdown()
|
|
{
|
|
eprintln!("Tracer shutdown error: {err:?}");
|
|
}
|
|
|
|
if let Some(provider) = self.meter_provider.take()
|
|
&& let Err(err) = provider.shutdown()
|
|
{
|
|
eprintln!("Meter shutdown error: {err:?}");
|
|
}
|
|
|
|
if let Some(provider) = self.logger_provider.take()
|
|
&& let Err(err) = provider.shutdown()
|
|
{
|
|
eprintln!("Logger shutdown error: {err:?}");
|
|
}
|
|
|
|
#[cfg(any(target_os = "macos", all(target_os = "linux", target_env = "gnu", target_arch = "x86_64")))]
|
|
if let Some(agent) = self.profiling_agent.take() {
|
|
match agent.stop() {
|
|
Err(err) => eprintln!("Profiling agent stop error: {err:?}"),
|
|
Ok(stopped) => {
|
|
stopped.shutdown();
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(all(target_os = "linux", target_env = "gnu", target_arch = "x86_64"))]
|
|
if let Some(agent) = self.memory_profiling_agent.take() {
|
|
match agent.stop() {
|
|
Err(err) => eprintln!("Memory profiling agent stop error: {err:?}"),
|
|
Ok(stopped) => {
|
|
stopped.shutdown();
|
|
}
|
|
}
|
|
}
|
|
|
|
if let Some(handle) = self.cleanup_handle.take() {
|
|
handle.abort();
|
|
eprintln!("Log cleanup task stopped");
|
|
}
|
|
|
|
if let Some(guard) = self.tracing_guard.take() {
|
|
drop(guard);
|
|
eprintln!("Tracing guard dropped, flushing logs.");
|
|
}
|
|
|
|
if let Some(guard) = self.stdout_guard.take() {
|
|
drop(guard);
|
|
eprintln!("Stdout guard dropped, flushing logs.");
|
|
}
|
|
}
|
|
}
|