Skip to content

Feature: support partial label currying #407

Closed as duplicate
Closed as duplicate

Description

@mgabeler-lee-6rs

The golang prometheus client has a useful feature for meric labels, where a subset of the label values can be curried, to produce a new metric object with those values pre-assigned, but leaving some values open: https://pkg.go.dev/github.com/prometheus/[email protected]/prometheus#MetricVec.CurryWith

This package only supports creating a metric object with all labels preset, via the labels method.

Having an equivalent currying method for this library would be similarly useful. The declaration for a Counter might look something like:

class Counter<T extends string> {
  // ...

  curry<U extends T>(labels: LabelValues<U>): Counter<Exclude<T, U>>;
}

That is, given a counter (or other metric) that has labels 'a', 'b', 'c' (AKA Counter<'a'|'b'|'c'>), then counter.curry({a: 'hello'}) would return a Counter<'b'|'c'> and counter.curry({a: 'hello', c: 'world'}) would return a Counter<'b'>, and counter.curry({d: 'oh dear'}) would be a compile/runtime error (depending on if one is using TypeScript and with what strictness settings).

Notably the returned object should support all the same methods as a full metric object (labels, curry, etc.), not just the observe or inc methods.

Activity

  1. zbjornson commented on Jan 23, 2021

    @zbjornson
    Contributor

    I'm neutral on this because it's easy enough to do it in user-space, but if you want to submit a PR, I'm happy to review it.

  2. mgabeler-lee-6rs commented on Jan 26, 2021

    @mgabeler-lee-6rs
    Author

    Happy to take a stab at it -- I indeed did implement it in "user space" for my use case, can make a more generic version of that code to submit.

  3. jdmarshall commented on Jul 9, 2025

    @jdmarshall
    Contributor

    This feature would be much more compelling if we could work out how to avoid storing the defaults with each result. I've been reworking storage but without this in mind, since the registry supports global defaults, and applies them late - I have an open PR to make that code about 25% faster #683.

    If you're adding something like hostname, appinstance, region, I would strongly recommend you use that feature as much as you possibly can. Right now adding defaults to every metric would make already slow metrics even slower, particularly at get(), which some people have complained is already gumming up the event loop.

  4. jdmarshall commented on Jul 2, 2026

    @jdmarshall
    Contributor

    Also as has been pointed out elsewhere, sometimes it might be useful to assign certain well known labels, like instance and host and service name in an aggregator running as a sidecar.

  5. jdmarshall commented on Jul 3, 2026

    @jdmarshall
    Contributor

    If anyone is still interested in working on this, the trick would be that you'd have to bind a special version of inc() and friends

    	labels(...args) {
    		const labels = getLabels(this.labelNames, args) || {};
    		return {
    			inc: this.inc.bind(this, labels),
    		};
    	}
    

    That did not drop the first argument but did an Object.assign(), but for chained calls of depth > 2 you'd also have to somehow access the previous values in order to merge them together, which could be a bit weird to plumb. The default bind() hides the bound variables in a symbol so you won't be looking it up that way.

    And you'd have to change getLabels() to not throw on an incomplete list.

  6. jdmarshall commented on Aug 20, 2026

    @jdmarshall
    Contributor

    This request can be fulfilled by #812

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions