google.golang.org/grpc@v1.72.2/internal/stats/metrics_recorder_list.go (about)

     1  /*
     2   * Copyright 2024 gRPC authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package stats
    18  
    19  import (
    20  	"fmt"
    21  
    22  	estats "google.golang.org/grpc/experimental/stats"
    23  	"google.golang.org/grpc/stats"
    24  )
    25  
    26  // MetricsRecorderList forwards Record calls to all of its metricsRecorders.
    27  //
    28  // It eats any record calls where the label values provided do not match the
    29  // number of label keys.
    30  type MetricsRecorderList struct {
    31  	// metricsRecorders are the metrics recorders this list will forward to.
    32  	metricsRecorders []estats.MetricsRecorder
    33  }
    34  
    35  // NewMetricsRecorderList creates a new metric recorder list with all the stats
    36  // handlers provided which implement the MetricsRecorder interface.
    37  // If no stats handlers provided implement the MetricsRecorder interface,
    38  // the MetricsRecorder list returned is a no-op.
    39  func NewMetricsRecorderList(shs []stats.Handler) *MetricsRecorderList {
    40  	var mrs []estats.MetricsRecorder
    41  	for _, sh := range shs {
    42  		if mr, ok := sh.(estats.MetricsRecorder); ok {
    43  			mrs = append(mrs, mr)
    44  		}
    45  	}
    46  	return &MetricsRecorderList{
    47  		metricsRecorders: mrs,
    48  	}
    49  }
    50  
    51  func verifyLabels(desc *estats.MetricDescriptor, labelsRecv ...string) {
    52  	if got, want := len(labelsRecv), len(desc.Labels)+len(desc.OptionalLabels); got != want {
    53  		panic(fmt.Sprintf("Received %d labels in call to record metric %q, but expected %d.", got, desc.Name, want))
    54  	}
    55  }
    56  
    57  // RecordInt64Count records the measurement alongside labels on the int
    58  // count associated with the provided handle.
    59  func (l *MetricsRecorderList) RecordInt64Count(handle *estats.Int64CountHandle, incr int64, labels ...string) {
    60  	verifyLabels(handle.Descriptor(), labels...)
    61  
    62  	for _, metricRecorder := range l.metricsRecorders {
    63  		metricRecorder.RecordInt64Count(handle, incr, labels...)
    64  	}
    65  }
    66  
    67  // RecordFloat64Count records the measurement alongside labels on the float
    68  // count associated with the provided handle.
    69  func (l *MetricsRecorderList) RecordFloat64Count(handle *estats.Float64CountHandle, incr float64, labels ...string) {
    70  	verifyLabels(handle.Descriptor(), labels...)
    71  
    72  	for _, metricRecorder := range l.metricsRecorders {
    73  		metricRecorder.RecordFloat64Count(handle, incr, labels...)
    74  	}
    75  }
    76  
    77  // RecordInt64Histo records the measurement alongside labels on the int
    78  // histo associated with the provided handle.
    79  func (l *MetricsRecorderList) RecordInt64Histo(handle *estats.Int64HistoHandle, incr int64, labels ...string) {
    80  	verifyLabels(handle.Descriptor(), labels...)
    81  
    82  	for _, metricRecorder := range l.metricsRecorders {
    83  		metricRecorder.RecordInt64Histo(handle, incr, labels...)
    84  	}
    85  }
    86  
    87  // RecordFloat64Histo records the measurement alongside labels on the float
    88  // histo associated with the provided handle.
    89  func (l *MetricsRecorderList) RecordFloat64Histo(handle *estats.Float64HistoHandle, incr float64, labels ...string) {
    90  	verifyLabels(handle.Descriptor(), labels...)
    91  
    92  	for _, metricRecorder := range l.metricsRecorders {
    93  		metricRecorder.RecordFloat64Histo(handle, incr, labels...)
    94  	}
    95  }
    96  
    97  // RecordInt64Gauge records the measurement alongside labels on the int
    98  // gauge associated with the provided handle.
    99  func (l *MetricsRecorderList) RecordInt64Gauge(handle *estats.Int64GaugeHandle, incr int64, labels ...string) {
   100  	verifyLabels(handle.Descriptor(), labels...)
   101  
   102  	for _, metricRecorder := range l.metricsRecorders {
   103  		metricRecorder.RecordInt64Gauge(handle, incr, labels...)
   104  	}
   105  }