forked from pallavidn/monitoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus_queries.go
59 lines (52 loc) · 2.83 KB
/
prometheus_queries.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package prometheus
import (
"fmt"
"github.com/turbonomic/turbo-go-monitoring/pkg/model"
"github.com/turbonomic/turbo-go-monitoring/pkg/model/entity"
"github.com/turbonomic/turbo-go-monitoring/pkg/model/property"
"github.com/turbonomic/turbo-go-monitoring/pkg/model/resource"
prometheusModel "github.com/prometheus/common/model"
"strings"
)
// Query defines the data type of Prometheus query strings
type Query struct {
queryString string
entityId func(sample *prometheusModel.Sample) (model.EntityId, error)
}
// MetricQueryMap defines for each given MetricKey which Prometheus query to use to collect the metric data
var MetricQueryMap = map[model.MetricKey]Query{
//
// TODO: Revisit Node CPU stats more carefully -
// Do we need per-core stats, or user vs system vs iowait vs steal stats?
//
{entity.NODE, resource.CPU, property.USED}: {"100 * (sum(delta(node_cpu{mode!='idle'}[10m])) by (job, instance) / sum(delta(node_cpu[10m])) by (job, instance))", fromInstanceName},
{entity.NODE, resource.MEM, property.USED}: {"node_memory_Active", fromInstanceName},
{entity.NODE, resource.MEM, property.CAP}: {"node_memory_MemTotal", fromInstanceName},
{entity.NODE, resource.MEM, property.AVERAGE}: {"avg_over_time(node_memory_Active[10m])", fromInstanceName},
{entity.NODE, resource.MEM, property.PEAK}: {"max_over_time(node_memory_Active[10m])", fromInstanceName},
//
// TODO: Handle multiple interfaces per node, transmit and receive
//
{entity.NODE, resource.NETWORK, property.USED}: {"sum(rate(node_network_receive_bytes[10m]) + rate(node_network_transmit_bytes[10m])) by (job, instance)", fromInstanceName},
{entity.POD, resource.MEM, property.USED}: {"sum(container_memory_usage_bytes{image!=\"\"}) by (pod_name)", fromPodName},
{entity.POD, resource.CPU, property.USED}: {"sum(rate(container_cpu_usage_seconds_total{image!=\"\"}[1m])) by (pod_name)", fromPodName},
{entity.POD, resource.DISK, property.USED}: {"sum(container_fs_usage_bytes{image!=\"\"}) by (pod_name)", fromPodName},
}
// fromInstanceName() extracts the entity id from the instance field of the given Prometheus metric sample.
func fromInstanceName(sample *prometheusModel.Sample) (model.EntityId, error) {
instanceName := string(sample.Metric["instance"])
if instanceName == "" {
return "", fmt.Errorf("Instance is missing in the returned metric: %v", sample.Metric)
}
entityId := model.EntityId(strings.Split(instanceName, ":")[0])
return entityId, nil
}
// fromPodName() extracts the entity id from the pod name field of the given Prometheus metric sample.
func fromPodName(sample *prometheusModel.Sample) (model.EntityId, error) {
podName := string(sample.Metric["pod_name"])
if podName == "" {
return "", fmt.Errorf("Pod name is missing in the returned metric: %v", sample.Metric)
}
entityId := model.EntityId(podName)
return entityId, nil
}