Skip to content

Commit b576712

Browse files
feat: Datapower object count
This is slightly different to the v1 implementation in that it uses a label to indicate object type rather than a unique metric for each. #125 Signed-off-by: Ricky Moorhouse <[email protected]>
1 parent 3fa51fc commit b576712

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

nets/datapower/datapower.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ type APIDocumentCachingSummaryResponse struct {
146146
}
147147
}
148148

149+
// Response from the /mgmt/status/{domain}/ObjectInstanceCounts endpoint
150+
// of the DataPower REST Management Interface.
151+
type ObjectInstanceCountsResponse struct {
152+
Links Links `json:"_links"`
153+
ObjectInstanceCounts []struct {
154+
Class string
155+
Count uint64
156+
}
157+
}
158+
149159
// Response from the /mgmt/status/{domain}/AnalyticsEndpointStatus2 endpoint of
150160
// the DataPower REST Management Interface.
151161
type AnalyticsEndpointStatus2Response struct {
@@ -234,6 +244,9 @@ func (d *DataPower) registerMetrics() {
234244
// User defined policies Status
235245
d.metrics["user_defined_policies_info"] = promauto.NewGaugeVec(prometheus.GaugeOpts{Name: "datapower_user_defined_policies_info"}, []string{"pod", "namespace", "policy", "version"})
236246

247+
// Object count
248+
d.metrics["object_count"] = promauto.NewGaugeVec(prometheus.GaugeOpts{Name: "datapower_object_count_total"}, []string{"pod", "namespace", "type"})
249+
237250
// Invoke API Tests
238251
d.metrics["invoke_api_size"] = promauto.NewGaugeVec(prometheus.GaugeOpts{Name: "datapower_invoke_api_size", Help: "invoke response content length"}, []string{"pod", "namespace", "name"})
239252
d.metrics["invoke_api_time"] = promauto.NewGaugeVec(prometheus.GaugeOpts{Name: "datapower_invoke_api_time", Help: "invoke time taken in ms"}, []string{"pod", "namespace", "name"})
@@ -265,9 +278,9 @@ func (d *DataPower) findGW(dynamicClient dynamic.DynamicClient) error {
265278
log.Log(alog.ERROR, "Failed to list pods: %v", err)
266279
return err
267280
}
268-
281+
log.Log(alog.TRACE, "pods found: ", len(pods.Items))
269282
for _, pod := range pods.Items {
270-
log.Log(alog.TRACE, "pod name: %v", pod.Name)
283+
log.Log(alog.DEBUG, "pod name: %v", pod.Name)
271284
var V5Compatible bool
272285
//Check for APICONNECT_V5_COMPAT_MODE set to on
273286
for _, env := range pod.Spec.Containers[0].Env {
@@ -277,6 +290,7 @@ func (d *DataPower) findGW(dynamicClient dynamic.DynamicClient) error {
277290
}
278291
}
279292
ip := pod.Status.PodIP
293+
log.Log(alog.DEBUG, "Pod IP is ", ip)
280294
if d.Config.Host != "" {
281295
log.Log(alog.TRACE, "Overriding host from %s to %s", ip, d.Config.Host)
282296
ip = d.Config.Host
@@ -288,8 +302,7 @@ func (d *DataPower) findGW(dynamicClient dynamic.DynamicClient) error {
288302
d.gatewayPeeringStatus(ip, pod.Name, pod.Namespace)
289303
d.openTelemetryExporterStatus(ip, pod.Name, pod.Namespace)
290304
d.apiConnectGatewayServiceStatus(ip, pod.Name, pod.Namespace)
291-
// Still to do:
292-
// - Object Counts
305+
d.objectCounts(ip, pod.Name, pod.Namespace)
293306
if V5Compatible {
294307
// - [v5c] WSMAgentStatus
295308
// - [v5c] Cache Summary
@@ -413,6 +426,35 @@ func (d *DataPower) firmwareVersion(ip string, podName string, podNamespace stri
413426
d.metrics["version"].WithLabelValues(podName, podNamespace, fw.FirmwareVersion3.Version, fw.FirmwareVersion3.Build).Set(1.0)
414427
}
415428

429+
// *DataPower.objectCounts makes a request to the ObjectInstanceCounts endpoint
430+
// and stores the resulting values to their matching *prometheus.GaugeVec items
431+
// in the *DataPower.metrics map.
432+
func (d *DataPower) objectCounts(ip string, podName string, podNamespace string) {
433+
log.Log(alog.TRACE, "Entering objectCounts(%s, %s, %s)", ip, podName, podNamespace)
434+
response, err := d.invokeRestMgmt(ip, "mgmt/status/apiconnect/ObjectInstanceCounts")
435+
if err != nil {
436+
log.Log(alog.ERROR, err.Error())
437+
return
438+
}
439+
440+
var oc ObjectInstanceCountsResponse
441+
err = json.NewDecoder(response.Body).Decode(&oc)
442+
if err != nil {
443+
log.Log(alog.ERROR, err.Error())
444+
}
445+
err2 := response.Body.Close()
446+
if err2 != nil {
447+
log.Log(alog.ERROR, err2.Error())
448+
}
449+
// Loop through the response array and publish metrics
450+
for _, ocdata := range oc.ObjectInstanceCounts {
451+
d.metrics["object_count"].WithLabelValues(podName, podNamespace, ocdata.Class).Set(float64(ocdata.Count))
452+
}
453+
if err != nil || err2 != nil {
454+
return
455+
}
456+
}
457+
416458
// *DataPower.logTargetStatus makes a request to the LogTargetStatus endpoint
417459
// and stores the resulting values to their matching *prometheus.GaugeVec items
418460
// in the *DataPower.metrics map.

0 commit comments

Comments
 (0)