Skip to content

pydantic/logfire-rust

Repository files navigation

Rust SDK for Pydantic Logfire

CI codecov crates.io license MSRV Join Slack

From the team behind Pydantic, Logfire is an observability platform built on the same belief as our open source library — that the most powerful tools can be easy to use.

What sets Logfire apart:

  • Simple and Powerful: Logfire's dashboard is simple relative to the power it provides, ensuring your entire engineering team will actually use it.
  • SQL: Query your data using standard SQL — all the control and (for many) nothing new to learn. Using SQL also means you can query your data with existing BI tools and database querying libraries.
  • OpenTelemetry: Logfire is an opinionated wrapper around OpenTelemetry, allowing you to leverage existing tooling, infrastructure, and instrumentation for many common Python packages, and enabling support for virtually any language. We offer full support for all OpenTelemetry signals (traces, metrics and logs).

This repository contains the Rust SDK for instrumenting with Logfire.

See also:

The Logfire server application for recording and displaying data is closed source.

Using Logfire's Rust SDK

First set up a Logfire project and create a write token. You'll need to set this token as an environment variable (LOGFIRE_TOKEN) to export to Logfire.

With a logfire project set up, start by adding the logfire crate to your Cargo.toml:

[dependencies]
logfire = "0.6"

Then, you can use the SDK to instrument the code. Here's a simple example which counts the size of files in the current directory, creating spans for the full operation and each file read:

use std::fs;
use std::sync::LazyLock;

use opentelemetry::{KeyValue, metrics::Counter};

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

fn main() -> Result<()> {
    let shutdown_handler = logfire::configure().install_panic_handler().finish()?;

    let mut total_size = 0u64;

    let cwd = std::env::current_dir()?;

    logfire::span!("counting size of {cwd}", cwd = cwd.display().to_string()).in_scope(|| {
        let entries = fs::read_dir(&cwd)?;
        for entry in entries {
            let entry = entry?;
            let path = entry.path();

            let _span = logfire::span!(
                "reading {path}",
                path = path
                    .strip_prefix(&cwd)
                    .unwrap_or(&path)
                    .display()
                    .to_string()
            )
            .entered();

            let metadata = entry.metadata()?;
            if metadata.is_file() {
                total_size += metadata.len();
            }
        }
        Result::Ok(())
    })?;

    logfire::info!(
        "total size of {cwd} is {size} bytes",
        cwd = cwd.display().to_string(),
        size = total_size as i64
    );

    shutdown_handler.shutdown()?;
    Ok(())
}

(Read the Logfire concepts documentation for additional detail on spans, events, and further Logfire concepts.)

See additional examples in the examples directory:

Integration

Logfire's Rust SDK is currently built directly upon tracing and opentelemetry.

This means that anything instrumented using tracing will just work with logfire (however this SDK's macros contain some additional customizations on top of tracing for best support directly on the Logfire platform).

There is also an integration with log, so anything using log will also be captured by Logfire.

Contributing

We'd love anyone interested to contribute to the Logfire SDK!

Please send us all your feedback and ideas about the current design and functionality of this SDK. Code contributions are also very welcome!

Reporting a Security Vulnerability

See our security policy.

About

No description, website, or topics provided.

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages