Skip to content

Commit

Permalink
Merge pull request #80 from philipgough/proxy-writes
Browse files Browse the repository at this point in the history
Prefer stdlib ReverseProxy implementation when no additional endpoint…
  • Loading branch information
philipgough authored May 21, 2024
2 parents 1b9840a + 7f45dca commit 12f76df
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
35 changes: 34 additions & 1 deletion internal/api/metrics/v1/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ func NewHandler(read, write *url.URL, opts ...HandlerOption) http.Handler {
})
}

if write != nil || c.endpoints != nil {
// If there are remote write endpoints, we should proxy the remote write requests to them.
// This is atypical behavior and the majority of configurations will not have remote write endpoints
// where we can defer to stdlib ReverseProxy as below.
if c.endpoints != nil && len(c.endpoints) > 0 {
proxyRemoteWrite := remotewrite.Proxy(write, c.endpoints, c.logger, c.registry)
r.Group(func(r chi.Router) {
r.Use(c.writeMiddlewares...)
Expand All @@ -155,6 +158,36 @@ func NewHandler(read, write *url.URL, opts ...HandlerOption) http.Handler {
proxyRemoteWrite,
))
})
return r
}

if write != nil {
var proxyWrite http.Handler
{
middlewares := proxy.Middlewares(
proxy.MiddlewareSetUpMetricsWritestream(write),
proxy.MiddlewareLogger(c.logger),
proxy.MiddlewareMetrics(c.registry, prometheus.Labels{"proxy": "metricsv1-write"}),
)

proxyWrite = &httputil.ReverseProxy{
Director: middlewares,
ErrorLog: proxy.Logger(c.logger),
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: dialTimeout,
}).DialContext,
},
}

r.Group(func(r chi.Router) {
r.Use(c.writeMiddlewares...)
r.Handle("/api/v1/receive", c.instrument.NewHandler(
prometheus.Labels{"group": "metricsv1", "handler": "receive"},
proxyWrite,
))
})
}
}

return r
Expand Down
8 changes: 4 additions & 4 deletions internal/remotewrite/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package remotewrite

import (
"bytes"
"io/ioutil"
"io"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -46,9 +46,9 @@ func remoteWrite(write *url.URL, endpoints []Endpoint, logger log.Logger) http.H
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests.With(prometheus.Labels{"method": r.Method}).Inc()

body, _ := ioutil.ReadAll(r.Body)
body, _ := io.ReadAll(r.Body)
_ = r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
r.Body = io.NopCloser(bytes.NewBuffer(body))

if write != nil {
remotewriteUrl := url.URL{}
Expand Down Expand Up @@ -115,7 +115,7 @@ func remoteWrite(write *url.URL, endpoints []Endpoint, logger log.Logger) http.H
defer resp.Body.Close()
remotewriteRequests.With(prometheus.Labels{"code": strconv.Itoa(resp.StatusCode), "name": ep.Name}).Inc()
if resp.StatusCode >= 300 || resp.StatusCode < 200 {
responseBody, err := ioutil.ReadAll(resp.Body)
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
//level.Error(rlogger).Log("msg", "Failed to read response of the forward request", "err", err, "return code", resp.Status, "url", ep.URL)
LogChannels[j] <- logMessage{
Expand Down

0 comments on commit 12f76df

Please sign in to comment.