go.temporal.io/server@v1.23.0/common/metrics/defs.go (about)

     1  // The MIT License
     2  
     3  //
     4  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     5  //
     6  // Copyright (c) 2020 Uber Technologies, Inc.
     7  //
     8  // Permission is hereby granted, free of charge, to any person obtaining a copy
     9  // of this software and associated documentation files (the "Software"), to deal
    10  // in the Software without restriction, including without limitation the rights
    11  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    12  // copies of the Software, and to permit persons to whom the Software is
    13  // furnished to do so, subject to the following conditions:
    14  //
    15  // The above copyright notice and this permission notice shall be included in
    16  // all copies or substantial portions of the Software.
    17  //
    18  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    19  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    20  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    21  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    22  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    23  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    24  // THE SOFTWARE.
    25  
    26  package metrics
    27  
    28  // types used/defined by the package
    29  type (
    30  	MetricUnit string
    31  
    32  	// metricDefinition contains the definition for a metric
    33  	metricDefinition struct {
    34  		name        string
    35  		description string
    36  		unit        MetricUnit
    37  	}
    38  
    39  	histogramDefinition struct {
    40  		metricDefinition
    41  	}
    42  
    43  	counterDefinition struct {
    44  		metricDefinition
    45  	}
    46  
    47  	gaugeDefinition struct {
    48  		metricDefinition
    49  	}
    50  
    51  	timerDefinition struct {
    52  		metricDefinition
    53  	}
    54  )
    55  
    56  // MetricUnit supported values
    57  // Values are pulled from https://pkg.go.dev/golang.org/x/exp/event#Unit
    58  const (
    59  	Dimensionless = "1"
    60  	Milliseconds  = "ms"
    61  	Bytes         = "By"
    62  )
    63  
    64  func (md metricDefinition) Name() string {
    65  	return md.name
    66  }
    67  
    68  func (md metricDefinition) Unit() MetricUnit {
    69  	return md.unit
    70  }
    71  
    72  func NewTimerDef(name string, opts ...Option) timerDefinition {
    73  	return timerDefinition{globalRegistry.register(name, append(opts, WithUnit(Milliseconds))...)}
    74  }
    75  
    76  func NewBytesHistogramDef(name string, opts ...Option) histogramDefinition {
    77  	return histogramDefinition{globalRegistry.register(name, append(opts, WithUnit(Bytes))...)}
    78  }
    79  
    80  func NewDimensionlessHistogramDef(name string, opts ...Option) histogramDefinition {
    81  	return histogramDefinition{globalRegistry.register(name, append(opts, WithUnit(Dimensionless))...)}
    82  }
    83  
    84  func NewCounterDef(name string, opts ...Option) counterDefinition {
    85  	return counterDefinition{globalRegistry.register(name, opts...)}
    86  }
    87  
    88  func NewGaugeDef(name string, opts ...Option) gaugeDefinition {
    89  	return gaugeDefinition{globalRegistry.register(name, opts...)}
    90  }
    91  
    92  func (d histogramDefinition) With(handler Handler) HistogramIface {
    93  	return handler.Histogram(d.name, d.unit)
    94  }
    95  
    96  func (d counterDefinition) With(handler Handler) CounterIface {
    97  	return handler.Counter(d.name)
    98  }
    99  
   100  func (d gaugeDefinition) With(handler Handler) GaugeIface {
   101  	return handler.Gauge(d.name)
   102  }
   103  
   104  func (d timerDefinition) With(handler Handler) TimerIface {
   105  	return handler.Timer(d.name)
   106  }