11package cmd
22
33import (
4+ "fmt"
45 "net/http"
56 "os"
67 "os/user"
2324 logEncoding string
2425 bind string
2526 uri string
27+ metricsPath string
2628 queryTimeout int
2729 rootCmd = & cobra.Command {
2830 Use : "mongodb_query_exporter" ,
@@ -64,23 +66,25 @@ var (
6466
6567 prometheus .MustRegister (c )
6668 c .StartCacheInvalidator ()
67- serve (prometheus .DefaultGatherer , conf . GetBindAddr () )
69+ serve (prometheus .DefaultGatherer , conf )
6870 },
6971 }
7072)
7173
72- // Run executes a blocking http server. Starts the http listener with the /metrics endpoint
73- // and parses all configured metrics passed by config
74- func serve (reg prometheus.Gatherer , addr string ) {
75- http .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
76- http .Error (w , "Use the /metrics endpoint" , http .StatusOK )
77- })
74+ // Run executes a blocking http server. Starts the http listener with the metrics and healthz endpoints.
75+ func serve (reg prometheus.Gatherer , conf config.Config ) {
76+ if conf .GetMetricsPath () != "/" {
77+ http .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
78+ http .Error (w , fmt .Sprintf ("Use the %s endpoint" , conf .GetMetricsPath ()), http .StatusOK )
79+ })
80+ }
81+
7882 http .HandleFunc ("/healthz" , func (w http.ResponseWriter , r * http.Request ) { http .Error (w , "OK" , http .StatusOK ) })
79- http .HandleFunc ("/metrics" , func (w http.ResponseWriter , r * http.Request ) {
83+ http .HandleFunc (conf . GetMetricsPath () , func (w http.ResponseWriter , r * http.Request ) {
8084 promhttp .HandlerFor (reg , promhttp.HandlerOpts {}).ServeHTTP (w , r )
8185 })
8286
83- err := http .ListenAndServe (addr , nil )
87+ err := http .ListenAndServe (conf . GetBindAddr () , nil )
8488
8589 // If the port is already in use or another fatal error panic
8690 if err != nil {
@@ -103,17 +107,20 @@ func init() {
103107 rootCmd .PersistentFlags ().StringVarP (& logLevel , "log-level" , "l" , "info" , "Define the log level (default is info) [debug,info,warning,error]" )
104108 rootCmd .PersistentFlags ().StringVarP (& logEncoding , "log-encoding" , "e" , "json" , "Define the log format (default is json) [json,console]" )
105109 rootCmd .PersistentFlags ().StringVarP (& bind , "bind" , "b" , ":9412" , "Address to bind http server (default is :9412)" )
110+ rootCmd .PersistentFlags ().StringVarP (& metricsPath , "path" , "p" , "/metrics" , "Metric path (default is /metrics)" )
106111 rootCmd .PersistentFlags ().IntVarP (& queryTimeout , "query-timeout" , "t" , 10 , "Timeout for MongoDB queries" )
107112 viper .BindPFlag ("log.level" , rootCmd .PersistentFlags ().Lookup ("log-level" ))
108113 viper .BindPFlag ("log.encoding" , rootCmd .PersistentFlags ().Lookup ("log-encoding" ))
109114 viper .BindPFlag ("bind" , rootCmd .PersistentFlags ().Lookup ("bind" ))
115+ viper .BindPFlag ("metricsPath" , rootCmd .PersistentFlags ().Lookup ("path" ))
110116 viper .BindPFlag ("mongodb.uri" , rootCmd .PersistentFlags ().Lookup ("uri" ))
111117 viper .BindPFlag ("mongodb.queryTimeout" , rootCmd .PersistentFlags ().Lookup ("query-timeout" ))
112118 viper .BindEnv ("mongodb.uri" , "MDBEXPORTER_MONGODB_URI" )
113119 viper .BindEnv ("global.queryTimeout" , "MDBEXPORTER_MONGODB_QUERY_TIMEOUT" )
114120 viper .BindEnv ("log.level" , "MDBEXPORTER_LOG_LEVEL" )
115121 viper .BindEnv ("log.encoding" , "MDBEXPORTER_LOG_ENCODING" )
116122 viper .BindEnv ("bind" , "MDBEXPORTER_BIND" )
123+ viper .BindEnv ("metricsPath" , "MDBEXPORTER_METRICSPATH" )
117124}
118125
119126func initConfig () {
0 commit comments