A single histogram and its samples. For use by custom collectors.
| 222 | |
| 223 | |
| 224 | class HistogramMetricFamily(Metric): |
| 225 | """A single histogram and its samples. |
| 226 | |
| 227 | For use by custom collectors. |
| 228 | """ |
| 229 | |
| 230 | def __init__(self, |
| 231 | name: str, |
| 232 | documentation: str, |
| 233 | buckets: Optional[Sequence[Union[Tuple[str, float], Tuple[str, float, Exemplar]]]] = None, |
| 234 | sum_value: Optional[float] = None, |
| 235 | labels: Optional[Sequence[str]] = None, |
| 236 | unit: str = '', |
| 237 | ): |
| 238 | Metric.__init__(self, name, documentation, 'histogram', unit) |
| 239 | if sum_value is not None and buckets is None: |
| 240 | raise ValueError('sum value cannot be provided without buckets.') |
| 241 | if labels is not None and buckets is not None: |
| 242 | raise ValueError('Can only specify at most one of buckets and labels.') |
| 243 | if labels is None: |
| 244 | labels = [] |
| 245 | self._labelnames = tuple(labels) |
| 246 | if buckets is not None: |
| 247 | self.add_metric([], buckets, sum_value) |
| 248 | |
| 249 | def add_metric(self, |
| 250 | labels: Sequence[str], |
| 251 | buckets: Sequence[Union[Tuple[str, float], Tuple[str, float, Exemplar]]], |
| 252 | sum_value: Optional[float], |
| 253 | timestamp: Optional[Union[Timestamp, float]] = None) -> None: |
| 254 | """Add a metric to the metric family. |
| 255 | |
| 256 | Args: |
| 257 | labels: A list of label values |
| 258 | buckets: A list of lists. |
| 259 | Each inner list can be a pair of bucket name and value, |
| 260 | or a triple of bucket name, value, and exemplar. |
| 261 | The buckets must be sorted, and +Inf present. |
| 262 | sum_value: The sum value of the metric. |
| 263 | """ |
| 264 | for b in buckets: |
| 265 | bucket, value = b[:2] |
| 266 | exemplar = None |
| 267 | if len(b) == 3: |
| 268 | exemplar = b[2] # type: ignore |
| 269 | self.samples.append(Sample( |
| 270 | self.name + '_bucket', |
| 271 | dict(list(zip(self._labelnames, labels)) + [('le', bucket)]), |
| 272 | value, |
| 273 | timestamp, |
| 274 | exemplar, |
| 275 | )) |
| 276 | # Don't include sum and thus count if there's negative buckets. |
| 277 | if float(buckets[0][0]) >= 0 and sum_value is not None: |
| 278 | # +Inf is last and provides the count value. |
| 279 | self.samples.append( |
| 280 | Sample(self.name + '_count', dict(zip(self._labelnames, labels)), buckets[-1][1], timestamp)) |
| 281 | self.samples.append( |
no outgoing calls