Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Promauto.With() where possible #3263

Merged
merged 1 commit into from Oct 13, 2020

Conversation

roidelapluie
Copy link
Contributor

Also ensures that other standalone objects are registered by adding a
registerer to their New() function.

Signed-off-by: Julien Pivotto roidelapluie@inuits.eu

What this PR does:

Which issue(s) this PR fixes:
Partially #2204 (still miss the default registry metrics)

Checklist

  • Tests updated there are existing tests covering this
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

Copy link
Contributor

@pstibrany pstibrany left a comment

Choose a reason for hiding this comment

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

Thank you. The primary goal of #2204 was to get rid of using global registry. I don't think adding dependency on prometheus.Registrerer to types implementing prometheus.Collector is a way to go here.

@@ -47,8 +47,8 @@ type alertmanagerMetrics struct {
silencesPropagatedMessagesTotal *prometheus.Desc
}

func newAlertmanagerMetrics() *alertmanagerMetrics {
return &alertmanagerMetrics{
func newAlertmanagerMetrics(r prometheus.Registerer) *alertmanagerMetrics {
Copy link
Contributor

Choose a reason for hiding this comment

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

alertmanagerMetrics implements prometheus.Collector interface. Collectors typically don't register into registry themselves, but are registered by client code using them. I think we should keep that, and not pass registerer into newAlertmanagerMetrics.

@@ -465,11 +465,15 @@ func (d *HistogramData) Metric(desc *prometheus.Desc, labelValues ...string) pro
}

// Creates new histogram data collector.
func NewHistogramDataCollector(desc *prometheus.Desc) *HistogramDataCollector {
return &HistogramDataCollector{
func NewHistogramDataCollector(r prometheus.Registerer, desc *prometheus.Desc) *HistogramDataCollector {
Copy link
Contributor

Choose a reason for hiding this comment

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

As commented earleir, collectors should not require registrerer.

@pstibrany
Copy link
Contributor

In general, I don't think object should register itself anywhere in its "new" method. (I'm sure you will find many counter-examples in Cortex codebase).

if reg != nil {
reg.MustRegister(userManagerMetrics)
}
userManagerMetrics := NewManagerMetrics(reg)
Copy link
Contributor

Choose a reason for hiding this comment

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

You can now initialise it directly below:

userManagerMetrics: NewManagerMetrics(reg),
``` 

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we want this New(reg) pattern or not? @pstibrany says no

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry @roidelapluie for the long reply. Me and Peter had a discussion offline. We have a different opinion here. I commit to Peter's take: given ManagerMetrics is a Collector, and a Collector shouldn't take the registerer, let's not pass the registerer to NewManagerMetrics().

Same applies to alertmanager metrics.

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
Copy link
Contributor

@pracucci pracucci left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@pracucci pracucci merged commit d3ab152 into cortexproject:master Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants