pub struct TelemetryGuard { /* private fields */ }Expand description
Guard that manages telemetry lifecycle
When dropped, ensures proper cleanup of telemetry resources including flushing any pending trace/metric data to exporters.
§Critical: Drop Behavior
The TelemetryGuard MUST outlive all traced code in your application.
When the guard is dropped, its Drop implementation:
- Flushes all pending traces to configured exporters (OTLP, etc.)
- Shuts down the OpenTelemetry tracer provider
- Releases telemetry resources
§Common Pitfall
ⓘ
// ❌ WRONG: Guard dropped too early
{
let _guard = TelemetryConfig::default().init()?;
} // Guard dropped here
my_traced_function().await; // Traces lost!ⓘ
// ✅ CORRECT: Guard outlives traced code
let _guard = TelemetryConfig::default().init()?;
my_traced_function().await;
// Guard dropped at end of scope, traces flushed§Best Practice
Store the guard in your main application struct or as a variable in main():
ⓘ
#[tokio::main]
async fn main() -> Result<()> {
let _telemetry = TelemetryConfig::default().init()?;
// Run your server
run_server().await?;
Ok(())
// Guard dropped here after server shutdown
}§Example
ⓘ
use turbomcp_telemetry::{TelemetryConfig, TelemetryGuard};
let config = TelemetryConfig::builder()
.service_name("my-server")
.build();
// Initialize telemetry - guard must be kept alive
let _guard = config.init()?;
// Your application code here...
// Telemetry is properly cleaned up when guard is droppedImplementations§
Source§impl TelemetryGuard
impl TelemetryGuard
Sourcepub fn init(config: TelemetryConfig) -> Result<Self, TelemetryError>
pub fn init(config: TelemetryConfig) -> Result<Self, TelemetryError>
Initialize telemetry with the provided configuration
Sourcepub fn service_name(&self) -> &str
pub fn service_name(&self) -> &str
Get the service name
Sourcepub fn service_version(&self) -> &str
pub fn service_version(&self) -> &str
Get the service version
Sourcepub fn config(&self) -> &TelemetryConfig
pub fn config(&self) -> &TelemetryConfig
Get the configuration
Trait Implementations§
Source§impl Debug for TelemetryGuard
impl Debug for TelemetryGuard
Auto Trait Implementations§
impl Freeze for TelemetryGuard
impl !RefUnwindSafe for TelemetryGuard
impl Send for TelemetryGuard
impl Sync for TelemetryGuard
impl Unpin for TelemetryGuard
impl UnsafeUnpin for TelemetryGuard
impl !UnwindSafe for TelemetryGuard
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more