[k8s] Horizontal Pod Autoscalers - matchExpressions
Describe what happened:
See https://github.com/DataDog/datadog-agent/issues/2541#issuecomment-489022566
Describe what you expected:
I'd expect that matchLabels and matchExpressions are handled similarly.
Steps to reproduce the issue:
Create a Horizontal Pod Autoscaler with matchExpressions under metricSelector.
Additional environment details (Operating System, Cloud provider, etc):
Kubernetes v1.11.8-gke.6 on GKE with HPA apiVersion autoscaling/v2beta1.
Not sure if this relates, but when using autoscaling/v2beta2, the format for External metrics blocks seems to change entirely:
- type: External
external:
metric:
name: aws.sqs.approximate_age_of_oldest_message
selector: "app=whatever"
target:
type: AverageValue
averageValue: 20
Can we get an update as to what this should look like? Specifically, I'm having a lot of problems with the metric.selector value - is this supposed to match a label from within Datadog, or is this a label within k8s as a selector?
For context:
> kubectl explain --api-version='autoscaling/v2beta2' hpa.spec.metrics.external.metric
KIND: HorizontalPodAutoscaler
VERSION: autoscaling/v2beta2
RESOURCE: metric <Object>
DESCRIPTION:
metric identifies the target metric by name and selector
MetricIdentifier defines the name and optionally selector for a metric
FIELDS:
name <string> -required-
name is the name of the given metric
selector <Object>
selector is the string-encoded form of a standard kubernetes label selector
for the given metric When set, it is passed as an additional parameter to
the metrics server for more specific metrics scoping. When unset, just the
metricName will be used to gather metrics.
If you simply add a value like "queue=worker_tasks" per the example given here: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#autoscaling-on-metrics-not-related-to-kubernetes-objects - you end up with an error:
Error: UPGRADE FAILED: v2beta2.HorizontalPodAutoscaler.Spec: v2beta2.HorizontalPodAutoscalerSpec.Metrics: []v2beta2.MetricSpec: v2beta2.MetricSpec.External: v2beta2.ExternalMetricSource.Metric: v2beta2.MetricIdentifier.Selector: readObjectStart: expect { or n, but found ", error found in #10 byte of ...|elector":"app=foobar|..., bigger context ...|qs.approximate_age_of_oldest_message","selector":"app=rise-foobar"},"target":{"averageValue":20,"t|...
Hey all,
Thank you very much for bringing this up to our attention. I am putting this in our backlog, I don't have a clear answer just yet but we're going to investigate and will get back to you with a path forward.
Thanks for your patience. Best, .C
Just as an update, we found that this worked best for us:
- type: External
external:
metric:
name: aws.whatever.metric
selector:
matchLabels:
foo: "bar"
target:
type: AverageValue
averageValue: 20
The idea here is that you can pass in however many matchLabels you want that correspond with Datadog tags. This passes validation and seems to format properly in the hpa. We still can't pull the metrics in to the cluster agents, but we're still looking into that.
@echoboomer I've arrived at the same place as you (via a different route!) using autoscaling/v2beta2 and am now at a state where the datadog-cluster-agent is aware of the HPA metrics e.g.
Custom Metrics Server
=====================
ConfigMap name: kube-system/datadog-custom-metrics
External Metrics
----------------
Total: 1
Valid: 0
hpa:
- name: test
- namespace: default
- uid: 536a6ade-a324-11e9-850a-0636ed6aea6c
labels:
- foo: bar
metricName: aws.foo.bar
ts: 1.562772738e+09
valid: false
value: 0
but I'm getting no metrics returned from external metrics API errors from the HPA. Have you managed to resolve this?
@echoboomer Would that work in case I want to filter by both foo: bar and foo: baz? Note that the key to the dictionary of labels is the same. Would that be equivalent to foo in [bar, baz], because that's the idea behind this issue?
@pchristos I think that section, unless someone else can correct me, corresponds to Datadog labels. For example, if we have labels in Datadog for env and app, I may want to do something like:
selector:
matchLabels:
env: "stage"
app: "myapp"
This way, the metric itself is calculated based on those specs. Hopefully that makes sense. This is what I've been able to figure out from reading docs, talking to others who use this, and experimentation.
@echoboomer That makes sense. But, would the following also work?
selector:
matchLabels:
foo: "bar"
foo: "baz"
For instance, a real-world scenario would be to be able to scale based on the cumulative length of multiple RabbitMQ queues, eg. if the length of queue A and queue B combined is above X, scale up.
@homme After opening a ticket with Datadog support, it turned out that the metrics I was trying to scale on were not working because they had a roughly 15 minute delay making it into Datadog. The cluster agent apparently queries the back end with a 5 minute rolling period (called a bucket) and thus the metric I was using was considered "invalid." I increased the "bucket_size" to a higher period but that did not resolve my issue. This is on the back burner for us now since we stopped trying to scale on things that had such a delay.
Edited my previous comment.
@pchristos Makes sense. I just went into the Datadog console and tried to filter metrics based on the same tag twice, like env, and it didn't let me use duplicates. I'm not sure if that's a possibility.
Running into a similar issue here; can't seem to filter DataDog based on tags, but it seems to be coming through into the cluster-agent:
- type: External
external:
metricName: kafka.consumer.partition_lag
metricSelector:
matchLabels:
cluster: "prod"
targetAverageValue: 70000
and here is what cluster-agent status tosses out:
External Metrics
---------------
Total: 1
Valid: 1
hpa:
- name: nginxext
- namespace: default
- uid: 13cee97a-a99d-11e9-baf6-0ad3a1ef891c
labels:
- cluster: prod
metricName: kafka.consumer.partition_lag
ts: 1.56348279e+09
valid: true
value: 78856.52485714285
Warning FailedGetExternalMetric 2m11s horizontal-pod-autoscaler unable to get external metric default/kafka.consumer.partition_lag/&LabelSelector{MatchLabels:map[string]string{cluster: prod,},MatchExpressions:[],}: no metrics returned from external metrics API
Warning FailedComputeMetricsReplicas 2m11s horizontal-pod-autoscaler failed to get kafka.consumer.partition_lag external metric: unable to get external metric default/kafka.consumer.partition_lag/&LabelSelector{MatchLabels:map[string]string{cluster: prod,},MatchExpressions:[],}: no metrics returned from external metrics API
Unfortunately I don't see any documentation anywhere on how to filter based on DD tags-- so if anybody has input it would be appreciated.
Edit: Sorry; formatting got a little out of hand here.
Hi @pchristos This:
selector:
matchLabels:
foo: "bar"
foo: "baz"
Doesn't work in our UI as pointed out by @echoboomer. So it won't be a valid query for our API.
For now, the same metric added multiple times with different tags in the same HPA is not supported by the DCA. One way to do it would be to add a specific tag to the queues you want to base your HPA on, so you could scope on this specific tag. If it doesn't work for you, feel free to open feature request to our solution team via a support ticket.
@kencieszykowski for your case, it seems that the issue is between the HPA and the Cluster Agent. Not on how the cluster agent is interpreting the tags. (it seems to work correctly in your case, by looking at the cluster agent status)
Do you happen to have the env var DD_LEADER_ELECTION set to true on the node agents?
If should not be set on the node agent, only on the cluster agent.
Also, make sure that the correct RBACs and service have been deployed: https://docs.datadoghq.com/agent/kubernetes/cluster/#custom-metrics-server
Feel free to open a support ticket if the steps I mentioned don't solve the issue.
Simon
So, to make this clear, at the moment, the only way around:
- type: External
external:
metricName: rabbitmq.queue.messages_ready
metricSelector:
matchExpressions:
- {key: rabbitmq_queue, operator: In, values: [a, b]}
targetAverageValue: 10
is something similar to what's being described here?
@pchristos if the queues share the same tag queue_family, you can use this tag to scope your query on several queues.
By default, the query will be forged with an average aggregator. Your use-case would need to use this env var to use a sum aggregator:
DD_EXTERNAL_METRICS_AGGREGATOR=sum
Ref: https://docs.datadoghq.com/agent/kubernetes/cluster/#cluster-agent-options
This issue has been automatically marked as stale because it has not had activity in the past 15 days.
It will be closed in 30 days if no further activity occurs. If this issue is still relevant, adding a comment will keep it open. Also, you can always reopen the issue if you missed the window.
Thank you for your contributions!
Hi @pchristos 👋 — thanks again for taking the time to report this issue.
We're closing this due to inactivity: it's been over 6 months since the last update, and the stale bot's check-in didn't receive a response. This isn't a reflection of the value of the contribution — it's purely a hygiene pass to keep the backlog actionable.
If this is still relevant to you:
- Feel free to reopen this issue or open a new one with updated reproduction steps
- If the underlying issue is still present, a fresh report against the current version will get faster attention
Thanks for your patience and for contributing to the Datadog Agent! 🙏