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 'collect' URL parameter to filter enabled collectors #235

Merged
merged 13 commits into from Oct 13, 2017

Conversation

siavashs
Copy link
Contributor

@siavashs siavashs commented Oct 7, 2017

As discussed on #234 and IRC, this PR adds a collect URL parameter to filter currently enabled collectors.

Closes: #234

@SuperQ SuperQ self-requested a review October 7, 2017 15:53
@@ -170,6 +173,76 @@ func init() {
prometheus.MustRegister(version.NewCollector("mysqld_exporter"))
}

func filter(filters map[string]bool, name string) bool {
flg := flag.Lookup("collect." + name)
Copy link
Member

Choose a reason for hiding this comment

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

Minor nit, I think the golang style for this would just be f.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had used f in the original implementation, but changed it to flg later 😄
I'll change it back.

HeartbeatTable: *collectHeartbeatTable,
}

//start := time.Now()
Copy link
Member

Choose a reason for hiding this comment

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

Please remove commented out extras.

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, some leftovers from my lazy copy/pasting 😅

@SuperQ
Copy link
Member

SuperQ commented Oct 7, 2017

Very nice! A few minor nits.

@SuperQ
Copy link
Member

SuperQ commented Oct 7, 2017

Please update the README.md with usage info.

@SuperQ
Copy link
Member

SuperQ commented Oct 7, 2017

/cc @roman-vynar This will be great for PMM.


if len(query) > 0 {
filters = make(map[string]bool)
params := strings.Split(query, ",")
Copy link
Contributor

Choose a reason for hiding this comment

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

The usage should be ?collect[]=foo&collect[]=bar to keep in line with other places in Prometheus

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point 👌🏻

HeartbeatTable: *collectHeartbeatTable,
}

registry := prometheus.NewRegistry()
Copy link
Contributor

Choose a reason for hiding this comment

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

This won't include the default process stuff, so you should add it back in

@roman-vynar
Copy link
Contributor

roman-vynar commented Oct 8, 2017

@SuperQ awesome 😎 Much better than having 3 exporters.
/cc @rnovikovP

@siavashs Great work!

@@ -170,6 +172,74 @@ func init() {
prometheus.MustRegister(version.NewCollector("mysqld_exporter"))
}

func filter(filters map[string]bool, name string) bool {
f := flag.Lookup("collect." + name)
Copy link
Member

Choose a reason for hiding this comment

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

The only down-side to this approach is that we want to switch to kingpin for flag parsing. I haven't been able to find an equivalent feature in kingpin. 😦

Copy link
Contributor Author

@siavashs siavashs Oct 8, 2017

Choose a reason for hiding this comment

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

Yes, it would have been nice if kingpin had a Lookup() method 😕
I can change the filter() implementation or submit a PR for kingpin 😅

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I totally forgot to merge #222, which does the kingpin change. I would like to merge that, and have the filter changed to match. I think it will take too much time to get it implemented in kingpin.

Sorry, I should have taken care of the flag issue first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem, I'll update my branch.

@siavashs
Copy link
Contributor Author

As discussed on IRC, currently the http metrics are missing.
I could not find an example of how to instrument a handler function which initialises a custom registry, blackbox and snmp exporters only calculate the scrape duration and expose it.
I also noticed that some instrumentation functions are deprecated now and we should use promehttp instead.
So I need some help, maybe @beorn7 can give me a hint 😄

@beorn7
Copy link
Member

beorn7 commented Oct 12, 2017

So I need some help, maybe @beorn7 can give me a hint 😄

The promhttp documentation is here: https://godoc.org/github.com/prometheus/client_golang/prometheus/promhttp

Prior to removing the deprecated functions, there will be more documentation, examples, and tooling. https://godoc.org/github.com/prometheus/client_golang/prometheus#InstrumentHandler is handy but doing so much wrong that we cannot just not declare it deprecated.

@siavashs
Copy link
Contributor Author

143b2d0 exposes the http metrics but the values are 0/Nan.

registry.MustRegister(collector.New(dsn, collect))
prometheus.DefaultRegisterer = registry

// Delegate http serving to Promethues client library, which will call collector.Collect.
Copy link
Member

Choose a reason for hiding this comment

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

Promethues -> Prometheus

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I copied this typo from another exporter 😄

registry.MustRegister(prometheus.NewGoCollector())
registry.MustRegister(prometheus.NewProcessCollector(os.Getpid(), ""))
registry.MustRegister(collector.New(dsn, collect))
prometheus.DefaultRegisterer = registry
Copy link
Member

Choose a reason for hiding this comment

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

This will create a data race if multiple requests hit /metrics concurrently.

I assume you are only doing this here to make prometheus.InstrumentHandler work.

If you use the individual handler instrumentation helpers in promhttp, you can avoid that. Also, you can instrument the handler appropriately. (I'm pretty sure that some of the things that get auto-instrumented now are not needed, like request size. And response latency might be better done in a histogram so that you can aggregate over many exporters.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I noticed the issue while trying to make the instrumentation metrics work.
The next commit fixes this issue by creating a Gatherers instance from default gatherer and newly created registry per request.


registry := prometheus.NewRegistry()
registry.MustRegister(collector.New(dsn, collect))
prometheus.DefaultRegisterer = registry
Copy link
Member

Choose a reason for hiding this comment

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

I think you can now delete this line. (As long as this line is there, you have a race.)

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, of course.

@beorn7
Copy link
Member

beorn7 commented Oct 13, 2017

👍 from my side, leaving final say to @SuperQ .

Copy link
Member

@SuperQ SuperQ left a comment

Choose a reason for hiding this comment

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

Great!

@SuperQ SuperQ merged commit 8b92fe8 into prometheus:master Oct 13, 2017
}

registry := prometheus.NewRegistry()
registry.MustRegister(collector.New(dsn, collect))
Copy link
Contributor

Choose a reason for hiding this comment

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

@SuperQ @siavashs I think this broke scrapes_total and scrape_errors_total metrics. Previously collector.New() was created once - in main func. Now it's created on every scrape and so is scrapes_total and scrape_errors_total leading to constant values as every time scrapes_total is reset to 0, so we get only 1 scrape. It's not total anymore.
https://github.com/prometheus/mysqld_exporter/blob/master/collector/exporter.go#L107-L118

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can confirm this bug, I think we can fix it by having a separate collector for exporter metrics.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds correct to me. Thanks for the report. I'll make a separate issue for this.

Copy link
Contributor

Choose a reason for hiding this comment

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

For the reference: #269

Copy link
Contributor

@arvenil arvenil Jun 21, 2018

Choose a reason for hiding this comment

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

@SuperQ @siavashs Actually, there is one more confusing thing. Looks like now this metric is constant 2 #245, and even after fixing it, this will be increased every http call by 2 because each call ask for metrics and their description, and Describe again increases scrapes_total metric. Should scrapes_total be increased also when Describe is called? If only Collect would increase this metric then each http call would increase this metric only by 1. I'm working right now on fix for this so let me please know ;)
But maybe increasing this metric by 2 is correct and we simply should have separate metric for http calls?

}

c := collector.New(dsn, collect)
prometheus.MustRegister(c)
Copy link
Contributor

@arvenil arvenil Jun 27, 2018

Choose a reason for hiding this comment

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

After this change, registering collector every http request, we now call Describe() every http request, and as in result we call Collect() twice every http request.

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

7 participants