Skip to content

Commit

Permalink
APIGOV-29054 - use time from metric report for metric event
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekschauhan committed Oct 22, 2024
1 parent 751c771 commit 1f25ba7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
3 changes: 3 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"fmt"
"time"

v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
)
Expand Down Expand Up @@ -81,6 +82,8 @@ type PolicyDetail struct {
}

type MetricEvent struct {
StartTime time.Time
EndTime time.Time
APIID string
Instance *v1.ResourceInstance
ClientID string
Expand Down
48 changes: 26 additions & 22 deletions pkg/traceability/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/Axway/agent-sdk/pkg/agent"
coreagent "github.com/Axway/agent-sdk/pkg/agent"
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"
cache "github.com/Axway/agent-sdk/pkg/cache"
Expand Down Expand Up @@ -112,21 +112,41 @@ func (a *Agent) processEvent(me cmn.MetricEvent) {
return
}

a.collector.AddAPIMetricDetail(metric.MetricDetail{
APIDetails: a.getAPIDetails(me),
AppDetails: a.getAppDetails(me),
StatusCode: me.StatusCode,
Count: me.Count,
Response: metric.ResponseMetrics{
Max: me.Max,
Min: me.Min,
},
Observation: metric.ObservationDetails{
Start: me.StartTime.UnixMilli(),
End: me.EndTime.UnixMilli(),
},
})
}

func (a *Agent) getAPIDetails(me cmn.MetricEvent) models.APIDetails {
apisRef := me.Instance.GetReferenceByGVK(management.APIServiceGVK())
externalAPIID, _ := coreutil.GetAgentDetailsValue(me.Instance, definitions.AttrExternalAPIID)
stage, _ := coreutil.GetAgentDetailsValue(me.Instance, definitions.AttrExternalAPIStage)
apiDetails := models.APIDetails{
return models.APIDetails{
ID: externalAPIID,
Name: apisRef.Name,
Revision: 1,
APIServiceInstance: me.Instance.Name,
Stage: stage,
}
}

func (a *Agent) getAppDetails(me cmn.MetricEvent) models.AppDetails {
appDetails := models.AppDetails{}
if me.ClientID != "" {
if ri, err := a.credentialCache.Get(me.ClientID); err == nil && ri != nil {
appRef := me.Instance.GetReferenceByGVK(management.ManagedApplicationGVK())
if item, err := a.credentialCache.Get(me.ClientID); err == nil && item != nil {
ri, ok := item.(*v1.ResourceInstance)
if ok && ri != nil {
appRef := ri.GetReferenceByGVK(management.ManagedApplicationGVK())
app := agent.GetCacheManager().GetManagedApplicationByName(appRef.Name)
if app != nil {
managedApp := &management.ManagedApplication{}
Expand All @@ -139,23 +159,7 @@ func (a *Agent) processEvent(me cmn.MetricEvent) {
}
}
}

response := metric.ResponseMetrics{
Max: me.Max,
Min: me.Min,
}

a.collector.AddAPIMetricDetail(metric.MetricDetail{
APIDetails: apiDetails,
AppDetails: appDetails,
StatusCode: me.StatusCode,
Count: me.Count,
Response: response,
Observation: metric.ObservationDetails{
Start: time.Now().UnixMilli(),
End: time.Now().UnixMilli(),
},
})
return appDetails
}

// onConfigChange apply configuration changes
Expand Down
9 changes: 7 additions & 2 deletions pkg/traceability/muleemitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ func (me *MuleEventEmitter) Start() error {

endTime := lastAPIReportTime
for _, metric := range metrics {
// Results are not sorted. We want the most recent time to bubble up
// Report only latest entries, ignore old entries
if metric.Time.After(lastAPIReportTime) {
endTime = metric.Time
for _, event := range metric.Events {
m := common.MetricEvent{
StartTime: lastAPIReportTime,
EndTime: metric.Time,
APIID: apiID,
Instance: instance,
StatusCode: event.StatusCode,
Expand All @@ -102,6 +103,10 @@ func (me *MuleEventEmitter) Start() error {
me.eventChannel <- m
}
}
// Results are not sorted. We want the most recent time to bubble up for next run cycle
if metric.Time.After(endTime) {
endTime = metric.Time
}
}
me.saveLastRun(apiID, endTime)
}
Expand Down

0 comments on commit 1f25ba7

Please sign in to comment.