• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19304

22 Jun 2024 12:06AM UTC coverage: 88.44% (+0.01%) from 88.428%
#19304

push

github

web-flow
core: Add label values size validation in MetricRecorder (#11306)

Enhance MetricRecorder: Validate label values count against registered label keys count for default record APIs

32302 of 36524 relevant lines covered (88.44%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.22
/../api/src/main/java/io/grpc/MetricRecorder.java
1
/*
2
 * Copyright 2024 The 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 io.grpc;
18

19
import static com.google.common.base.Preconditions.checkArgument;
20

21
import java.util.List;
22

23
/**
24
 * An interface used for recording gRPC metrics. Implementations of this interface are responsible
25
 * for collecting and potentially reporting metrics from various gRPC components.
26
 */
27
@Internal
28
public interface MetricRecorder {
29
  /**
30
   * Adds a value for a double-precision counter metric instrument.
31
   *
32
   * @param metricInstrument The counter metric instrument to add the value against.
33
   * @param value The value to add.
34
   * @param requiredLabelValues A list of required label values for the metric.
35
   * @param optionalLabelValues A list of additional, optional label values for the metric.
36
   */
37
  default void addDoubleCounter(DoubleCounterMetricInstrument metricInstrument, double value,
38
      List<String> requiredLabelValues, List<String> optionalLabelValues) {
39
    checkArgument(requiredLabelValues != null
1✔
40
            && requiredLabelValues.size() == metricInstrument.getRequiredLabelKeys().size(),
1✔
41
        "Incorrect number of required labels provided. Expected: %s",
42
        metricInstrument.getRequiredLabelKeys().size());
1✔
43
    checkArgument(optionalLabelValues != null
1✔
44
            && optionalLabelValues.size() == metricInstrument.getOptionalLabelKeys().size(),
1✔
45
        "Incorrect number of optional labels provided. Expected: %s",
46
        metricInstrument.getOptionalLabelKeys().size());
1✔
47
  }
1✔
48

49
  /**
50
   * Adds a value for a long valued counter metric instrument.
51
   *
52
   * @param metricInstrument The counter metric instrument to add the value against.
53
   * @param value The value to add.
54
   * @param requiredLabelValues A list of required label values for the metric.
55
   * @param optionalLabelValues A list of additional, optional label values for the metric.
56
   */
57
  default void addLongCounter(LongCounterMetricInstrument metricInstrument, long value,
58
      List<String> requiredLabelValues, List<String> optionalLabelValues) {
59
    checkArgument(requiredLabelValues != null
1✔
60
            && requiredLabelValues.size() == metricInstrument.getRequiredLabelKeys().size(),
1✔
61
        "Incorrect number of required labels provided. Expected: %s",
62
        metricInstrument.getRequiredLabelKeys().size());
1✔
63
    checkArgument(optionalLabelValues != null
1✔
64
            && optionalLabelValues.size() == metricInstrument.getOptionalLabelKeys().size(),
1✔
65
        "Incorrect number of optional labels provided. Expected: %s",
66
        metricInstrument.getOptionalLabelKeys().size());
1✔
67
  }
1✔
68

69
  /**
70
   * Records a value for a double-precision histogram metric instrument.
71
   *
72
   * @param metricInstrument The histogram metric instrument to record the value against.
73
   * @param value The value to record.
74
   * @param requiredLabelValues A list of required label values for the metric.
75
   * @param optionalLabelValues A list of additional, optional label values for the metric.
76
   */
77
  default void recordDoubleHistogram(DoubleHistogramMetricInstrument metricInstrument, double value,
78
      List<String> requiredLabelValues, List<String> optionalLabelValues) {
79
    checkArgument(requiredLabelValues != null
1✔
80
            && requiredLabelValues.size() == metricInstrument.getRequiredLabelKeys().size(),
1✔
81
        "Incorrect number of required labels provided. Expected: %s",
82
        metricInstrument.getRequiredLabelKeys().size());
1✔
83
    checkArgument(optionalLabelValues != null
1✔
84
            && optionalLabelValues.size() == metricInstrument.getOptionalLabelKeys().size(),
1✔
85
        "Incorrect number of optional labels provided. Expected: %s",
86
        metricInstrument.getOptionalLabelKeys().size());
1✔
87
  }
1✔
88

89
  /**
90
   * Records a value for a long valued histogram metric instrument.
91
   *
92
   * @param metricInstrument The histogram metric instrument to record the value against.
93
   * @param value The value to record.
94
   * @param requiredLabelValues A list of required label values for the metric.
95
   * @param optionalLabelValues A list of additional, optional label values for the metric.
96
   */
97
  default void recordLongHistogram(LongHistogramMetricInstrument metricInstrument, long value,
98
      List<String> requiredLabelValues, List<String> optionalLabelValues) {
99
    checkArgument(requiredLabelValues != null
1✔
100
            && requiredLabelValues.size() == metricInstrument.getRequiredLabelKeys().size(),
1✔
101
        "Incorrect number of required labels provided. Expected: %s",
102
        metricInstrument.getRequiredLabelKeys().size());
1✔
103
    checkArgument(optionalLabelValues != null
1✔
104
            && optionalLabelValues.size() == metricInstrument.getOptionalLabelKeys().size(),
1✔
105
        "Incorrect number of optional labels provided. Expected: %s",
106
        metricInstrument.getOptionalLabelKeys().size());
1✔
107
  }
1✔
108

109
  /**
110
   * Registers a callback to produce metric values for only the listed instruments. The returned
111
   * registration must be closed when no longer needed, which will remove the callback.
112
   *
113
   * @param callback The callback to call to record.
114
   * @param metricInstruments The metric instruments the callback will record against.
115
   */
116
  default Registration registerBatchCallback(BatchCallback callback,
117
      CallbackMetricInstrument... metricInstruments) {
118
    return () -> { };
×
119
  }
120

121
  /** Callback to record gauge values. */
122
  interface BatchCallback {
123
    /** Records instrument values into {@code recorder}. */
124
    void accept(BatchRecorder recorder);
125
  }
126

127
  /** Recorder for instrument values produced by a batch callback. */
128
  interface BatchRecorder {
129
    /**
130
     * Record a long gauge value.
131
     *
132
     * @param value The value to record.
133
     * @param requiredLabelValues A list of required label values for the metric.
134
     * @param optionalLabelValues A list of additional, optional label values for the metric.
135
     */
136
    default void recordLongGauge(LongGaugeMetricInstrument metricInstrument, long value,
137
        List<String> requiredLabelValues, List<String> optionalLabelValues) {
138
      checkArgument(requiredLabelValues != null
1✔
139
              && requiredLabelValues.size() == metricInstrument.getRequiredLabelKeys().size(),
1✔
140
          "Incorrect number of required labels provided. Expected: %s",
141
          metricInstrument.getRequiredLabelKeys().size());
1✔
142
      checkArgument(optionalLabelValues != null
1✔
143
              && optionalLabelValues.size() == metricInstrument.getOptionalLabelKeys().size(),
1✔
144
          "Incorrect number of optional labels provided. Expected: %s",
145
          metricInstrument.getOptionalLabelKeys().size());
1✔
146
    }
1✔
147
  }
148

149
  /** A handle to a registration, that allows unregistration. */
150
  interface Registration extends AutoCloseable {
151
    // Redefined to not throw an exception.
152
    /** Unregister. */
153
    @Override
154
    void close();
155
  }
156
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc