Skip to content

Commit

Permalink
Merge pull request #1 from weisdd/feature/post
Browse files Browse the repository at this point in the history
Added support for POST requests, updated metricsql
  • Loading branch information
weisdd authored Mar 28, 2021
2 parents 4e5873f + a94ee98 commit 8b30fd5
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 42 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 0.3.0

* Added support for POST requests;
* Updated metricsql from `v0.10.1` to `v0.14.0`.

## 0.2.3

* Added `/federate` to a list of requests that should be rewritten.
Expand Down
35 changes: 0 additions & 35 deletions cmd/lfgw/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package main
import (
"fmt"
"net/http"
"net/url"
"strings"

"github.com/VictoriaMetrics/metricsql"
)

// serverError sends a generic 500 Internal Server Error response to the user.
Expand All @@ -26,38 +23,6 @@ func (app *application) clientErrorMessage(w http.ResponseWriter, status int, er
fmt.Fprintf(w, "%s", err)
}

// TODO: refactor/move somewhere else
// prepareRawQueryParams rewwrites GET "query" and "match" parameters to filter out metrics.
func (app *application) prepareRawQueryParams(r *http.Request, lf metricsql.LabelFilter) (string, error) {
rawQueryParams := &url.Values{}

err := r.ParseForm()
if err != nil {
return "", err
}

// TODO: refactor code
for k, vv := range r.Form {
switch k {
case "query", "match[]":
for _, v := range vv {
{
newVal, err := app.modifyMetricExpr(v, lf)
if err != nil {
return "", err
}
rawQueryParams.Add(k, newVal)
}
}
default:
for _, v := range vv {
rawQueryParams.Add(k, v)
}
}
}
return rawQueryParams.Encode(), nil
}

// getRawAccessToken returns a raw access token
func (app *application) getRawAccessToken(r *http.Request) (string, error) {
headers := []string{"X-Forwarded-Access-Token", "X-Auth-Request-Access-Token", "Authorization"}
Expand Down
27 changes: 27 additions & 0 deletions cmd/lfgw/lf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"net/url"

"github.com/VictoriaMetrics/metricsql"
)
Expand Down Expand Up @@ -76,3 +77,29 @@ func (app *application) modifyMetricExpr(query string, newFilter metricsql.Label

return string(expr.AppendString(nil)), nil
}

// prepareQueryParams rewrites GET/POST "query" and "match" parameters to filter out metrics.
func (app *application) prepareQueryParams(params *url.Values, lf metricsql.LabelFilter) (string, error) {
newParams := &url.Values{}

for k, vv := range *params {
switch k {
case "query", "match[]":
for _, v := range vv {
{
newVal, err := app.modifyMetricExpr(v, lf)
if err != nil {
return "", err
}
newParams.Add(k, newVal)
}
}
default:
for _, v := range vv {
newParams.Add(k, v)
}
}
}

return newParams.Encode(), nil
}
33 changes: 28 additions & 5 deletions cmd/lfgw/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"io"
"net/http"
"strings"

Expand All @@ -20,11 +21,11 @@ func (app *application) healthzMiddleware(next http.Handler) http.Handler {
})
}

// prohibitedMethods forbids all the methods aside from "GET".
// prohibitedMethods forbids all the methods aside from "GET" and "POST".
func (app *application) prohibitedMethodsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
if r.Method != http.MethodGet && r.Method != http.MethodPost {
w.Header().Set("Allow", fmt.Sprintf("%s, %s", http.MethodGet, http.MethodPost))
app.clientError(w, http.StatusMethodNotAllowed)
return
}
Expand Down Expand Up @@ -133,14 +134,36 @@ func (app *application) rewriteRequestMiddleware(next http.Handler) http.Handler
return
}

rawQueryParams, err := app.prepareRawQueryParams(r, lf)
err := r.ParseForm()
if err != nil {
app.errorLog.Printf("%s", err)
app.clientError(w, http.StatusBadRequest)
return
}

r.URL.RawQuery = rawQueryParams
// Adjust GET params
getParams := r.URL.Query()
newGetParams, err := app.prepareQueryParams(&getParams, lf)
if err != nil {
app.errorLog.Printf("%s", err)
app.clientError(w, http.StatusBadRequest)
return
}
r.URL.RawQuery = newGetParams

// Adjust POST params
// Partially inspired by https://github.com/bitsbeats/prometheus-acls/blob/master/internal/labeler/middleware.go
if r.Method == http.MethodPost {
newPostParams, err := app.prepareQueryParams(&r.PostForm, lf)
if err != nil {
app.errorLog.Printf("%s", err)
app.clientError(w, http.StatusBadRequest)
return
}
newBody := strings.NewReader(newPostParams)
r.ContentLength = newBody.Size()
r.Body = io.NopCloser(newBody)
}

next.ServeHTTP(w, r)
})
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module gitlab.rshbdev.ru/rshbintech/integrations/ckpr/infra/images/lfgw
go 1.16

require (
github.com/VictoriaMetrics/metrics v1.14.0 // indirect
github.com/VictoriaMetrics/metricsql v0.10.1
github.com/VictoriaMetrics/metrics v1.17.2 // indirect
github.com/VictoriaMetrics/metricsql v0.14.0
github.com/caarlos0/env/v6 v6.5.0
github.com/coreos/go-oidc/v3 v3.0.0
github.com/golang/protobuf v1.4.3 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/VictoriaMetrics/metrics v1.12.2/go.mod h1:Z1tSfPfngDn12bTfZSCqArT3OPY3u88J12hSoOhuiRE=
github.com/VictoriaMetrics/metrics v1.14.0 h1:yvyEVo7cPN2Hv+Hrm1zPTA1f/squmEZTq6xtPH/8F64=
github.com/VictoriaMetrics/metrics v1.14.0/go.mod h1:Z1tSfPfngDn12bTfZSCqArT3OPY3u88J12hSoOhuiRE=
github.com/VictoriaMetrics/metrics v1.17.2 h1:9zPJ7DPfxdJWshOGLPLpAtPL0ZZ9AeUyQC3fIqG6Lvo=
github.com/VictoriaMetrics/metrics v1.17.2/go.mod h1:Z1tSfPfngDn12bTfZSCqArT3OPY3u88J12hSoOhuiRE=
github.com/VictoriaMetrics/metricsql v0.10.1 h1:wLl/YbMmBGFPyLKMfqNLC333iygibosSM5iSvlH2B4A=
github.com/VictoriaMetrics/metricsql v0.10.1/go.mod h1:ylO7YITho/Iw6P71oEaGyHbO94bGoGtzWfLGqFhMIg8=
github.com/VictoriaMetrics/metricsql v0.14.0 h1:XGbpZJVskUPJFo2C7vG6ATxXBwkBFPe7EWZXB2HZt2U=
github.com/VictoriaMetrics/metricsql v0.14.0/go.mod h1:ylO7YITho/Iw6P71oEaGyHbO94bGoGtzWfLGqFhMIg8=
github.com/caarlos0/env/v6 v6.5.0 h1:f4C7ZQwm0nRFo8vETCQviLUOtOlOwsOhgc/QXp0zrTM=
github.com/caarlos0/env/v6 v6.5.0/go.mod h1:5ZqhjfyF261xGkANuSuMQ1FeA9ikA3wzDY64wSd9k8k=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down

0 comments on commit 8b30fd5

Please sign in to comment.