Skip to content

Commit b6dd338

Browse files
Jim Ryanvepatel
andauthored
add service count metric to telemetry (nginx#5408)
* add service metric to telemetry * add secretStore to service count tests * add backup and grpc service counting * fix test for ingress counts * Add tests for mergeable and standard ingress * Update docs/content/overview/product-telemetry.md Co-authored-by: Venktesh Shivam Patel <[email protected]> Signed-off-by: Jim Ryan <[email protected]> --------- Signed-off-by: Jim Ryan <[email protected]> Co-authored-by: Venktesh Shivam Patel <[email protected]>
1 parent 2ad69d0 commit b6dd338

File tree

7 files changed

+763
-5
lines changed

7 files changed

+763
-5
lines changed

docs/content/overview/product-telemetry.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ These are the data points collected and reported by NGINX Ingress Controller:
3434
- **TransportServers** The number of TransportServer resources managed by NGINX Ingress Controller.
3535
- **Replicas** Number of Deployment replicas, or Daemonset instances.
3636
- **Secrets** Number of Secret resources managed by NGINX Ingress Controller.
37-
- **Ingress Count** Number of Ingresses.
37+
- **Services** Number of Services referenced by VirtualServers, VirtualServerRoutes, TransportServers and Ingresses.
38+
- **IngressCount** Number of Ingresses.
3839

3940

4041
## Opt out

internal/configs/configurator.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ type Configurator struct {
123123
templateExecutorV2 *version2.TemplateExecutor
124124
ingresses map[string]*IngressEx
125125
minions map[string]map[string]bool
126+
mergeableIngresses map[string]*MergeableIngresses
126127
virtualServers map[string]*VirtualServerEx
127128
transportServers map[string]*TransportServerEx
128129
tlsPassthroughPairs map[string]tlsPassthroughPair
@@ -180,6 +181,7 @@ func NewConfigurator(p ConfiguratorParams) *Configurator {
180181
templateExecutor: p.TemplateExecutor,
181182
templateExecutorV2: p.TemplateExecutorV2,
182183
minions: make(map[string]map[string]bool),
184+
mergeableIngresses: make(map[string]*MergeableIngresses),
183185
tlsPassthroughPairs: make(map[string]tlsPassthroughPair),
184186
isPlus: p.IsPlus,
185187
isWildcardEnabled: p.IsWildcardEnabled,
@@ -475,6 +477,9 @@ func (cnf *Configurator) addOrUpdateMergeableIngress(mergeableIngs *MergeableIng
475477
minionName := objectMetaToFileName(&minion.Ingress.ObjectMeta)
476478
cnf.minions[name][minionName] = true
477479
}
480+
481+
cnf.mergeableIngresses[name] = mergeableIngs
482+
478483
if (cnf.isPlus && cnf.isPrometheusEnabled) || cnf.isLatencyMetricsEnabled {
479484
cnf.updateIngressMetricsLabels(mergeableIngs.Master, nginxCfg.Upstreams)
480485
}
@@ -955,6 +960,7 @@ func (cnf *Configurator) DeleteIngress(key string, skipReload bool) error {
955960

956961
delete(cnf.ingresses, name)
957962
delete(cnf.minions, name)
963+
delete(cnf.mergeableIngresses, name)
958964

959965
if (cnf.isPlus && cnf.isPrometheusEnabled) || cnf.isLatencyMetricsEnabled {
960966
cnf.deleteIngressMetricsLabels(key)
@@ -1542,6 +1548,106 @@ func (cnf *Configurator) GetIngressCounts() map[string]int {
15421548
return counters
15431549
}
15441550

1551+
// GetServiceCount returns the total number of unique services referenced by Ingresses, VS's, VSR's, and TS's
1552+
func (cnf *Configurator) GetServiceCount() int {
1553+
setOfUniqueServices := make(map[string]bool)
1554+
cnf.addVSAndVSRServicesToSet(setOfUniqueServices)
1555+
cnf.addTSServicesToSet(setOfUniqueServices)
1556+
cnf.addIngressesServicesToSet(setOfUniqueServices)
1557+
return len(setOfUniqueServices)
1558+
}
1559+
1560+
// addVSAndVSRServicesToSet adds services from VirtualServers and VirtualServerRoutes to the set
1561+
func (cnf *Configurator) addVSAndVSRServicesToSet(set map[string]bool) {
1562+
for _, vs := range cnf.virtualServers {
1563+
ns := vs.VirtualServer.Namespace
1564+
for _, upstream := range vs.VirtualServer.Spec.Upstreams {
1565+
svc := upstream.Service
1566+
addServiceToSet(set, ns, svc)
1567+
1568+
if upstream.Backup != "" {
1569+
addServiceToSet(set, ns, upstream.Backup)
1570+
}
1571+
1572+
if upstream.HealthCheck != nil && upstream.HealthCheck.GRPCService != "" {
1573+
addServiceToSet(set, ns, upstream.HealthCheck.GRPCService)
1574+
}
1575+
}
1576+
1577+
for _, vsr := range vs.VirtualServerRoutes {
1578+
ns := vsr.Namespace
1579+
for _, upstream := range vsr.Spec.Upstreams {
1580+
svc := upstream.Service
1581+
addServiceToSet(set, ns, svc)
1582+
1583+
if upstream.Backup != "" {
1584+
addServiceToSet(set, ns, upstream.Backup)
1585+
}
1586+
1587+
if upstream.HealthCheck != nil && upstream.HealthCheck.GRPCService != "" {
1588+
addServiceToSet(set, ns, upstream.HealthCheck.GRPCService)
1589+
}
1590+
}
1591+
}
1592+
}
1593+
}
1594+
1595+
// addTSServicesToSet adds services from TransportServers to the set
1596+
func (cnf *Configurator) addTSServicesToSet(set map[string]bool) {
1597+
for _, ts := range cnf.transportServers {
1598+
ns := ts.TransportServer.Namespace
1599+
for _, upstream := range ts.TransportServer.Spec.Upstreams {
1600+
svc := upstream.Service
1601+
addServiceToSet(set, ns, svc)
1602+
1603+
if upstream.Backup != "" {
1604+
addServiceToSet(set, ns, upstream.Backup)
1605+
}
1606+
1607+
}
1608+
}
1609+
}
1610+
1611+
// addIngressesServicesToSet adds services from Ingresses to the set
1612+
func (cnf *Configurator) addIngressesServicesToSet(set map[string]bool) {
1613+
for _, ing := range cnf.ingresses {
1614+
cnf.addIngressServicesToSet(ing, set)
1615+
}
1616+
for _, mergeIngs := range cnf.mergeableIngresses {
1617+
cnf.addIngressServicesToSet(mergeIngs.Master, set)
1618+
for _, minion := range mergeIngs.Minions {
1619+
cnf.addIngressServicesToSet(minion, set)
1620+
}
1621+
}
1622+
}
1623+
1624+
// addIngressServicesToSet processes a single ingress and adds its services to the set
1625+
func (cnf *Configurator) addIngressServicesToSet(ing *IngressEx, set map[string]bool) {
1626+
if ing == nil || ing.Ingress == nil {
1627+
return
1628+
}
1629+
ns := ing.Ingress.Namespace
1630+
if ing.Ingress.Spec.DefaultBackend != nil && ing.Ingress.Spec.DefaultBackend.Service != nil {
1631+
svc := ing.Ingress.Spec.DefaultBackend.Service.Name
1632+
addServiceToSet(set, ns, svc)
1633+
}
1634+
for _, rule := range ing.Ingress.Spec.Rules {
1635+
if rule.HTTP != nil {
1636+
for _, path := range rule.HTTP.Paths {
1637+
if path.Backend.Service != nil {
1638+
svc := path.Backend.Service.Name
1639+
addServiceToSet(set, ns, svc)
1640+
}
1641+
}
1642+
}
1643+
}
1644+
}
1645+
1646+
// Helper function to add services to the set
1647+
func addServiceToSet(set map[string]bool, ns string, svc string) {
1648+
set[fmt.Sprintf("%s/%s", ns, svc)] = true
1649+
}
1650+
15451651
// GetVirtualServerCounts returns the total count of
15461652
// VirtualServer and VirtualServerRoute resources that are handled by the Ingress Controller
15471653
func (cnf *Configurator) GetVirtualServerCounts() (int, int) {

internal/telemetry/collector.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func (c *Collector) Collect(ctx context.Context) {
116116
TransportServers: int64(report.TransportServers),
117117
Replicas: int64(report.NICReplicaCount),
118118
Secrets: int64(report.Secrets),
119+
Services: int64(report.ServiceCount),
119120
Ingresses: int64(report.IngressCount),
120121
},
121122
}
@@ -142,6 +143,7 @@ type Report struct {
142143
NICReplicaCount int
143144
VirtualServers int
144145
VirtualServerRoutes int
146+
ServiceCount int
145147
TransportServers int
146148
Secrets int
147149
IngressCount int
@@ -152,10 +154,12 @@ func (c *Collector) BuildReport(ctx context.Context) (Report, error) {
152154
vsCount := 0
153155
vsrCount := 0
154156
tsCount := 0
157+
serviceCount := 0
155158

156159
if c.Config.Configurator != nil {
157160
vsCount, vsrCount = c.Config.Configurator.GetVirtualServerCounts()
158161
tsCount = c.Config.Configurator.GetTransportServerCounts()
162+
serviceCount = c.Config.Configurator.GetServiceCount()
159163
}
160164

161165
clusterID, err := c.ClusterID(ctx)
@@ -206,6 +210,7 @@ func (c *Collector) BuildReport(ctx context.Context) (Report, error) {
206210
NICReplicaCount: replicas,
207211
VirtualServers: vsCount,
208212
VirtualServerRoutes: vsrCount,
213+
ServiceCount: serviceCount,
209214
TransportServers: tsCount,
210215
Secrets: secrets,
211216
IngressCount: ingressCount,

0 commit comments

Comments
 (0)