github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/metric/unaggregated/types.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package unaggregated
    22  
    23  import (
    24  	"errors"
    25  	"fmt"
    26  
    27  	"github.com/m3db/m3/src/metrics/generated/proto/metricpb"
    28  	"github.com/m3db/m3/src/metrics/metadata"
    29  	"github.com/m3db/m3/src/metrics/metric"
    30  	"github.com/m3db/m3/src/metrics/metric/id"
    31  	"github.com/m3db/m3/src/metrics/policy"
    32  	"github.com/m3db/m3/src/x/pool"
    33  	xtime "github.com/m3db/m3/src/x/time"
    34  )
    35  
    36  var (
    37  	errNilCounterWithMetadatasProto    = errors.New("nil counter with metadatas proto message")
    38  	errNilBatchTimerWithMetadatasProto = errors.New("nil batch timer with metadatas proto message")
    39  	errNilGaugeWithMetadatasProto      = errors.New("nil gauge with metadatas proto message")
    40  )
    41  
    42  // Counter is a counter containing the counter ID and the counter value.
    43  type Counter struct {
    44  	ID              id.RawID
    45  	Annotation      []byte
    46  	Value           int64
    47  	ClientTimeNanos xtime.UnixNano
    48  }
    49  
    50  // ToUnion converts the counter to a metric union.
    51  func (c Counter) ToUnion() MetricUnion {
    52  	return MetricUnion{
    53  		Type:            metric.CounterType,
    54  		ID:              c.ID,
    55  		CounterVal:      c.Value,
    56  		Annotation:      c.Annotation,
    57  		ClientTimeNanos: c.ClientTimeNanos,
    58  	}
    59  }
    60  
    61  // ToProto converts the counter to a protobuf message in place.
    62  func (c Counter) ToProto(pb *metricpb.Counter) {
    63  	pb.Id = c.ID
    64  	pb.Value = c.Value
    65  	pb.Annotation = c.Annotation
    66  	pb.ClientTimeNanos = int64(c.ClientTimeNanos)
    67  }
    68  
    69  // FromProto converts the protobuf message to a counter in place.
    70  func (c *Counter) FromProto(pb metricpb.Counter) {
    71  	c.ID = pb.Id
    72  	c.Value = pb.Value
    73  	c.Annotation = pb.Annotation
    74  	c.ClientTimeNanos = xtime.UnixNano(pb.ClientTimeNanos)
    75  }
    76  
    77  // BatchTimer is a timer containing the timer ID and a list of timer values.
    78  type BatchTimer struct {
    79  	ID              id.RawID
    80  	Values          []float64
    81  	Annotation      []byte
    82  	ClientTimeNanos xtime.UnixNano
    83  }
    84  
    85  // ToUnion converts the batch timer to a metric union.
    86  func (t BatchTimer) ToUnion() MetricUnion {
    87  	return MetricUnion{
    88  		Type:            metric.TimerType,
    89  		ID:              t.ID,
    90  		BatchTimerVal:   t.Values,
    91  		Annotation:      t.Annotation,
    92  		ClientTimeNanos: t.ClientTimeNanos,
    93  	}
    94  }
    95  
    96  // ToProto converts the batch timer to a protobuf message in place.
    97  func (t BatchTimer) ToProto(pb *metricpb.BatchTimer) {
    98  	pb.Id = t.ID
    99  	pb.Values = t.Values
   100  	pb.Annotation = t.Annotation
   101  	pb.ClientTimeNanos = int64(t.ClientTimeNanos)
   102  }
   103  
   104  // FromProto converts the protobuf message to a batch timer in place.
   105  func (t *BatchTimer) FromProto(pb metricpb.BatchTimer) {
   106  	t.ID = pb.Id
   107  	t.Values = pb.Values
   108  	t.Annotation = pb.Annotation
   109  }
   110  
   111  // Gauge is a gauge containing the gauge ID and the value at certain time.
   112  type Gauge struct {
   113  	ID              id.RawID
   114  	Annotation      []byte
   115  	Value           float64
   116  	ClientTimeNanos xtime.UnixNano
   117  }
   118  
   119  // ToUnion converts the gauge to a metric union.
   120  func (g Gauge) ToUnion() MetricUnion {
   121  	return MetricUnion{
   122  		Type:            metric.GaugeType,
   123  		ID:              g.ID,
   124  		GaugeVal:        g.Value,
   125  		Annotation:      g.Annotation,
   126  		ClientTimeNanos: g.ClientTimeNanos,
   127  	}
   128  }
   129  
   130  // ToProto converts the gauge to a protobuf message in place.
   131  func (g Gauge) ToProto(pb *metricpb.Gauge) {
   132  	pb.Id = g.ID
   133  	pb.Value = g.Value
   134  	pb.Annotation = g.Annotation
   135  	pb.ClientTimeNanos = int64(g.ClientTimeNanos)
   136  }
   137  
   138  // FromProto converts the protobuf message to a gauge in place.
   139  func (g *Gauge) FromProto(pb metricpb.Gauge) {
   140  	g.ID = pb.Id
   141  	g.Value = pb.Value
   142  	g.Annotation = pb.Annotation
   143  	g.ClientTimeNanos = xtime.UnixNano(pb.ClientTimeNanos)
   144  }
   145  
   146  // CounterWithPoliciesList is a counter with applicable policies list.
   147  type CounterWithPoliciesList struct {
   148  	policy.PoliciesList
   149  	Counter
   150  }
   151  
   152  // BatchTimerWithPoliciesList is a batch timer with applicable policies list.
   153  type BatchTimerWithPoliciesList struct {
   154  	policy.PoliciesList
   155  	BatchTimer
   156  }
   157  
   158  // GaugeWithPoliciesList is a gauge with applicable policies list.
   159  type GaugeWithPoliciesList struct {
   160  	policy.PoliciesList
   161  	Gauge
   162  }
   163  
   164  // CounterWithMetadatas is a counter with applicable metadatas.
   165  type CounterWithMetadatas struct {
   166  	metadata.StagedMetadatas
   167  	Counter
   168  }
   169  
   170  // ToProto converts the counter with metadatas to a protobuf message in place.
   171  func (cm CounterWithMetadatas) ToProto(pb *metricpb.CounterWithMetadatas) error {
   172  	if err := cm.StagedMetadatas.ToProto(&pb.Metadatas); err != nil {
   173  		return err
   174  	}
   175  	cm.Counter.ToProto(&pb.Counter)
   176  	return nil
   177  }
   178  
   179  // FromProto converts the protobuf message to a counter with metadatas in place.
   180  func (cm *CounterWithMetadatas) FromProto(pb *metricpb.CounterWithMetadatas) error {
   181  	if pb == nil {
   182  		return errNilCounterWithMetadatasProto
   183  	}
   184  	if err := cm.StagedMetadatas.FromProto(pb.Metadatas); err != nil {
   185  		return err
   186  	}
   187  	cm.Counter.FromProto(pb.Counter)
   188  	return nil
   189  }
   190  
   191  // BatchTimerWithMetadatas is a batch timer with applicable metadatas.
   192  type BatchTimerWithMetadatas struct {
   193  	metadata.StagedMetadatas
   194  	BatchTimer
   195  }
   196  
   197  // ToProto converts the batch timer with metadatas to a protobuf message in place.
   198  func (bm BatchTimerWithMetadatas) ToProto(pb *metricpb.BatchTimerWithMetadatas) error {
   199  	if err := bm.StagedMetadatas.ToProto(&pb.Metadatas); err != nil {
   200  		return err
   201  	}
   202  	bm.BatchTimer.ToProto(&pb.BatchTimer)
   203  	return nil
   204  }
   205  
   206  // FromProto converts the protobuf message to a batch timer with metadatas in place.
   207  func (bm *BatchTimerWithMetadatas) FromProto(pb *metricpb.BatchTimerWithMetadatas) error {
   208  	if pb == nil {
   209  		return errNilBatchTimerWithMetadatasProto
   210  	}
   211  	if err := bm.StagedMetadatas.FromProto(pb.Metadatas); err != nil {
   212  		return err
   213  	}
   214  	bm.BatchTimer.FromProto(pb.BatchTimer)
   215  	return nil
   216  }
   217  
   218  // GaugeWithMetadatas is a gauge with applicable metadatas.
   219  type GaugeWithMetadatas struct {
   220  	metadata.StagedMetadatas
   221  	Gauge
   222  }
   223  
   224  // ToProto converts the gauge with metadatas to a protobuf message in place.
   225  func (gm GaugeWithMetadatas) ToProto(pb *metricpb.GaugeWithMetadatas) error {
   226  	if err := gm.StagedMetadatas.ToProto(&pb.Metadatas); err != nil {
   227  		return err
   228  	}
   229  	gm.Gauge.ToProto(&pb.Gauge)
   230  	return nil
   231  }
   232  
   233  // FromProto converts the protobuf message to a gauge with metadatas in place.
   234  func (gm *GaugeWithMetadatas) FromProto(pb *metricpb.GaugeWithMetadatas) error {
   235  	if pb == nil {
   236  		return errNilGaugeWithMetadatasProto
   237  	}
   238  	if err := gm.StagedMetadatas.FromProto(pb.Metadatas); err != nil {
   239  		return err
   240  	}
   241  	gm.Gauge.FromProto(pb.Gauge)
   242  	return nil
   243  }
   244  
   245  // MetricUnion is a union of different types of metrics, only one of which is valid
   246  // at any given time. The actual type of the metric depends on the type field,
   247  // which determines which value field is valid. Note that if the timer values are
   248  // allocated from a pool, the TimerValPool should be set to the originating pool,
   249  // and the caller is responsible for returning the timer values to the pool.
   250  type MetricUnion struct {
   251  	TimerValPool    pool.FloatsPool
   252  	Annotation      []byte
   253  	ID              id.RawID
   254  	BatchTimerVal   []float64
   255  	CounterVal      int64
   256  	GaugeVal        float64
   257  	Type            metric.Type
   258  	ClientTimeNanos xtime.UnixNano
   259  }
   260  
   261  var emptyMetricUnion MetricUnion
   262  
   263  // String is the string representation of a metric union.
   264  func (m *MetricUnion) String() string {
   265  	switch m.Type {
   266  	case metric.CounterType:
   267  		return fmt.Sprintf("{type:%s,id:%s,value:%d}", m.Type, m.ID.String(), m.CounterVal)
   268  	case metric.TimerType:
   269  		return fmt.Sprintf("{type:%s,id:%s,value:%v}", m.Type, m.ID.String(), m.BatchTimerVal)
   270  	case metric.GaugeType:
   271  		return fmt.Sprintf("{type:%s,id:%s,value:%f}", m.Type, m.ID.String(), m.GaugeVal)
   272  	default:
   273  		return fmt.Sprintf(
   274  			"{type:%d,id:%s,counterVal:%d,batchTimerVal:%v,gaugeVal:%f}",
   275  			m.Type, m.ID.String(), m.CounterVal, m.BatchTimerVal, m.GaugeVal,
   276  		)
   277  	}
   278  }
   279  
   280  // Reset resets the metric union.
   281  func (m *MetricUnion) Reset() { *m = emptyMetricUnion }
   282  
   283  // Counter returns the counter metric.
   284  func (m *MetricUnion) Counter() Counter {
   285  	return Counter{ID: m.ID, Value: m.CounterVal, Annotation: m.Annotation, ClientTimeNanos: m.ClientTimeNanos}
   286  }
   287  
   288  // BatchTimer returns the batch timer metric.
   289  func (m *MetricUnion) BatchTimer() BatchTimer {
   290  	return BatchTimer{ID: m.ID, Values: m.BatchTimerVal, Annotation: m.Annotation, ClientTimeNanos: m.ClientTimeNanos}
   291  }
   292  
   293  // Gauge returns the gauge metric.
   294  func (m *MetricUnion) Gauge() Gauge {
   295  	return Gauge{ID: m.ID, Value: m.GaugeVal, Annotation: m.Annotation, ClientTimeNanos: m.ClientTimeNanos}
   296  }