Skip to content

Commit 41e6fdf

Browse files
authored
docs: add more cases to usage guide (#70)
1 parent e8f6d93 commit 41e6fdf

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ use tracing_subscriber::layer::SubscriberExt;
8989
use tracing_subscriber::registry::LookupSpan;
9090

9191
use crate::__macros_impl::LogfireValue;
92-
use crate::bridges::tracing::LogfireTracingLayer;
9392
use crate::config::{
9493
AdvancedOptions, BoxedSpanProcessor, ConsoleOptions, MetricsOptions, SendToLogfire,
9594
};
9695
use crate::internal::exporters::console::{ConsoleWriter, SimpleConsoleSpanProcessor};
96+
use crate::ulid_id_generator::UlidIdGenerator;
9797

9898
#[cfg(any(docsrs, doctest))]
9999
pub mod usage;
@@ -105,9 +105,9 @@ mod macros;
105105
mod metrics;
106106
mod ulid_id_generator;
107107

108+
pub use crate::bridges::tracing::LogfireTracingLayer;
108109
pub use macros::*;
109110
pub use metrics::*;
110-
use ulid_id_generator::UlidIdGenerator;
111111

112112
mod internal;
113113

src/usage/mod.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//!
1010
//! ## Integrations
1111
//!
12-
//! The following sections describe briefly the interaction which this SDK has with other libraries.
12+
//! This crate has integrations with the following libraries:
1313
//!
1414
//! ### With `tracing`
1515
//!
@@ -42,6 +42,56 @@
4242
//!
4343
//! All code instrumented with `log` will therefore automatically be captured by Logfire.
4444
//!
45+
//! # Dynamically adding data to spans
46+
//!
47+
//! Occasionally you may want to add data to a span after it has been created.
48+
//!
49+
//! This works the same way as with `tracing`, using the `record` method on the span. When creating
50+
//! the span, you need to initialize the attribute with [`tracing::field::Empty`].
51+
//!
52+
//! ```rust
53+
//! // 1. create span with placeholder attribute
54+
//! let span = logfire::span!("My span", my_attr = tracing::field::Empty);
55+
//!
56+
//! // 2. record data later
57+
//! span.record("my_attr", "some value");
58+
//! ```
59+
//!
60+
//! # Using `logfire` as a layer in an existing `tracing` application
61+
//!
62+
//! If you have an existing application which already has a [`tracing_subscriber`] and you
63+
//! want to use `logfire` to quickly configure the OpenTelemetry SDK and send traces and logs to Logfire, the
64+
//! [`LogfireTracingLayer`][crate::LogfireTracingLayer] can be used to achieve this.
65+
//!
66+
//! First, configure `logfire` as usual, setting it as a `.local()` instance to avoid configuring
67+
//! global state:
68+
//!
69+
//! ```rust
70+
//! use tracing_subscriber::prelude::*;
71+
//!
72+
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
73+
//! // 1. configure logfire as usual, setting it as a `.local()` instance
74+
//! let shutdown_handler = logfire::configure()
75+
//! .local()
76+
//! .install_panic_handler()
77+
//! .finish()?;
78+
//!
79+
//! // 2. create a tracing subscriber
80+
//! let subscriber = tracing_subscriber::registry()
81+
//! .with(shutdown_handler.tracing_layer());
82+
//!
83+
//! // 3. set the subscriber as the default (or otherwise set it up for your application)
84+
//! tracing::subscriber::set_global_default(subscriber)?;
85+
//!
86+
//! // 4. now tracing's spans and logs will be sent to Logfire
87+
//! tracing::info!("This will be sent to Logfire");
88+
//!
89+
//! // 5. when finished, call shutdown_handler.shutdown() to flush and clean up
90+
//! shutdown_handler.shutdown()?;
91+
//! # Ok(())
92+
//! # }
93+
//! ```
94+
//!
4595
//! # Examples
4696
//!
4797
//! See [examples] subchapter of this documentation.

0 commit comments

Comments
 (0)