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

Public encoding functions? #152

Open
blckngm opened this issue Jun 7, 2023 · 4 comments
Open

Public encoding functions? #152

blckngm opened this issue Jun 7, 2023 · 4 comments

Comments

@blckngm
Copy link

blckngm commented Jun 7, 2023

Sometimes I have already e.g. nodes: Vec<Node> in the application state where Node is like

struct Node {
   url: String,
   requests: AtomicU64,
}

(When you are already working with a &Node, bumping the requests counter is just an atomic inc. It's much more efficient than get_or_create(...).inc().)

And I'd like to expose the requests counter of all nodes as a metric family. It might be possible with the collector API, but it would still require quite some boxing, cloning and iterator mapping.

Instead I'd like to encode the metrics myself, e.g.:

encode_descriptor(&mut buf, ...);
for n in &ctx.nodes {
    encode_counter_with_labels(&mut buf, [("node", &n.url)], n.requests.load(...));
}
encode_eof(&mut buf);

This will not require any extra allocation or cloning. By fusing the collecting and encoding phase, metrics can be encoded very efficiently.

@mxinden
Copy link
Member

mxinden commented Jul 10, 2023

@blckngm this will be possible with the new Collector trait. It would not require any boxing, i.e. you can encode your custom metrics into a MetricEncoder right away.

#149

Let me know if this would solve your use-case.

@blckngm
Copy link
Author

blckngm commented Jul 10, 2023

Thanks for considering this. I'll try it.

@blckngm
Copy link
Author

blckngm commented Jul 11, 2023

It's definitely a big improvement for my use case.

I have to a clone an Arc and define a wrapper struct to make my collector impl struct Debug and 'static, which wouldn't be necessary if e.g. Collector has more loose bounds or if DescriptorEncoder have a public constructor (then I'd probably still skip the collector).

But it's pretty good so if it's released I'll probably use it instead of my current hacky solution where I'm creating a new registry and register metrics on each scraping.

@mxinden
Copy link
Member

mxinden commented Jul 12, 2023

I have to a clone an Arc and define a wrapper struct to make my collector impl struct Debug and 'static

I don't see how we can implement Collector without 'static, given that, when registering, we need ownership in order to store it in Registry.

As for Debug, I would consider implementing Debug for all externally used structs a best practice. I am assuming this is not too painful for you.

As for Arc, given that you want to access the Node struct in order to increase the counter and given that Registry needs access in order to encode the metrics, I don't see a way to do without it.

But it's pretty good so if it's released I'll probably use it instead of my current hacky solution where I'm creating a new registry and register metrics on each scraping.

Glad it can improve your use-case.

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

2 participants