Skip to content

Commit 1d50340

Browse files
authored
Merge pull request #2994 from swetharepakula/separate-psc-metrics
Move PSC related metrics into PSC metrics directory
2 parents 78ca0d5 + de2b6b0 commit 1d50340

File tree

8 files changed

+391
-275
lines changed

8 files changed

+391
-275
lines changed

pkg/metrics/features.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ const (
102102
// FrontendConfig Features
103103
sslPolicy = feature("SSLPolicy")
104104
httpsRedirects = feature("HTTPSRedirects")
105-
106-
// PSC Features
107-
sa = feature("ServiceAttachments")
108-
saInSuccess = feature("ServiceAttachmentInSuccess")
109-
saInError = feature("ServiceAttachmentInError")
110-
services = feature("Services")
111105
)
112106

113107
// featuresForIngress returns the list of features for given ingress.

pkg/metrics/metrics.go

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,6 @@ var (
5656
},
5757
[]string{label},
5858
)
59-
serviceAttachmentCount = prometheus.NewGaugeVec(
60-
prometheus.GaugeOpts{
61-
Name: "number_of_service_attachments",
62-
Help: "Number of Service Attachments",
63-
},
64-
[]string{label},
65-
)
66-
serviceCount = prometheus.NewGaugeVec(
67-
prometheus.GaugeOpts{
68-
Name: "number_of_services",
69-
Help: "Number of Services",
70-
},
71-
[]string{label},
72-
)
7359
componentVersion = prometheus.NewGaugeVec(
7460
prometheus.GaugeOpts{
7561
Name: "component_version",
@@ -91,10 +77,6 @@ func init() {
9177
klog.V(3).Infof("Registering Ingress usage metrics %v and %v", ingressCount, servicePortCount)
9278
prometheus.MustRegister(ingressCount, servicePortCount)
9379

94-
klog.V(3).Infof("Registering PSC usage metrics %v", serviceAttachmentCount)
95-
prometheus.MustRegister(serviceAttachmentCount)
96-
prometheus.MustRegister(serviceCount)
97-
9880
klog.V(3).Infof("Registering metric export failures count %v", MetricExportFailureCount)
9981
prometheus.MustRegister(MetricExportFailureCount)
10082

@@ -189,48 +171,6 @@ func (im *ControllerMetrics) DeleteIngress(ingKey string) {
189171
delete(im.ingressMap, ingKey)
190172
}
191173

192-
// SetServiceAttachment adds sa state to the map to be counted during metrics computation.
193-
// SetServiceAttachment implements PSCMetricsCollector.
194-
func (im *ControllerMetrics) SetServiceAttachment(saKey string, state pscmetrics.PSCState) {
195-
im.Lock()
196-
defer im.Unlock()
197-
198-
if im.pscMap == nil {
199-
klog.Fatalf("PSC Metrics failed to initialize correctly.")
200-
}
201-
im.pscMap[saKey] = state
202-
}
203-
204-
// DeleteServiceAttachment removes sa state to the map to be counted during metrics computation.
205-
// DeleteServiceAttachment implements PSCMetricsCollector.
206-
func (im *ControllerMetrics) DeleteServiceAttachment(saKey string) {
207-
im.Lock()
208-
defer im.Unlock()
209-
210-
delete(im.pscMap, saKey)
211-
}
212-
213-
// SetService adds the service to the map to be counted during metrics computation.
214-
// SetService implements PSCMetricsCollector.
215-
func (im *ControllerMetrics) SetService(serviceKey string) {
216-
im.Lock()
217-
defer im.Unlock()
218-
219-
if im.serviceMap == nil {
220-
klog.Fatalf("PSC Metrics failed to initialize correctly.")
221-
}
222-
im.serviceMap[serviceKey] = struct{}{}
223-
}
224-
225-
// DeleteService removes the service from the map to be counted during metrics computation.
226-
// DeleteService implements PSCMetricsCollector.
227-
func (im *ControllerMetrics) DeleteService(serviceKey string) {
228-
im.Lock()
229-
defer im.Unlock()
230-
231-
delete(im.serviceMap, serviceKey)
232-
}
233-
234174
// export computes and exports ingress usage metrics.
235175
func (im *ControllerMetrics) export() {
236176
defer func() {
@@ -251,21 +191,6 @@ func (im *ControllerMetrics) export() {
251191
}
252192

253193
im.logger.V(3).Info("Ingress usage metrics exported")
254-
255-
saCount := im.computePSCMetrics()
256-
im.logger.V(3).Info("Exporting PSC Usage Metrics", "serviceAttachmentsCount", saCount)
257-
for feature, count := range saCount {
258-
serviceAttachmentCount.With(prometheus.Labels{label: feature.String()}).Set(float64(count))
259-
}
260-
im.logger.V(3).Info("Exported PSC Usage Metrics", "serviceAttachmentsCount", saCount)
261-
262-
services := im.computeServiceMetrics()
263-
im.logger.V(3).Info("Exporting Service Metrics", "serviceCount", serviceCount)
264-
for feature, count := range services {
265-
serviceCount.With(prometheus.Labels{label: feature.String()}).Set(float64(count))
266-
}
267-
im.logger.V(3).Info("Exported Service Metrics", "serviceCount", serviceCount)
268-
269194
}
270195

271196
// computeIngressMetrics traverses all ingresses and computes,
@@ -323,34 +248,6 @@ func (im *ControllerMetrics) computeIngressMetrics() (map[feature]int, map[featu
323248
return ingCount, svcPortCount
324249
}
325250

326-
func (im *ControllerMetrics) computePSCMetrics() map[feature]int {
327-
im.Lock()
328-
defer im.Unlock()
329-
im.logger.V(4).Info("Compute PSC Usage metrics from psc state map", "pscStateMap", im.pscMap)
330-
331-
counts := map[feature]int{
332-
sa: 0,
333-
saInSuccess: 0,
334-
saInError: 0,
335-
}
336-
337-
for _, state := range im.pscMap {
338-
counts[sa]++
339-
if state.InSuccess {
340-
counts[saInSuccess]++
341-
} else {
342-
counts[saInError]++
343-
}
344-
}
345-
return counts
346-
}
347-
348-
func (im *ControllerMetrics) computeServiceMetrics() map[feature]int {
349-
return map[feature]int{
350-
services: len(im.serviceMap),
351-
}
352-
}
353-
354251
// initializeCounts initializes feature count maps for ingress and service ports.
355252
// This is required in order to reset counts for features that do not exist now
356253
// but existed before.

pkg/metrics/metrics_test.go

Lines changed: 0 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package metrics
1818

1919
import (
2020
"fmt"
21-
"strconv"
2221
"testing"
2322

2423
"k8s.io/klog/v2"
@@ -30,7 +29,6 @@ import (
3029
"k8s.io/ingress-gce/pkg/annotations"
3130
backendconfigv1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1"
3231
frontendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/frontendconfig/v1beta1"
33-
pscmetrics "k8s.io/ingress-gce/pkg/psc/metrics"
3432
"k8s.io/ingress-gce/pkg/utils"
3533
"k8s.io/utils/ptr"
3634
)
@@ -1242,157 +1240,3 @@ func TestComputeIngressMetrics(t *testing.T) {
12421240
})
12431241
}
12441242
}
1245-
1246-
func TestComputePSCMetrics(t *testing.T) {
1247-
t.Parallel()
1248-
for _, tc := range []struct {
1249-
desc string
1250-
saStates []pscmetrics.PSCState
1251-
// service attachments to delete
1252-
deleteStates []string
1253-
expectSACount map[feature]int
1254-
}{
1255-
{
1256-
desc: "empty input",
1257-
saStates: []pscmetrics.PSCState{},
1258-
expectSACount: map[feature]int{
1259-
sa: 0,
1260-
saInSuccess: 0,
1261-
saInError: 0,
1262-
},
1263-
},
1264-
{
1265-
desc: "one service attachment",
1266-
saStates: []pscmetrics.PSCState{
1267-
newPSCState(true),
1268-
},
1269-
expectSACount: map[feature]int{
1270-
sa: 1,
1271-
saInSuccess: 1,
1272-
saInError: 0,
1273-
},
1274-
},
1275-
{
1276-
desc: "one service attachment in error",
1277-
saStates: []pscmetrics.PSCState{
1278-
newPSCState(false),
1279-
},
1280-
expectSACount: map[feature]int{
1281-
sa: 1,
1282-
saInSuccess: 0,
1283-
saInError: 1,
1284-
},
1285-
},
1286-
{
1287-
desc: "many service attachments, some in error",
1288-
saStates: []pscmetrics.PSCState{
1289-
newPSCState(true),
1290-
newPSCState(true),
1291-
newPSCState(true),
1292-
newPSCState(false),
1293-
newPSCState(false),
1294-
},
1295-
expectSACount: map[feature]int{
1296-
sa: 5,
1297-
saInSuccess: 3,
1298-
saInError: 2,
1299-
},
1300-
},
1301-
{
1302-
desc: "some additions, and some deletions",
1303-
saStates: []pscmetrics.PSCState{
1304-
newPSCState(true),
1305-
newPSCState(true),
1306-
newPSCState(true),
1307-
newPSCState(false),
1308-
newPSCState(false),
1309-
},
1310-
deleteStates: []string{"0", "3"},
1311-
expectSACount: map[feature]int{
1312-
sa: 3,
1313-
saInSuccess: 2,
1314-
saInError: 1,
1315-
},
1316-
},
1317-
} {
1318-
tc := tc
1319-
t.Run(tc.desc, func(t *testing.T) {
1320-
t.Parallel()
1321-
newMetrics := FakeControllerMetrics()
1322-
for i, serviceState := range tc.saStates {
1323-
newMetrics.SetServiceAttachment(strconv.Itoa(i), serviceState)
1324-
}
1325-
1326-
for _, key := range tc.deleteStates {
1327-
newMetrics.DeleteServiceAttachment(key)
1328-
}
1329-
got := newMetrics.computePSCMetrics()
1330-
if diff := cmp.Diff(tc.expectSACount, got); diff != "" {
1331-
t.Fatalf("Got diff for service attachment counts (-want +got):\n%s", diff)
1332-
}
1333-
})
1334-
}
1335-
}
1336-
1337-
func newPSCState(inSuccess bool) pscmetrics.PSCState {
1338-
return pscmetrics.PSCState{
1339-
InSuccess: inSuccess,
1340-
}
1341-
}
1342-
1343-
func TestComputeServiceMetrics(t *testing.T) {
1344-
t.Parallel()
1345-
for _, tc := range []struct {
1346-
desc string
1347-
services []string
1348-
deleteServices []string
1349-
expectSACount map[feature]int
1350-
}{
1351-
{
1352-
desc: "empty input",
1353-
expectSACount: map[feature]int{
1354-
services: 0,
1355-
},
1356-
},
1357-
{
1358-
desc: "one service",
1359-
services: []string{"service-1"},
1360-
expectSACount: map[feature]int{
1361-
services: 1,
1362-
},
1363-
},
1364-
{
1365-
desc: "many services",
1366-
services: []string{"service-1", "service-2", "service-3", "service-4", "service-5", "service-6"},
1367-
expectSACount: map[feature]int{
1368-
services: 6,
1369-
},
1370-
},
1371-
{
1372-
desc: "some additions, and some deletions",
1373-
services: []string{"service-1", "service-2", "service-3", "service-4", "service-5", "service-6"},
1374-
deleteServices: []string{"service-2", "service-5"},
1375-
expectSACount: map[feature]int{
1376-
services: 4,
1377-
},
1378-
},
1379-
} {
1380-
tc := tc
1381-
t.Run(tc.desc, func(t *testing.T) {
1382-
t.Parallel()
1383-
newMetrics := FakeControllerMetrics()
1384-
for _, service := range tc.services {
1385-
newMetrics.SetService(service)
1386-
}
1387-
1388-
for _, service := range tc.deleteServices {
1389-
newMetrics.DeleteService(service)
1390-
}
1391-
1392-
got := newMetrics.computeServiceMetrics()
1393-
if diff := cmp.Diff(tc.expectSACount, got); diff != "" {
1394-
t.Fatalf("Got diff for service counts (-want +got):\n%s", diff)
1395-
}
1396-
})
1397-
}
1398-
}

pkg/psc/controller.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"k8s.io/ingress-gce/pkg/context"
4545
"k8s.io/ingress-gce/pkg/flags"
4646
"k8s.io/ingress-gce/pkg/psc/metrics"
47+
"k8s.io/ingress-gce/pkg/psc/metrics/metricscollector"
4748
serviceattachmentclient "k8s.io/ingress-gce/pkg/serviceattachment/client/clientset/versioned"
4849
"k8s.io/ingress-gce/pkg/utils"
4950
"k8s.io/ingress-gce/pkg/utils/namer"
@@ -56,6 +57,7 @@ import (
5657
func init() {
5758
// register prometheus metrics
5859
metrics.RegisterMetrics()
60+
metricscollector.RegisterMetrics()
5961
}
6062

6163
const (
@@ -92,7 +94,7 @@ type Controller struct {
9294
svcAttachmentLister cache.Indexer
9395
serviceLister cache.Indexer
9496
recorder func(string) record.EventRecorder
95-
collector metrics.PSCMetricsCollector
97+
collector *metricscollector.PSCMetricsCollector
9698

9799
hasSynced func() bool
98100

@@ -116,6 +118,7 @@ type Controller struct {
116118
func NewController(ctx *context.ControllerContext, stopCh <-chan struct{}, logger klog.Logger) *Controller {
117119
logger = logger.WithName("PSCController")
118120
saNamer := namer.NewServiceAttachmentNamer(ctx.ClusterNamer, string(ctx.KubeSystemUID))
121+
metricsCollector := metricscollector.NewPSCMetricsCollector(flags.F.MetricsExportInterval, logger)
119122
controller := &Controller{
120123
cloud: ctx.Cloud,
121124
saClient: ctx.SAClient,
@@ -125,7 +128,7 @@ func NewController(ctx *context.ControllerContext, stopCh <-chan struct{}, logge
125128
serviceLister: ctx.ServiceInformer.GetIndexer(),
126129
hasSynced: ctx.HasSynced,
127130
recorder: ctx.Recorder,
128-
collector: ctx.ControllerMetrics,
131+
collector: metricsCollector,
129132
clusterName: flags.F.GKEClusterName,
130133
regionalCluster: ctx.RegionalCluster,
131134
readOnlyMode: ctx.ReadOnlyMode,
@@ -183,6 +186,7 @@ func (c *Controller) Run() {
183186
time.Sleep(ServiceAttachmentGCPeriod)
184187
wait.Until(c.garbageCollectServiceAttachments, ServiceAttachmentGCPeriod, c.stopCh)
185188
}()
189+
go c.collector.Run(c.stopCh)
186190

187191
<-c.stopCh
188192
}

0 commit comments

Comments
 (0)