github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/prom1/storage/metric/metric.go (about)

     1  // This file was taken from Prometheus (https://github.com/prometheus/prometheus).
     2  // The original license header is included below:
     3  //
     4  // Copyright 2014 The Prometheus Authors
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  // http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package metric
    18  
    19  import "github.com/prometheus/common/model"
    20  
    21  // Metric wraps a model.Metric and copies it upon modification if Copied is false.
    22  type Metric struct {
    23  	Copied bool
    24  	Metric model.Metric
    25  }
    26  
    27  // Set sets a label name in the wrapped Metric to a given value and copies the
    28  // Metric initially, if it is not already a copy.
    29  func (m *Metric) Set(ln model.LabelName, lv model.LabelValue) {
    30  	m.Copy()
    31  	m.Metric[ln] = lv
    32  }
    33  
    34  // Del deletes a given label name from the wrapped Metric and copies the
    35  // Metric initially, if it is not already a copy.
    36  func (m *Metric) Del(ln model.LabelName) {
    37  	m.Copy()
    38  	delete(m.Metric, ln)
    39  }
    40  
    41  // Get the value for the given label name. An empty value is returned
    42  // if the label does not exist in the metric.
    43  func (m *Metric) Get(ln model.LabelName) model.LabelValue {
    44  	return m.Metric[ln]
    45  }
    46  
    47  // Gets behaves as Get but the returned boolean is false iff the label
    48  // does not exist.
    49  func (m *Metric) Gets(ln model.LabelName) (model.LabelValue, bool) {
    50  	lv, ok := m.Metric[ln]
    51  	return lv, ok
    52  }
    53  
    54  // Copy the underlying Metric if it is not already a copy.
    55  func (m *Metric) Copy() *Metric {
    56  	if !m.Copied {
    57  		m.Metric = m.Metric.Clone()
    58  		m.Copied = true
    59  	}
    60  	return m
    61  }
    62  
    63  // String implements fmt.Stringer.
    64  func (m Metric) String() string {
    65  	return m.Metric.String()
    66  }