Skip to content

Commit

Permalink
(prometheus): Lowercase host value for metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
erbesharat committed Nov 30, 2019
1 parent e8632aa commit dc1fdb5
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func fallback(w http.ResponseWriter, r *http.Request, fallbackType string, code

func (f *Fallback) countFallback(recType string) {
if f.config.Prometheus.Enable {
FallbacksCount.WithLabelValues(f.request.Host, recType, f.fallbackType).Add(1)
RequestsByStatus.WithLabelValues(f.request.URL.Host, strconv.Itoa(f.code)).Add(1)
FallbacksCount.WithLabelValues(strings.ToLower(f.request.Host), recType, f.fallbackType).Add(1)
RequestsByStatus.WithLabelValues(strings.ToLower(f.request.URL.Host), strconv.Itoa(f.code)).Add(1)
}
}

Expand Down
2 changes: 1 addition & 1 deletion gometa.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (g *Gometa) Serve() error {

gosource := strings.Contains(g.rec.To, "github.com")

RequestsByStatus.WithLabelValues(g.req.Host, strconv.Itoa(http.StatusFound)).Add(1)
RequestsByStatus.WithLabelValues(strings.ToLower(g.req.Host), strconv.Itoa(http.StatusFound)).Add(1)
return tmpl.Execute(g.rw, struct {
Host string
Path string
Expand Down
3 changes: 2 additions & 1 deletion host.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net/http"
"strconv"
"strings"
)

type Host struct {
Expand Down Expand Up @@ -38,7 +39,7 @@ func (h *Host) Redirect() error {
h.rw.Header().Add("Status-Code", strconv.Itoa(code))
http.Redirect(h.rw, h.req, to, code)
if h.c.Prometheus.Enable {
RequestsByStatus.WithLabelValues(h.req.Host, strconv.Itoa(code)).Add(1)
RequestsByStatus.WithLabelValues(strings.ToLower(h.req.Host), strconv.Itoa(code)).Add(1)
}
return nil
}
2 changes: 1 addition & 1 deletion path.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (p *Path) RedirectRoot() error {
p.rw.Header().Add("Status-Code", strconv.Itoa(p.rec.Code))
http.Redirect(p.rw, p.req, p.rec.Root, p.rec.Code)
if p.c.Prometheus.Enable {
RequestsByStatus.WithLabelValues(p.req.Host, strconv.Itoa(p.rec.Code)).Add(1)
RequestsByStatus.WithLabelValues(strings.ToLower(p.req.Host), strconv.Itoa(p.rec.Code)).Add(1)
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"log"
"net/http"
"os"
"strings"

lumberjack "gopkg.in/natefinch/lumberjack.v2"

Expand Down Expand Up @@ -215,7 +216,7 @@ func (rd TXTDirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, erro
// Count total redirects if prometheus is enabled and set cache header
if w.Header().Get("Status-Code") == "301" || w.Header().Get("Status-Code") == "302" {
if rd.Config.Prometheus.Enable {
RequestsCount.WithLabelValues(r.Host).Add(1)
RequestsCount.WithLabelValues(strings.ToLower(r.Host)).Add(1)
}
// Set Cache-Control header on permanent redirects
if w.Header().Get("Status-Code") == "301" {
Expand Down
14 changes: 7 additions & 7 deletions txtdirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func blacklistRedirect(w http.ResponseWriter, r *http.Request, c Config) error {
w.Header().Add("Status-Code", strconv.Itoa(http.StatusNotFound))
http.Redirect(w, r, redirect, http.StatusNotFound)
if c.Prometheus.Enable {
RequestsByStatus.WithLabelValues(r.Host, strconv.Itoa(http.StatusNotFound)).Add(1)
RequestsByStatus.WithLabelValues(strings.ToLower(r.Host), strconv.Itoa(http.StatusNotFound)).Add(1)
}
}
return nil
Expand Down Expand Up @@ -200,8 +200,8 @@ func Redirect(w http.ResponseWriter, r *http.Request, c Config) error {
}

if rec.Type == "path" {
RequestsCountBasedOnType.WithLabelValues(host, "path").Add(1)
PathRedirectCount.WithLabelValues(host, path).Add(1)
RequestsCountBasedOnType.WithLabelValues(strings.ToLower(host), "path").Add(1)
PathRedirectCount.WithLabelValues(strings.ToLower(host), path).Add(1)

path := NewPath(w, r, path, rec, c)

Expand Down Expand Up @@ -231,7 +231,7 @@ func Redirect(w http.ResponseWriter, r *http.Request, c Config) error {
}

if rec.Type == "proxy" {
RequestsCountBasedOnType.WithLabelValues(host, "proxy").Add(1)
RequestsCountBasedOnType.WithLabelValues(strings.ToLower(host), "proxy").Add(1)
log.Printf("[txtdirect]: %s > %s", rec.From, rec.To)

proxy := NewProxy(w, r, rec, c)
Expand All @@ -244,7 +244,7 @@ func Redirect(w http.ResponseWriter, r *http.Request, c Config) error {
}

if rec.Type == "dockerv2" {
RequestsCountBasedOnType.WithLabelValues(host, "dockerv2").Add(1)
RequestsCountBasedOnType.WithLabelValues(strings.ToLower(host), "dockerv2").Add(1)

docker := NewDockerv2(w, r, rec, c)

Expand All @@ -262,7 +262,7 @@ func Redirect(w http.ResponseWriter, r *http.Request, c Config) error {
}

if rec.Type == "host" {
RequestsCountBasedOnType.WithLabelValues(host, "host").Add(1)
RequestsCountBasedOnType.WithLabelValues(strings.ToLower(host), "host").Add(1)

host := NewHost(w, r, rec, c)

Expand All @@ -273,7 +273,7 @@ func Redirect(w http.ResponseWriter, r *http.Request, c Config) error {
}

if rec.Type == "gometa" {
RequestsCountBasedOnType.WithLabelValues(host, "gometa").Add(1)
RequestsCountBasedOnType.WithLabelValues(strings.ToLower(host), "gometa").Add(1)

gometa := NewGometa(w, r, rec, c)

Expand Down

0 comments on commit dc1fdb5

Please sign in to comment.