Skip to content

Commit f9109c3

Browse files
committed
final copy for first pass at docs
1 parent 3728e85 commit f9109c3

File tree

2 files changed

+125
-3
lines changed

2 files changed

+125
-3
lines changed

src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,38 @@
7373
//!
7474
//! ## Integrations
7575
//!
76+
//! The following sections describe briefly the interaction which this SDK has with other libraries.
77+
//!
7678
//! ### With `tracing`
7779
//!
80+
//! This SDK is built upon `tracing` (and `tracing-opentelemtry`) for the [`span!`] macro. This means
81+
//! that any code instrumented with `tracing` will automatically be captured by Logfire, and also
82+
//! that [`span!`] produces a `tracing::Span` which is fully compatible with the `tracing` ecosystem.
83+
//!
84+
//! If you are an existing `tracing` user, it is fine to continue to use the `tracing` APIs directly
85+
//! and ignore [`logfire::span!`][span]. The upside of [`span!`] is that it will show the fields
86+
//! directly in the logfire UI.
87+
//!
88+
//! There are many great APIs in `tracing` which we do not yet provide equivalents for, such as the
89+
//! [`#[tracing::instrument]`][`macro@tracing::instrument`] proc macro, so even if using [`logfire::span!`][span]
90+
//! you will likely use `tracing` APIs directly too.
91+
//!
7892
//! ### With `opentelemetry`
7993
//!
94+
//! This SDK is built upon the `opentelemetry` Rust SDK and will configure the global `opentelemetry`
95+
//! state as part of a call to [`logfire::configure()`][configure].
96+
//!
97+
//! All calls to [`logfire::info!`][info] and similar macros are directly forwarded to `opentelemetry`
98+
//! machinery without going through `tracing`, for performance.
99+
//!
100+
//! The metrics helpers exported by this SDK, such as [`logfire::u64_counter()`][u64_counter], are
101+
//! very thin wrappers around the `opentelemetry` SDK.
102+
//!
80103
//! ### With `log`
104+
//!
105+
//! This SDK configures the global `log` state to use an exporter which forwards logs to opentelemetry.
106+
//!
107+
//! All code instrumented with `log` will therefore automatically be captured by Logfire.
81108
82109
use std::borrow::Cow;
83110
use std::cell::RefCell;

src/macros/mod.rs

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,53 @@
22
#[path = "impl_.rs"]
33
pub mod __macros_impl;
44

5-
/// Create a new span.
5+
/// Create a new [`Span`][tracing::Span]. This macro is a simplified version of `tracing::span!` which accepts
6+
/// a smaller set of possible arguments and syntaxes.
67
///
7-
/// The return type of this macro is a [`tracing::Span`], which can be used to enter the span
8-
/// or to pass it to `tracing::Instrument` methods.
8+
/// In exchange, the macro automatically adds metadata which leads the span and its fields
9+
/// to present nicely in the Logfire UI.
10+
///
11+
/// # Syntax
12+
///
13+
/// The macro accepts the following syntax:
14+
///
15+
/// ```rust,ignore
16+
/// span!(
17+
/// parent: tracing::Span, // optional, can emit this
18+
/// level: tracing::Level, // optional
19+
/// "format string", // required, must be a format string accepted by `format!()`
20+
/// arg1 = value1, // optional, can be repeated
21+
/// .. // as many additional arg = value pairs as desired
22+
/// )
23+
/// ```
24+
///
25+
/// The format string only accepts arguments by name.
26+
///
27+
/// These can be automatically captured from the surrounding scope by the macro,
28+
/// in which case they will render in the message but will not be.
29+
///
30+
/// # Examples
31+
///
32+
/// ```rust,no_run
33+
/// // Span without any arguments
34+
/// let root_span = logfire::span!("Root span");
35+
///
36+
/// // Span with attributes x and y
37+
/// let span = logfire::span!(parent: &root_span, "Child span", x = 42, y = "hello");
38+
///
39+
/// // Typically a span will be "entered" to set the parent implicitly
40+
/// root_span.in_scope(|| {
41+
/// // This span will be a child of root_span
42+
/// let child_span = logfire::span!("Nested span", x = 42, y = "hello");
43+
///
44+
/// // Debug-level child span
45+
/// let debug_span = logfire::span!(level: tracing::Level::DEBUG, "Debugging", x = 42, y = "hello");
46+
/// });
47+
///
48+
/// // With x included in the formatted message but not as an attribute
49+
/// let x = 42;
50+
/// let span = logfire::span!("Span with x = {x}, y = {y}", y = "hello");
51+
/// ```
952
#[macro_export]
1053
macro_rules! span {
1154
(parent: $parent:expr, level: $level:expr, $format:expr $(, $arg:ident = $value:expr)* $(,)?) => {
@@ -75,6 +118,58 @@ macro_rules! debug {
75118
}
76119

77120
/// Export a log message at the specified level.
121+
///
122+
/// # Syntax
123+
///
124+
/// The macro accepts the following syntax:
125+
///
126+
/// ```rust,ignore
127+
/// log!(
128+
/// parent: tracing::Span, // optional, can emit this
129+
/// tracing::Level, // required, see `info!` and variants for convenience
130+
/// "format string", // required, must be a format string accepted by `format!()`
131+
/// arg1 = value1, // optional, can be repeated
132+
/// .. // as many additional arg = value pairs as desired
133+
/// )
134+
/// ```
135+
///
136+
/// The format string only accepts arguments by name.
137+
///
138+
/// These can be automatically captured from the surrounding scope by the macro,
139+
/// in which case they will render in the message but will not be.
140+
///
141+
/// # Examples
142+
///
143+
/// ```rust,no_run
144+
/// use tracing::Level;
145+
///
146+
/// // Root span
147+
/// let root_span = logfire::span!("Root span");
148+
///
149+
/// // Log with attributes x and y
150+
/// logfire::log!(parent: &root_span, Level::INFO, "Child log", x = 42, y = "hello");
151+
/// // or
152+
/// logfire::info!(parent: &root_span, "Child log", x = 42, y = "hello");
153+
///
154+
/// // Typically a span will be "entered" to set the parent implicitly
155+
/// root_span.in_scope(|| {
156+
/// // This logf will be a child of root_span
157+
/// logfire::log!(level::INFO, "Nested log", x = 42, y = "hello");
158+
/// // or
159+
/// logfire::info!("Nested log", x = 42, y = "hello");
160+
///
161+
/// // Debug-level child log
162+
/// logfire::log!(Level::DEBUG, "Debugging", x = 42, y = "hello");
163+
/// // or
164+
/// logfire::debug!("Debugging", x = 42, y = "hello");
165+
/// });
166+
///
167+
/// // With x included in the formatted message but not as an attribute
168+
/// let x = 42;
169+
/// logfire::log!(Level::INFO, "Log with x = {x}, y = {y}", y = "hello");
170+
/// // or
171+
/// logfire::info!("Log with x = {x}, y = {y}", y = "hello");
172+
/// ```
78173
#[macro_export]
79174
macro_rules! log {
80175
(parent: $parent:expr, $level:expr, $format:expr, $($($arg:ident = $value:expr),+)?) => {{

0 commit comments

Comments
 (0)