github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/metric/aggregated/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 aggregated
    22  
    23  import (
    24  	"errors"
    25  	"fmt"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/metrics/generated/proto/metricpb"
    29  	"github.com/m3db/m3/src/metrics/metadata"
    30  	"github.com/m3db/m3/src/metrics/metric"
    31  	"github.com/m3db/m3/src/metrics/metric/id"
    32  	"github.com/m3db/m3/src/metrics/policy"
    33  )
    34  
    35  var (
    36  	errNilForwardedMetricWithMetadataProto   = errors.New("nil forwarded metric with metadata proto message")
    37  	errNilTimedMetricWithMetadataProto       = errors.New("nil timed metric with metadata proto message")
    38  	errNilPassthroughMetricWithMetadataProto = errors.New("nil passthrough metric with metadata proto message")
    39  )
    40  
    41  // Metric is a metric, which is essentially a named value at certain time.
    42  type Metric struct {
    43  	ID         id.RawID
    44  	Annotation []byte
    45  	Type       metric.Type
    46  	TimeNanos  int64
    47  	Value      float64
    48  }
    49  
    50  // ToProto converts the metric to a protobuf message in place.
    51  func (m Metric) ToProto(pb *metricpb.TimedMetric) error {
    52  	if err := m.Type.ToProto(&pb.Type); err != nil {
    53  		return err
    54  	}
    55  	pb.Id = m.ID
    56  	pb.TimeNanos = m.TimeNanos
    57  	pb.Value = m.Value
    58  	pb.Annotation = m.Annotation
    59  	return nil
    60  }
    61  
    62  // FromProto converts the protobuf message to a metric in place.
    63  func (m *Metric) FromProto(pb metricpb.TimedMetric) error {
    64  	if err := m.Type.FromProto(pb.Type); err != nil {
    65  		return err
    66  	}
    67  	m.ID = pb.Id
    68  	m.TimeNanos = pb.TimeNanos
    69  	m.Value = pb.Value
    70  	m.Annotation = pb.Annotation
    71  	return nil
    72  }
    73  
    74  // String is the string representation of a metric.
    75  func (m Metric) String() string {
    76  	return fmt.Sprintf(
    77  		"{id:%s,timestamp:%s,value:%f}",
    78  		m.ID.String(),
    79  		time.Unix(0, m.TimeNanos).String(),
    80  		m.Value,
    81  	)
    82  }
    83  
    84  // ChunkedMetric is a metric with a chunked ID.
    85  type ChunkedMetric struct {
    86  	id.ChunkedID
    87  	Annotation []byte
    88  	TimeNanos  int64
    89  	Value      float64
    90  }
    91  
    92  // RawMetric is a metric in its raw form (e.g., encoded bytes associated with
    93  // a metric object).
    94  type RawMetric interface {
    95  	// ID is the metric identifier.
    96  	ID() (id.RawID, error)
    97  
    98  	// TimeNanos is the metric timestamp in nanoseconds.
    99  	TimeNanos() (int64, error)
   100  
   101  	// Value is the metric value.
   102  	Value() (float64, error)
   103  
   104  	// Metric is the metric object represented by the raw metric.
   105  	Metric() (Metric, error)
   106  
   107  	// Bytes are the bytes backing this raw metric.
   108  	Bytes() []byte
   109  
   110  	// Reset resets the raw data.
   111  	Reset(data []byte)
   112  }
   113  
   114  // MetricWithStoragePolicy is a metric with applicable storage policy.
   115  type MetricWithStoragePolicy struct {
   116  	Metric
   117  	policy.StoragePolicy
   118  }
   119  
   120  // ToProto converts the chunked metric with storage policy to a protobuf message in place.
   121  func (m MetricWithStoragePolicy) ToProto(pb *metricpb.TimedMetricWithStoragePolicy) error {
   122  	if err := m.Metric.ToProto(&pb.TimedMetric); err != nil {
   123  		return err
   124  	}
   125  
   126  	return m.StoragePolicy.ToProto(&pb.StoragePolicy)
   127  }
   128  
   129  // FromProto converts the protobuf message to a chunked metric with storage policy in place.
   130  func (m *MetricWithStoragePolicy) FromProto(pb metricpb.TimedMetricWithStoragePolicy) error {
   131  	if err := m.Metric.FromProto(pb.TimedMetric); err != nil {
   132  		return err
   133  	}
   134  	return m.StoragePolicy.FromProto(pb.StoragePolicy)
   135  }
   136  
   137  // String is the string representation of a metric with storage policy.
   138  func (m MetricWithStoragePolicy) String() string {
   139  	return fmt.Sprintf("{metric:%s,policy:%s}", m.Metric.String(), m.StoragePolicy.String())
   140  }
   141  
   142  // ChunkedMetricWithStoragePolicy is a chunked metric with applicable storage policy.
   143  type ChunkedMetricWithStoragePolicy struct {
   144  	ChunkedMetric
   145  	policy.StoragePolicy
   146  }
   147  
   148  // ForwardedMetric is a forwarded metric.
   149  type ForwardedMetric struct {
   150  	ID         id.RawID
   151  	Values     []float64
   152  	PrevValues []float64
   153  	Annotation []byte
   154  	Type       metric.Type
   155  	TimeNanos  int64
   156  	Version    uint32
   157  }
   158  
   159  // ToProto converts the forwarded metric to a protobuf message in place.
   160  func (m ForwardedMetric) ToProto(pb *metricpb.ForwardedMetric) error {
   161  	if err := m.Type.ToProto(&pb.Type); err != nil {
   162  		return err
   163  	}
   164  	pb.Id = m.ID
   165  	pb.TimeNanos = m.TimeNanos
   166  	pb.Values = m.Values
   167  	pb.PrevValues = m.PrevValues
   168  	pb.Annotation = m.Annotation
   169  	pb.Version = m.Version
   170  	return nil
   171  }
   172  
   173  // FromProto converts the protobuf message to a forwarded metric in place.
   174  func (m *ForwardedMetric) FromProto(pb metricpb.ForwardedMetric) error {
   175  	if err := m.Type.FromProto(pb.Type); err != nil {
   176  		return err
   177  	}
   178  	m.ID = pb.Id
   179  	m.TimeNanos = pb.TimeNanos
   180  	m.Values = pb.Values
   181  	m.PrevValues = pb.PrevValues
   182  	m.Annotation = pb.Annotation
   183  	m.Version = pb.Version
   184  	return nil
   185  }
   186  
   187  // String is a string representation of the forwarded metric.
   188  func (m ForwardedMetric) String() string {
   189  	return fmt.Sprintf(
   190  		"{id:%s,timestamp:%s,values:%v,prev_values:%v}",
   191  		m.ID.String(),
   192  		time.Unix(0, m.TimeNanos).String(),
   193  		m.Values,
   194  		m.PrevValues,
   195  	)
   196  }
   197  
   198  // ForwardedMetricWithMetadata is a forwarded metric with metadata.
   199  type ForwardedMetricWithMetadata struct {
   200  	ForwardedMetric
   201  	metadata.ForwardMetadata
   202  }
   203  
   204  // ToProto converts the forwarded metric with metadata to a protobuf message in place.
   205  func (fm ForwardedMetricWithMetadata) ToProto(pb *metricpb.ForwardedMetricWithMetadata) error {
   206  	if err := fm.ForwardedMetric.ToProto(&pb.Metric); err != nil {
   207  		return err
   208  	}
   209  	return fm.ForwardMetadata.ToProto(&pb.Metadata)
   210  }
   211  
   212  // FromProto converts the protobuf message to a forwarded metric with metadata in place.
   213  func (fm *ForwardedMetricWithMetadata) FromProto(pb *metricpb.ForwardedMetricWithMetadata) error {
   214  	if pb == nil {
   215  		return errNilForwardedMetricWithMetadataProto
   216  	}
   217  	if err := fm.ForwardedMetric.FromProto(pb.Metric); err != nil {
   218  		return err
   219  	}
   220  	return fm.ForwardMetadata.FromProto(pb.Metadata)
   221  }
   222  
   223  // TimedMetricWithMetadata is a timed metric with metadata.
   224  type TimedMetricWithMetadata struct {
   225  	Metric
   226  	metadata.TimedMetadata
   227  }
   228  
   229  // ToProto converts the timed metric with metadata to a protobuf message in place.
   230  func (tm TimedMetricWithMetadata) ToProto(pb *metricpb.TimedMetricWithMetadata) error {
   231  	if err := tm.Metric.ToProto(&pb.Metric); err != nil {
   232  		return err
   233  	}
   234  	return tm.TimedMetadata.ToProto(&pb.Metadata)
   235  }
   236  
   237  // FromProto converts the protobuf message to a timed metric with metadata in place.
   238  func (tm *TimedMetricWithMetadata) FromProto(pb *metricpb.TimedMetricWithMetadata) error {
   239  	if pb == nil {
   240  		return errNilTimedMetricWithMetadataProto
   241  	}
   242  	if err := tm.Metric.FromProto(pb.Metric); err != nil {
   243  		return err
   244  	}
   245  	return tm.TimedMetadata.FromProto(pb.Metadata)
   246  }
   247  
   248  // TimedMetricWithMetadatas is a timed metric with staged metadatas.
   249  type TimedMetricWithMetadatas struct {
   250  	metadata.StagedMetadatas
   251  	Metric
   252  }
   253  
   254  // ToProto converts the timed metric with metadata to a protobuf message in place.
   255  func (tm TimedMetricWithMetadatas) ToProto(pb *metricpb.TimedMetricWithMetadatas) error {
   256  	if err := tm.Metric.ToProto(&pb.Metric); err != nil {
   257  		return err
   258  	}
   259  	return tm.StagedMetadatas.ToProto(&pb.Metadatas)
   260  }
   261  
   262  // FromProto converts the protobuf message to a timed metric with metadata in place.
   263  func (tm *TimedMetricWithMetadatas) FromProto(pb *metricpb.TimedMetricWithMetadatas) error {
   264  	if pb == nil {
   265  		return errNilTimedMetricWithMetadataProto
   266  	}
   267  	if err := tm.Metric.FromProto(pb.Metric); err != nil {
   268  		return err
   269  	}
   270  	return tm.StagedMetadatas.FromProto(pb.Metadatas)
   271  }
   272  
   273  // PassthroughMetricWithMetadata is a passthrough metric with metadata.
   274  type PassthroughMetricWithMetadata struct {
   275  	Metric
   276  	policy.StoragePolicy
   277  }
   278  
   279  // ToProto converts the passthrough metric with metadata to a protobuf message in place.
   280  func (pm PassthroughMetricWithMetadata) ToProto(pb *metricpb.TimedMetricWithStoragePolicy) error {
   281  	if err := pm.Metric.ToProto(&pb.TimedMetric); err != nil {
   282  		return err
   283  	}
   284  	return pm.StoragePolicy.ToProto(&pb.StoragePolicy)
   285  }
   286  
   287  // FromProto converts the protobuf message to a timed metric with metadata in place.
   288  func (pm *PassthroughMetricWithMetadata) FromProto(pb *metricpb.TimedMetricWithStoragePolicy) error {
   289  	if pb == nil {
   290  		return errNilPassthroughMetricWithMetadataProto
   291  	}
   292  	if err := pm.Metric.FromProto(pb.TimedMetric); err != nil {
   293  		return err
   294  	}
   295  	return pm.StoragePolicy.FromProto(pb.StoragePolicy)
   296  }