github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/selfmonitor/metrics_type.go (about)

     1  // Copyright 2024 iLogtail Authors
     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 selfmonitor
    16  
    17  type SelfMetricType int
    18  
    19  const (
    20  	_                     SelfMetricType = iota
    21  	CounterType                          // counter in the last window.
    22  	CumulativeCounterType                // cumulative counter.
    23  	AverageType                          // average value in the last window.
    24  	MaxType                              // max value in the last window.
    25  	LatencyType                          // average latency in the last window.
    26  	StringType                           // string value.
    27  	GaugeType                            // gauge value in the last window.
    28  
    29  	/*
    30  		Following Type are not used and not implemented yet.
    31  	*/
    32  	RateType // qps in the last window.
    33  	SummaryType
    34  	HistogramType
    35  )
    36  
    37  type LabelPair struct {
    38  	Key   string
    39  	Value string
    40  }
    41  
    42  type MetricValue[T string | float64] struct {
    43  	Name  string // Value Name, e.g. "cpu_usage"
    44  	Value T
    45  }
    46  
    47  // MetricSet is a Collector to bundle metrics of the same name that differ in  their label values.
    48  // A MetricSet has three properties:
    49  // 1. Metric Name.
    50  // 2. Constant Labels: Labels has constant value, e.g., "hostname=localhost", "namespace=default"
    51  // 3. LabelKeys is label Keys that may have different values, e.g., "status_code=200", "status_code=404".
    52  type MetricSet interface {
    53  	Name() string
    54  	Type() SelfMetricType
    55  	ConstLabels() []LabelPair
    56  	LabelKeys() []string
    57  }
    58  
    59  // MetricCollector is the interface implemented by anything that can be used by iLogtail to collect metrics.
    60  type MetricCollector interface {
    61  	Collect() []Metric
    62  }
    63  
    64  // MetricVector is a Collector to bundle metrics of the same name that differ in  their label values.
    65  // WithLabels will return a unique Metric that is bound to a set of label values.
    66  // If the labels contain label names that are not in the MetricSet, the Metric will be invalid.
    67  type MetricVector[T Metric] interface {
    68  	WithLabels(labels ...LabelPair) T
    69  }
    70  
    71  type Metric interface {
    72  	// Export as a map[string]string
    73  	Export() map[string]string
    74  	Type() SelfMetricType
    75  }
    76  
    77  // CounterMetric has three implementations:
    78  // cumulativeCounter: a cumulative counter metric that represents a single monotonically increasing counter whose value can only increase or be reset to zero on restart.
    79  // counter: the increased value in the last window.
    80  // average: the cumulative average value.
    81  type CounterMetric interface {
    82  	Metric
    83  	Add(int64)
    84  	Collect() MetricValue[float64]
    85  }
    86  
    87  type GaugeMetric interface {
    88  	Metric
    89  	Set(float64)
    90  	Collect() MetricValue[float64]
    91  }
    92  
    93  type LatencyMetric interface {
    94  	Metric
    95  	Observe(float64)
    96  	Collect() MetricValue[float64]
    97  }
    98  
    99  // SummaryMetric is used to compute pctXX for a batch of data
   100  type SummaryMetric interface {
   101  	Metric
   102  	Observe(float64)
   103  	Get() []MetricValue[float64]
   104  }
   105  
   106  // HistogramMetric is used to compute distribution of a batch of data.
   107  type HistogramMetric interface {
   108  	Metric
   109  	Observe(float64)
   110  	Get() []MetricValue[float64]
   111  }
   112  
   113  type StringMetric interface {
   114  	Metric
   115  	Set(v string)
   116  	Collect() MetricValue[string]
   117  }