Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce join_standard_tags setting #6253

Merged
merged 4 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def _create_kubernetes_state_prometheus_instance(self, instance):
extra_labels = ksm_instance.get('label_joins', {})
hostname_override = is_affirmative(ksm_instance.get('hostname_override', True))
join_kube_labels = is_affirmative(ksm_instance.get('join_kube_labels', False))
join_standard_tags = is_affirmative(ksm_instance.get('join_standard_tags', False))

ksm_instance.update(
{
Expand Down Expand Up @@ -370,6 +371,76 @@ def _create_kubernetes_state_prometheus_instance(self, instance):
}
)

if join_standard_tags:
ksm_instance['label_joins'].update(
{
"kube_pod_labels": {
"labels_to_match": ["pod", "namespace"],
"labels_to_get": [
"label_tags_datadoghq_com_env",
"label_tags_datadoghq_com_service",
"label_tags_datadoghq_com_version"
]
},
"kube_deployment_labels": {
"labels_to_match": ["deployment", "namespace"],
"labels_to_get": [
"label_tags_datadoghq_com_env",
"label_tags_datadoghq_com_service",
"label_tags_datadoghq_com_version"
]
},
"kube_replicaset_labels": {
"labels_to_match": ["replicaset", "namespace"],
"labels_to_get": [
"label_tags_datadoghq_com_env",
"label_tags_datadoghq_com_service",
"label_tags_datadoghq_com_version"
]
},
"kube_daemonset_labels": {
"labels_to_match": ["daemonset", "namespace"],
"labels_to_get": [
"label_tags_datadoghq_com_env",
"label_tags_datadoghq_com_service",
"label_tags_datadoghq_com_version"
]
},
"kube_statefulset_labels": {
"labels_to_match": ["statefulset", "namespace"],
"labels_to_get": [
"label_tags_datadoghq_com_env",
"label_tags_datadoghq_com_service",
"label_tags_datadoghq_com_version"
]
},
"kube_job_labels": {
"labels_to_match": ["job", "namespace"],
"labels_to_get": [
"label_tags_datadoghq_com_env",
"label_tags_datadoghq_com_service",
"label_tags_datadoghq_com_version"
]
},
"kube_cronjob_labels": {
"labels_to_match": ["cronjob", "namespace"],
"labels_to_get": [
"label_tags_datadoghq_com_env",
"label_tags_datadoghq_com_service",
"label_tags_datadoghq_com_version"
]
},
}
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we declare these as global variables and refactor them a bit? we're duplicating labels_to_get

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point, will refactor, thank you!


ksm_instance.setdefault("labels_mapper", {}).update(
{
"label_tags_datadoghq_com_env": "env",
"label_tags_datadoghq_com_service": "service",
"label_tags_datadoghq_com_version": "version"
}
)

ksm_instance['label_joins'].update(extra_labels)
if hostname_override:
ksm_instance['label_to_hostname'] = 'node'
Expand Down
4 changes: 2 additions & 2 deletions kubernetes_state/tests/fixtures/prometheus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ kube_deployment_created{deployment="tiller-deploy",namespace="kube-system"} 1.50
# HELP kube_deployment_labels Kubernetes labels converted to Prometheus labels.
# TYPE kube_deployment_labels gauge
kube_deployment_labels{deployment="failingtest",label_app="failingtest",namespace="default"} 1
kube_deployment_labels{deployment="tiller-deploy",label_app="helm",label_name="tiller",namespace="kube-system"} 1
kube_deployment_labels{deployment="tiller-deploy",label_tags_datadoghq_com_env="dev",label_tags_datadoghq_com_service="tiller",label_tags_datadoghq_com_version="xyz",label_app="helm",label_name="tiller",namespace="kube-system"} 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the tests, could you please cover all the targeted objects in this PR not only deployments and pods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will add those!

kube_deployment_labels{deployment="kube-dns",label_addonmanager_kubernetes_io_mode="Reconcile",label_k8s_app="kube-dns",label_version="v20",namespace="kube-system"} 1
kube_deployment_labels{deployment="jaundiced-numbat-kube-state-metrics",label_app="kube-state-metrics",label_chart="kube-state-metrics-0.3.1",label_heritage="Tiller",label_release="jaundiced-numbat",namespace="default"} 1
# HELP kube_deployment_metadata_generation Sequence number representing a specific generation of the desired state.
Expand Down Expand Up @@ -526,7 +526,7 @@ kube_pod_labels{label_controller_uid="f2d4c3f7-c32c-11e7-a7e7-080027bdd864",labe
kube_pod_labels{label_app="failingtest",label_pod_template_hash="91416680",namespace="default",pod="failingtest-f585bbd4-2fsml"} 1
kube_pod_labels{label_k8s_app="kube-dns",label_pod_template_hash="1326421443",namespace="kube-system",pod="kube-dns-1326421443-hj4hx"} 1
kube_pod_labels{label_addonmanager_kubernetes_io_mode="Reconcile",label_app="kubernetes-dashboard",label_version="v1.6.3",namespace="kube-system",pod="kubernetes-dashboard-zhtjl"} 1
kube_pod_labels{label_addonmanager_kubernetes_io_mode="Reconcile",label_name="registry-creds",label_version="v1.8",namespace="kube-system",pod="registry-creds-hq249"} 1
kube_pod_labels{label_addonmanager_kubernetes_io_mode="Reconcile",label_tags_datadoghq_com_env="dev",label_tags_datadoghq_com_service="registry-creds",label_tags_datadoghq_com_version="v1.8",label_name="registry-creds",label_version="v1.8",namespace="kube-system",pod="registry-creds-hq249"} 1
kube_pod_labels{label_app="helm",label_name="tiller",label_pod_template_hash="1651615695",namespace="kube-system",pod="tiller-deploy-1651615695-dcphn"} 1
kube_pod_labels{label_app="jaundiced-numbat-dd-k8state",label_controller_revision_hash="3690254688",label_pod_template_generation="1",namespace="default",pod="jaundiced-numbat-dd-k8state-b6s77"} 1
kube_pod_labels{label_component="kube-addon-manager",label_kubernetes_io_minikube_addons="addon-manager",label_version="v6.4",namespace="kube-system",pod="kube-addon-manager-minikube"} 1
Expand Down
49 changes: 48 additions & 1 deletion kubernetes_state/tests/test_kubernetes_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ def check_with_join_kube_labels(instance):
return _check(instance)


@pytest.fixture
def check_with_join_standard_tag_labels(instance):
instance['join_standard_tags'] = True
return _check(instance)


def assert_not_all_zeroes(aggregator, metric_name):
for m in aggregator.metrics(metric_name):
if m.value != 0:
Expand Down Expand Up @@ -417,6 +423,47 @@ def test_join_kube_labels(aggregator, instance, check_with_join_kube_labels):
)


def test_join_standard_tags_labels(aggregator, instance, check_with_join_standard_tag_labels):
# run check twice to have pod/node mapping
for _ in range(2):
check_with_join_standard_tag_labels.check(instance)

aggregator.assert_metric(
NAMESPACE + '.container.waiting',
tags=[
'container:registry-creds',
'kube_container_name:registry-creds',
'kube_namespace:kube-system',
'namespace:kube-system',
'node:minikube',
'optional:tag1',
'phase:pending',
'pod:registry-creds-hq249',
'pod_name:registry-creds-hq249',
'pod_phase:pending',
"env:dev",
"service:registry-creds",
"version:v1.8"
],
value=1,
)

aggregator.assert_metric(
NAMESPACE + '.deployment.replicas',
tags=[
'deployment:tiller-deploy',
'kube_deployment:tiller-deploy',
'kube_namespace:kube-system',
'namespace:kube-system',
'optional:tag1',
"env:dev",
"service:tiller",
"version:xyz"
],
value=1,
)


def test_join_custom_labels(aggregator, instance, check):
instance['label_joins'] = {
'kube_deployment_labels': {
Expand Down Expand Up @@ -656,7 +703,7 @@ def test_telemetry(aggregator, instance):

for _ in range(2):
check.check(instance)
aggregator.assert_metric(NAMESPACE + '.telemetry.payload.size', tags=['optional:tag1'], value=90707.0)
aggregator.assert_metric(NAMESPACE + '.telemetry.payload.size', tags=['optional:tag1'], value=90948.0)
aggregator.assert_metric(NAMESPACE + '.telemetry.metrics.processed.count', tags=['optional:tag1'], value=914.0)
aggregator.assert_metric(NAMESPACE + '.telemetry.metrics.input.count', tags=['optional:tag1'], value=1288.0)
aggregator.assert_metric(NAMESPACE + '.telemetry.metrics.blacklist.count', tags=['optional:tag1'], value=24.0)
Expand Down