Skip to content

Conversation

mrueg
Copy link
Contributor

@mrueg mrueg commented Aug 25, 2025

We have a use case here, where it would be nice to support custom labels in the build info: kubernetes/kube-state-metrics#2739

This would allow us adding labels to the build info metric exposed by the version collector, e.g. in this case the version of the kubernetes client library we're using.

This also adds some tests for the version collector.

@mrueg mrueg force-pushed the version-collector-custom-labels branch 4 times, most recently from 7c65de7 to 8d75511 Compare August 25, 2025 15:55
@mrueg mrueg changed the title WIP: collectors/version: Allow custom labels collectors/version: Allow custom additional labels Aug 25, 2025
@mrueg mrueg force-pushed the version-collector-custom-labels branch 4 times, most recently from d1379a0 to 76fa0f3 Compare August 25, 2025 19:07
Copy link
Member

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Makes sense, just one suggestion to make it flexible for future options, WDYT?

Comment on lines 31 to 33
// NewCollectorWithLabels returns a collector that exports metrics about current
// version information and allows to set additional custom labels.
func NewCollectorWithLabels(program string, labels prometheus.Labels) prometheus.Collector {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanna spend extra 10m and add proper options to this type? Will help in future changes (e.g. filtering some labels etc)

type Option func(*options)

type options struct {
    extraConstLabels prometheus.Labels
}

func WithExtraConstLabels(l prometheus.Labels) Option {
  return func(o *options) {
     o.extraConstLabels = l
  }
}


func NewCollector(program string, opts ...Option) prometheus.Collector {
   o := option{}
   for _, opt := range opts {
       opt(&o)
   }
//...

Copy link
Contributor Author

@mrueg mrueg Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to rename the func to NewCollectorWithOptions then or should I add the opts arg to NewCollector?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, understood, it's a variadic function now.

@mrueg mrueg force-pushed the version-collector-custom-labels branch 2 times, most recently from 43cf50a to 1439d34 Compare August 26, 2025 09:09
@mrueg
Copy link
Contributor Author

mrueg commented Aug 26, 2025

@bwplotka Thanks for the suggestion, I applied you change.

Co-Authored-by: bwplotka <bwplotka@gmail.com>
Signed-off-by: Manuel Rüger <manuel@rueg.eu>
@mrueg mrueg force-pushed the version-collector-custom-labels branch from 1439d34 to a8cbb1f Compare August 26, 2025 09:12
@kakkoyun kakkoyun merged commit 84a4734 into prometheus:main Aug 26, 2025
8 checks passed
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

Successfully merging this pull request may close these issues.

3 participants