-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for kube_scheduler SLI metrics (#15731)
* Create scraper for SLI metrics * Add sli metrics test fixture * Capture sli kubernetes_healthcheck and healthchecks_total metrics * Add unit tests for detecting sli endpoint logic * Remove unused urljoin helper function * Add new metrics to metadata * Add changelog entry * Fix logic for checking if sli endpoint is available * Add check to prevent querying metrics if url unavailable * Use general instance fixture * Use get_here helper function to get current directory * Use format instead of f string for python 2 support * Remove custom tag from default test instance fixture
- Loading branch information
Showing
7 changed files
with
179 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
kube_scheduler/datadog_checks/kube_scheduler/sli_metrics.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# (C) Datadog, Inc. 2023-present | ||
# All rights reserved | ||
# Licensed under a 3-clause BSD style license (see LICENSE) | ||
from __future__ import division | ||
|
||
from copy import deepcopy | ||
|
||
SLI_METRICS_PATH = '/slis' | ||
|
||
SLI_GAUGES = { | ||
'kubernetes_healthcheck': 'kubernetes_healthcheck', | ||
} | ||
|
||
SLI_COUNTERS = { | ||
'kubernetes_healthchecks_total': 'kubernetes_healthchecks_total', | ||
} | ||
|
||
|
||
class SliMetricsScraperMixin(object): | ||
""" | ||
This class scrapes metrics for the kube scheduler "/metrics/sli" prometheus endpoint and submits | ||
them on behalf of a check. | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(SliMetricsScraperMixin, self).__init__(*args, **kwargs) | ||
self._slis_available = None | ||
|
||
def create_sli_prometheus_instance(self, instance): | ||
""" | ||
Create a copy of the instance and set default values. | ||
This is so the base class can create a scraper_config with the proper values. | ||
""" | ||
KUBE_SCHEDULER_SLI_NAMESPACE = "kube_scheduler.slis" | ||
|
||
sli_instance = deepcopy(instance) | ||
sli_instance.update( | ||
{ | ||
'namespace': KUBE_SCHEDULER_SLI_NAMESPACE, | ||
'prometheus_url': instance.get('prometheus_url') + SLI_METRICS_PATH, | ||
'metrics': [SLI_GAUGES, SLI_COUNTERS], | ||
} | ||
) | ||
return sli_instance | ||
|
||
def detect_sli_endpoint(self, http_handler, url): | ||
""" | ||
Whether the sli metrics endpoint is available (k8s 1.26+). | ||
:return: false if the endpoint throws a 404 or 403, true otherwise. | ||
""" | ||
if self._slis_available is not None: | ||
return self._slis_available | ||
try: | ||
r = http_handler.head(url) | ||
except Exception as e: | ||
self.log.debug("Error querying SLIs endpoint: %s", e) | ||
return False | ||
if r.status_code == 403: | ||
self.log.debug( | ||
"The /metrics/slis endpoint was introduced in Kubernetes v1.26. If you expect to see SLI metrics, \ | ||
please check that your permissions are configured properly." | ||
) | ||
self._slis_available = r.status_code != 404 and r.status_code != 403 | ||
return self._slis_available |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# HELP kubernetes_healthcheck [ALPHA] This metric records the result of a single healthcheck. | ||
# TYPE kubernetes_healthcheck gauge | ||
kubernetes_healthcheck{name="ping",type="healthz"} 1 | ||
# HELP kubernetes_healthchecks_total [ALPHA] This metric records the results of all healthcheck. | ||
# TYPE kubernetes_healthchecks_total counter | ||
kubernetes_healthchecks_total{name="ping",status="success",type="healthz"} 2450 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# (C) Datadog, Inc. 2023-present | ||
# All rights reserved | ||
# Licensed under a 3-clause BSD style license (see LICENSE) | ||
|
||
import os | ||
|
||
import mock | ||
import pytest | ||
import requests | ||
import requests_mock | ||
|
||
from datadog_checks.kube_scheduler import KubeSchedulerCheck | ||
|
||
from .common import HERE | ||
|
||
# Constants | ||
CHECK_NAME = 'kube_scheduler' | ||
|
||
|
||
@pytest.fixture() | ||
def mock_metrics(): | ||
f_name = os.path.join(HERE, 'fixtures', 'metrics_slis_1.27.3.txt') | ||
with open(f_name, 'r') as f: | ||
text_data = f.read() | ||
with mock.patch( | ||
'requests.get', | ||
return_value=mock.MagicMock( | ||
status_code=200, iter_lines=lambda **kwargs: text_data.split("\n"), headers={'Content-Type': "text/plain"} | ||
), | ||
): | ||
yield | ||
|
||
|
||
def test_check_metrics_slis(aggregator, mock_metrics, mock_request, instance): | ||
mock_request.head('http://localhost:10251/metrics/slis', status_code=200) | ||
c = KubeSchedulerCheck(CHECK_NAME, {}, [instance]) | ||
c.check(instance) | ||
|
||
def assert_metric(name, **kwargs): | ||
# Wrapper to keep assertions < 120 chars | ||
aggregator.assert_metric("{}.{}".format(CHECK_NAME, name), **kwargs) | ||
|
||
assert_metric('slis.kubernetes_healthcheck', value=1, tags=['name:ping', 'type:healthz']) | ||
assert_metric( | ||
'slis.kubernetes_healthchecks_total', | ||
value=2450, | ||
tags=['name:ping', 'status:success', 'type:healthz'], | ||
) | ||
|
||
aggregator.assert_all_metrics_covered() | ||
|
||
|
||
@pytest.fixture() | ||
def mock_request(): | ||
with requests_mock.Mocker() as m: | ||
yield m | ||
|
||
|
||
def test_detect_sli_endpoint(mock_metrics, mock_request, instance): | ||
mock_request.head('http://localhost:10251/metrics/slis', status_code=200) | ||
c = KubeSchedulerCheck(CHECK_NAME, {}, [instance]) | ||
c.check(instance) | ||
assert c._slis_available is True | ||
assert mock_request.call_count == 1 | ||
|
||
|
||
def test_detect_sli_endpoint_404(mock_metrics, mock_request, instance): | ||
mock_request.head('http://localhost:10251/metrics/slis', status_code=404) | ||
c = KubeSchedulerCheck(CHECK_NAME, {}, [instance]) | ||
c.check(instance) | ||
assert c._slis_available is False | ||
assert mock_request.call_count == 1 | ||
|
||
|
||
def test_detect_sli_endpoint_403(mock_metrics, mock_request, instance): | ||
mock_request.head('http://localhost:10251/metrics/slis', status_code=403) | ||
c = KubeSchedulerCheck(CHECK_NAME, {}, [instance]) | ||
c.check(instance) | ||
assert c._slis_available is False | ||
assert mock_request.call_count == 1 | ||
|
||
|
||
def test_detect_sli_endpoint_timeout(mock_metrics, mock_request, instance): | ||
mock_request.head('http://localhost:10251/metrics/slis', exc=requests.exceptions.ConnectTimeout) | ||
c = KubeSchedulerCheck(CHECK_NAME, {}, [instance]) | ||
c.check(instance) | ||
assert c._slis_available is None | ||
assert mock_request.call_count == 1 |