github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/metrics/implementation/prometheus/count.go (about)

     1  // Copyright 2022 Meta Platforms, Inc. and affiliates.
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     4  //
     5  // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     6  //
     7  // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8  //
     9  // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    10  //
    11  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    12  
    13  // Copyright (c) Facebook, Inc. and its affiliates.
    14  //
    15  // This source code is licensed under the MIT license found in the
    16  // LICENSE file in the root directory of this source tree.
    17  
    18  package prometheus
    19  
    20  import (
    21  	"sync"
    22  
    23  	"github.com/facebookincubator/go-belt/pkg/field"
    24  	"github.com/facebookincubator/go-belt/tool/experimental/metrics/types"
    25  	"github.com/prometheus/client_golang/prometheus"
    26  	io_prometheus_client "github.com/prometheus/client_model/go"
    27  )
    28  
    29  var (
    30  	_ types.Count = &Count{}
    31  )
    32  
    33  // Count is an implementation of metrics.Count.
    34  type Count struct {
    35  	sync.Mutex
    36  	*Metrics
    37  	Key string
    38  	*CounterVec
    39  	io_prometheus_client.Metric
    40  	prometheus.Counter
    41  }
    42  
    43  // Add implementations metrics.Count.
    44  func (metric *Count) Add(delta uint64) types.Count {
    45  	metric.Counter.Add(float64(delta))
    46  	metric.Lock()
    47  	defer metric.Unlock()
    48  	err := metric.Write(&metric.Metric)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  	return metric
    53  }
    54  
    55  // Value implementations metrics.Metric.
    56  func (metric *Count) Value() any {
    57  	metric.Lock()
    58  	defer metric.Unlock()
    59  	return uint64(*metric.Metric.Counter.Value)
    60  }
    61  
    62  // WithResetFields implements metrics.Count
    63  func (metric *Count) WithResetFields(fields field.AbstractFields) types.Count {
    64  	result, err := metric.CounterVec.GetMetricWith(fieldsToLabels(fields))
    65  	if err == nil {
    66  		return &Count{Metrics: metric.Metrics, Key: metric.Key, CounterVec: metric.CounterVec, Counter: result}
    67  	}
    68  
    69  	return metric.Metrics.WithContextFields(nil, -1).(types.Metrics).CountFields(metric.Key, fields)
    70  }