Skip to content

Commit

Permalink
Merge pull request #3594 from bboreham/metrics-per-host-optional
Browse files Browse the repository at this point in the history
Add a flag to make per-host metrics optional
  • Loading branch information
k8s-ci-robot authored Dec 21, 2018
2 parents 63b0137 + f33a209 commit ecc527b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
3 changes: 3 additions & 0 deletions cmd/nginx/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ Feature backed by OpenResty Lua libraries. Requires that OCSP stapling is not en

enableMetrics = flags.Bool("enable-metrics", true,
`Enables the collection of NGINX metrics`)
metricsPerHost = flags.Bool("metrics-per-host", true,
`Export metrics per-host`)

httpPort = flags.Int("http-port", 80, `Port to use for servicing HTTP traffic.`)
httpsPort = flags.Int("https-port", 443, `Port to use for servicing HTTPS traffic.`)
Expand Down Expand Up @@ -227,6 +229,7 @@ Feature backed by OpenResty Lua libraries. Requires that OCSP stapling is not en
ElectionID: *electionID,
EnableProfiling: *profiling,
EnableMetrics: *enableMetrics,
MetricsPerHost: *metricsPerHost,
EnableSSLPassthrough: *enableSSLPassthrough,
EnableSSLChainCompletion: *enableSSLChainCompletion,
ResyncPeriod: *resyncPeriod,
Expand Down
2 changes: 1 addition & 1 deletion cmd/nginx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func main() {

mc := metric.NewDummyCollector()
if conf.EnableMetrics {
mc, err = metric.NewCollector(conf.ListenPorts.Status, reg)
mc, err = metric.NewCollector(conf.ListenPorts.Status, conf.MetricsPerHost, reg)
if err != nil {
klog.Fatalf("Error creating prometheus collector: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ type Configuration struct {

EnableProfiling bool

EnableMetrics bool
EnableMetrics bool
MetricsPerHost bool

EnableSSLChainCompletion bool

Expand Down
18 changes: 14 additions & 4 deletions internal/ingress/metric/collectors/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ type SocketCollector struct {
metricMapping map[string]interface{}

hosts sets.String

metricsPerHost bool
}

var (
requestTags = []string{
"host",

"status",

"method",
Expand All @@ -95,7 +95,7 @@ var (

// NewSocketCollector creates a new SocketCollector instance using
// the ingress watch namespace and class used by the controller
func NewSocketCollector(pod, namespace, class string) (*SocketCollector, error) {
func NewSocketCollector(pod, namespace, class string, metricsPerHost bool) (*SocketCollector, error) {
socket := "/tmp/prometheus-nginx.socket"
listener, err := net.Listen("unix", socket)
if err != nil {
Expand All @@ -113,9 +113,16 @@ func NewSocketCollector(pod, namespace, class string) (*SocketCollector, error)
"controller_pod": pod,
}

requestTags := requestTags
if metricsPerHost {
requestTags = append(requestTags, "host")
}

sc := &SocketCollector{
listener: listener,

metricsPerHost: metricsPerHost,

responseTime: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "response_duration_seconds",
Expand Down Expand Up @@ -219,15 +226,18 @@ func (sc *SocketCollector) handleMessage(msg []byte) {
continue
}

// Note these must match the order in requestTags at the top
requestLabels := prometheus.Labels{
"host": stats.Host,
"status": stats.Status,
"method": stats.Method,
"path": stats.Path,
"namespace": stats.Namespace,
"ingress": stats.Ingress,
"service": stats.Service,
}
if sc.metricsPerHost {
requestLabels["host"] = stats.Host
}

collectorLabels := prometheus.Labels{
"namespace": stats.Namespace,
Expand Down
2 changes: 1 addition & 1 deletion internal/ingress/metric/collectors/socket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func TestCollector(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
registry := prometheus.NewPedanticRegistry()

sc, err := NewSocketCollector("pod", "default", "ingress")
sc, err := NewSocketCollector("pod", "default", "ingress", true)
if err != nil {
t.Errorf("%v: unexpected error creating new SocketCollector: %v", c.name, err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/ingress/metric/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type collector struct {
}

// NewCollector creates a new metric collector the for ingress controller
func NewCollector(statusPort int, registry *prometheus.Registry) (Collector, error) {
func NewCollector(statusPort int, metricsPerHost bool, registry *prometheus.Registry) (Collector, error) {
podNamespace := os.Getenv("POD_NAMESPACE")
if podNamespace == "" {
podNamespace = "default"
Expand All @@ -77,7 +77,7 @@ func NewCollector(statusPort int, registry *prometheus.Registry) (Collector, err
return nil, err
}

s, err := collectors.NewSocketCollector(podName, podNamespace, class.IngressClass)
s, err := collectors.NewSocketCollector(podName, podNamespace, class.IngressClass, metricsPerHost)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ecc527b

Please sign in to comment.