Skip to content

Add args to serve application behind a route prefix/external URL #1335

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 26 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
"fmt"
"log/slog"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"path"
"strings"
"sync"
"syscall"

"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/promauto"
Expand All @@ -50,11 +51,9 @@
concurrency = kingpin.Flag("snmp.module-concurrency", "The number of modules to fetch concurrently per scrape").Default("1").Int()
debugSNMP = kingpin.Flag("snmp.debug-packets", "Include a full debug trace of SNMP packet traffics.").Default("false").Bool()
expandEnvVars = kingpin.Flag("config.expand-environment-variables", "Expand environment variables to source secrets").Default("false").Bool()
metricsPath = kingpin.Flag(
"web.telemetry-path",
"Path under which to expose metrics.",
).Default("/metrics").String()
toolkitFlags = webflag.AddFlags(kingpin.CommandLine, ":9116")
routePrefix = toolkitFlags.WebRoutePrefix

Check failure on line 54 in main.go

View workflow job for this annotation

GitHub Actions / lint

toolkitFlags.WebRoutePrefix undefined (type *web.FlagConfig has no field or method WebRoutePrefix)

Check failure on line 54 in main.go

View workflow job for this annotation

GitHub Actions / lint

toolkitFlags.WebRoutePrefix undefined (type *web.FlagConfig has no field or method WebRoutePrefix)
metricsPath = toolkitFlags.WebMetricsPath

Check failure on line 55 in main.go

View workflow job for this annotation

GitHub Actions / lint

toolkitFlags.WebMetricsPath undefined (type *web.FlagConfig has no field or method WebMetricsPath)

Check failure on line 55 in main.go

View workflow job for this annotation

GitHub Actions / lint

toolkitFlags.WebMetricsPath undefined (type *web.FlagConfig has no field or method WebMetricsPath)
toolkitFlags = webflag.AddFlags(kingpin.CommandLine, ":9116")

// Metrics about the SNMP exporter itself.
snmpRequestErrors = promauto.NewCounter(
Expand Down Expand Up @@ -293,23 +292,16 @@
),
}

http.Handle(*metricsPath, promhttp.Handler()) // Normal metrics endpoint for SNMP exporter itself.
// Endpoint to do SNMP scrapes.
http.HandleFunc(proberPath, func(w http.ResponseWriter, r *http.Request) {
handler(w, r, logger, exporterMetrics)
})
http.HandleFunc("/-/reload", updateConfiguration) // Endpoint to reload configuration.
// Endpoint to respond to health checks
http.HandleFunc("/-/healthy", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Healthy"))
})

if *metricsPath != "/" && *metricsPath != "" {
landingConfig := web.LandingConfig{
Name: "SNMP Exporter",
Description: "Prometheus Exporter for SNMP targets",
Version: version.Info(),
Name: "SNMP Exporter",
Description: "Prometheus Exporter for SNMP targets",
Version: version.Info(),
RoutePrefix: *routePrefix,
ExternalURL: *toolkitFlags.WebExternalURL,

Check failure on line 301 in main.go

View workflow job for this annotation

GitHub Actions / lint

unknown field ExternalURL in struct literal of type web.LandingConfig

Check failure on line 301 in main.go

View workflow job for this annotation

GitHub Actions / lint

toolkitFlags.WebExternalURL undefined (type *web.FlagConfig has no field or method WebExternalURL)

Check failure on line 301 in main.go

View workflow job for this annotation

GitHub Actions / lint

unknown field ExternalURL in struct literal of type web.LandingConfig

Check failure on line 301 in main.go

View workflow job for this annotation

GitHub Actions / lint

toolkitFlags.WebExternalURL undefined (type *web.FlagConfig has no field or method WebExternalURL)
ListenAddresses: *toolkitFlags.WebListenAddresses,

Check failure on line 302 in main.go

View workflow job for this annotation

GitHub Actions / lint

unknown field ListenAddresses in struct literal of type web.LandingConfig

Check failure on line 302 in main.go

View workflow job for this annotation

GitHub Actions / lint

unknown field ListenAddresses in struct literal of type web.LandingConfig
UseSystemdSocket: *toolkitFlags.WebSystemdSocket,

Check failure on line 303 in main.go

View workflow job for this annotation

GitHub Actions / lint

unknown field UseSystemdSocket in struct literal of type web.LandingConfig

Check failure on line 303 in main.go

View workflow job for this annotation

GitHub Actions / lint

unknown field UseSystemdSocket in struct literal of type web.LandingConfig
Logger: logger,

Check failure on line 304 in main.go

View workflow job for this annotation

GitHub Actions / lint

unknown field Logger in struct literal of type web.LandingConfig

Check failure on line 304 in main.go

View workflow job for this annotation

GitHub Actions / lint

unknown field Logger in struct literal of type web.LandingConfig
Form: web.LandingForm{
Action: proberPath,
Inputs: []web.LandingFormInput{
Expand Down Expand Up @@ -347,15 +339,25 @@
},
},
}
landingPage, err := web.NewLandingPage(landingConfig)
landingPage, processedRoutePrefix, err := web.NewLandingPage(landingConfig)

Check failure on line 342 in main.go

View workflow job for this annotation

GitHub Actions / lint

assignment mismatch: 3 variables but web.NewLandingPage returns 2 values (typecheck)

Check failure on line 342 in main.go

View workflow job for this annotation

GitHub Actions / lint

assignment mismatch: 3 variables but web.NewLandingPage returns 2 values (typecheck)
if err != nil {
logger.Error("Error creating landing page", "err", err)
os.Exit(1)
}
http.Handle("/", landingPage)
*routePrefix = processedRoutePrefix
http.Handle(*routePrefix, landingPage)
}

http.HandleFunc(configPath, func(w http.ResponseWriter, r *http.Request) {
http.Handle(path.Join(*routePrefix, *metricsPath), promhttp.Handler()) // Normal metrics endpoint for SNMP exporter itself.

// Endpoint to do SNMP scrapes.
http.HandleFunc(path.Join(*routePrefix, proberPath), func(w http.ResponseWriter, r *http.Request) {
handler(w, r, logger, exporterMetrics)
})

http.HandleFunc(path.Join(*routePrefix, "/-/reload"), updateConfiguration) // Endpoint to reload configuration.

http.HandleFunc(path.Join(*routePrefix, configPath), func(w http.ResponseWriter, r *http.Request) {
sc.RLock()
c, err := yaml.Marshal(sc.C)
sc.RUnlock()
Expand Down
Loading