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

Add MustCollector API for pusher #1067

Closed
wants to merge 1 commit into from
Closed

Conversation

oiooj
Copy link
Contributor

@oiooj oiooj commented Jun 17, 2022

@kakkoyun PTAL

Signed-off-by: kun <oiooj@qq.com>
@kakkoyun
Copy link
Member

@oiooj Could you please explain the justification behind this change? It's hard to understand without any context.

@oiooj
Copy link
Contributor Author

oiooj commented Jun 20, 2022

Hi @kakkoyun , We use the pusher heavily, and we'll register many collectors with the pusher, but the pusher.Collector API only can register one collector at a time,and we can't get the error immediately, so the new API is more useful and convenient.

//
// For convenience, this method returns a pointer to the Pusher itself.
func (p *Pusher) MustCollector(c ...prometheus.Collector) *Pusher {
p.registerer.MustRegister(c...)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think adding an API that panics to the pusher is the right solution here.
If I'm not mistaken, I understand from you comment that you want to have an API to add multiple collectors. Would a simple for loop not satisfy your needs?

p := push.New("http://example.org/metrics", "my_job").
for _, c := range []prometheus.Collector{myCollector1, myCollector2} {
    p.Collector(myCollector1)
} 
p.Grouping("zone", "xy").Client(&myHTTPClient).BasicAuth("top", "secret").Add()

If somehow you don't want to use this, I might consider making Collector method variadic.

func (p *Pusher) Collector(collectors ...prometheus.Collector) *Pusher {
      if p.error != nil {
            return p
      }
       for _, c := range collectors {
            p.error = p.registerer.Register(c)		
            if p.error != nil {
                  return p
            }
	}
	return p
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is myCollector1 registered successfully? We want to know the result when the collectors is being registered. Maybe that is why Registerer has the MustRegister API.

Copy link
Member

Choose a reason for hiding this comment

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

I see what you mean. We can consider adding a method the obtain the error from outside. I just don't want to add a method that panics in the pusher. None of the other APIs in the package do it. I'd prefer to keep the consistent behaviour.

@oiooj oiooj mentioned this pull request Jun 30, 2022
@oiooj
Copy link
Contributor Author

oiooj commented Jul 4, 2022

New API #1075 merged, this PR closed.

@oiooj oiooj closed this Jul 4, 2022
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.

None yet

2 participants