A single gauge histogram and its samples. For use by custom collectors.
| 283 | |
| 284 | |
| 285 | class GaugeHistogramMetricFamily(Metric): |
| 286 | """A single gauge histogram and its samples. |
| 287 | |
| 288 | For use by custom collectors. |
| 289 | """ |
| 290 | |
| 291 | def __init__(self, |
| 292 | name: str, |
| 293 | documentation: str, |
| 294 | buckets: Optional[Sequence[Tuple[str, float]]] = None, |
| 295 | gsum_value: Optional[float] = None, |
| 296 | labels: Optional[Sequence[str]] = None, |
| 297 | unit: str = '', |
| 298 | ): |
| 299 | Metric.__init__(self, name, documentation, 'gaugehistogram', unit) |
| 300 | if labels is not None and buckets is not None: |
| 301 | raise ValueError('Can only specify at most one of buckets and labels.') |
| 302 | if labels is None: |
| 303 | labels = [] |
| 304 | self._labelnames = tuple(labels) |
| 305 | if buckets is not None: |
| 306 | self.add_metric([], buckets, gsum_value) |
| 307 | |
| 308 | def add_metric(self, |
| 309 | labels: Sequence[str], |
| 310 | buckets: Sequence[Tuple[str, float]], |
| 311 | gsum_value: Optional[float], |
| 312 | timestamp: Optional[Union[float, Timestamp]] = None, |
| 313 | ) -> None: |
| 314 | """Add a metric to the metric family. |
| 315 | |
| 316 | Args: |
| 317 | labels: A list of label values |
| 318 | buckets: A list of pairs of bucket names and values. |
| 319 | The buckets must be sorted, and +Inf present. |
| 320 | gsum_value: The sum value of the metric. |
| 321 | """ |
| 322 | for bucket, value in buckets: |
| 323 | self.samples.append(Sample( |
| 324 | self.name + '_bucket', |
| 325 | dict(list(zip(self._labelnames, labels)) + [('le', bucket)]), |
| 326 | value, timestamp)) |
| 327 | # +Inf is last and provides the count value. |
| 328 | self.samples.extend([ |
| 329 | Sample(self.name + '_gcount', dict(zip(self._labelnames, labels)), buckets[-1][1], timestamp), |
| 330 | # TODO: Handle None gsum_value correctly. Currently a None will fail exposition but is allowed here. |
| 331 | Sample(self.name + '_gsum', dict(zip(self._labelnames, labels)), gsum_value, timestamp), # type: ignore |
| 332 | ]) |
| 333 | |
| 334 | |
| 335 | class InfoMetricFamily(Metric): |
no outgoing calls