8000 Add metric about total number of TransportServers · nginx/kubernetes-ingress@032d483 · GitHub
[go: up one dir, main page]

Skip to content

Commit 032d483

Browse files
committed
Add metric about total number of TransportServers
1 parent 40804ac commit 032d483

File tree

5 files changed

+203
-23
lines changed

5 files changed

+203
-23
lines changed

docs/content/logging-and-monitoring/prometheus.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ The Ingress Controller exports the following metrics:
4949
* `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](/nginx-ingress-controller/configuration/ingress-resources/cross-namespace-configuration)). **Note**: The metric doesn't count minions without a master.
5050
* `controller_virtualserver_resources_total`. Number of handled VirtualServer resources.
5151
* `controller_virtualserverroute_resources_total`. Number of handled VirtualServerRoute resources. **Note**: The metric counts only VirtualServerRoutes that have a reference from a VirtualServer.
52+
* `controller_transportserver_resources_total`. Number of handled TransportServer resources. This metric includes the label type, that groups the TransportServer resources by their type (passthrough, tcp or udp).
5253
* Workqueue metrics. **Note**: the workqueue is a queue used by the Ingress Controller to process changes to the relevant resources in the cl 8000 uster like Ingress resources. The Ingress Controller uses only one queue. The metrics for that queue will have the label `name="taskQueue"`
5354
* `workqueue_depth`. Current depth of the workqueue.
5455
* `workqueue_queue_duration_second`. How long in seconds an item stays in the workqueue before being requested.

internal/k8s/configuration.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,13 @@ func compareObjectMetasWithAnnotations(meta1 *metav1.ObjectMeta, meta2 *metav1.O
312312
return compareObjectMetas(meta1, meta2) && reflect.DeepEqual(meta1.Annotations, meta2.Annotations)
313313
}
314314

315+
// TransportServerMetrics holds metrics about TransportServer resources
316+
type TransportServerMetrics struct {
317+
TotalTLSPassthrough int
318+
TotalTCP int
319+
TotalUDP int
320+
}
321+
315322
// Configuration represents the configuration of the Ingress Controller - a collection of configuration objects
316323
// (Ingresses, VirtualServers, VirtualServerRoutes) ready to be transformed into NGINX config.
317324
// It holds the latest valid state of those objects.
@@ -1442,6 +1449,30 @@ func (c *Configuration) buildVirtualServerRoutes(vs *conf_v1.VirtualServer) ([]*
14421449
return vsrs, warnings
14431450
}
14441451

1452+
// GetTransportServerMetrics returns metrics about TransportServers
1453+
func (c *Configuration) GetTransportServerMetrics() *TransportServerMetrics {
1454+
var metrics TransportServerMetrics
1455+
1456+
if c.isTLSPassthroughEnabled {
1457+
for _, resource := range c.hosts {
1458+
_, ok := resource.(*TransportServerConfiguration)
1459+
if ok {
1460+
metrics.TotalTLSPassthrough++
1461+
}
1462+
}
1463+
}
1464+
1465+
for _, tsConfig := range c.listeners {
1466+
if tsConfig.TransportServer.Spec.Listener.Protocol == "TCP" {
1467+
metrics.TotalTCP++
1468+
} else {
1469+
metrics.TotalUDP++
1470+
}
1471+
}
1472+
1473+
return &metrics
1474+
}
1475+
14451476
func getSortedIngressKeys(m map[string]*networking.Ingress) []string {
14461477
var keys []string
14471478

internal/k8s/configuration_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,6 +3256,104 @@ func TestGetResources(t *testing.T) {
32563256
}
32573257
}
32583258

3259+
func TestGetTransportServerMetrics(t *testing.T) {
3260+
tsPass := createTestTLSPassthroughTransportServer("transportserver", "abc.example.com")
3261+
tsTCP := createTestTransportServer("transportserver-tcp", "tcp-7777", "TCP")
3262+
tsUDP := createTestTransportServer("transportserver-udp", "udp-7777", "UDP")
3263+
3264+
tests := []struct {
3265+
tses []*conf_v1alpha1.TransportServer
3266+
expected *TransportServerMetrics
3267+
msg string
3268+
}{
3269+
{
3270+
tses: nil,
3271+
expected: &TransportServerMetrics{
3272+
TotalTLSPassthrough: 0,
3273+
TotalTCP: 0,
3274+
TotalUDP: 0,
3275+
},
3276+
msg: "no TransportServers",
3277+
},
3278+
{
3279+
tses: []*conf_v1alpha1.TransportServer{
3280+
tsPass,
3281+
},
3282+
expected: &TransportServerMetrics{
3283+
TotalTLSPassthrough: 1,
3284+
TotalTCP: 0,
3285+
TotalUDP: 0,
3286+
},
3287+
msg: "one TLSPassthrough TransportServer",
3288+
},
3289+
{
3290+
tses: []*conf_v1alpha1.TransportServer{
3291+
tsTCP,
3292+
},
3293+
expected: &TransportServerMetrics{
3294+
TotalTLSPassthrough: 0,
3295+
TotalTCP: 1,
3296+
TotalUDP: 0,
3297+
},
3298+
msg: "one TCP TransportServer",
3299+
},
3300+
{
3301+
tses: []*conf_v1alpha1.TransportServer{
3302+
tsUDP,
3303+
},
3304+
expected: &TransportServerMetrics{
3305+
TotalTLSPassthrough: 0,
3306+
TotalTCP: 0,
3307+
TotalUDP: 1,
3308+
},
3309+
msg: "one UDP TransportServer",
3310+
},
3311+
{
3312+
tses: []*conf_v1alpha1.TransportServer{
3313+
tsPass, tsTCP, tsUDP,
3314+
},
3315+
expected: &TransportServerMetrics{
3316+
TotalTLSPassthrough: 1,
3317+
TotalTCP: 1,
3318+
TotalUDP: 1,
3319+
},
3320+
msg: "TLSPasstrough, TCP and UDP TransportServers",
3321+
},
3322+
}
3323+
3324+
listeners := []conf_v1alpha1.Listener{
3325+
{
3326+
Name: "tcp-7777",
3327+
Port: 7777,
3328+
Protocol: "TCP",
3329+
},
3330+
{
3331+
Name: "udp-7777",
3332+
Port: 7777,
3333+
Protocol: "UDP",
3334+
},
3335+
}
3336+
gc := createTestGlobalConfiguration(listeners)
3337+
3338+
for _, test := range tests {
3339+
configuration := createTestConfiguration()
3340+
3341+
_, _, err := configuration.AddOrUpdateGlobalConfiguration(gc)
3342+
if err != nil {
3343+
t.Fatalf("AddOrUpdateGlobalConfiguration() returned unexpected error %v", err)
3344+
}
3345+
3346+
for _, ts := range test.tses {
3347+
configuration.AddOrUpdateTransportServer(ts)
3348+
}
3349+
3350+
result := configuration.GetTransportServerMetrics()
3351+
if diff := cmp.Diff(test.expected, result); diff != "" {
3352+
t.Errorf("GetTransportServerMetrics() returned unexpected result for the case of %s (-want +got):\n%s", test.msg, diff)
3353+
}
3354+
}
3355+
}
3356+
32593357
func TestIsEqualForIngressConfigurationes(t *testing.T) {
32603358
regularIng := createTestIngress("regular-ingress", "foo.example.com")
32613359

internal/k8s/controller.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc
315315

316316
lbc.secretStore = secrets.NewLocalSecretStore(lbc.configurator)
317317

318-
lbc.updateIngressMetrics()
319318
return lbc
320319
}
321320

@@ -712,6 +711,7 @@ func (lbc *LoadBalancerController) sync(task task) {
712711
case ingress:
713712
lbc.syncIngress(task)
714713
lbc.updateIngressMetrics()
714+
lbc.updateTransportServerMetrics()
715715
case configMap:
716716
lbc.syncConfigMap(task)
717717
case endpoints:
@@ -723,13 +723,16 @@ func (lbc *LoadBalancerController) sync(task task) {
723723
case virtualserver:
724724
lbc.syncVirtualServer(task)
725725
lbc.updateVirtualServerMetrics()
726+
lbc.updateTransportServerMetrics()
726727
case virtualServerRoute:
727728
lbc.syncVirtualServerRoute(task)
728729
lbc.updateVirtualServerMetrics()
729730
case globalConfiguration:
730731
lbc.syncGlobalConfiguration(task)
732+
lbc.updateTransportServerMetrics()
731733
case transportserver:
732734
lbc.syncTransportServer(task)
735+
lbc.updateTransportServerMetrics()
733736
case policy:
734737
lbc.syncPolicy(task)
735738
case appProtectPolicy:
@@ -1638,6 +1641,15 @@ func (lbc *LoadBalancerController) updateVirtualServerMetrics() {
16381641
lbc.metricsCollector.SetVirtualServerRoutes(vsrCount)
16391642
}
16401643

1644+
func (lbc *LoadBalancerController) updateTransportServerMetrics() {
1645+
if !lbc.areCustomResourcesEnabled {
1646+
return
1647+
}
1648+
1649+
metrics := lbc.configuration.GetTransportServerMetrics()
1650+
lbc.metricsCollector.SetTransportServers(metrics.TotalTLSPassthrough, metrics.TotalTCP, metrics.TotalUDP)
1651+
}
1652+
16411653
func (lbc *LoadBalancerController) syncService(task task) {
16421654
key := task.Key
16431655
glog.V(3).Infof("Syncing service %v", key)

internal/metrics/collectors/controller.go

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type ControllerCollector interface {
99
SetIngresses(ingressType string, count int)
1010
SetVirtualServers(count int)
1111
SetVirtualServerRoutes(count int)
12+
SetTransportServers(tlsPassthroughCount, tcpCount, udpCount int)
1213
Register(registry *prometheus.Registry) error
1314
}
1415

@@ -18,6 +19,7 @@ type ControllerMetricsCollector struct {
1819
ingressesTotal *prometheus.GaugeVec
1920
virtualServersTotal prometheus.Gauge
2021
virtualServerRoutesTotal prometheus.Gauge
22+
transportServersTotal *prometheus.GaugeVec
2123
}
2224

2325
// NewControllerMetricsCollector creates a new ControllerMetricsCollector
@@ -32,34 +34,58 @@ func NewControllerMetricsCollector(crdsEnabled bool, constLabels map[string]stri
3234
labelNamesController,
3335
)
3436

35-
if !crdsEnabled {
36-
return &ControllerMetricsCollector{ingressesTotal: ingResTotal}
37+
var vsResTotal, vsrResTotal prometheus.Gauge
38+
var tsResTotal *prometheus.GaugeVec
39+
40+
if crdsEnabled {
41+
vsResTotal = prometheus.NewGauge(
42+
prometheus.GaugeOpts{
43+
Name: "virtualserver_resources_total",
44+
Namespace: metricsNamespace,
45+
Help: "Number of handled VirtualServer resources",
46+
ConstLabels: constLabels,
47+
},
48+
)
49+
50+
vsrResTotal = prometheus.NewGauge(
51+
prometheus.GaugeOpts{
52+
Name: "virtualserverroute_resources_total",
53+
Namespace: metricsNamespace,
54+
Help: "Number of handled VirtualServerRoute resources",
55+
ConstLabels: constLabels,
56+
},
57+
)
58+
59+
tsResTotal = prometheus.NewGaugeVec(
60+
prometheus.GaugeOpts{
61+
Name: "transportserver_resources_total",
62+
Namespace: metricsNamespace,
63+
Help: "Number of handled TransportServer resources",
64+
ConstLabels: constLabels,
65+
},
66+
labelNamesController,
67+
)
3768
}
3869

39-
vsResTotal := prometheus.NewGauge(
40-
prometheus.GaugeOpts{
41-
Name: "virtualserver_resources_total",
42-
Namespace: metricsNamespace,
43-
Help: "Number of handled VirtualServer resources",
44-
ConstLabels: constLabels,
45-
},
46-
)
47-
48-
vsrResTotal := prometheus.NewGauge(
49-
prometheus.GaugeOpts{
50-
Name: "virtualserverroute_resources_total",
51-
Namespace: metricsNamespace,
52-
Help: "Number of handled VirtualServerRoute resources",
53-
ConstLabels: constLabels,
54-
},
55-
)
56-
57-
return &ControllerMetricsCollector{
58-
crdsEnabled: true,
70+
c := &ControllerMetricsCollector{
71+
crdsEnabled: crdsEnabled,
5972
ingressesTotal: ingResTotal,
6073
virtualServersTotal: vsResTotal,
6174
virtualServerRoutesTotal: vsrResTotal,
75+
transportServersTotal: tsResTotal,
76+
}
77+
78+
// if we don't set to 0 metrics with the label type, the metrics will not be created initially
79+
80+
c.SetIngresses("regular", 0)
81+
c.SetIngresses("master", 0)
82+
c.SetIngresses("minion", 0)
83+
84+
if crdsEnabled {
85+
c.SetTransportServers(0, 0, 0)
6286
}
87+
88+
return c
6389
}
6490

6591
// SetIngresses sets the value of the ingress resources gauge for a given type
@@ -77,12 +103,20 @@ func (cc *ControllerMetricsCollector) SetVirtualServerRoutes(count int) {
77103
cc.virtualServerRoutesTotal.Set(float64(count))
78104
}
79105

106+
// SetTransportServers sets the value of the TransportServer resources gauge
107+
func (cc *ControllerMetricsCollector) SetTransportServers(tlsPassthroughCount, tcpCount, udpCount int) {
108+
cc.transportServersTotal.WithLabelValues("passthrough").Set(float64(tlsPassthroughCount))
109+
cc.transportServersTotal.WithLabelValues("tcp").Set(float64(tcpCount))
110+
cc.transportServersTotal.WithLabelValues("udp").Set(float64(udpCount))
111+
}
112+
80113
// Describe implements prometheus.Collector interface Describe method
81114
func (cc *ControllerMetricsCollector) Describe(ch chan<- *prometheus.Desc) {
82115
cc.ingressesTotal.Describe(ch)
83116
if cc.crdsEnabled {
84117
cc.virtualServersTotal.Describe(ch)
85118
cc.virtualServerRoutesTotal.Describe(ch)
119+
cc.transportServersTotal.Describe(ch)
86120
}
87121
}
88122

@@ -92,6 +126,7 @@ func (cc *ControllerMetricsCollector) Collect(ch chan<- prometheus.Metric) {
92126
if cc.crdsEnabled {
93127
cc.virtualServersTotal.Collect(ch)
94128
cc.virtualServerRoutesTotal.Collect(ch)
129+
cc.transportServersTotal.Collect(ch)
95130
}
96131
}
97132

@@ -119,3 +154,6 @@ func (cc *ControllerFakeCollector) SetVirtualServers(count int) {}
119154

120155
// SetVirtualServerRoutes implements a fake SetVirtualServerRoutes
121156
func (cc *ControllerFakeCollector) SetVirtualServerRoutes(count int) {}
157+
158+
// SetTransportServers implements a fake SetTransportServers
159+
func (cc *ControllerFakeCollector) SetTransportServers(int, int, int) {}

0 commit comments

Comments
 (0)
0