Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VS/VSR Prometheus metrics #724

Merged
merged 7 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ The Ingress Controller exports the following metrics:
* `controller_nginx_last_reload_status`. Status of the last NGINX reload, 0 meaning down and 1 up.
* `controller_nginx_last_reload_milliseconds`. Duration in milliseconds of the last NGINX reload.
* `controller_ingress_resources_total`. Number of handled Ingress resources. This metric includes the label type, that groups the Ingress resources by their type (regular, [minion or master](./../examples/mergeable-ingress-types))
* `controller_virtualserver_resources_total`. Number of handled VirtualServer resources.
* `controller_virtualserverroute_resources_total`. Number of handled VirtualServerRoute resources. **Note**: Only counts VirtualServerRoutes that have a reference from a VirtualServer.
Dean-Coakley marked this conversation as resolved.
Show resolved Hide resolved

**Note**: all metrics have the namespace nginx_ingress. For example, nginx_ingress_controller_nginx_reloads_total.
16 changes: 16 additions & 0 deletions internal/configs/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Configurator struct {
templateExecutorV2 *version2.TemplateExecutor
ingresses map[string]*IngressEx
minions map[string]map[string]bool
virtualServers map[string]*VirtualServerEx
isWildcardEnabled bool
isPlus bool
}
Expand All @@ -49,6 +50,7 @@ func NewConfigurator(nginxManager nginx.Manager, staticCfgParams *StaticConfigPa
staticCfgParams: staticCfgParams,
cfgParams: config,
ingresses: make(map[string]*IngressEx),
virtualServers: make(map[string]*VirtualServerEx),
templateExecutor: templateExecutor,
templateExecutorV2: templateExecutorV2,
minions: make(map[string]map[string]bool),
Expand Down Expand Up @@ -170,6 +172,8 @@ func (cnf *Configurator) addOrUpdateVirtualServer(virtualServerEx *VirtualServer
}
cnf.nginxManager.CreateConfig(name, content)

cnf.virtualServers[name] = virtualServerEx

return warnings, nil
}

Expand Down Expand Up @@ -339,6 +343,8 @@ func (cnf *Configurator) DeleteVirtualServer(key string) error {
name := getFileNameForVirtualServerFromKey(key)
cnf.nginxManager.DeleteConfig(name)

delete(cnf.virtualServers, name)

if err := cnf.nginxManager.Reload(); err != nil {
return fmt.Errorf("Error when removing VirtualServer %v: %v", key, err)
}
Expand Down Expand Up @@ -637,3 +643,13 @@ func (cnf *Configurator) GetIngressCounts() map[string]int {

return counters
}

// GetVSCounts returns the total count of Vs/Vsr resources that are handled by the Ingress Controller
func (cnf *Configurator) GetVSCounts() (vsCount int, vsrCount int) {
Dean-Coakley marked this conversation as resolved.
Show resolved Hide resolved
vsCount = len(cnf.virtualServers)
for _, vs := range cnf.virtualServers {
vsrCount += len(vs.VirtualServerRoutes)
}

return vsCount, vsrCount
}
8 changes: 8 additions & 0 deletions internal/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,10 @@ func (lbc *LoadBalancerController) sync(task task) {
lbc.syncExternalService(task)
case virtualserver:
lbc.syncVirtualServer(task)
lbc.updateVsMetrics()
case virtualServerRoute:
lbc.syncVirtualServerRoute(task)
lbc.updateVsMetrics()
}
}

Expand Down Expand Up @@ -851,6 +853,12 @@ func (lbc *LoadBalancerController) updateIngressMetrics() {
}
}

func (lbc *LoadBalancerController) updateVsMetrics() {
vsCount, vsrCount := lbc.configurator.GetVSCounts()
lbc.metricsCollector.SetVsResources(vsCount)
lbc.metricsCollector.SetVsrResources(vsrCount)
}

// syncExternalService does not sync all services.
// We only watch the Service specified by the external-service flag.
func (lbc *LoadBalancerController) syncExternalService(task task) {
Expand Down
64 changes: 53 additions & 11 deletions internal/metrics/collectors/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,79 @@ var labelNamesController = []string{"type"}
// ControllerCollector is an interface for the metrics of the Controller
type ControllerCollector interface {
SetIngressResources(ingressType string, count int)
SetVsResources(count int)
Dean-Coakley marked this conversation as resolved.
Show resolved Hide resolved
SetVsrResources(count int)
Register(registry *prometheus.Registry) error
}

// ControllerMetricsCollector implements the ControllerCollector interface and prometheus.Collector interface
type ControllerMetricsCollector struct {
ingressResourcesTotal *prometheus.GaugeVec
vsResourcesTotal prometheus.Gauge
vsrResourcesTotal prometheus.Gauge
}

// NewControllerMetricsCollector creates a new ControllerMetricsCollector
func NewControllerMetricsCollector() *ControllerMetricsCollector {
cc := &ControllerMetricsCollector{
ingressResourcesTotal: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ingress_resources_total",
Namespace: metricsNamespace,
Help: "Number of handled ingress resources",
},
labelNamesController,
),
}
ingResTotal := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ingress_resources_total",
Namespace: metricsNamespace,
Help: "Number of handled ingress resources",
},
labelNamesController,
)

vsResTotal := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "virtualserver_resources_total",
Namespace: metricsNamespace,
Help: "Number of handled VirtualServer resources",
},
)

vsrResTotal := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "virtualserverroute_resources_total",
Namespace: metricsNamespace,
Help: "Number of handled VirtualServerRoute resources",
},
)

return cc
return &ControllerMetricsCollector{
ingressResourcesTotal: ingResTotal,
vsResourcesTotal: vsResTotal,
vsrResourcesTotal: vsrResTotal,
}
}

// SetIngressResources sets the value of the ingress resources gauge for a given type
func (cc *ControllerMetricsCollector) SetIngressResources(ingressType string, count int) {
cc.ingressResourcesTotal.WithLabelValues(ingressType).Set(float64(count))
}

// SetVsResources sets the value of the VS resources gauge
func (cc *ControllerMetricsCollector) SetVsResources(count int) {
cc.vsResourcesTotal.Set(float64(count))
}

// SetVsrResources sets the value of the VSR resources gauge
func (cc *ControllerMetricsCollector) SetVsrResources(count int) {
cc.vsrResourcesTotal.Set(float64(count))
}

// Describe implements prometheus.Collector interface Describe method
func (cc *ControllerMetricsCollector) Describe(ch chan<- *prometheus.Desc) {
cc.ingressResourcesTotal.Describe(ch)
cc.vsResourcesTotal.Describe(ch)
cc.vsrResourcesTotal.Describe(ch)
}

// Collect implements the prometheus.Collector interface Collect method
func (cc *ControllerMetricsCollector) Collect(ch chan<- prometheus.Metric) {
cc.ingressResourcesTotal.Collect(ch)
cc.vsResourcesTotal.Collect(ch)
cc.vsrResourcesTotal.Collect(ch)
}

// Register registers all the metrics of the collector
Expand All @@ -64,3 +100,9 @@ func (cc *ControllerFakeCollector) Register(registry *prometheus.Registry) error

// SetIngressResources implements a fake SetIngressResources
func (cc *ControllerFakeCollector) SetIngressResources(ingressType string, count int) {}

// SetVsResources implements a fake SetVsResources
func (cc *ControllerFakeCollector) SetVsResources(count int) {}

// SetVsrResources implements a fake SetVsrResources
func (cc *ControllerFakeCollector) SetVsrResources(count int) {}