Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I register custom metrics? #73

Closed
DFINITYManu opened this issue Oct 6, 2023 · 3 comments
Closed

How do I register custom metrics? #73

DFINITYManu opened this issue Oct 6, 2023 · 3 comments

Comments

@DFINITYManu
Copy link
Contributor

DFINITYManu commented Oct 6, 2023

I've tried

lazy_static! {
    static ref CACHE_HIT_COUNTER: IntCounterVec = register_int_counter_vec!(
        "http_cache_hits_total",
        "Number of HTTP cache hits",
        &["http_response_status_code"]
    )
    .unwrap();
    static ref CACHE_MISS_COUNTER: IntCounterVec = register_int_counter_vec!(
        "http_cache_misses_total",
        "Number of HTTP cache misses",
        &["http_response_status_code"]
    )
    .unwrap();
}

but these metrics aren't getting registered in the registry that the HttpMetricsLayerBuilder creates and assigns to the HttpMetricsLayer my system uses.

How do I obtain a reference to the registry such that my code can use register_int_counter_vec_with_registry? Alternatively, how do I instruct the HttpMetricsLayerBuilder to use the default prometheus crate registry?

Thanks in advance.

@DFINITYManu
Copy link
Contributor Author

I think what I am asking is more probably how do I add custom metrics that will be exporter by axum-otel-metrics in general. I'm stuck and I'm not sure how to do this. The documentation does not mention this.

@DFINITYManu
Copy link
Contributor Author

DFINITYManu commented Oct 6, 2023

I also tried this:

use opentelemetry::global;
use opentelemetry::metrics::Counter;

#[derive(Clone)]
pub struct CustomMetrics {
    pub http_cache_misses: Counter<u64>,
    pub http_cache_hits: Counter<u64>,
}

impl CustomMetrics {
    pub fn new() -> Self {
        let meter = global::meter("axum-app");
        // Create two instruments.
        let http_cache_misses = meter
            .u64_counter("http.cache.misses")
            .with_description("Total number of HTTP cache misses")
            .init();
        let http_cache_hits = meter
            .u64_counter("http.cache.hits")
            .with_description("Total number of HTTP cache hits")
            .init();
        CustomMetrics {
            http_cache_misses,
            http_cache_hits,
        }
    }
}

but nothing seems to have happened. Metrics are not served, despite me initializing the struct, and calling the .add() methods on each call.

@DFINITYManu
Copy link
Contributor Author

Oh mang, I got it to work! Thanks for your software.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant