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

     1  // Copyright (c) 2020 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 prom
    22  
    23  import (
    24  	"errors"
    25  	"net/http"
    26  
    27  	"github.com/prometheus/prometheus/promql/parser"
    28  	promstorage "github.com/prometheus/prometheus/storage"
    29  
    30  	"github.com/m3db/m3/src/query/api/v1/options"
    31  	"github.com/m3db/m3/src/query/block"
    32  	"github.com/m3db/m3/src/query/storage/prometheus"
    33  )
    34  
    35  // opts defines options for PromQL handler.
    36  type opts struct {
    37  	instant    bool
    38  	queryable  promstorage.Queryable
    39  	newQueryFn NewQueryFn
    40  }
    41  
    42  // Option is a Prometheus handler option.
    43  type Option func(*opts) error
    44  
    45  // WithEngine sets the PromQL engine.
    46  func WithEngine(promQLEngineFn options.PromQLEngineFn) Option {
    47  	return withEngine(promQLEngineFn, false)
    48  }
    49  
    50  // WithInstantEngine sets the PromQL instant engine.
    51  func WithInstantEngine(promQLEngineFn options.PromQLEngineFn) Option {
    52  	return withEngine(promQLEngineFn, true)
    53  }
    54  
    55  func withEngine(promQLEngineFn options.PromQLEngineFn, instant bool) Option {
    56  	return func(o *opts) error {
    57  		if promQLEngineFn == nil {
    58  			return errors.New("invalid engine fn")
    59  		}
    60  		o.instant = instant
    61  		o.newQueryFn = newRangeQueryFn(promQLEngineFn, o.queryable)
    62  		if instant {
    63  			o.newQueryFn = newInstantQueryFn(promQLEngineFn, o.queryable)
    64  		}
    65  		return nil
    66  	}
    67  }
    68  
    69  func newDefaultOptions(hOpts options.HandlerOptions) opts {
    70  	queryable := prometheus.NewPrometheusQueryable(
    71  		prometheus.PrometheusOptions{
    72  			Storage:           hOpts.Storage(),
    73  			InstrumentOptions: hOpts.InstrumentOpts(),
    74  		})
    75  	return opts{
    76  		queryable:  queryable,
    77  		instant:    false,
    78  		newQueryFn: newRangeQueryFn(hOpts.PrometheusEngineFn(), queryable),
    79  	}
    80  }
    81  
    82  // NewReadHandler creates a handler to handle PromQL requests.
    83  func NewReadHandler(hOpts options.HandlerOptions, options ...Option) (http.Handler, error) {
    84  	opts := newDefaultOptions(hOpts)
    85  	for _, optionFn := range options {
    86  		if err := optionFn(&opts); err != nil {
    87  			return nil, err
    88  		}
    89  	}
    90  	return newReadHandler(hOpts, opts)
    91  }
    92  
    93  // ApplyRangeWarnings applies warnings encountered during execution.
    94  func ApplyRangeWarnings(
    95  	query string, meta *block.ResultMetadata,
    96  ) error {
    97  	expr, err := parser.ParseExpr(query)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	parser.Inspect(expr, func(node parser.Node, path []parser.Node) error {
   103  		if n, ok := node.(*parser.MatrixSelector); ok {
   104  			meta.VerifyTemporalRange(n.Range)
   105  		}
   106  
   107  		return nil
   108  	})
   109  
   110  	return nil
   111  }