grafana/pkg/promlib/middleware/force_http_get.go
ismail simsek 3fb6319d1b
Prometheus: Introduce prometheus backend library (#83952)
* Move files to prometheus-library

* refactor core prometheus to use prometheus-library

* modify client transport options

* mock

* have a type

* import aliases

* rename

* call the right method

* remove unrelated test from the library

* update codeowners

* go work sync

* update go.work.sum

* make swagger-clean && make openapi3-gen

* add promlib to makefile

* remove clilogger

* Export the function

* update unit test

* add prometheus_test.go

* fix mock type

* use mapUtil from grafana-plugin-sdk-go
2024-03-11 17:22:33 +01:00

31 lines
1.0 KiB
Go

package middleware
import (
"net/http"
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
)
func ForceHttpGet(logger log.Logger) sdkhttpclient.Middleware {
return sdkhttpclient.NamedMiddlewareFunc("force-http-get", func(opts sdkhttpclient.Options, next http.RoundTripper) http.RoundTripper {
// the prometheus library we use does not allow us to set the http method.
// it's behavior is to first try POST, and if it fails in certain ways
// (for example, by returning a method-not-allowed error), it will try GET.
// so here, we check if the http-method is POST, and if it is, we
// return an artificial method-not-allowed response.
// this will cause the prometheus library to retry with GET.
return sdkhttpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
if req.Method == http.MethodPost {
resp := &http.Response{
StatusCode: http.StatusMethodNotAllowed,
}
return resp, nil
}
return next.RoundTrip(req)
})
})
}