diff --git a/_examples/basic/main.go b/_examples/basic/main.go index 8dec1a5..e842743 100644 --- a/_examples/basic/main.go +++ b/_examples/basic/main.go @@ -9,9 +9,7 @@ import ( "github.com/go-chi/telemetry" ) -var ( - AppMetrics = &MyAppMetrics{telemetry.NewScope("app")} -) +var AppMetrics = &MyAppMetrics{telemetry.NewScope("app")} type MyAppMetrics struct { *telemetry.Scope diff --git a/collector.go b/collector.go index 9f007f9..c567e33 100644 --- a/collector.go +++ b/collector.go @@ -22,9 +22,7 @@ func Collector(cfg Config, optPathPrefixFilters ...[]string) func(next http.Hand pathPrefixFilters := []string{} if len(optPathPrefixFilters) > 0 { - for _, v := range optPathPrefixFilters[0] { - pathPrefixFilters = append(pathPrefixFilters, v) - } + pathPrefixFilters = append(pathPrefixFilters, optPathPrefixFilters[0]...) } authHandler := middleware.BasicAuth( @@ -35,7 +33,6 @@ func Collector(cfg Config, optPathPrefixFilters ...[]string) func(next http.Hand metricsHandler := chi.Chain( func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // Maybe allow internal traffic if cfg.AllowInternal { ipAddress := net.ParseIP(getIPAddress(r)) @@ -65,9 +62,26 @@ func Collector(cfg Config, optPathPrefixFilters ...[]string) func(next http.Hand }, ) + metricsPath := cfg.HTTPPath + + switch len(strings.Trim(metricsPath, "")) { + + case 0: + metricsPath = "/metrics" + default: + + s := metricsPath + + if s[0] != '/' { + s = "/" + s + } + + metricsPath = s + } + return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Method == "GET" && strings.EqualFold(r.URL.Path, "/metrics") { + if r.Method == "GET" && strings.EqualFold(r.URL.Path, metricsPath) { // serve metrics page metricsHandler.Handler(next).ServeHTTP(w, r) return diff --git a/config.go b/config.go index d5f6706..93116b4 100644 --- a/config.go +++ b/config.go @@ -12,4 +12,8 @@ type Config struct { // Allow internal private subnet traffic AllowInternal bool `toml:"allow_internal"` + + // Defines the path the metrics data would be mounted. + // If not provided, it defaults to /metrics + HTTPPath string `toml:"http_path"` }