<p>Is it possible to aggregate 2 gauge metrics (i.e. <code>kube_pod_labels</code> and <code>kube_pod_container_resource_requests_cpu_cores</code>) so that by executing the query both resulting elements would be combined (i.e. all pod labels as resulting element and request_cpu_cores as value)?</p>
<p>Query for cpu request value looks like this <code>sum by (namespace, pod) (kube_pod_container_resource_requests_cpu_cores{cluster=&quot;my-cluster&quot;})</code></p>
<p>Actual result:</p>
<pre><code>{namespace=&quot;my-ns&quot;,pod=&quot;my-pod&quot;} 0.05
</code></pre>
<p>Query for pod labels is <code>kube_pod_labels{label_foo=&quot;bar&quot;}</code></p>
<p>Actual result:</p>
<pre><code>kube_pod_labels{cluster=&quot;my-cluster&quot;,label_foo=&quot;bar&quot;,label_app=&quot;my-app-label&quot;,namespace=&quot;my-ns&quot;,pod=&quot;my-pod&quot;,service=&quot;my-svc&quot;} 1
</code></pre>
<hr />
<p>I have tried using a left joint but it seems that grouping by a given label (pod, namespace etc) is required as explained in this <a href="https://www.robustperception.io/left-joins-in-promql" rel="nofollow noreferrer">https://www.robustperception.io/left-joins-in-promql</a>.</p>
<p>With a <code>multiplication</code> operator <code>*</code> is possible to obtain the desired result set but the set would only contain labels specified in the <code>by</code> clause. Example query:</p>
<pre><code>group by (namespace,pod) (kube_pod_labels{label_foo=&quot;bar&quot;,cluster=&quot;my-cluster&quot;}) * sum by (namespace, pod) (kube_pod_container_resource_requests_cpu_cores{cluster=&quot;my-cluster&quot;})
</code></pre>
<p>Example result:</p>
<pre><code>{namespace=&quot;my-ns&quot;,pod=&quot;my-pod&quot;}    0.05
</code></pre>
<hr />
<p>What I am trying to obtain is a resulting set containing all labels without having to filter by an arbitrary label/value</p>
<p>The <strong>desired</strong> result by joining the 2 queries should be:</p>
<pre><code>{cluster=&quot;my-cluster&quot;,label_foo=&quot;bar&quot;, label_app=&quot;my-app-label&quot;,namespace=&quot;my-ns&quot;,pod=&quot;my-pod&quot;,service=&quot;my-svc&quot;} 0.05
</code></pre>

<p>This can be achieved with a combination of the following:</p>
<ul>
<li><p><code>label_replace</code> query function: For each timeseries in v, label_replace(v instant-vector, dst_label string, replacement string, src_label string, regex string) matches the regular expression regex against the value of the label src_label. If it matches, the value of the label dst_label in the returned timeseries will be the expansion of replacement, together with the original labels in the input. Capturing groups in the regular expression can be referenced with $1, $2, etc. If the regular expression doesn't match then the timeseries is returned unchanged. <a href="https://prometheus.io/docs/prometheus/latest/querying/functions/#label_replace" rel="nofollow noreferrer">https://prometheus.io/docs/prometheus/latest/querying/functions/#label_replace</a></p>
</li>
<li><p><code>multiplication *</code> operator and <code>group_left()</code> modifier: Many-to-one and one-to-many matchings refer to the case where each vector element on the &quot;one&quot;-side can match with multiple elements on the &quot;many&quot;-side. This has to be explicitly requested using the group_left or group_right modifier, where left/right determines which vector has the higher cardinality. <a href="https://prometheus.io/docs/prometheus/latest/querying/operators/" rel="nofollow noreferrer">https://prometheus.io/docs/prometheus/latest/querying/operators/</a></p>
</li>
</ul>
<p>Example query:</p>
<pre><code>label_replace(kube_pod_labels{},&quot;label&quot;,&quot;$1&quot;,&quot;label_&quot;, &quot;(.+)&quot;) 
* on (cluster,namespace, pod) group_left() 
(sum by (cluster,namespace, pod) (kube_pod_container_resource_requests_cpu_cores{}))
</code></pre>
<p>Note that: <code>If the regular expression doesn't match then the timeseries is returned unchanged</code>. In this case the regular expression does not match - hence the full set of labels is return unchanged.</p>
<p>Example result:</p>
<pre><code>{cluster=&quot;my-cluster&quot;,label_foo=&quot;bar&quot;, label_app=&quot;my-app-label&quot;,namespace=&quot;my-ns&quot;,pod=&quot;my-pod&quot;,service=&quot;my-svc&quot;} 0.05
</code></pre>
<hr />
<p>Felipe provided a valuable hint on how to achieve this result in a comment for the original question.</p>
