Don't return error in ContextFromRequest function.
Previously it could return error if RemoteAddr didn't have correct format, but since this field has no specified format, that was little too strict. Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
This commit is contained in:
parent
babcb86cd0
commit
318cd413fc
|
@ -33,20 +33,24 @@ func ContextWithPath(ctx context.Context, path string) context.Context {
|
|||
|
||||
// ContextFromRequest returns a new context from a requests with identifiers of
|
||||
// the request to be used later when logging the query.
|
||||
func ContextFromRequest(ctx context.Context, r *http.Request) (context.Context, error) {
|
||||
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
func ContextFromRequest(ctx context.Context, r *http.Request) context.Context {
|
||||
m := map[string]string{
|
||||
"method": r.Method,
|
||||
}
|
||||
|
||||
// r.RemoteAddr has no defined format, so don't return error if we cannot split it into IP:Port.
|
||||
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
|
||||
if ip != "" {
|
||||
m["clientIP"] = ip
|
||||
}
|
||||
|
||||
var path string
|
||||
if v := ctx.Value(pathParam); v != nil {
|
||||
path = v.(string)
|
||||
m["path"] = path
|
||||
}
|
||||
|
||||
return promql.NewOriginContext(ctx, map[string]interface{}{
|
||||
"httpRequest": map[string]string{
|
||||
"clientIP": ip,
|
||||
"method": r.Method,
|
||||
"path": path,
|
||||
},
|
||||
}), nil
|
||||
"httpRequest": m,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -348,10 +348,7 @@ func (api *API) query(r *http.Request) apiFuncResult {
|
|||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
}
|
||||
|
||||
ctx, err = httputil.ContextFromRequest(ctx, r)
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, returnAPIError(err), nil, nil}
|
||||
}
|
||||
ctx = httputil.ContextFromRequest(ctx, r)
|
||||
|
||||
res := qry.Exec(ctx)
|
||||
if res.Err != nil {
|
||||
|
@ -423,10 +420,7 @@ func (api *API) queryRange(r *http.Request) apiFuncResult {
|
|||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
}
|
||||
|
||||
ctx, err = httputil.ContextFromRequest(ctx, r)
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, returnAPIError(err), nil, nil}
|
||||
}
|
||||
ctx = httputil.ContextFromRequest(ctx, r)
|
||||
|
||||
res := qry.Exec(ctx)
|
||||
if res.Err != nil {
|
||||
|
|
|
@ -653,11 +653,7 @@ func (h *Handler) consoles(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
ctx, err = httputil.ContextFromRequest(ctx, r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
ctx = httputil.ContextFromRequest(ctx, r)
|
||||
|
||||
// Provide URL parameters as a map for easy use. Advanced users may have need for
|
||||
// parameters beyond the first, so provide RawParams.
|
||||
|
|
Loading…
Reference in New Issue