1
0
mirror of https://github.com/Lusitaniae/apache_exporter.git synced 2025-04-18 15:04:01 +03:00
apache_exporter/apache_exporter.go
Daniel Swarbrick 24b33e80e3
Update to latest Prometheus dependencies (#225)
The Prometheus ecosystem is moving away from go-kit/log, and instead
adopting the Go standard library log/slog.

prometheus/exporter-toolkit v0.13.0 introduced a
non-backwards-compatible change, requiring exporters to supply a
*slog.Logger to the web.ListenAndServe() function.

The end-user changes are minimal. Logging is still in logfmt by default,
albeit with some minor changes:
- "ts" (timestamp) field is now "time", and expressed in local time with
  timezone offset (previously it was UTC)
- "caller" field is replaced by "source", which is also now more
  verbose, with a fully qualified path
- "level" field values are now capitalized, e.g. "INFO"

Signed-off-by: Daniel Swarbrick <daniel.swarbrick@gmail.com>
2024-09-19 08:46:06 -07:00

105 lines
3.4 KiB
Go

// Copyright (c) 2015 neezgee
//
// Licensed under the MIT license: https://opensource.org/licenses/MIT
// Permission is granted to use, copy, modify, and redistribute the work.
// Full license information available in the project LICENSE file.
//
package main
import (
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/client_golang/prometheus"
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promslog"
"github.com/prometheus/common/promslog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
"github.com/prometheus/exporter-toolkit/web/kingpinflag"
"github.com/Lusitaniae/apache_exporter/collector"
)
var (
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() {
promslogConfig := &promslog.Config{}
// Parse flags
flag.AddFlags(kingpin.CommandLine, promslogConfig)
kingpin.HelpFlag.Short('h')
kingpin.Version(version.Print("apache_exporter"))
kingpin.Parse()
logger := promslog.New(promslogConfig)
// listen to termination signals from the OS
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
signal.Notify(gracefulStop, syscall.SIGHUP)
signal.Notify(gracefulStop, syscall.SIGQUIT)
config := &collector.Config{
ScrapeURI: *scrapeURI,
HostOverride: *hostOverride,
Insecure: *insecure,
CustomHeaders: *customHeaders,
}
exporter := collector.NewExporter(logger, config)
prometheus.MustRegister(exporter)
prometheus.MustRegister(versioncollector.NewCollector("apache_exporter"))
logger.Info("Starting apache_exporter", "version", version.Info())
logger.Info("Build context", "build", version.BuildContext())
logger.Info("Collect metrics from", "scrape_uri", *scrapeURI)
// listener for the termination signals from the OS
go func() {
logger.Debug("Listening and waiting for graceful stop")
sig := <-gracefulStop
logger.Info("Caught signal. Wait 2 seconds...", "sig", sig)
time.Sleep(2 * time.Second)
os.Exit(0)
}()
http.Handle(*metricsEndpoint, promhttp.Handler())
landingConfig := web.LandingConfig{
Name: "Apache Exporter",
Description: "Prometheus exporter for Apache HTTP server metrics",
Version: version.Info(),
Links: []web.LandingLinks{
{
Address: *metricsEndpoint,
Text: "Metrics",
},
},
}
landingPage, err := web.NewLandingPage(landingConfig)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
http.Handle("/", landingPage)
server := &http.Server{}
if err := web.ListenAndServe(server, toolkitFlags, logger); err != nil {
logger.Error(err.Error())
os.Exit(1)
}
}