...

Source file src/github.com/linkerd/linkerd2/pkg/prometheus/test_helper.go

Documentation: github.com/linkerd/linkerd2/pkg/prometheus

     1  package prometheus
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  	"time"
     7  
     8  	promv1 "github.com/prometheus/client_golang/api/prometheus/v1"
     9  	"github.com/prometheus/common/model"
    10  )
    11  
    12  //
    13  // Prometheus client
    14  //
    15  
    16  // MockProm satisfies the promv1.API interface for testing.
    17  // TODO: move this into something shared under /controller, or into /pkg
    18  type MockProm struct {
    19  	Res             model.Value
    20  	QueriesExecuted []string // expose the queries our Mock Prometheus receives, to test query generation
    21  	rwLock          sync.Mutex
    22  }
    23  
    24  // Query performs a query for the given time.
    25  func (m *MockProm) Query(ctx context.Context, query string, ts time.Time, opts ...promv1.Option) (model.Value, promv1.Warnings, error) {
    26  	m.rwLock.Lock()
    27  	defer m.rwLock.Unlock()
    28  	m.QueriesExecuted = append(m.QueriesExecuted, query)
    29  	return m.Res, nil, nil
    30  }
    31  
    32  // QueryRange performs a query for the given range.
    33  func (m *MockProm) QueryRange(ctx context.Context, query string, r promv1.Range, opts ...promv1.Option) (model.Value, promv1.Warnings, error) {
    34  	m.rwLock.Lock()
    35  	defer m.rwLock.Unlock()
    36  	m.QueriesExecuted = append(m.QueriesExecuted, query)
    37  	return m.Res, nil, nil
    38  }
    39  
    40  // AlertManagers returns an overview of the current state of the Prometheus alert
    41  // manager discovery.
    42  func (m *MockProm) AlertManagers(ctx context.Context) (promv1.AlertManagersResult, error) {
    43  	return promv1.AlertManagersResult{}, nil
    44  }
    45  
    46  // Alerts returns a list of all active alerts.
    47  func (m *MockProm) Alerts(ctx context.Context) (promv1.AlertsResult, error) {
    48  	return promv1.AlertsResult{}, nil
    49  }
    50  
    51  // CleanTombstones removes the deleted data from disk and cleans up the existing
    52  // tombstones.
    53  func (m *MockProm) CleanTombstones(ctx context.Context) error {
    54  	return nil
    55  }
    56  
    57  // Config returns the current Prometheus configuration.
    58  func (m *MockProm) Config(ctx context.Context) (promv1.ConfigResult, error) {
    59  	return promv1.ConfigResult{}, nil
    60  }
    61  
    62  // DeleteSeries deletes data for a selection of series in a time range.
    63  func (m *MockProm) DeleteSeries(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) error {
    64  	return nil
    65  }
    66  
    67  // Flags returns the flag values that Prometheus was launched with.
    68  func (m *MockProm) Flags(ctx context.Context) (promv1.FlagsResult, error) {
    69  	return promv1.FlagsResult{}, nil
    70  }
    71  
    72  // LabelValues performs a query for the values of the given label, time range and matchers.
    73  func (m *MockProm) LabelValues(ctx context.Context, label string, matches []string, startTime time.Time, endTime time.Time) (model.LabelValues, promv1.Warnings, error) {
    74  	return nil, nil, nil
    75  }
    76  
    77  // Series finds series by label matchers.
    78  func (m *MockProm) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, promv1.Warnings, error) {
    79  	return nil, nil, nil
    80  }
    81  
    82  // Snapshot creates a snapshot of all current data into
    83  // snapshots/<datetime>-<rand> under the TSDB's data directory and returns the
    84  // directory as response.
    85  func (m *MockProm) Snapshot(ctx context.Context, skipHead bool) (promv1.SnapshotResult, error) {
    86  	return promv1.SnapshotResult{}, nil
    87  }
    88  
    89  // Targets returns an overview of the current state of the Prometheus target
    90  // discovery.
    91  func (m *MockProm) Targets(ctx context.Context) (promv1.TargetsResult, error) {
    92  	return promv1.TargetsResult{}, nil
    93  }
    94  
    95  // LabelNames returns the unique label names present in the block in sorted order by given time range and matchers.
    96  func (m *MockProm) LabelNames(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]string, promv1.Warnings, error) {
    97  	return []string{}, nil, nil
    98  }
    99  
   100  // Runtimeinfo returns the runtime info about Prometheus
   101  func (m *MockProm) Runtimeinfo(ctx context.Context) (promv1.RuntimeinfoResult, error) {
   102  	return promv1.RuntimeinfoResult{}, nil
   103  }
   104  
   105  // Metadata returns the metadata of the specified metric
   106  func (m *MockProm) Metadata(ctx context.Context, metric string, limit string) (map[string][]promv1.Metadata, error) {
   107  	return nil, nil
   108  }
   109  
   110  // Rules returns a list of alerting and recording rules that are currently loaded.
   111  func (m *MockProm) Rules(ctx context.Context) (promv1.RulesResult, error) {
   112  	return promv1.RulesResult{}, nil
   113  }
   114  
   115  // TargetsMetadata returns metadata about metrics currently scraped by the target.
   116  func (m *MockProm) TargetsMetadata(ctx context.Context, matchTarget string, metric string, limit string) ([]promv1.MetricMetadata, error) {
   117  	return []promv1.MetricMetadata{}, nil
   118  }
   119  
   120  // Buildinfo returns various build information properties about the Prometheus server
   121  func (m *MockProm) Buildinfo(ctx context.Context) (promv1.BuildinfoResult, error) {
   122  	return promv1.BuildinfoResult{}, nil
   123  }
   124  
   125  // QueryExemplars performs a query for exemplars by the given query and time range.
   126  func (m *MockProm) QueryExemplars(ctx context.Context, query string, startTime time.Time, endTime time.Time) ([]promv1.ExemplarQueryResult, error) {
   127  	return []promv1.ExemplarQueryResult{}, nil
   128  }
   129  
   130  // TSDB returns the cardinality statistics.
   131  func (m *MockProm) TSDB(ctx context.Context) (promv1.TSDBResult, error) {
   132  	return promv1.TSDBResult{}, nil
   133  }
   134  
   135  // WalReplay returns the current replay status of the wal.
   136  func (m *MockProm) WalReplay(ctx context.Context) (promv1.WalReplayStatus, error) {
   137  	return promv1.WalReplayStatus{}, nil
   138  }
   139  

View as plain text