Skip to content
Permalink
Browse files

(prometheus): Lowercase host of incoming requests

  • Loading branch information
erbesharat committed Dec 1, 2019
1 parent dc1fdb5 commit a664146bf009e072d78dd40b7f1ed5cb617ffce9
Showing with 14 additions and 14 deletions.
  1. +2 −2 fallback.go
  2. +1 −1 gometa.go
  3. +1 −2 host.go
  4. +1 −1 path.go
  5. +2 −1 setup.go
  6. +7 −7 txtdirect.go
@@ -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(strings.ToLower(f.request.Host), recType, f.fallbackType).Add(1)
RequestsByStatus.WithLabelValues(strings.ToLower(f.request.URL.Host), strconv.Itoa(f.code)).Add(1)
FallbacksCount.WithLabelValues(f.request.Host, recType, f.fallbackType).Add(1)
RequestsByStatus.WithLabelValues(f.request.URL.Host, strconv.Itoa(f.code)).Add(1)
}
}

@@ -56,7 +56,7 @@ func (g *Gometa) Serve() error {

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

RequestsByStatus.WithLabelValues(strings.ToLower(g.req.Host), strconv.Itoa(http.StatusFound)).Add(1)
RequestsByStatus.WithLabelValues(g.req.Host, strconv.Itoa(http.StatusFound)).Add(1)
return tmpl.Execute(g.rw, struct {
Host string
Path string
@@ -5,7 +5,6 @@ import (
"log"
"net/http"
"strconv"
"strings"
)

type Host struct {
@@ -39,7 +38,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(strings.ToLower(h.req.Host), strconv.Itoa(code)).Add(1)
RequestsByStatus.WithLabelValues(h.req.Host, strconv.Itoa(code)).Add(1)
}
return nil
}
@@ -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(strings.ToLower(p.req.Host), strconv.Itoa(p.rec.Code)).Add(1)
RequestsByStatus.WithLabelValues(p.req.Host, strconv.Itoa(p.rec.Code)).Add(1)
}
return nil
}
@@ -206,6 +206,7 @@ type TXTDirect struct {
}

func (rd TXTDirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
r.URL.Host = strings.ToLower(r.URL.Host)
if err := Redirect(w, r, rd.Config); err != nil {
if err.Error() == "option disabled" {
return rd.Next.ServeHTTP(w, r)
@@ -216,7 +217,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(strings.ToLower(r.Host)).Add(1)
RequestsCount.WithLabelValues(r.Host).Add(1)
}
// Set Cache-Control header on permanent redirects
if w.Header().Get("Status-Code") == "301" {
@@ -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(strings.ToLower(r.Host), strconv.Itoa(http.StatusNotFound)).Add(1)
RequestsByStatus.WithLabelValues(r.Host, strconv.Itoa(http.StatusNotFound)).Add(1)
}
}
return nil
@@ -200,8 +200,8 @@ func Redirect(w http.ResponseWriter, r *http.Request, c Config) error {
}

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

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit a664146

Please sign in to comment.
You can’t perform that action at this time.