def _os(self) -> Iterator[Metric]: os_dict = self._metrics_dict["os"] yield from ( TimestampGaugeMetricFamily( "kibana_os_load_%s" % key, "Kibana OS load %s" % key, value=value, timestamp=self._timestamp ) for key, value in os_dict["load"].items() ) yield from ( TimestampGaugeMetricFamily( "kibana_os_memory_%s_bytes" % key.split("_")[0], "Kibana %s OS memory" % key.split("_")[0], value=value, timestamp=self._timestamp, ) for key, value in os_dict["memory"].items() ) yield TimestampCounterMetricFamily( "kibana_os_uptime_seconds", "Kibana OS uptime in seconds", value=os_dict["uptime_in_millis"] / 1000, timestamp=self._timestamp, )
def test_timestamp_metric_family_keeps_timestamp_from_add_metric(self): timestamp = 214 t = TimestampGaugeMetricFamily("some_name", "some description", timestamp=timestamp) t.add_metric(labels=[], value=123, timestamp=234) self.assertEqual(t.samples[0].timestamp, 234)
def test_timestamp_metric_family_sets_timestamp_on_metrics_implicit_metrics( self): timestamp = 214 t = TimestampGaugeMetricFamily("some_name", "some description", timestamp=timestamp, value=2) self.assertEqual(t.samples[0].timestamp, timestamp)
def _response_times(self) -> Iterator[Metric]: rt_dict = self._metrics_dict["response_times"] yield TimestampGaugeMetricFamily( "kibana_response_time_max_seconds", "Kibana maximum response time in seconds", value=rt_dict["max_in_millis"] / 1000, timestamp=self._timestamp, ) # Kibana statistics lib can sometimes return NaN for this value. # If that is the case, this is set to 0 in order to avoid gaps in the time series. # Reference: https://github.com/elastic/kibana/blob/6.7/src/server/status/lib/metrics.js#L73 # NaN is converted to `undefined` which then has the whole field removed from the response JSON yield TimestampGaugeMetricFamily( "kibana_response_time_avg_seconds", "Kibana average response time in seconds", value=rt_dict.setdefault("avg_in_millis", 0) / 1000, timestamp=self._timestamp, )
def _process(self) -> Iterator[Metric]: process_dict = self._metrics_dict["process"] yield TimestampGaugeMetricFamily( "kibana_process_memory_heap_total_bytes", "Total heap size in bytes", value=process_dict["memory"]["heap"]["total_in_bytes"], timestamp=self._timestamp, ) yield TimestampGaugeMetricFamily( "kibana_process_memory_heap_used_bytes", "Used heap size in bytes", value=process_dict["memory"]["heap"]["used_in_bytes"], timestamp=self._timestamp, ) yield TimestampGaugeMetricFamily( "kibana_process_memory_heap_size_limit_bytes", "Heap size limit in bytes", value=process_dict["memory"]["heap"]["size_limit"], timestamp=self._timestamp, ) yield TimestampGaugeMetricFamily( "kibana_process_memory_resident_set_size_bytes", "Memory resident set size", value=process_dict["memory"]["resident_set_size_in_bytes"], timestamp=self._timestamp, ) yield TimestampCounterMetricFamily( "kibana_process_uptime_seconds", "Kibana process uptime in seconds", value=process_dict["uptime_in_millis"] / 1000, timestamp=self._timestamp, )
def _requests(self) -> Iterator[Metric]: req_dict = self._metrics_dict["requests"] yield TimestampGaugeMetricFamily( "kibana_requests_total", "Total requests serviced", value=req_dict["total"], timestamp=self._timestamp ) yield TimestampGaugeMetricFamily( "kibana_requests_disconnects", "Total requests disconnected", value=req_dict["disconnects"], timestamp=self._timestamp, ) per_status = TimestampGaugeMetricFamily( "kibana_requests", "Total requests by status code", labels=["status_code"], timestamp=self._timestamp ) for code, count in req_dict["status_codes"].items(): per_status.add_metric(labels=[code], value=count) yield per_status
def test_timestamp_metric_family_has_timestamp_if_set(self): timestamp = 214 t = TimestampGaugeMetricFamily("some_name", "some description", timestamp=timestamp) self.assertEqual(t._timestamp, timestamp)
def test_timestamp_metric_family_has_none_timestamp_if_unset(self): t = TimestampGaugeMetricFamily("some_name", "some description") self.assertIsNone(t._timestamp)