Skip to content

Commit

Permalink
Merge pull request #162 from dswarbrick/exporter-kit-flags
Browse files Browse the repository at this point in the history
Modernize exporter-toolkit flag parsing
  • Loading branch information
Lusitaniae authored Jun 26, 2023
2 parents 6b843ee + ab61e46 commit 2452944
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
37 changes: 21 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,29 @@ With working golang environment it can be built with `go get`. There is a [good
Help on flags:

<pre>
-h, --help Show context-sensitive help (also try --help-long and
--help-man).
--telemetry.address=":9117"
Address on which to expose metrics.
-h, --[no-]help Show context-sensitive help (also try
--help-long and --help-man).
--telemetry.endpoint="/metrics"
Path under which to expose metrics.
--scrape_uri="http://localhost/server-status?auto"
URI to apache stub status page.
--host_override="" Override for HTTP Host header; empty string for no
override.
--insecure Ignore server certificate if using https.
Path under which to expose metrics.
--scrape_uri="http://localhost/server-status/?auto"
URI to apache stub status page.
--host_override="" Override for HTTP Host header; empty string for
no override.
--[no-]insecure Ignore server certificate if using https.
--custom_headers Adds custom headers to the collector.
--web.config="" Path to config yaml file that can enable TLS or
authentication.
--log.level=info Only log messages with the given severity or above.
One of: [debug, info, warn, error]
--log.format=logfmt Output format of log messages. One of: [logfmt, json]
--version Show application version.
--[no-]web.systemd-socket Use systemd socket activation listeners instead
of port listeners (Linux only).
--web.listen-address=:9117 ...
Addresses on which to expose metrics and web
interface. Repeatable for multiple addresses.
--web.config.file="" [EXPERIMENTAL] Path to configuration file that
can enable TLS or authentication.
--log.level=info Only log messages with the given severity or
above. One of: [debug, info, warn, error]
--log.format=logfmt Output format of log messages. One of: [logfmt,
json]
--[no-]version Show application version.

</pre>

Tested on Apache 2.2 and Apache 2.4.
Expand Down
36 changes: 11 additions & 25 deletions apache_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package main
import (
"net/http"
"os"

"os/signal"
"syscall"
"time"
Expand All @@ -24,21 +23,20 @@ import (
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
"github.com/prometheus/exporter-toolkit/web/kingpinflag"
)

var (
listeningAddress = kingpin.Flag("telemetry.address", "Address on which to expose metrics.").Default(":9117").String()
metricsEndpoint = kingpin.Flag("telemetry.endpoint", "Path under which to expose metrics.").Default("/metrics").String()
scrapeURI = kingpin.Flag("scrape_uri", "URI to apache stub status page.").Default("http://localhost/server-status/?auto").String()
hostOverride = kingpin.Flag("host_override", "Override for HTTP Host header; empty string for no override.").Default("").String()
insecure = kingpin.Flag("insecure", "Ignore server certificate if using https.").Bool()
customHeaders = kingpin.Flag("custom_headers", "Adds custom headers to the collector.").StringMap()
configFile = kingpin.Flag("web.config", "Path to config yaml file that can enable TLS or authentication.").Default("").String()
gracefulStop = make(chan os.Signal, 1)
metricsEndpoint = kingpin.Flag("telemetry.endpoint", "Path under which to expose metrics.").Default("/metrics").String()
scrapeURI = kingpin.Flag("scrape_uri", "URI to apache stub status page.").Default("http://localhost/server-status/?auto").String()
hostOverride = kingpin.Flag("host_override", "Override for HTTP Host header; empty string for no override.").Default("").String()
insecure = kingpin.Flag("insecure", "Ignore server certificate if using https.").Bool()
toolkitFlags = kingpinflag.AddFlags(kingpin.CommandLine, ":9117")
gracefulStop = make(chan os.Signal, 1)
customHeaders = kingpin.Flag("custom_headers", "Adds custom headers to the collector.").StringMap()
)

func main() {

promlogConfig := &promlog.Config{}

// Parse flags
Expand All @@ -60,20 +58,12 @@ func main() {
CustomHeaders: *customHeaders,
}

webSystemdSocket := false
webListenAddresses := []string{*listeningAddress}
flagConfig := &web.FlagConfig{
WebConfigFile: configFile,
WebSystemdSocket: &webSystemdSocket,
WebListenAddresses: &webListenAddresses,
}
exporter := collector.NewExporter(logger, config)
prometheus.MustRegister(exporter)
prometheus.MustRegister(version.NewCollector("apache_exporter"))

level.Info(logger).Log("msg", "Starting apache_exporter", "version", version.Info())
level.Info(logger).Log("msg", "Build context", "build", version.BuildContext())
level.Info(logger).Log("msg", "Starting Server: ", "listen_address", *listeningAddress)
level.Info(logger).Log("msg", "Collect from: ", "scrape_uri", *scrapeURI)

// listener for the termination signals from the OS
Expand All @@ -82,7 +72,6 @@ func main() {
sig := <-gracefulStop
level.Info(logger).Log("msg", "caught sig: %+v. Wait 2 seconds...", "sig", sig)
time.Sleep(2 * time.Second)
level.Info(logger).Log("msg", "Terminate apache-exporter on port:", "listen_address", *listeningAddress)
os.Exit(0)
}()

Expand All @@ -96,13 +85,10 @@ func main() {
</body>
</html>`))
})
//log.Fatal(http.ListenAndServe(*listeningAddress, nil))

server := &http.Server{Addr: *listeningAddress}

if err := web.ListenAndServe(server, flagConfig, logger); err != nil {
level.Error(logger).Log("msg", "Listening error", "reason", err)
server := &http.Server{}
if err := web.ListenAndServe(server, toolkitFlags, logger); err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}

}

0 comments on commit 2452944

Please sign in to comment.