@ -14,6 +14,7 @@
package main
package main
import (
import (
"fmt"
"net/http"
"net/http"
_ "net/http/pprof"
_ "net/http/pprof"
@ -29,6 +30,40 @@ func init() {
prometheus . MustRegister ( version . NewCollector ( "node_exporter" ) )
prometheus . MustRegister ( version . NewCollector ( "node_exporter" ) )
}
}
func handler ( w http . ResponseWriter , r * http . Request ) {
filters := r . URL . Query ( ) [ "collect[]" ]
log . Debugln ( "collect query:" , filters )
nc , err := collector . NewNodeCollector ( filters ... )
if err != nil {
log . Warnln ( "Couldn't create" , err )
w . WriteHeader ( http . StatusBadRequest )
w . Write ( [ ] byte ( fmt . Sprintf ( "Couldn't create %s" , err ) ) )
return
}
registry := prometheus . NewRegistry ( )
err = registry . Register ( nc )
if err != nil {
log . Errorln ( "Couldn't register collector:" , err )
w . WriteHeader ( http . StatusInternalServerError )
w . Write ( [ ] byte ( fmt . Sprintf ( "Couldn't register collector: %s" , err ) ) )
return
}
gatherers := prometheus . Gatherers {
prometheus . DefaultGatherer ,
registry ,
}
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp . HandlerFor ( gatherers ,
promhttp . HandlerOpts {
ErrorLog : log . NewErrorLogger ( ) ,
ErrorHandling : promhttp . ContinueOnError ,
} )
h . ServeHTTP ( w , r )
}
func main ( ) {
func main ( ) {
var (
var (
listenAddress = kingpin . Flag ( "web.listen-address" , "Address on which to expose metrics and web interface." ) . Default ( ":9100" ) . String ( )
listenAddress = kingpin . Flag ( "web.listen-address" , "Address on which to expose metrics and web interface." ) . Default ( ":9100" ) . String ( )
@ -43,6 +78,7 @@ func main() {
log . Infoln ( "Starting node_exporter" , version . Info ( ) )
log . Infoln ( "Starting node_exporter" , version . Info ( ) )
log . Infoln ( "Build context" , version . BuildContext ( ) )
log . Infoln ( "Build context" , version . BuildContext ( ) )
// This instance is only used to check collector creation and logging.
nc , err := collector . NewNodeCollector ( )
nc , err := collector . NewNodeCollector ( )
if err != nil {
if err != nil {
log . Fatalf ( "Couldn't create collector: %s" , err )
log . Fatalf ( "Couldn't create collector: %s" , err )
@ -52,17 +88,8 @@ func main() {
log . Infof ( " - %s" , n )
log . Infof ( " - %s" , n )
}
}
if err := prometheus . Register ( nc ) ; err != nil {
// TODO(ts): Remove deprecated and problematic InstrumentHandlerFunc usage.
log . Fatalf ( "Couldn't register collector: %s" , err )
http . HandleFunc ( * metricsPath , prometheus . InstrumentHandlerFunc ( "prometheus" , handler ) )
}
handler := promhttp . HandlerFor ( prometheus . DefaultGatherer ,
promhttp . HandlerOpts {
ErrorLog : log . NewErrorLogger ( ) ,
ErrorHandling : promhttp . ContinueOnError ,
} )
// TODO(ts): Remove deprecated and problematic InstrumentHandler usage.
http . Handle ( * metricsPath , prometheus . InstrumentHandler ( "prometheus" , handler ) )
http . HandleFunc ( "/" , func ( w http . ResponseWriter , r * http . Request ) {
http . HandleFunc ( "/" , func ( w http . ResponseWriter , r * http . Request ) {
w . Write ( [ ] byte ( ` < html >
w . Write ( [ ] byte ( ` < html >
< head > < title > Node Exporter < / title > < / head >
< head > < title > Node Exporter < / title > < / head >