github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/util/dummies.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package util
    16  
    17  import (
    18  	"sync"
    19  	"time"
    20  
    21  	"k8s.io/heapster/metrics/core"
    22  )
    23  
    24  type DummySink struct {
    25  	name        string
    26  	mutex       sync.Mutex
    27  	exportCount int
    28  	stopped     bool
    29  	latency     time.Duration
    30  }
    31  
    32  func (this *DummySink) Name() string {
    33  	return this.name
    34  }
    35  func (this *DummySink) ExportData(*core.DataBatch) {
    36  	this.mutex.Lock()
    37  	this.exportCount++
    38  	this.mutex.Unlock()
    39  
    40  	time.Sleep(this.latency)
    41  }
    42  
    43  func (this *DummySink) Stop() {
    44  	this.mutex.Lock()
    45  	this.stopped = true
    46  	this.mutex.Unlock()
    47  
    48  	time.Sleep(this.latency)
    49  }
    50  
    51  func (this *DummySink) IsStopped() bool {
    52  	this.mutex.Lock()
    53  	defer this.mutex.Unlock()
    54  	return this.stopped
    55  }
    56  
    57  func (this *DummySink) GetExportCount() int {
    58  	this.mutex.Lock()
    59  	defer this.mutex.Unlock()
    60  	return this.exportCount
    61  }
    62  
    63  func NewDummySink(name string, latency time.Duration) *DummySink {
    64  	return &DummySink{
    65  		name:        name,
    66  		latency:     latency,
    67  		exportCount: 0,
    68  		stopped:     false,
    69  	}
    70  }
    71  
    72  type DummyMetricsSource struct {
    73  	latency   time.Duration
    74  	metricSet core.MetricSet
    75  }
    76  
    77  func (this *DummyMetricsSource) Name() string {
    78  	return "dummy"
    79  }
    80  
    81  func (this *DummyMetricsSource) ScrapeMetrics(start, end time.Time) (*core.DataBatch, error) {
    82  	time.Sleep(this.latency)
    83  	return &core.DataBatch{
    84  		Timestamp: end,
    85  		MetricSets: map[string]*core.MetricSet{
    86  			this.metricSet.Labels["name"]: &this.metricSet,
    87  		},
    88  	}, nil
    89  }
    90  
    91  func newDummyMetricSet(name string) core.MetricSet {
    92  	return core.MetricSet{
    93  		MetricValues: map[string]core.MetricValue{},
    94  		Labels: map[string]string{
    95  			"name": name,
    96  		},
    97  	}
    98  }
    99  
   100  func NewDummyMetricsSource(name string, latency time.Duration) *DummyMetricsSource {
   101  	return &DummyMetricsSource{
   102  		latency:   latency,
   103  		metricSet: newDummyMetricSet(name),
   104  	}
   105  }
   106  
   107  type DummyMetricsSourceProvider struct {
   108  	sources []core.MetricsSource
   109  }
   110  
   111  func (this *DummyMetricsSourceProvider) GetMetricsSources() []core.MetricsSource {
   112  	return this.sources
   113  }
   114  
   115  func NewDummyMetricsSourceProvider(sources ...core.MetricsSource) *DummyMetricsSourceProvider {
   116  	return &DummyMetricsSourceProvider{
   117  		sources: sources,
   118  	}
   119  }
   120  
   121  type DummyDataProcessor struct {
   122  	latency time.Duration
   123  }
   124  
   125  func (this *DummyDataProcessor) Name() string {
   126  	return "dummy"
   127  }
   128  
   129  func (this *DummyDataProcessor) Process(data *core.DataBatch) (*core.DataBatch, error) {
   130  	time.Sleep(this.latency)
   131  	return data, nil
   132  }
   133  
   134  func NewDummyDataProcessor(latency time.Duration) *DummyDataProcessor {
   135  	return &DummyDataProcessor{
   136  		latency: latency,
   137  	}
   138  }