github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/prometheus/native/middleware.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  // Package native handles the prometheus api endpoints using m3.
    22  package native
    23  
    24  import (
    25  	"net/http"
    26  	"time"
    27  
    28  	"go.uber.org/zap"
    29  
    30  	"github.com/m3db/m3/src/query/api/v1/handler/prometheus"
    31  	"github.com/m3db/m3/src/query/api/v1/middleware"
    32  )
    33  
    34  // WithQueryParams adds the query request parameters to the middleware options.
    35  var WithQueryParams middleware.OverrideOptions = func(opts middleware.Options) middleware.Options {
    36  	opts.Logging.Fields = opts.Logging.Fields.Append(func(r *http.Request, start time.Time) []zap.Field {
    37  		params, err := middlewareParseParams(r, start)
    38  		if err != nil {
    39  			opts.InstrumentOpts.Logger().Warn("failed to parse query params for response logging", zap.Error(err))
    40  			return nil
    41  		}
    42  		return []zap.Field{
    43  			zap.String("query", params.Query),
    44  			zap.Time("start", params.Start),
    45  			zap.Time("end", params.End),
    46  			zap.Duration("queryRange", params.Range()),
    47  		}
    48  	})
    49  	opts.Metrics.ParseQueryParams = middlewareParseParams
    50  	return opts
    51  }
    52  
    53  // WithRangeQueryParamsAndRangeRewriting adds the range query request parameters to the
    54  // middleware options and enables range rewriting
    55  var WithRangeQueryParamsAndRangeRewriting middleware.OverrideOptions = func(
    56  	opts middleware.Options,
    57  ) middleware.Options {
    58  	opts = WithQueryParams(opts)
    59  	opts.PrometheusRangeRewrite.Enabled = true
    60  
    61  	return opts
    62  }
    63  
    64  // WithInstantQueryParamsAndRangeRewriting adds the instant query request parameters to the
    65  // middleware options and enables range rewriting
    66  var WithInstantQueryParamsAndRangeRewriting middleware.OverrideOptions = func(
    67  	opts middleware.Options,
    68  ) middleware.Options {
    69  	opts = WithQueryParams(opts)
    70  	opts.PrometheusRangeRewrite.Enabled = true
    71  	opts.PrometheusRangeRewrite.Instant = true
    72  
    73  	return opts
    74  }
    75  
    76  var middlewareParseParams middleware.ParseQueryParams = func(r *http.Request, requestStart time.Time) (
    77  	middleware.QueryParams, error) {
    78  	query := r.FormValue(QueryParam)
    79  	if query == "" {
    80  		return middleware.QueryParams{}, nil
    81  	}
    82  
    83  	// N.B - instant queries set startParam/endParam to "now" if not set. ParseTime can handle this special
    84  	// "now" value. Use when this middleware ran as the approximate now value.
    85  	start, err := prometheus.ParseTime(r, startParam, requestStart)
    86  	if err != nil {
    87  		return middleware.QueryParams{}, err
    88  	}
    89  
    90  	end, err := prometheus.ParseTime(r, endParam, requestStart)
    91  	if err != nil {
    92  		return middleware.QueryParams{}, err
    93  	}
    94  	return middleware.QueryParams{
    95  		Query: query,
    96  		Start: start,
    97  		End:   end,
    98  	}, nil
    99  }