From 9766ad8f4be7432354b30e6be6ade730751d1207 Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Sat, 22 Sep 2018 14:25:57 -0300 Subject: [PATCH] Filter hostnames before creation of metrics --- internal/ingress/metric/collectors/socket.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/ingress/metric/collectors/socket.go b/internal/ingress/metric/collectors/socket.go index ebfa37b07..1f30c9b72 100644 --- a/internal/ingress/metric/collectors/socket.go +++ b/internal/ingress/metric/collectors/socket.go @@ -17,7 +17,6 @@ limitations under the License. package collectors import ( - "encoding/json" "fmt" "io" "io/ioutil" @@ -25,6 +24,7 @@ import ( "os" "github.com/golang/glog" + jsoniter "github.com/json-iterator/go" "github.com/prometheus/client_golang/prometheus" "k8s.io/apimachinery/pkg/util/sets" ) @@ -75,6 +75,8 @@ type SocketCollector struct { listener net.Listener metricMapping map[string]interface{} + + hosts sets.String } var ( @@ -208,13 +210,18 @@ func (sc *SocketCollector) handleMessage(msg []byte) { // Unmarshall bytes var statsBatch []socketData - err := json.Unmarshal(msg, &statsBatch) + err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(msg, &statsBatch) if err != nil { glog.Errorf("Unexpected error deserializing JSON paylod: %v. Payload:\n%v", err, string(msg)) return } for _, stats := range statsBatch { + if !sc.hosts.Has(stats.Host) { + glog.V(3).Infof("skiping metric for host %v that is not being served", stats.Host) + continue + } + requestLabels := prometheus.Labels{ "host": stats.Host, "status": stats.Status, @@ -411,6 +418,12 @@ func (sc SocketCollector) Collect(ch chan<- prometheus.Metric) { sc.bytesSent.Collect(ch) } +// SetHosts sets the hostnames that are being served by the ingress controller +// This set of hostnames is used to filter the metrics to be exposed +func (sc *SocketCollector) SetHosts(hosts sets.String) { + sc.hosts = hosts +} + // handleMessages process the content received in a network connection func handleMessages(conn io.ReadCloser, fn func([]byte)) { defer conn.Close()