From 71472265acb42ad323a025a64834ec9813cc0cd4 Mon Sep 17 00:00:00 2001 From: Jason Collins Date: Mon, 28 Oct 2024 11:17:41 -0700 Subject: [PATCH] api service id lookup --- pkg/transaction/metric/cachestorage.go | 2 +- pkg/transaction/metric/metricscollector.go | 41 ++++++++++++---------- pkg/transaction/metric/util.go | 7 ++-- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/pkg/transaction/metric/cachestorage.go b/pkg/transaction/metric/cachestorage.go index 65d37733b..56f3bdc18 100644 --- a/pkg/transaction/metric/cachestorage.go +++ b/pkg/transaction/metric/cachestorage.go @@ -168,7 +168,7 @@ func (c *cacheStorage) loadMetrics(storageCache cache.Cache) { apiDetails.Name = cm.API.Name } appDetails := models.AppDetails{} - if cm.API != nil { + if cm.App != nil { appDetails.ID = cm.App.ID appDetails.ConsumerOrgID = cm.App.ConsumerOrgID } diff --git a/pkg/transaction/metric/metricscollector.go b/pkg/transaction/metric/metricscollector.go index 0a30477bc..fe78bb27b 100644 --- a/pkg/transaction/metric/metricscollector.go +++ b/pkg/transaction/metric/metricscollector.go @@ -272,14 +272,14 @@ func (c *collector) AddMetricDetail(metricDetail Detail) { c.AddMetric(metricDetail.APIDetails, metricDetail.StatusCode, metricDetail.Duration, metricDetail.Bytes, metricDetail.APIDetails.Name) c.createOrUpdateHistogram(metricDetail) // TODO remove this after testing - // c.AddCustomMetricDetail(CustomMetricDetail{ - // APIDetails: metricDetail.APIDetails, - // AppDetails: metricDetail.AppDetails, - // UnitDetails: models.Unit{ - // Name: "x-custom-token", - // }, - // Count: 30, - // }) + c.AddCustomMetricDetail(CustomMetricDetail{ + APIDetails: metricDetail.APIDetails, + AppDetails: metricDetail.AppDetails, + UnitDetails: models.Unit{ + Name: "x-ai-tokens", + }, + Count: 30, + }) } // AddAPIMetricDetail - add metric details for several response codes and transactions @@ -610,9 +610,9 @@ func (c *collector) createAPIDetail(api models.APIDetails) *models.APIResourceRe }, Name: api.Name, } - svc, err := agent.GetAPICache().GetBySecondaryKey(strings.TrimPrefix(api.ID, transutil.SummaryEventProxyIDPrefix)) - if err == nil { - ref.APIServiceID = svc.(v1.ResourceInstance).Metadata.ID + svc := agent.GetCacheManager().GetAPIServiceWithAPIID(strings.TrimPrefix(api.ID, transutil.SummaryEventProxyIDPrefix)) + if svc != nil { + ref.APIServiceID = svc.Metadata.ID } return ref } @@ -681,28 +681,33 @@ func (c *collector) getQuota(accessRequest *management.AccessRequest, unitName s if unitName == "" && accessRequest.Spec.Quota != nil { // get transactions quota for _, r := range accessRequest.References { - rMap := r.(map[string]string) - if rMap["kind"] != catalog.QuotaGVK().Kind { + rMap := r.(map[string]interface{}) + if rMap["kind"].(string) != catalog.QuotaGVK().Kind { continue } if _, ok := rMap["unit"]; !ok { // no unit is transactions - quotaName = strings.Split(rMap["name"], "/")[2] + quotaName = strings.Split(rMap["name"].(string), "/")[2] + break } } } else { // get custom unit quota for _, r := range accessRequest.References { - rMap := r.(map[string]string) - if rMap["kind"] != catalog.QuotaGVK().Kind { + rMap := r.(map[string]interface{}) + if rMap["kind"].(string) != catalog.QuotaGVK().Kind { continue } - if unit, ok := rMap["unit"]; ok && unitName == unit { + if unit, ok := rMap["unit"]; ok && unitName == unit.(string) { // no unit is transactions - quotaName = strings.Split(rMap["name"], "/")[2] + quotaName = strings.Split(rMap["name"].(string), "/")[2] + break } } } + if quotaName == "" { + return nil + } quotaRef := accessRequest.GetReferenceByNameAndGVK(quotaName, catalog.QuotaGVK()) if quotaRef.ID == "" { diff --git a/pkg/transaction/metric/util.go b/pkg/transaction/metric/util.go index 4367809f1..bedc2686a 100644 --- a/pkg/transaction/metric/util.go +++ b/pkg/transaction/metric/util.go @@ -4,7 +4,6 @@ import ( "strings" "github.com/Axway/agent-sdk/pkg/agent" - v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" "github.com/Axway/agent-sdk/pkg/transaction/models" transutil "github.com/Axway/agent-sdk/pkg/transaction/util" ) @@ -65,9 +64,9 @@ func centralMetricFromAPIMetric(in *APIMetric) *centralMetric { }, Name: in.API.Name, } - svc, err := agent.GetAPICache().GetBySecondaryKey(strings.TrimPrefix(in.API.ID, transutil.SummaryEventProxyIDPrefix)) - if err == nil { - out.API.APIServiceID = svc.(v1.ResourceInstance).Metadata.ID + svc := agent.GetCacheManager().GetAPIServiceWithAPIID(strings.TrimPrefix(in.API.ID, transutil.SummaryEventProxyIDPrefix)) + if svc != nil { + out.API.APIServiceID = svc.Metadata.ID } }