github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/prom/mocks.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  	"context"
    25  	"errors"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/x/instrument"
    29  
    30  	"github.com/go-kit/kit/log"
    31  	kitlogzap "github.com/go-kit/kit/log/zap"
    32  	"github.com/prometheus/prometheus/pkg/labels"
    33  	"github.com/prometheus/prometheus/promql"
    34  	promstorage "github.com/prometheus/prometheus/storage"
    35  	"go.uber.org/zap/zapcore"
    36  )
    37  
    38  type mockQuerier struct {
    39  	mockOptions
    40  }
    41  
    42  type mockSeriesSet struct {
    43  	mockOptions
    44  	promstorage.SeriesSet
    45  }
    46  
    47  func (m *mockSeriesSet) Next() bool                     { return false }
    48  func (m *mockSeriesSet) At() promstorage.Series         { return nil }
    49  func (m *mockSeriesSet) Err() error                     { return nil }
    50  func (m *mockSeriesSet) Warnings() promstorage.Warnings { return nil }
    51  
    52  func (q *mockQuerier) Select(
    53  	sortSeries bool,
    54  	hints *promstorage.SelectHints,
    55  	labelMatchers ...*labels.Matcher,
    56  ) promstorage.SeriesSet {
    57  	if q.mockOptions.selectFn != nil {
    58  		return q.mockOptions.selectFn(sortSeries, hints, labelMatchers...)
    59  	}
    60  	return &mockSeriesSet{mockOptions: q.mockOptions}
    61  }
    62  
    63  func (*mockQuerier) LabelValues(name string, matchers ...*labels.Matcher) ([]string, promstorage.Warnings, error) {
    64  	return nil, nil, errors.New("not implemented")
    65  }
    66  
    67  func (*mockQuerier) LabelNames() ([]string, promstorage.Warnings, error) {
    68  	return nil, nil, errors.New("not implemented")
    69  }
    70  
    71  func (*mockQuerier) Close() error {
    72  	return nil
    73  }
    74  
    75  type mockOptions struct {
    76  	selectFn func(
    77  		sortSeries bool,
    78  		hints *promstorage.SelectHints,
    79  		labelMatchers ...*labels.Matcher,
    80  	) promstorage.SeriesSet
    81  }
    82  
    83  type mockQueryable struct {
    84  	mockOptions
    85  }
    86  
    87  func (q *mockQueryable) Querier(_ context.Context, _, _ int64) (promstorage.Querier, error) {
    88  	return &mockQuerier{mockOptions: q.mockOptions}, nil
    89  }
    90  
    91  func durationMilliseconds(d time.Duration) int64 {
    92  	return int64(d / (time.Millisecond / time.Nanosecond))
    93  }
    94  
    95  func newMockPromQLEngine() *promql.Engine {
    96  	var (
    97  		instrumentOpts = instrument.NewOptions()
    98  		kitLogger      = kitlogzap.NewZapSugarLogger(instrumentOpts.Logger(), zapcore.InfoLevel)
    99  		opts           = promql.EngineOpts{
   100  			Logger:     log.With(kitLogger, "component", "query engine"),
   101  			MaxSamples: 100,
   102  			Timeout:    1 * time.Minute,
   103  			NoStepSubqueryIntervalFn: func(rangeMillis int64) int64 {
   104  				return durationMilliseconds(1 * time.Minute)
   105  			},
   106  		}
   107  	)
   108  	return promql.NewEngine(opts)
   109  }