Johnny Metz
Johnny Metz

Reputation: 5975

Filter kubernetes file

I have the following file:

# HELP container_cpu_usage_seconds_total [ALPHA] Cumulative cpu time consumed by the container in core-seconds
# TYPE container_cpu_usage_seconds_total counter
container_cpu_usage_seconds_total{container="coredns",namespace="kube-system",pod="coredns-64897985d-qzvj8"} 1075.30302335 1641411355244
container_cpu_usage_seconds_total{container="etcd",namespace="kube-system",pod="etcd-minikube"} 7948.244422673 1641411341787
container_cpu_usage_seconds_total{container="kindnet-cni",namespace="kube-system",pod="kindnet-v9rn4"} 253.401092815 1641411342227
container_cpu_usage_seconds_total{container="kube-apiserver",namespace="kube-system",pod="kube-apiserver-minikube"} 21314.526032702 1641411341706
container_cpu_usage_seconds_total{container="kube-controller-manager",namespace="kube-system",pod="kube-controller-manager-minikube"} 9960.616171401 1641411346752
container_cpu_usage_seconds_total{container="kube-proxy",namespace="kube-system",pod="kube-proxy-ktclh"} 220.17024815 1641411352327
container_cpu_usage_seconds_total{container="kube-scheduler",namespace="kube-system",pod="kube-scheduler-minikube"} 1216.162832124 1641411355059
container_cpu_usage_seconds_total{container="metrics-server",namespace="kube-system",pod="metrics-server-6b76bd68b6-lpx4q"} 715.97119974 1641411344274
container_cpu_usage_seconds_total{container="storage-provisioner",namespace="kube-system",pod="storage-provisioner"} 47.685435216 1641411354429
# HELP container_memory_working_set_bytes [ALPHA] Current working set of the container in bytes
# TYPE container_memory_working_set_bytes gauge
container_memory_working_set_bytes{container="coredns",namespace="kube-system",pod="coredns-64897985d-qzvj8"} 1.5364096e+07 1641411355244
container_memory_working_set_bytes{container="etcd",namespace="kube-system",pod="etcd-minikube"} 5.9752448e+07 1641411341787
container_memory_working_set_bytes{container="kindnet-cni",namespace="kube-system",pod="kindnet-v9rn4"} 1.0326016e+07 1641411342227
container_memory_working_set_bytes{container="kube-apiserver",namespace="kube-system",pod="kube-apiserver-minikube"} 2.66002432e+08 1641411341706
container_memory_working_set_bytes{container="kube-controller-manager",namespace="kube-system",pod="kube-controller-manager-minikube"} 5.9129856e+07 1641411346752
container_memory_working_set_bytes{container="kube-proxy",namespace="kube-system",pod="kube-proxy-ktclh"} 2.00704e+07 1641411352327
container_memory_working_set_bytes{container="kube-scheduler",namespace="kube-system",pod="kube-scheduler-minikube"} 2.3130112e+07 1641411355059
container_memory_working_set_bytes{container="metrics-server",namespace="kube-system",pod="metrics-server-6b76bd68b6-lpx4q"} 2.6923008e+07 1641411344274
container_memory_working_set_bytes{container="storage-provisioner",namespace="kube-system",pod="storage-provisioner"} 1.4209024e+07 1641411354429

A few questions:

container_cpu_usage_seconds_total{container="coredns",namespace="kube-system",pod="coredns-64897985d-qzvj8"} 1075.30302335 1641411355244
container_memory_working_set_bytes{container="coredns",namespace="kube-system",pod="coredns-64897985d-qzvj8"} 1.5364096e+07 1641411355244

Upvotes: 1

Views: 78

Answers (1)

peak
peak

Reputation: 116780

You could convert your file to JSON using https://github.com/prometheus/prom2json

Then it's jq all the way down, if you wish. E.g. with your input:

prom2json sample.prom | jq '
  .[] | .metrics |= map(select(.labels.container=="coredns") )'

yields

{
  "name": "container_memory_working_set_bytes",
  "help": "[ALPHA] Current working set of the container in bytes",
  "type": "GAUGE",
  "metrics": [
    {
      "labels": {
        "container": "coredns",
        "namespace": "kube-system",
        "pod": "coredns-64897985d-qzvj8"
      },
      "timestamp_ms": "1641411355244",
      "value": "1.5364096e+07"
    }
  ]
}
{
  "name": "container_cpu_usage_seconds_total",
  "help": "[ALPHA] Cumulative cpu time consumed by the container in core-seconds",
  "type": "COUNTER",
  "metrics": [
    {
      "labels": {
        "container": "coredns",
        "namespace": "kube-system",
        "pod": "coredns-64897985d-qzvj8"
      },
      "timestamp_ms": "1641411355244",
      "value": "1075.30302335"
    }
  ]
}

Upvotes: 4

Related Questions