gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/metric/condmetric.go (about)

     1  // Copyright 2022 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package metric
    16  
    17  import (
    18  	"fmt"
    19  
    20  	pb "gvisor.dev/gvisor/pkg/metric/metric_go_proto"
    21  )
    22  
    23  // FakeUint64Metric is a type that implements all the methods of a Uint64Metric
    24  // as a no-op.
    25  type FakeUint64Metric struct{}
    26  
    27  // FakeDistributionMetric is a type that implements all the methods of a
    28  // DistributionMetric as a no-op.
    29  type FakeDistributionMetric struct{}
    30  
    31  // FakeTimerMetric is a type that implements all the methods of a TimerMetric
    32  // as a no-op.
    33  type FakeTimerMetric struct{}
    34  
    35  // FakeTimedOperation is a type that implements all the methods of a
    36  // TimedOperation as a no-op.
    37  type FakeTimedOperation struct{}
    38  
    39  // Value from a FakeUint64Metric always returns a meaningless value.
    40  //
    41  //go:nosplit
    42  func (m *FakeUint64Metric) Value(fieldValues ...*FieldValue) uint64 {
    43  	return 0
    44  }
    45  
    46  // Increment on a FakeUint64Metric does nothing.
    47  //
    48  //go:nosplit
    49  func (m *FakeUint64Metric) Increment(fieldValues ...*FieldValue) {}
    50  
    51  // IncrementBy on a FakeUint64Metric does nothing.
    52  //
    53  //go:nosplit
    54  func (m *FakeUint64Metric) IncrementBy(v uint64, fieldValues ...*FieldValue) {}
    55  
    56  // AddSample on a FakeUint64Metric does nothing.
    57  //
    58  //go:nosplit
    59  func (d *FakeDistributionMetric) AddSample(sample int64, fields ...*FieldValue) {}
    60  
    61  // Start on a FakeUint64Metric returns a FakeTimedOperation struct, which does
    62  // nothing and does not keep the time.
    63  //
    64  //go:nosplit
    65  func (t *FakeTimerMetric) Start(fields ...*FieldValue) FakeTimedOperation {
    66  	return FakeTimedOperation{}
    67  }
    68  
    69  // Finish on a FakeTimedOperation does nothing.
    70  //
    71  //go:nosplit
    72  func (o FakeTimedOperation) Finish(extraFields ...*FieldValue) {}
    73  
    74  // FakeMetricBuilder is a type used to produce conditionally compiled metrics.
    75  // Methods of this struct produce fake, inactive metrics.
    76  type FakeMetricBuilder struct{}
    77  
    78  // NewUint64Metric creates a fake Uint64 metric.
    79  func (b *FakeMetricBuilder) NewUint64Metric(name string, sync bool, units pb.MetricMetadata_Units, description string, fields ...Field) (*FakeUint64Metric, error) {
    80  	return &FakeUint64Metric{}, nil
    81  }
    82  
    83  // MustCreateNewUint64Metric creates a fake Uint64 metric.
    84  func (b *FakeMetricBuilder) MustCreateNewUint64Metric(name string, sync bool, description string, fields ...Field) *FakeUint64Metric {
    85  	return &FakeUint64Metric{}
    86  }
    87  
    88  // NewDistributionMetric creates a fake distribution metric.
    89  func (b *FakeMetricBuilder) NewDistributionMetric(name string, sync bool, bucketer Bucketer, unit pb.MetricMetadata_Units, description string, fields ...Field) (*FakeDistributionMetric, error) {
    90  	return &FakeDistributionMetric{}, nil
    91  }
    92  
    93  // MustCreateNewDistributionMetric creates a fake distribution metric.
    94  func (b *FakeMetricBuilder) MustCreateNewDistributionMetric(name string, sync bool, bucketer Bucketer, unit pb.MetricMetadata_Units, description string, fields ...Field) *FakeDistributionMetric {
    95  	return &FakeDistributionMetric{}
    96  }
    97  
    98  // NewTimerMetric creates a fake timer metric.
    99  func (b *FakeMetricBuilder) NewTimerMetric(name string, nanoBucketer Bucketer, description string, fields ...Field) (*FakeTimerMetric, error) {
   100  	return &FakeTimerMetric{}, nil
   101  }
   102  
   103  // MustCreateNewTimerMetric creates a fake timer metric.
   104  func (b *FakeMetricBuilder) MustCreateNewTimerMetric(name string, nanoBucketer Bucketer, description string, fields ...Field) *FakeTimerMetric {
   105  	return &FakeTimerMetric{}
   106  }
   107  
   108  // RealMetricBuilder is a type used to produce conditionally compiled metrics.
   109  // Methods of this struct produce real active metrics.
   110  type RealMetricBuilder struct{}
   111  
   112  // NewUint64Metric calls the generic metric.NewUint64Metric to produce a real
   113  // Uint64 metric.
   114  func (b *RealMetricBuilder) NewUint64Metric(name string, sync bool, units pb.MetricMetadata_Units, description string, fields ...Field) (*Uint64Metric, error) {
   115  	m, err := NewUint64Metric(name, sync, units, description, fields...)
   116  	if err != nil {
   117  		return m, err
   118  	}
   119  	definedProfilingMetrics = append(definedProfilingMetrics, m.name)
   120  	return m, err
   121  }
   122  
   123  // MustCreateNewUint64Metric creates a real Uint64 metric or panics if unable to
   124  // do so.
   125  func (b *RealMetricBuilder) MustCreateNewUint64Metric(name string, sync bool, description string, fields ...Field) *Uint64Metric {
   126  	m, err := b.NewUint64Metric(name, sync, pb.MetricMetadata_UNITS_NONE, description, fields...)
   127  	if err != nil {
   128  		panic(fmt.Sprintf("Unable to create metric %q: %s", name, err))
   129  	}
   130  	return m
   131  }
   132  
   133  // NewDistributionMetric calls the generic metric.NewDistributionMetric to
   134  // produce a real distribution metric.
   135  func (b *RealMetricBuilder) NewDistributionMetric(name string, sync bool, bucketer Bucketer, unit pb.MetricMetadata_Units, description string, fields ...Field) (*DistributionMetric, error) {
   136  	return NewDistributionMetric(name, sync, bucketer, unit, description, fields...)
   137  }
   138  
   139  // MustCreateNewDistributionMetric creates a real distribution metric or panics
   140  // if unable to do so.
   141  func (b *RealMetricBuilder) MustCreateNewDistributionMetric(name string, sync bool, bucketer Bucketer, unit pb.MetricMetadata_Units, description string, fields ...Field) *DistributionMetric {
   142  	return MustCreateNewDistributionMetric(name, sync, bucketer, unit, description, fields...)
   143  }
   144  
   145  // NewTimerMetric calls the generic metric.NewTimerMetric to produce a real timer
   146  // metric.
   147  func (b *RealMetricBuilder) NewTimerMetric(name string, nanoBucketer Bucketer, description string, fields ...Field) (*TimerMetric, error) {
   148  	return NewTimerMetric(name, nanoBucketer, description, fields...)
   149  }
   150  
   151  // MustCreateNewTimerMetric creates a real timer metric or panics if unable to
   152  // do so.
   153  func (b *RealMetricBuilder) MustCreateNewTimerMetric(name string, nanoBucketer Bucketer, description string, fields ...Field) *TimerMetric {
   154  	return MustCreateNewTimerMetric(name, nanoBucketer, description, fields...)
   155  }