Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A simple procedural macro that generates a static metric for rust-prometheus.
extern crate proc_macro;
use quote::quote;
use proc_macro::TokenStream;
use syn::*;
use syn::parse::{Parse, ParseStream};
struct MetricDefinition {
vis: Visibility,
name: Ident,
values: Vec<Ident>,
impl Parse for MetricDefinition {
fn parse(input: ParseStream) -> Result<Self> {
let vis = input.parse::<Visibility>()?;
let name = input.parse::<Ident>()?;
let content; braced!(content in input);
let values = content
.parse_terminated::<_, Token![,]>(Ident::parse)?
Ok(Self { vis, name, values })
pub fn make_metrics(input: TokenStream) -> TokenStream {
let data = parse_macro_input!(input as MetricDefinition);
let vis = &data.vis;
let name = &;
let values = &data.values;
let values_str = data.values.iter().map(|ident| LitStr::new(
let expanded = quote! {
#vis struct #name {
#(#values: Counter,)*
impl #name {
pub fn new() -> Self {
Self {
#(#values: COUNTER.with_label_values(&[#values_str]),)*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment