Skip to content

Commit

Permalink
add latency
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Sep 24, 2021
1 parent f4ef7f4 commit 691c693
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions core/component/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,32 @@ import (
"net/url"
"strconv"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
)

var (
services = prometheus.NewCounterVec(
httpRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "helmet",
Name: "services",
Help: "API GW Backend Services Statistics",
Name: "srv_total_http_requests",
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
}, []string{"id", "method", "uri", "code"})

requestDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: "peanut",
Name: "srv_request_duration_seconds",
Help: "The HTTP request latencies in seconds.",
},
[]string{"id", "method", "uri", "code"},
)
)

func init() {
prometheus.MustRegister(services)
prometheus.MustRegister(httpRequests)
prometheus.MustRegister(requestDuration)
}

// Proxy type
Expand Down Expand Up @@ -58,6 +69,8 @@ func NewProxy(httpRequest *http.Request, httpWriter http.ResponseWriter, name, u
func (p *Proxy) Redirect() {
origin, _ := url.Parse(p.Upstream)

start := time.Now()

director := func(req *http.Request) {
req.Header.Add("X-Forwarded-Host", origin.Host)
req.Header.Add("X-Origin-Host", req.Host)
Expand All @@ -78,13 +91,22 @@ func (p *Proxy) Redirect() {
}

modifyResponse := func(res *http.Response) error {
services.WithLabelValues(
httpRequests.WithLabelValues(
p.RequestMeta[0],
p.RequestMeta[1],
p.RequestMeta[2],
strconv.Itoa(res.StatusCode),
).Inc()

elapsed := float64(time.Since(start)) / float64(time.Second)

requestDuration.WithLabelValues(
p.RequestMeta[0],
p.RequestMeta[1],
p.RequestMeta[2],
strconv.Itoa(res.StatusCode),
).Observe(elapsed)

return nil
}

Expand Down

0 comments on commit 691c693

Please sign in to comment.