Skip to content

Commit

Permalink
custom unit report processing
Browse files Browse the repository at this point in the history
  • Loading branch information
jcollins-axway committed Nov 5, 2024
1 parent 05c3d86 commit e6d3d75
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
37 changes: 26 additions & 11 deletions pkg/customunit/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -171,6 +179,7 @@ func (c *customUnitClient) buildCustomMetricDetail(metricReport *cu.MetricReport
APIDetails: *apiDetails,
AppDetails: *appDetails,
UnitDetails: *planUnitDetails,
Count: metricReport.Count,
}, nil
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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 == "" {
Expand All @@ -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,
Expand All @@ -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(),
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/customunit/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ 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
}
// iterate over each metric service config
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()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/transaction/metric/metricscollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit e6d3d75

Please sign in to comment.