In etcd-version-monitor, Remove grpc labels used only in etcd 3 format when translating metric back to 3.0 format

pull/6/head
Joe Betz 2018-02-14 16:54:43 -08:00
parent c007efcceb
commit 23b9f65861
4 changed files with 68 additions and 2 deletions

View File

@ -17,6 +17,7 @@ go_library(
srcs = ["etcd-version-monitor.go"],
importpath = "k8s.io/kubernetes/cluster/images/etcd-version-monitor",
deps = [
"//vendor/github.com/gogo/protobuf/proto:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus/promhttp:go_default_library",

View File

@ -20,7 +20,7 @@
ARCH:=amd64
GOLANG_VERSION?=1.8.3
REGISTRY?=staging-k8s.gcr.io
TAG?=0.1.1
TAG?=0.1.2
IMAGE:=$(REGISTRY)/etcd-version-monitor:$(TAG)
CURRENT_DIR:=$(pwd)
TEMP_DIR:=$(shell mktemp -d)

View File

@ -17,12 +17,14 @@ limitations under the License.
package main
import (
"bytes"
"encoding/json"
goflag "flag"
"fmt"
"net/http"
"time"
"github.com/gogo/protobuf/proto"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
@ -97,6 +99,13 @@ var (
"grpc_method": "method",
"grpc_service": "service",
})
filterMetricsByLabels(mf, map[string]string{
"grpc_type": "unary",
})
groupCounterMetricsByLabels(mf, map[string]bool{
"grpc_type": true,
"grpc_code": true,
})
return mf, nil
},
},
@ -280,6 +289,62 @@ func renameLabels(mf *dto.MetricFamily, nameMapping map[string]string) {
}
}
func filterMetricsByLabels(mf *dto.MetricFamily, labelValues map[string]string) {
buf := mf.Metric[:0]
for _, m := range mf.Metric {
shouldRemove := false
for _, lbl := range m.Label {
if val, ok := labelValues[*lbl.Name]; ok && val != *lbl.Value {
shouldRemove = true
break
}
}
if !shouldRemove {
buf = append(buf, m)
}
}
mf.Metric = buf
}
func groupCounterMetricsByLabels(mf *dto.MetricFamily, names map[string]bool) {
buf := mf.Metric[:0]
deleteLabels(mf, names)
byLabels := map[string]*dto.Metric{}
for _, m := range mf.Metric {
if metric, ok := byLabels[labelsKey(m.Label)]; ok {
metric.Counter.Value = proto.Float64(*metric.Counter.Value + *m.Counter.Value)
} else {
byLabels[labelsKey(m.Label)] = m
buf = append(buf, m)
}
}
mf.Metric = buf
}
func labelsKey(lbls []*dto.LabelPair) string {
var buf bytes.Buffer
for i, lbl := range lbls {
buf.WriteString(lbl.String())
if i < len(lbls)-1 {
buf.WriteString(",")
}
}
return buf.String()
}
func deleteLabels(mf *dto.MetricFamily, names map[string]bool) {
for _, m := range mf.Metric {
buf := m.Label[:0]
for _, lbl := range m.Label {
shouldRemove := names[*lbl.Name]
if !shouldRemove {
buf = append(buf, lbl)
}
}
m.Label = buf
}
}
func identity(mf *dto.MetricFamily) (*dto.MetricFamily, error) {
return mf, nil
}

View File

@ -7,7 +7,7 @@ spec:
hostNetwork: true
containers:
- name: etcd-version-monitor
image: k8s.gcr.io/etcd-version-monitor:0.1.0
image: k8s.gcr.io/etcd-version-monitor:0.1.2
command:
- /etcd-version-monitor
- --logtostderr