diff --git a/pkg/customunit/client.go b/pkg/customunit/client.go index 398d87640..b5533382e 100644 --- a/pkg/customunit/client.go +++ b/pkg/customunit/client.go @@ -8,6 +8,8 @@ import ( agentcache "github.com/Axway/agent-sdk/pkg/agent/cache" cu "github.com/Axway/agent-sdk/pkg/amplify/agent/customunits" v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" + management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1" + "github.com/Axway/agent-sdk/pkg/apic/definitions" "github.com/Axway/agent-sdk/pkg/transaction/models" "github.com/Axway/agent-sdk/pkg/util" "github.com/sirupsen/logrus" @@ -59,6 +61,12 @@ func NewCustomUnitClientFactory(url string, agentCache cache.Manager, quotaInfo } } +func WithMetricCollector(collector metricCollector) CustomUnitOption { + return func(c *customUnitClient) { + c.metricCollector = collector + } +} + func WithGRPCDialOption(opt grpc.DialOption) CustomUnitOption { return func(c *customUnitClient) { c.dialOpts = append(c.dialOpts, opt) @@ -171,6 +179,7 @@ func (c *customUnitClient) buildCustomMetricDetail(metricReport *cu.MetricReport APIDetails: *apiDetails, AppDetails: *appDetails, UnitDetails: *planUnitDetails, + Count: metricReport.Count, }, nil } @@ -220,8 +229,10 @@ func (c *customUnitClient) APIServiceLookup(apiServiceLookup *cu.APIServiceLooku return nil, nil } + id, _ := util.GetAgentDetailsValue(apiSvc, definitions.AttrExternalAPIID) //TODO err handle + return &models.APIDetails{ - ID: apiSvc.Metadata.ID, + ID: id, Name: apiSvc.Name, }, nil } @@ -230,7 +241,7 @@ func (c *customUnitClient) ManagedApplicationLookup(appLookup *cu.AppLookup) (*m appValue := appLookup.GetValue() appLookupType := appLookup.GetType() appCustomAttr := appLookup.GetCustomAttribute() - var managedApp *v1.ResourceInstance + var managedAppRI *v1.ResourceInstance var err error if appLookupType == cu.AppLookupType_CustomAppLookup && appValue == "" { @@ -242,29 +253,34 @@ func (c *customUnitClient) ManagedApplicationLookup(appLookup *cu.AppLookup) (*m } switch appLookupType { + case cu.AppLookupType_ExternalAppID: + appCustomAttr = definitions.AttrExternalAPIID + fallthrough case cu.AppLookupType_CustomAppLookup: for _, key := range c.cache.GetAPIServiceKeys() { app := c.cache.GetManagedApplication(key) val, _ := util.GetAgentDetailsValue(app, appCustomAttr) if val == appValue { - managedApp = app + managedAppRI = app break } } - case cu.AppLookupType_ExternalAppID: - managedApp = c.cache.GetManagedApplication(appValue) case cu.AppLookupType_ManagedAppID: - managedApp = c.cache.GetManagedApplication(appValue) + managedAppRI = c.cache.GetManagedApplication(appValue) case cu.AppLookupType_ManagedAppName: - managedApp = c.cache.GetManagedApplicationByName(appValue) + managedAppRI = c.cache.GetManagedApplicationByName(appValue) } - if managedApp == nil { + if managedAppRI == nil { return nil, nil } + managedApp := &management.ManagedApplication{} + managedApp.FromInstance(managedAppRI) //TODO err handle + consumerOrgID := "" - if managedApp.Owner != nil { - consumerOrgID = managedApp.Owner.ID + if managedApp.Marketplace.Resource.Owner != nil { + consumerOrgID = managedApp.Marketplace.Resource.Owner.ID } + return &models.AppDetails{ ID: managedApp.Metadata.ID, Name: managedApp.Name, @@ -273,7 +289,6 @@ func (c *customUnitClient) ManagedApplicationLookup(appLookup *cu.AppLookup) (*m } func (c *customUnitClient) PlanUnitLookup(planUnitLookup *cu.UnitLookup) *models.Unit { - return &models.Unit{ Name: planUnitLookup.GetUnitName(), } diff --git a/pkg/customunit/manager.go b/pkg/customunit/manager.go index a57310e24..a2a90af00 100644 --- a/pkg/customunit/manager.go +++ b/pkg/customunit/manager.go @@ -133,7 +133,7 @@ func (h *CustomUnitMetricServerManager) getServiceInstance(_ context.Context, ar return instance, nil } -func (m *CustomUnitMetricServerManager) HandleMetricReporting(ctx context.Context, cancelCtx context.CancelFunc) { +func (m *CustomUnitMetricServerManager) HandleMetricReporting(ctx context.Context, cancelCtx context.CancelFunc, metricCollector metricCollector) { if m.agentType != config.TraceabilityAgent { return } @@ -141,7 +141,7 @@ func (m *CustomUnitMetricServerManager) HandleMetricReporting(ctx context.Contex for _, config := range m.configs { // Initialize custom units client factory := NewCustomUnitClientFactory(config.URL, m.cache, &customunits.QuotaInfo{}) - client, _ := factory(ctx, cancelCtx) + client, _ := factory(ctx, cancelCtx, WithMetricCollector(metricCollector)) go client.MetricReporting() } diff --git a/pkg/transaction/metric/metricscollector.go b/pkg/transaction/metric/metricscollector.go index bb583c870..d64df778a 100644 --- a/pkg/transaction/metric/metricscollector.go +++ b/pkg/transaction/metric/metricscollector.go @@ -150,7 +150,7 @@ func GetMetricCollector() Collector { globalMetricCollector = createMetricCollector() } ctx, ctxCancel := context.WithCancel(context.Background()) - agent.GetCustomUnitMetricServerManager().HandleMetricReporting(ctx, ctxCancel) + agent.GetCustomUnitMetricServerManager().HandleMetricReporting(ctx, ctxCancel, globalMetricCollector) return globalMetricCollector }