Skip to content

Commit 86cfa60

Browse files
authored
docs: add usage guide (#68)
* docs: add usage guide * add further examples & signposting to them * fill out usage guide content, trim `lib.rs`
1 parent e27ec9e commit 86cfa60

File tree

7 files changed

+109
-61
lines changed

7 files changed

+109
-61
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repos:
2626
pass_filenames: false
2727
- id: docs
2828
name: Check docs
29-
entry: env RUSTDOCFLAGS="-D warnings" cargo doc --no-deps
29+
entry: env RUSTDOCFLAGS="-D warnings --cfg docsrs" cargo doc --no-deps
3030
types: [rust]
3131
language: system
3232
pass_filenames: false

examples/actix-web.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Example demonstrating Logfire integration with Actix Web framework.
1+
//! Example of using `logfire` to instrument an `actix-web` webserver
22
//!
33
//! This example shows how to:
44
//! - Set up Logfire with Actix Web

examples/axum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Example demonstrating Logfire integration with Axum web framework.
1+
//! Example of using `logfire` to instrument an `axum` webserver
22
//!
33
//! This example shows how to:
44
//! - Set up Logfire with Axum

examples/basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! A basic example of using Logfire to instrument Rust code.
1+
//! Example of using `logfire` to instrument a toy Rust application
22
33
use std::fs;
44
use std::sync::LazyLock;

src/lib.rs

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
//! # Rust SDK for Pydantic Logfire
77
//!
8-
//! This goal of this SDK is to provide a first-class experience instrumenting Rust code for the Pydantic Logfire platform.
8+
//! This SDK provides a first-class experience instrumenting Rust code for the Pydantic Logfire platform.
99
//!
1010
//! The most important API is [`logfire::configure()`][configure], which is used to set up
1111
//! integrations with `tracing` and `log`, as well as exporters for `opentelemetry`. Code
@@ -20,86 +20,54 @@
2020
//! See also:
2121
//! - The [integrations](#integrations) section below for more information on the relationship of
2222
//! this SDK to other libraries.
23+
//! - The [examples][usage::examples] subchapter of this documentation.
2324
//! - The [Logfire documentation](https://logfire.pydantic.dev/docs/) for more information about Logfire in general.
2425
//! - The [Logfire GitHub repository](https://github.com/pydantic/logfire) for the source of the documentation, the Python SDK and an issue tracker for general questions about Logfire.
2526
//!
26-
//! > ***Initial release - feedback wanted!***
27-
//! >
28-
//! > This is an initial release of the Logfire Rust SDK. We've been using it internally to build
29-
//! > Logfire for some time, and it is serving us well. As we're using it ourselves in production,
30-
//! > we figured it's ready for everyone else also using Logfire.
31-
//! >
32-
//! > We are continually iterating to make this SDK better. We'd love your feedback on all aspects
33-
//! > of the SDK and are keen to make the design as idiomatic and performant as possible. There are
34-
//! > also many features currently supported by the Python SDK which are not yet supported by this
35-
//! > SDK; please open issues to help us prioritize these to close this gap. For example, we have not
36-
//! > yet implemented scrubbing in this Rust SDK, although we are aware it is important!
37-
//! >
38-
//! > In particular, the current coupling to `tracing` is an open design point. By building on top
39-
//! > of tracing we get widest compatibility and a relatively simple SDK, however to make
40-
//! > Logfire-specific adjustments we might prefer in future to move `tracing` to be an optional
41-
//! > integration.
27+
//! # Usage
28+
//!
29+
//! See below for a quick summary of how to use this SDK. The [usage guide][usage] contains more detailed
30+
//! information about how to use this SDK to its full potential.
4231
//!
4332
//! ## Getting Started
4433
//!
45-
//! To use Logfire in your Rust project, add the following to your `Cargo.toml`:
34+
//! To use `logfire` in your Rust project, add the following to your `Cargo.toml`:
4635
//!
4736
//! ```toml
4837
//! [dependencies]
4938
#![doc = concat!("logfire = \"", env!("CARGO_PKG_VERSION"), "\"\n")]
5039
//! ```
5140
//!
52-
//! Then, in your Rust code, add a call to `logfire::configure()` at the beginning of your program:
41+
//! Then, in your Rust code, add a call to [`logfire::configure()`][configure] at the beginning of your program:
5342
//!
5443
//! ```rust
55-
#![doc = include_str!("../examples/basic.rs")]
44+
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
45+
//! let shutdown_handler = logfire::configure()
46+
//! .install_panic_handler()
47+
//! # .send_to_logfire(logfire::config::SendToLogfire::IfTokenPresent)
48+
//! .finish()?;
49+
//!
50+
//! logfire::info!("Hello world");
51+
//!
52+
//! shutdown_handler.shutdown()?;
53+
//! Ok(())
54+
//! }
5655
//! ```
5756
//!
5857
//! ## Configuration
5958
//!
6059
//! After adding basic setup as per above, the most two important environment variables are:
61-
//! - `LOGFIRE_TOKEN` - (required) the token to send data to the Logfire platform
62-
//! - `RUST_LOG` - (optional) the level of verbosity to send to the Logfire platform. By default
63-
//! logs are captured at `TRACE` level so that all data is available for you to analyze in the
64-
//! Logfire platform.
60+
//! - [`LOGFIRE_TOKEN`](https://logfire.pydantic.dev/docs/how-to-guides/create-write-tokens/) (required) - the token to send data to the Logfire platform
61+
//! - [`RUST_LOG`](https://docs.rs/env_logger/latest/env_logger/#filtering-results) (optional) - the level of verbosity to send to the Logfire platform. By default
62+
//! data is captured at `TRACE` level so that all data is available for you to analyze in the
63+
//! Logfire platform. This format should match the format used by the [`env_logger`](https://docs.rs/env_logger/) crate.
6564
//!
6665
//! All environment variables supported by the Rust Opentelemetry SDK are also supported by the
6766
//! Logfire SDK.
6867
//!
69-
//! ## Integrations
70-
//!
71-
//! The following sections describe briefly the interaction which this SDK has with other libraries.
72-
//!
73-
//! ### With `tracing`
74-
//!
75-
//! This SDK is built upon `tracing` (and `tracing-opentelemtry`) for the [`span!`] macro. This means
76-
//! that any code instrumented with `tracing` will automatically be captured by Logfire, and also
77-
//! that [`span!`] produces a `tracing::Span` which is fully compatible with the `tracing` ecosystem.
78-
//!
79-
//! If you are an existing `tracing` user, it is fine to continue to use the `tracing` APIs directly
80-
//! and ignore [`logfire::span!`][span]. The upside of [`span!`] is that it will show the fields
81-
//! directly in the logfire UI.
82-
//!
83-
//! There are many great APIs in `tracing` which we do not yet provide equivalents for, such as the
84-
//! [`#[tracing::instrument]`][`macro@tracing::instrument`] proc macro, so even if using [`logfire::span!`][span]
85-
//! you will likely use `tracing` APIs directly too.
68+
//! # Examples
8669
//!
87-
//! ### With `opentelemetry`
88-
//!
89-
//! This SDK is built upon the `opentelemetry` Rust SDK and will configure the global `opentelemetry`
90-
//! state as part of a call to [`logfire::configure()`][configure].
91-
//!
92-
//! All calls to [`logfire::info!`][info] and similar macros are directly forwarded to `opentelemetry`
93-
//! machinery without going through `tracing`, for performance.
94-
//!
95-
//! The metrics helpers exported by this SDK, such as [`logfire::u64_counter()`][u64_counter], are
96-
//! very thin wrappers around the `opentelemetry` SDK.
97-
//!
98-
//! ### With `log`
99-
//!
100-
//! This SDK configures the global `log` state to use an exporter which forwards logs to opentelemetry.
101-
//!
102-
//! All code instrumented with `log` will therefore automatically be captured by Logfire.
70+
//! See [examples][usage::examples] subchapter of this documentation.
10371
10472
use std::borrow::Cow;
10573
use std::cell::RefCell;
@@ -127,6 +95,9 @@ use crate::config::{
12795
};
12896
use crate::internal::exporters::console::{ConsoleWriter, SimpleConsoleSpanProcessor};
12997

98+
#[cfg(any(docsrs, doctest))]
99+
pub mod usage;
100+
130101
mod bridges;
131102
pub mod config;
132103
pub mod exporters;

src/usage/examples.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//! # Examples of using the `logfire` SDK
2+
//!
3+
//! These are complete code examples which show how to use the `logfire` SDK
4+
//! with various frameworks and libraries.
5+
//!
6+
//! These can also be found in the [`examples/`](https://github.com/pydantic/logfire-rust/tree/main/examples)
7+
//! directory of the repository.
8+
9+
/// # Example of using `logfire` to instrument an `actix-web` webserver
10+
///
11+
/// ```rust,no_run
12+
#[doc = include_str!("../../examples/actix-web.rs")]
13+
/// ```
14+
pub mod actix_web {}
15+
16+
/// # Example of using `logfire` to instrument an `axum` webserver
17+
///
18+
/// ```rust,no_run
19+
#[doc = include_str!("../../examples/axum.rs")]
20+
/// ```
21+
pub mod axum {}
22+
23+
/// # Example of using `logfire` to instrument a toy Rust application
24+
///
25+
/// ```rust
26+
#[doc = include_str!("../../examples/basic.rs")]
27+
/// ```
28+
pub mod basic {}

src/usage/mod.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! # Usage Guide
2+
//!
3+
//! This section of the document is dedicated to guide material which shows how to
4+
//! use the Logfire Rust SDK to instrument applications.
5+
//!
6+
//! # Architecture
7+
//!
8+
//! This section briefly documents how this SDK is built upon other libraries.
9+
//!
10+
//! ## Integrations
11+
//!
12+
//! The following sections describe briefly the interaction which this SDK has with other libraries.
13+
//!
14+
//! ### With `tracing`
15+
//!
16+
//! This SDK is built upon `tracing` (and `tracing-opentelemetry`) for the [`logfire::span!`][crate::span] macro. This means
17+
//! that any code instrumented with `tracing` will automatically be captured by Logfire, and also
18+
//! that [`logfire::span!`][crate::span] produces a `tracing::Span` which is fully compatible with the `tracing` ecosystem.
19+
//!
20+
//! If you are an existing `tracing` user, it is fine to continue to use the `tracing` APIs directly
21+
//! and ignore [`logfire::span!`][crate::span]. The upside of [`logfire::span!`][crate::span] is that it will show the fields
22+
//! directly in the logfire UI.
23+
//!
24+
//! There are many great APIs in `tracing` which we do not yet provide equivalents for, such as the
25+
//! [`#[tracing::instrument]`][`macro@tracing::instrument`] proc macro, so even if using [`logfire::span!`][crate::span]
26+
//! you will likely use `tracing` APIs directly too.
27+
//!
28+
//! ### With `opentelemetry`
29+
//!
30+
//! This SDK is built upon the `opentelemetry` Rust SDK and will configure the global `opentelemetry`
31+
//! state as part of a call to [`logfire::configure()`][crate::configure].
32+
//!
33+
//! All calls to [`logfire::info!`][crate::info] and similar macros are directly forwarded to `opentelemetry`
34+
//! machinery without going through `tracing`, for performance.
35+
//!
36+
//! The metrics helpers exported by this SDK, such as [`logfire::u64_counter()`][crate::u64_counter], are
37+
//! very thin wrappers around the `opentelemetry` SDK.
38+
//!
39+
//! ### With `log`
40+
//!
41+
//! This SDK configures the global `log` state to use an exporter which forwards logs to opentelemetry.
42+
//!
43+
//! All code instrumented with `log` will therefore automatically be captured by Logfire.
44+
//!
45+
//! # Examples
46+
//!
47+
//! See [examples] subchapter of this documentation.
48+
49+
pub mod examples;

0 commit comments

Comments
 (0)