You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Relax necessity of declaring labelset in advance #298
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.
Reacted by Boris Okunskiy, Chris Pinola, sklose and ND Tai
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.
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.
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.
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.
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.
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
@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?
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.
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?)
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.
Activity
zbjornson commentedon Nov 13, 2019
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.
dt-rush commentedon Nov 15, 2019
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.ioblog post on predeclaring metrics:(emphasis added)
dt-rush commentedon Apr 19, 2020
@zbjornson as mentioned in the discussion in #299, that'll be reverted as it adds to the unnecessary rigidity of using this client.
dt-rush commentedon Apr 19, 2020
That is all sorted nicely, however, there is one more issue to bring up:
In the Writing client libaries # Labels docs, we read:
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
between
and
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.
remove unnecessary necessity of predeclared, known-at-declaration-tim…
dt-rush commentedon Apr 19, 2020
Branch pushed here: https://github.com/dt-rush/prom-client/tree/relax-label-predeclaration
dt-rush commentedon Apr 19, 2020
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
dt-rush commentedon Apr 19, 2020
@siimon I wonder if you'd be able to give any insight here, it seems this can go ahead, but not sure
dt-rush commentedon Apr 19, 2020
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 exportedathroughmfor a year, then suddenly we export a metric withathroughr. The "added" labels were "part of the labelset" from the very beginning, but they "just now appeared" from the perspective of the scrape.dt-rush commentedon Apr 24, 2020
bump @zbjornson, perhaps you can weigh in on this?
zbjornson commentedon Apr 24, 2020
Sorry for the delay. Hoping to have time to look this weekend.
siimon commentedon Apr 28, 2020
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
dt-rush commentedon May 6, 2020
@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?
dt-rush commentedon Jul 22, 2020
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.
natewaddoups commentedon Jul 29, 2024
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?)
jdmarshall commentedon Jul 2, 2026
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:
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
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.