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 default metrics #24

Merged
merged 7 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions rpi_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus"
promcollectors "github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
Expand All @@ -40,12 +41,24 @@ import (
type handler struct {
unfilteredHandler http.Handler
// There are only three collectors in this program, so that's seven combinations at most.
filteredHandlers map[string]http.Handler
filteredHandlers map[string]http.Handler
exporterMetricsRegistry *prometheus.Registry
includeExporterMetrics bool
}

func newHandler() *handler {
func newHandler(includeExporterMetrics bool) *handler {
h := &handler{
filteredHandlers: make(map[string]http.Handler),
filteredHandlers: make(map[string]http.Handler),
includeExporterMetrics: includeExporterMetrics,
}

// Add default collectors, if they aren't disabled.
if h.includeExporterMetrics {
h.exporterMetricsRegistry = prometheus.NewRegistry()
h.exporterMetricsRegistry.MustRegister(
promcollectors.NewProcessCollector(promcollectors.ProcessCollectorOpts{}),
promcollectors.NewGoCollector(),
)
}

// Create the unfiltered default handler.
Expand Down Expand Up @@ -112,10 +125,24 @@ func (h *handler) filteredHandler(filters ...string) (http.Handler, error) {

// Delegate http serving to Prometheus client library, which will call
// collector.Collect.
handler = promhttp.HandlerFor(reg, promhttp.HandlerOpts{
ErrorLog: log.NewErrorLogger(),
ErrorHandling: promhttp.HTTPErrorOnError,
})
if h.includeExporterMetrics {
handler = promhttp.HandlerFor(
prometheus.Gatherers{h.exporterMetricsRegistry, reg},
promhttp.HandlerOpts{
ErrorLog: log.NewErrorLogger(),
ErrorHandling: promhttp.HTTPErrorOnError,
Registry: h.exporterMetricsRegistry,
})
handler = promhttp.InstrumentMetricHandler(
h.exporterMetricsRegistry, handler,
)
} else {
handler = promhttp.HandlerFor(reg,
promhttp.HandlerOpts{
ErrorLog: log.NewErrorLogger(),
ErrorHandling: promhttp.HTTPErrorOnError,
})
}

// Store handler in cache if it isn't unfiltered.
if len(filters) > 0 {
Expand All @@ -134,9 +161,10 @@ func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
func main() {
// Command line flags.
var (
webListenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").Default(":9243").String()
webMetricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
webHealthPath = kingpin.Flag("web.healthcheck-path", "Path under which the exporter expose its status.").Default("/health").String()
webListenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").Default(":9243").String()
webMetricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
webHealthPath = kingpin.Flag("web.healthcheck-path", "Path under which the exporter exposes its status.").Default("/health").String()
webDisableExporterMetrics = kingpin.Flag("web.disable-exporter-metrics", "Exclude metrics about the exporter itself (promhttp_*, process_*, go_*).").Bool()
)

// Setup the command line flags and commands.
Expand All @@ -151,7 +179,7 @@ func main() {

// Setup router and handlers.
mux := http.NewServeMux()
mux.Handle(*webMetricsPath, newHandler())
mux.Handle(*webMetricsPath, newHandler(!*webDisableExporterMetrics))
mux.HandleFunc(*webHealthPath, HealthCheckHandler)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading