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

Allow for /metrics path to be configurable #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions _examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 19 additions & 5 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}