A single counter and its samples. For use by custom collectors.
| 100 | |
| 101 | |
| 102 | class CounterMetricFamily(Metric): |
| 103 | """A single counter and its samples. |
| 104 | |
| 105 | For use by custom collectors. |
| 106 | """ |
| 107 | |
| 108 | def __init__(self, |
| 109 | name: str, |
| 110 | documentation: str, |
| 111 | value: Optional[float] = None, |
| 112 | labels: Optional[Sequence[str]] = None, |
| 113 | created: Optional[float] = None, |
| 114 | unit: str = '', |
| 115 | exemplar: Optional[Exemplar] = None, |
| 116 | ): |
| 117 | # Glue code for pre-OpenMetrics metrics. |
| 118 | if name.endswith('_total'): |
| 119 | name = name[:-6] |
| 120 | Metric.__init__(self, name, documentation, 'counter', unit) |
| 121 | if labels is not None and value is not None: |
| 122 | raise ValueError('Can only specify at most one of value and labels.') |
| 123 | if labels is None: |
| 124 | labels = [] |
| 125 | self._labelnames = tuple(labels) |
| 126 | if value is not None: |
| 127 | self.add_metric([], value, created, exemplar=exemplar) |
| 128 | |
| 129 | def add_metric(self, |
| 130 | labels: Sequence[str], |
| 131 | value: float, |
| 132 | created: Optional[float] = None, |
| 133 | timestamp: Optional[Union[Timestamp, float]] = None, |
| 134 | exemplar: Optional[Exemplar] = None, |
| 135 | ) -> None: |
| 136 | """Add a metric to the metric family. |
| 137 | |
| 138 | Args: |
| 139 | labels: A list of label values |
| 140 | value: The value of the metric |
| 141 | created: Optional unix timestamp the child was created at. |
| 142 | """ |
| 143 | self.samples.append(Sample(self.name + '_total', dict(zip(self._labelnames, labels)), value, timestamp, exemplar)) |
| 144 | if created is not None: |
| 145 | self.samples.append(Sample(self.name + '_created', dict(zip(self._labelnames, labels)), created, timestamp)) |
| 146 | |
| 147 | |
| 148 | class GaugeMetricFamily(Metric): |
no outgoing calls