Filter hostnames before creation of metrics
This commit is contained in:
parent
91ae204f6c
commit
9766ad8f4b
1 changed files with 15 additions and 2 deletions
|
@ -17,7 +17,6 @@ limitations under the License.
|
||||||
package collectors
|
package collectors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -25,6 +24,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
)
|
)
|
||||||
|
@ -75,6 +75,8 @@ type SocketCollector struct {
|
||||||
listener net.Listener
|
listener net.Listener
|
||||||
|
|
||||||
metricMapping map[string]interface{}
|
metricMapping map[string]interface{}
|
||||||
|
|
||||||
|
hosts sets.String
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -208,13 +210,18 @@ func (sc *SocketCollector) handleMessage(msg []byte) {
|
||||||
|
|
||||||
// Unmarshall bytes
|
// Unmarshall bytes
|
||||||
var statsBatch []socketData
|
var statsBatch []socketData
|
||||||
err := json.Unmarshal(msg, &statsBatch)
|
err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(msg, &statsBatch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Unexpected error deserializing JSON paylod: %v. Payload:\n%v", err, string(msg))
|
glog.Errorf("Unexpected error deserializing JSON paylod: %v. Payload:\n%v", err, string(msg))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, stats := range statsBatch {
|
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{
|
requestLabels := prometheus.Labels{
|
||||||
"host": stats.Host,
|
"host": stats.Host,
|
||||||
"status": stats.Status,
|
"status": stats.Status,
|
||||||
|
@ -411,6 +418,12 @@ func (sc SocketCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
sc.bytesSent.Collect(ch)
|
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
|
// handleMessages process the content received in a network connection
|
||||||
func handleMessages(conn io.ReadCloser, fn func([]byte)) {
|
func handleMessages(conn io.ReadCloser, fn func([]byte)) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
Loading…
Reference in a new issue