Adding Glean to your Rust project

This page provides a step-by-step guide on how to integrate the Glean library into a Rust project.

Nevertheless this is just one of the required steps for integrating Glean successfully into a project. Check you the full Glean integration checklist for a comprehensive list of all the steps involved in doing so.

Setting up the dependency

The Glean Rust SDK is published on Add it to your dependencies in Cargo.toml:

glean = "50.0.0"

Setting up metrics and pings code generation

glean-build is in beta.

The glean-build crate is new and currently in beta. It can be used as a git dependency. Please file a bug if it does not work for you.

At build time you need to generate the metrics and ping API from your definition files. Add the glean-build crate as a build dependency in your Cargo.toml:

glean-build = { git = "" }

Then add a file next to your Cargo.toml and call the builder:

use glean_build::Builder;

fn main() {
        .expect("Error generating Glean Rust bindings");

Ensure your metrics.yaml and pings.yaml files are placed next to your Cargo.toml or adjust the path in the code above. You can also leave out any of the files.

Include the generated code

glean-build will generate a file that needs to be included in your source code. To do so add the following lines of code in your src/ file:

mod metrics {
    include!(concat!(env!("OUT_DIR"), "/"));

Alternatively create src/ (or a different name) with only the include line:

include!(concat!(env!("OUT_DIR"), "/"));

Then add mod metrics; to your src/ file.

Use the metrics

In your code you can then access generated metrics nested within their category under the metrics module (or your chosen name):


See the metric API reference for details.