sumValues calculates the sum of all metrics in the metricFamily that contain the given labels. filterLabels must be key-value pairs.
(metricFamily map[string]*io_prometheus_client.MetricFamily, metric string, filterLabels []string)
| 399 | // sumValues calculates the sum of all metrics in the metricFamily that contain the given labels. |
| 400 | // filterLabels must be key-value pairs. |
| 401 | func sumValues(metricFamily map[string]*io_prometheus_client.MetricFamily, metric string, filterLabels []string) float64 { |
| 402 | if len(filterLabels)%2 != 0 { |
| 403 | panic(fmt.Sprintf("filterLabels must be pairs: %v", filterLabels)) |
| 404 | } |
| 405 | filterLabelsMap := map[string]string{} |
| 406 | for i := 0; i < len(filterLabels); i += 2 { |
| 407 | filterLabelsMap[filterLabels[i]] = filterLabels[i+1] |
| 408 | } |
| 409 | |
| 410 | sum := 0.0 |
| 411 | |
| 412 | outer: |
| 413 | for _, metric := range metricFamily[metric].GetMetric() { |
| 414 | labelMap := map[string]string{} |
| 415 | for _, label := range metric.GetLabel() { |
| 416 | labelMap[label.GetName()] = label.GetValue() |
| 417 | } |
| 418 | |
| 419 | for key, expectedValue := range filterLabelsMap { |
| 420 | value, ok := labelMap[key] |
| 421 | if !ok || value != expectedValue { |
| 422 | continue outer |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // since we fetch metrics using /federate they are all untyped |
| 427 | sum += metric.GetUntyped().GetValue() |
| 428 | } |
| 429 | |
| 430 | return sum |
| 431 | } |
| 432 | |
| 433 | func stringPtr(s string) *string { |
| 434 | return &s |
no test coverage detected