Skip to content

Relax necessity of declaring labelset in advance #298

Closed

Description

@dt-rush

There is no inherent restriction in prometheus for this. Labels can appear or disappear from the timeseries a node exports. This restriction in this JS client appears to be a consequence of choices made in the design of the internal hashmap property used in the 4 metrics classes, the utils.setValue() function, and possibly the utils.getLabels() function. This issue is a placeholder for further investigation.

Activity

  1. zbjornson commented on Nov 13, 2019

    @zbjornson
    Contributor

    Not opposed to relaxing the requirement, but also note that #299 (comment) made it so Typescript/ts-lint users will get a warning if they try to use a label that wasn't pre-declared.

  2. dt-rush commented on Nov 15, 2019

    @dt-rush
    Author

    Thanks for pointing that out @zbjornson.

    I'm working on a PR and tests for this atm.

    For further justification for this change, I cite the robustperception.io blog post on predeclaring metrics:

    The first reason relates to what was previously covered in Existential issues with metrics, metrics that only some times exist are difficult to deal with in PromQL. By declaring the metric before your application uses it, the client library can initialise it to 0. This applies even if the Counter is never subsequently incremented. However this doesn't work for metrics with labels: the client library doesn't know what labels might come up, so you have to do it yourself.

    (emphasis added)

  3. dt-rush commented on Apr 19, 2020

    @dt-rush
    Author

    @zbjornson as mentioned in the discussion in #299, that'll be reverted as it adds to the unnecessary rigidity of using this client.

  4. dt-rush commented on Apr 19, 2020

    @dt-rush
    Author

    That is all sorted nicely, however, there is one more issue to bring up:

    In the Writing client libaries # Labels docs, we read:

    Client libraries MUST NOT under any circumstances allow users to have different label names for the same metric for Gauge/Counter/Summary/Histogram or any other Collector offered by the library.

    The wording of this is not exactly easy to interpret, since its only possible meaning seems a contradiction with what is said (and observed in practice) elsewhere, that the appearance of a new labelset will create a new timeseries (that is, there's no problem having different label names for the same metric). There is no problem even in the current system of pre-declaring a labelset, {a, b, c, d} and then at various times exporting metrics labelled with subsets: {a, b}, {c, d},{c}, {b, d}, etc... that is, "different label names for the same metric".

    This is brought up before in this SO question where the user similarly brings up this contradiction.

    There is really no difference, if exporting

    some_counter {f="1"} 1 
    some_counter {f="1", g="1"} 1 
    some_counter {g="1"} 1 
    some_counter {g="1", h="1"} 1 
    some_counter {f="1", h="1"} 1 
    

    between

    new Counter({name: "some_counter", labelNames: ["f", "g", "h"]})
    

    and

    new Counter({name: "some_counter"});
    

    from the standpoint of the exported metrics, in cases like these, if we simply allow labels to appear whenever they occur.

    Implicitly, the "MUST NOT" amounts to a statement (can't be correct, can it?) that the only metric permitted is one which uses its full labelspace all of the time. But that's nonsense, as shown above.

  5. dt-rush commented on Apr 19, 2020

    @dt-rush
    Author

    Note: that branch can be merged, but it still won't work for typescript clients unless #299 https://github.com/siimon/prom-client/pull/299/files is reverted

  6. dt-rush commented on Apr 19, 2020

    @dt-rush
    Author

    @siimon I wonder if you'd be able to give any insight here, it seems this can go ahead, but not sure

  7. dt-rush commented on Apr 19, 2020

    @dt-rush
    Author

    I see other client libraries also (unnecessarily?) restricting label names in this way, and yet as explained above, this doesn't seem to be at odds with how exporters already work even in the case of predeclared labels. There's literally no difference to the prometheus process running the scrape; say we declared labelNames {a, b, c, ... , y, z} but only exported a through m for a year, then suddenly we export a metric with a through r. The "added" labels were "part of the labelset" from the very beginning, but they "just now appeared" from the perspective of the scrape.

  8. dt-rush commented on Apr 24, 2020

    @dt-rush
    Author

    bump @zbjornson, perhaps you can weigh in on this?

  9. zbjornson commented on Apr 24, 2020

    @zbjornson
    Contributor

    Sorry for the delay. Hoping to have time to look this weekend.

  10. siimon commented on Apr 28, 2020

    @siimon
    Contributor

    Sorry for being late on the ball here, thanks for your suggestion and input. Me personally would love to drop this too since it just adds complexity.

    My main concern though is to keep it aligned to other prometheus clients in other languages, specifically the offically supported ones. Have they dropped this requirement too? If not then I probably would wait until they have

  11. dt-rush commented on May 6, 2020

    @dt-rush
    Author

    @siimon this logic produces a deadlock, though, no? We can't proceed until all proceed? If this works, and the tests added in the PR #368 show that it does, could we not go ahead?

  12. dt-rush commented on Jul 22, 2020

    @dt-rush
    Author

    For posterity: As of now, until or unless this is implemented, simply declaring all possible labels in advance is an option if that set is finite and known in advance.

  13. natewaddoups commented on Jul 29, 2024

    @natewaddoups
    simply declaring all possible labels in advance is an option if that set is finite and known in advance.
    

    Or you could commit to to never learn anything new about the scenario that you're instrumenting.

    ...which is just not reasonable.

    Would be OK to merge the changes proposed earlier if they're gated by a flag that disables them by default?

    That would keep the existing behavior for people who for whatever want to keep the existing behavior, while providing a path forward for those of us who wish to improve our instrumentation as we learn new things about the scenarios that we're instrumenting.

    I realize those changes are ~4 year old now, and might need work to get caught up with the current state of prom-client. But is the approach good?

    I ask because my team at work is adopting prometheus for a service that is currently using DataDog and the lack of flexibility seems really unfortunate. Our code sometimes evolves in ways that benefit from additional insight from existing metrics. (Whose doesn't?)

  14. jdmarshall commented on Jul 2, 2026

    @jdmarshall
    Contributor

    I've dug this hole a little bit deeper with the performance improvements in trunk that lean into the known labels functionality in the existing library.

    It appears that in the interim the relevant section of the docs has been adjusted to remove the proscription against mixing multiple label sets into a given metric name, which seems to be more in keeping with what OpenTelemetry has done in this regard.

    Instead it simply states that labels with no value are equivalent to removing the label entirely, which is in keeping with:

    For posterity: As of now, until or unless this is implemented, simply declaring all possible labels in advance is an option if that set is finite and known in advance.

    If you don't know the label set in advance, you are already lost in the brambles.

    Practically speaking, the services that will host your OTEL or Prometheus data for you have pricing structures that favor a finite set of not only label names but also cardinality of labels+values.

    How does one, in a large organization

    • create back pressure for telemetry costs on cardinality
    • create dashboards that reflect labels that are only known at run time instead of at design time
    • control per-process (and likely, per-core) memory usage in NodeJS when each label:name combination is retained in perpetuity

    And I think the answer is, that you don't, for any of these. The only way to win is not to play, because it will be a very expensive lesson.

    With respect to PostHog/posthog#25193 I believe the Key data type has the labels associated with the key, so when you do your lookup for a Metric that matches the key, you will want to create a Metric with the Key's labels rather than simply creating one with no labels assigned. That would allow you to use the current API along with the Posthog code.

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