prometheus-cpp icon indicating copy to clipboard operation
prometheus-cpp copied to clipboard

Optimistically return metrics from the cache (if present)

Open nbrachet opened this issue 4 years ago • 9 comments

before building a metric to add to the cache.

nbrachet avatar Nov 30 '20 22:11 nbrachet

@jupp0r any advice on how to address this jenkins error:

no coveralls.io token specified and no travis job id found

It doesn't seem related to this PR?

nbrachet avatar Dec 04 '20 18:12 nbrachet

@jupp0r any advice on how to address this jenkins error

Ignore it, it's a know issue that I though I had fixed.

jupp0r avatar Dec 04 '20 19:12 jupp0r

Hi,

Could you please add a test for the expected behavior?

Thanks, Gregor

gjasny avatar Dec 04 '20 19:12 gjasny

I've been thinking about adding a test but I'm struggling to come with anything. This is a transparent change and I can't come up with a way to externally check whether a new unique_ptr was created or not.

PS: I'm not super happy about the code duplication either but again I couldn't come up with a better approach.

nbrachet avatar Dec 04 '20 22:12 nbrachet

Is the idea that hashing the labels is less expensive than the call to make_unique? Seems doubtful but not sure. Regardless, seems like labels should only be hashed once if the metric is missing.

danevandyck avatar Dec 08 '20 01:12 danevandyck

The idea is that the vast majority of the time Add() is called it will return the metric from the cache. Such that the first call with a new set of labels will be indeed more expensive (2 hashes + 1 make_unique), but it will be amortized over time since subsequent calls will be cheaper (1 hash with no make_unique and no ~unique_ptr).

As an illustration, we have the following pseudo-code:

 Family<Counter> counter{"total_requests", "Count all requests"};
 
 void handle_request(...) {
    auto vhost = <get virtual host>;
    counter.Add({ "vhost", vhost }).Increment();
    ...
 }

The first request for each vhost initializes the cache (ie. Family<Counter>::metrics_, Family<Counter>::labels_, and Family<Counter>::labels_reverse_lookup_) after that all subsequent requests simply retrieve the Counter from there, saving a call to make_unique<Counter> and ~unique_ptr<Counter>.

nbrachet avatar Dec 08 '20 03:12 nbrachet

I get it. How about implementing a helper which accepts a pre-computed hash value, thereby avoiding the new first-lookup penalty and avoiding the duplication mentioned earlier?

danevandyck avatar Dec 08 '20 14:12 danevandyck

How about this?

nbrachet avatar Dec 09 '20 21:12 nbrachet

Could you please reset your branch to "our" forgeAI-nick/optimistic-family-add?

gjasny avatar Dec 26 '20 17:12 gjasny