@@ -85,20 +85,20 @@ func NewPrometheusServer(vpp *vpplink.VppLink, log *logrus.Entry) *PrometheusSer
85
85
86
86
func cleanVppIfStatName (vppStatName string ) string {
87
87
vppStatName = strings .TrimPrefix (vppStatName , "/if/" )
88
- vppStatName = strings .Replace (vppStatName , "-" , "_" , - 1 )
88
+ vppStatName = strings .ReplaceAll (vppStatName , "-" , "_" )
89
89
return vppStatName
90
90
}
91
91
92
92
func cleanVppTCPStatName (vppStatName string , prefix string ) string {
93
93
vppStatName = strings .TrimPrefix (vppStatName , prefix )
94
- vppStatName = strings .Replace (vppStatName , "-" , "_" , - 1 )
95
- vppStatName = strings .Replace (vppStatName , "/" , "_" , - 1 )
94
+ vppStatName = strings .ReplaceAll (vppStatName , "-" , "_" )
95
+ vppStatName = strings .ReplaceAll (vppStatName , "/" , "_" )
96
96
return vppStatName
97
97
}
98
98
99
99
func cleanVppSessionStatName (vppStatName string ) string {
100
100
vppStatName = strings .TrimPrefix (vppStatName , "/sys/session/" )
101
- vppStatName = strings .Replace (vppStatName , "/" , "_" , - 1 )
101
+ vppStatName = strings .ReplaceAll (vppStatName , "/" , "_" )
102
102
return vppStatName
103
103
}
104
104
@@ -107,10 +107,10 @@ const (
107
107
UnitBytes = "bytes"
108
108
)
109
109
110
- func (self * PrometheusServer ) exportMetrics () error {
111
- ifStats , err := self .statsclient .DumpStats ("/if/" )
110
+ func (p * PrometheusServer ) exportMetrics () error {
111
+ ifStats , err := p .statsclient .DumpStats ("/if/" )
112
112
if err != nil {
113
- self .log .Errorf ("Error running statsclient.DumpStats for Interface stats %v" , err )
113
+ p .log .Errorf ("Error running statsclient.DumpStats for Interface stats %v" , err )
114
114
return nil
115
115
}
116
116
var ifNames adapter.NameStat
@@ -121,79 +121,79 @@ func (self *PrometheusServer) exportMetrics() error {
121
121
}
122
122
}
123
123
124
- self .lock .Lock ()
125
- defer self .lock .Unlock ()
124
+ p .lock .Lock ()
125
+ defer p .lock .Unlock ()
126
126
127
127
// Export Interface stats
128
128
for _ , vppStat := range ifStats {
129
129
switch values := vppStat .Data .(type ) {
130
130
case adapter.SimpleCounterStat :
131
- self .exportInterfaceSimpleCounterStat (string (vppStat .Name ), ifNames , values )
131
+ p .exportInterfaceSimpleCounterStat (string (vppStat .Name ), ifNames , values )
132
132
case adapter.CombinedCounterStat :
133
- self .exportInterfaceCombinedCounterStat (string (vppStat .Name )+ "_packets" , ifNames , UnitPackets , values )
134
- self .exportInterfaceCombinedCounterStat (string (vppStat .Name )+ "_bytes" , ifNames , UnitBytes , values )
133
+ p .exportInterfaceCombinedCounterStat (string (vppStat .Name )+ "_packets" , ifNames , UnitPackets , values )
134
+ p .exportInterfaceCombinedCounterStat (string (vppStat .Name )+ "_bytes" , ifNames , UnitBytes , values )
135
135
}
136
136
}
137
137
138
138
// Export TCP stats
139
- tcpStats , err := self .statsclient .DumpStats ("/sys/tcp" )
139
+ tcpStats , err := p .statsclient .DumpStats ("/sys/tcp" )
140
140
if err != nil {
141
- self .log .Errorf ("Error running statsclient.DumpStats for TCP stats %v" , err )
141
+ p .log .Errorf ("Error running statsclient.DumpStats for TCP stats %v" , err )
142
142
return nil
143
143
}
144
144
for _ , vppStat := range tcpStats {
145
145
switch values := vppStat .Data .(type ) {
146
146
case adapter.SimpleCounterStat :
147
- self .exportTCPSimpleCounterStat (cleanVppTCPStatName (string (vppStat .Name ), "/sys/" ), values )
147
+ p .exportTCPSimpleCounterStat (cleanVppTCPStatName (string (vppStat .Name ), "/sys/" ), values )
148
148
}
149
149
}
150
150
151
151
// Export TCP4 error stats
152
- tcp4ErrStats , err := self .statsclient .DumpStats ("/err/tcp4" )
152
+ tcp4ErrStats , err := p .statsclient .DumpStats ("/err/tcp4" )
153
153
if err != nil {
154
- self .log .Errorf ("Error running statsclient.DumpStats for TCP4 error stats %v" , err )
154
+ p .log .Errorf ("Error running statsclient.DumpStats for TCP4 error stats %v" , err )
155
155
return nil
156
156
}
157
157
for _ , vppStat := range tcp4ErrStats {
158
158
switch values := vppStat .Data .(type ) {
159
159
case adapter.SimpleCounterStat :
160
- self .exportTCPSimpleCounterStat (cleanVppTCPStatName (string (vppStat .Name ), "/err/" ), values )
160
+ p .exportTCPSimpleCounterStat (cleanVppTCPStatName (string (vppStat .Name ), "/err/" ), values )
161
161
}
162
162
}
163
163
164
164
// Export TCP6 error stats
165
- tcp6ErrStats , err := self .statsclient .DumpStats ("/err/tcp6" )
165
+ tcp6ErrStats , err := p .statsclient .DumpStats ("/err/tcp6" )
166
166
if err != nil {
167
- self .log .Errorf ("Error running statsclient.DumpStats for TCP6 error stats %v" , err )
167
+ p .log .Errorf ("Error running statsclient.DumpStats for TCP6 error stats %v" , err )
168
168
return nil
169
169
}
170
170
for _ , vppStat := range tcp6ErrStats {
171
171
switch values := vppStat .Data .(type ) {
172
172
case adapter.SimpleCounterStat :
173
- self .exportTCPSimpleCounterStat (cleanVppTCPStatName (string (vppStat .Name ), "/err/" ), values )
173
+ p .exportTCPSimpleCounterStat (cleanVppTCPStatName (string (vppStat .Name ), "/err/" ), values )
174
174
}
175
175
}
176
176
177
177
// Export Session stats
178
- sessionStats , err := self .statsclient .DumpStats ("/sys/session" )
178
+ sessionStats , err := p .statsclient .DumpStats ("/sys/session" )
179
179
if err != nil {
180
- self .log .Errorf ("Error running statsclient.DumpStats for Session stats %v" , err )
180
+ p .log .Errorf ("Error running statsclient.DumpStats for Session stats %v" , err )
181
181
return nil
182
182
}
183
183
for _ , vppStat := range sessionStats {
184
184
switch values := vppStat .Data .(type ) {
185
185
case adapter.SimpleCounterStat :
186
- self .exportSessionSimpleCounter (string (vppStat .Name ), values )
186
+ p .exportSessionSimpleCounter (string (vppStat .Name ), values )
187
187
case adapter.ScalarStat :
188
188
// ScalarStat is a single value, not per-worker
189
- self .exportSessionScalarStat (string (vppStat .Name ), int64 (values ))
189
+ p .exportSessionScalarStat (string (vppStat .Name ), int64 (values ))
190
190
}
191
191
}
192
192
193
193
return nil
194
194
}
195
195
196
- func (self * PrometheusServer ) exportInterfaceCombinedCounterStat (name string , ifNames adapter.NameStat , unit string , values adapter.CombinedCounterStat ) {
196
+ func (p * PrometheusServer ) exportInterfaceCombinedCounterStat (name string , ifNames adapter.NameStat , unit string , values adapter.CombinedCounterStat ) {
197
197
metric := & metricspb.Metric {
198
198
MetricDescriptor : & metricspb.MetricDescriptor {
199
199
Name : cleanVppIfStatName (name ),
@@ -212,8 +212,8 @@ func (self *PrometheusServer) exportInterfaceCombinedCounterStat(name string, if
212
212
}
213
213
for worker , perWorkerValues := range values {
214
214
for swIfIndex , counter := range perWorkerValues {
215
- self .log .Warnf ("Export for IF=%d" , swIfIndex )
216
- pod := self .podInterfacesDetailsBySwifIndex [uint32 (swIfIndex )]
215
+ p .log .Warnf ("Export for IF=%d" , swIfIndex )
216
+ pod := p .podInterfacesDetailsBySwifIndex [uint32 (swIfIndex )]
217
217
vppIfName := ""
218
218
if swIfIndex < len (ifNames ) {
219
219
vppIfName = string (ifNames [swIfIndex ])
@@ -238,18 +238,18 @@ func (self *PrometheusServer) exportInterfaceCombinedCounterStat(name string, if
238
238
})
239
239
}
240
240
}
241
- err := self .exporter .ExportMetric (
241
+ err := p .exporter .ExportMetric (
242
242
context .Background (),
243
243
nil , /* node */
244
244
nil , /* resource */
245
245
metric ,
246
246
)
247
247
if err != nil {
248
- self .log .Errorf ("Error prometheus exporter.ExportMetric %v" , err )
248
+ p .log .Errorf ("Error prometheus exporter.ExportMetric %v" , err )
249
249
}
250
250
}
251
251
252
- func (self * PrometheusServer ) exportInterfaceSimpleCounterStat (name string , ifNames adapter.NameStat , values adapter.SimpleCounterStat ) {
252
+ func (p * PrometheusServer ) exportInterfaceSimpleCounterStat (name string , ifNames adapter.NameStat , values adapter.SimpleCounterStat ) {
253
253
metric := & metricspb.Metric {
254
254
MetricDescriptor : & metricspb.MetricDescriptor {
255
255
Name : cleanVppIfStatName (name ),
@@ -267,7 +267,7 @@ func (self *PrometheusServer) exportInterfaceSimpleCounterStat(name string, ifNa
267
267
}
268
268
for worker , perWorkerValues := range values {
269
269
for swIfIndex , counter := range perWorkerValues {
270
- pod := self .podInterfacesDetailsBySwifIndex [uint32 (swIfIndex )]
270
+ pod := p .podInterfacesDetailsBySwifIndex [uint32 (swIfIndex )]
271
271
vppIfName := ""
272
272
if swIfIndex < len (ifNames ) {
273
273
vppIfName = string (ifNames [swIfIndex ])
@@ -290,18 +290,18 @@ func (self *PrometheusServer) exportInterfaceSimpleCounterStat(name string, ifNa
290
290
})
291
291
}
292
292
}
293
- err := self .exporter .ExportMetric (
293
+ err := p .exporter .ExportMetric (
294
294
context .Background (),
295
295
nil , /* node */
296
296
nil , /* resource */
297
297
metric ,
298
298
)
299
299
if err != nil {
300
- self .log .Errorf ("Error prometheus exporter.ExportMetric %v" , err )
300
+ p .log .Errorf ("Error prometheus exporter.ExportMetric %v" , err )
301
301
}
302
302
}
303
303
304
- func (self * PrometheusServer ) exportTCPSimpleCounterStat (name string , values adapter.SimpleCounterStat ) {
304
+ func (p * PrometheusServer ) exportTCPSimpleCounterStat (name string , values adapter.SimpleCounterStat ) {
305
305
metric := & metricspb.Metric {
306
306
MetricDescriptor : & metricspb.MetricDescriptor {
307
307
Name : name ,
@@ -330,18 +330,18 @@ func (self *PrometheusServer) exportTCPSimpleCounterStat(name string, values ada
330
330
}
331
331
}
332
332
333
- err := self .exporter .ExportMetric (
333
+ err := p .exporter .ExportMetric (
334
334
context .Background (),
335
335
nil , /* node */
336
336
nil , /* resource */
337
337
metric ,
338
338
)
339
339
if err != nil {
340
- self .log .Errorf ("Error prometheus exporter.ExportMetric for TCP %v" , err )
340
+ p .log .Errorf ("Error prometheus exporter.ExportMetric for TCP %v" , err )
341
341
}
342
342
}
343
343
344
- func (self * PrometheusServer ) exportSessionSimpleCounter (name string , values adapter.SimpleCounterStat ) {
344
+ func (p * PrometheusServer ) exportSessionSimpleCounter (name string , values adapter.SimpleCounterStat ) {
345
345
metric := & metricspb.Metric {
346
346
MetricDescriptor : & metricspb.MetricDescriptor {
347
347
Name : cleanVppSessionStatName (name ),
@@ -370,19 +370,19 @@ func (self *PrometheusServer) exportSessionSimpleCounter(name string, values ada
370
370
}
371
371
}
372
372
373
- err := self .exporter .ExportMetric (
373
+ err := p .exporter .ExportMetric (
374
374
context .Background (),
375
375
nil , /* node */
376
376
nil , /* resource */
377
377
metric ,
378
378
)
379
379
if err != nil {
380
- self .log .Errorf ("Error prometheus exporter.ExportMetric for Session %v" , err )
380
+ p .log .Errorf ("Error prometheus exporter.ExportMetric for Session %v" , err )
381
381
}
382
382
}
383
383
384
- func (self * PrometheusServer ) exportSessionScalarStat (name string , value int64 ) {
385
- err := self .exporter .ExportMetric (
384
+ func (p * PrometheusServer ) exportSessionScalarStat (name string , value int64 ) {
385
+ err := p .exporter .ExportMetric (
386
386
context .Background (),
387
387
nil , /* node */
388
388
nil , /* resource */
@@ -404,83 +404,91 @@ func (self *PrometheusServer) exportSessionScalarStat(name string, value int64)
404
404
},
405
405
)
406
406
if err != nil {
407
- self .log .Errorf ("Error prometheus exporter.ExportMetric for Session %v" , err )
407
+ p .log .Errorf ("Error prometheus exporter.ExportMetric for Session %v" , err )
408
408
}
409
409
}
410
410
411
- func (self * PrometheusServer ) ServePrometheus (t * tomb.Tomb ) error {
411
+ func (p * PrometheusServer ) ServePrometheus (t * tomb.Tomb ) error {
412
412
if ! (* config .GetCalicoVppFeatureGates ().PrometheusEnabled ) {
413
413
return nil
414
414
}
415
- self .log .Infof ("Serve() Prometheus exporter" )
415
+ p .log .Infof ("Serve() Prometheus exporter" )
416
416
go func () {
417
417
for t .Alive () {
418
418
/* Note: we will only receive events we ask for when registering the chan */
419
- evt := <- self .channel
419
+ evt := <- p .channel
420
420
switch evt .Type {
421
421
case common .PodAdded :
422
422
podSpec , ok := evt .New .(* storage.LocalPodSpec )
423
423
if ! ok {
424
- self .log .Errorf ("evt.New is not a *storage.LocalPodSpec %v" , evt .New )
424
+ p .log .Errorf ("evt.New is not a *storage.LocalPodSpec %v" , evt .New )
425
425
continue
426
426
}
427
- splittedWorkloadId := strings .SplitN (podSpec .WorkloadID , "/" , 2 )
428
- if len (splittedWorkloadId ) != 2 {
427
+ splittedWorkloadID := strings .SplitN (podSpec .WorkloadID , "/" , 2 )
428
+ if len (splittedWorkloadID ) != 2 {
429
429
continue
430
430
}
431
- self .lock .Lock ()
431
+ p .lock .Lock ()
432
432
if podSpec .MemifSwIfIndex != vpplink .InvalidSwIfIndex {
433
433
memifName := podSpec .InterfaceName
434
434
if podSpec .NetworkName == "" {
435
435
memifName = "vpp/memif-" + podSpec .InterfaceName
436
436
}
437
- self .podInterfacesDetailsBySwifIndex [podSpec .MemifSwIfIndex ] = podInterfaceDetails {
438
- podNamespace : splittedWorkloadId [0 ],
439
- podName : splittedWorkloadId [1 ],
437
+ p .podInterfacesDetailsBySwifIndex [podSpec .MemifSwIfIndex ] = podInterfaceDetails {
438
+ podNamespace : splittedWorkloadID [0 ],
439
+ podName : splittedWorkloadID [1 ],
440
440
interfaceName : memifName ,
441
441
}
442
442
}
443
443
if podSpec .TunTapSwIfIndex != vpplink .InvalidSwIfIndex {
444
- self .podInterfacesDetailsBySwifIndex [podSpec .TunTapSwIfIndex ] = podInterfaceDetails {
445
- podNamespace : splittedWorkloadId [0 ],
446
- podName : splittedWorkloadId [1 ],
444
+ p .podInterfacesDetailsBySwifIndex [podSpec .TunTapSwIfIndex ] = podInterfaceDetails {
445
+ podNamespace : splittedWorkloadID [0 ],
446
+ podName : splittedWorkloadID [1 ],
447
447
interfaceName : podSpec .InterfaceName ,
448
448
}
449
449
}
450
- self .podInterfacesByKey [podSpec .Key ()] = * podSpec
451
- self .lock .Unlock ()
450
+ p .podInterfacesByKey [podSpec .Key ()] = * podSpec
451
+ p .lock .Unlock ()
452
452
case common .PodDeleted :
453
453
podSpec , ok := evt .Old .(* storage.LocalPodSpec )
454
454
if ! ok {
455
- self .log .Errorf ("evt.Old is not a *storage.LocalPodSpec %v" , evt .Old )
455
+ p .log .Errorf ("evt.Old is not a *storage.LocalPodSpec %v" , evt .Old )
456
456
continue
457
457
}
458
- self .lock .Lock ()
459
- initialPod := self .podInterfacesByKey [podSpec .Key ()]
460
- delete (self .podInterfacesByKey , initialPod .Key ())
458
+ p .lock .Lock ()
459
+ initialPod := p .podInterfacesByKey [podSpec .Key ()]
460
+ delete (p .podInterfacesByKey , initialPod .Key ())
461
461
if podSpec .MemifSwIfIndex != vpplink .InvalidSwIfIndex {
462
- delete (self .podInterfacesDetailsBySwifIndex , initialPod .MemifSwIfIndex )
462
+ delete (p .podInterfacesDetailsBySwifIndex , initialPod .MemifSwIfIndex )
463
463
}
464
464
if podSpec .TunTapSwIfIndex != vpplink .InvalidSwIfIndex {
465
- delete (self .podInterfacesDetailsBySwifIndex , initialPod .TunTapSwIfIndex )
465
+ delete (p .podInterfacesDetailsBySwifIndex , initialPod .TunTapSwIfIndex )
466
466
}
467
- self .lock .Unlock ()
467
+ p .lock .Unlock ()
468
468
}
469
469
}
470
470
}()
471
- err := self .statsclient .Connect ()
471
+ err := p .statsclient .Connect ()
472
472
if err != nil {
473
473
return errors .Wrap (err , "could not connect statsclient" )
474
474
}
475
475
476
- go self .httpServer .ListenAndServe ()
476
+ go func () {
477
+ err := p .httpServer .ListenAndServe ()
478
+ if err != nil && err != http .ErrServerClosed {
479
+ p .log .Errorf ("HTTP server error: %v" , err )
480
+ }
481
+ }()
477
482
ticker := time .NewTicker (* config .GetCalicoVppInitialConfig ().PrometheusRecordMetricInterval )
478
483
for ; t .Alive (); <- ticker .C {
479
- self .exportMetrics ()
484
+ err := p .exportMetrics ()
485
+ if err != nil {
486
+ p .log .Errorf ("Error exporting metrics: %v" , err )
487
+ }
480
488
}
481
489
ticker .Stop ()
482
- self .log .Warn ("Prometheus Server returned" )
483
- err = self .httpServer .Shutdown (context .Background ())
490
+ p .log .Warn ("Prometheus Server returned" )
491
+ err = p .httpServer .Shutdown (context .Background ())
484
492
if err != nil {
485
493
return errors .Wrap (err , "Could not shutdown http server" )
486
494
}
0 commit comments