Skip to content

Commit

Permalink
Merge pull request #150 from Axway/APIGOV-19474
Browse files Browse the repository at this point in the history
APIGOV-19474 - show log if count is > 0
  • Loading branch information
sbolosan authored Jun 28, 2021
2 parents 765647a + cc8524b commit b6d31fc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
13 changes: 10 additions & 3 deletions pkg/traceability/traceability.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/Axway/agent-sdk/pkg/jobs"
"github.com/Axway/agent-sdk/pkg/traceability/sampling"
"github.com/Axway/agent-sdk/pkg/util/log"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/transport/tlscommon"
Expand Down Expand Up @@ -207,15 +206,23 @@ func (client *Client) Publish(batch publisher.Batch) error {
}

publishCount := len(batch.Events())
log.Infof("Creating %d transaction events", publishCount)

if publishCount > 0 {
log.Infof("Creating %d transaction events", publishCount)
}

//update the local activity timestamp for the event to compare against
agent.UpdateLocalActivityTime()
err = client.transportClient.Publish(batch)
if err != nil {
log.Error("Failed to publish transaction event : ", err.Error())
return err
}
log.Infof("%d events have been published", publishCount-len(batch.Events()))

if publishCount-len(batch.Events()) > 0 {
log.Infof("%d events have been published", publishCount-len(batch.Events()))
}

return nil
}

Expand Down
18 changes: 6 additions & 12 deletions pkg/transaction/metric/metricscollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Axway/agent-sdk/pkg/cmd"
"github.com/Axway/agent-sdk/pkg/jobs"
"github.com/Axway/agent-sdk/pkg/traceability"
"github.com/Axway/agent-sdk/pkg/util"
"github.com/Axway/agent-sdk/pkg/util/log"
jwt "github.com/dgrijalva/jwt-go"
metrics "github.com/rcrowley/go-metrics"
Expand Down Expand Up @@ -131,10 +132,7 @@ func (c *collector) Status() error {

// Ready - indicates that the collector job is ready to process
func (c *collector) Ready() bool {
if agent.GetCentralConfig().GetEnvironmentID() == "" {
return false
}
return true
return agent.GetCentralConfig().GetEnvironmentID() != ""
}

// Execute - process the metric collection and generation of usage/metric event
Expand All @@ -144,7 +142,7 @@ func (c *collector) Execute() error {

c.endTime = time.Now()
c.orgGUID = c.getOrgGUID()
log.Debugf("Generating usage/metric event [start timestamp: %d, end timestamp: %d]", convertTimeToMillis(c.startTime), convertTimeToMillis(c.endTime))
log.Debugf("Generating usage/metric event [start timestamp: %d, end timestamp: %d]", util.ConvertTimeToMillis(c.startTime), util.ConvertTimeToMillis(c.endTime))
defer func() {
c.cleanup()
}()
Expand All @@ -154,10 +152,6 @@ func (c *collector) Execute() error {
return nil
}

func convertTimeToMillis(tm time.Time) int64 {
return tm.UnixNano() / 1e6
}

// AddMetric - add metric for API transaction to collection
func (c *collector) AddMetric(apiID, apiName, statusCode string, duration int64, appName, teamName string) {
c.lock.Lock()
Expand Down Expand Up @@ -228,7 +222,7 @@ func (c *collector) generateEvents() {

c.registry.Each(c.processUsageFromRegistry)
if len(c.publishItemQueue) == 0 {
log.Infof("No usage/metric event generated as no transactions recorded [start timestamp: %d, end timestamp: %d]", convertTimeToMillis(c.startTime), convertTimeToMillis(c.endTime))
log.Infof("No usage/metric event generated as no transactions recorded [start timestamp: %d, end timestamp: %d]", util.ConvertTimeToMillis(c.startTime), util.ConvertTimeToMillis(c.endTime))
}
}

Expand Down Expand Up @@ -275,7 +269,7 @@ func (c *collector) generateLighthouseUsageEvent(transactionCount metrics.Counte
metric: transactionCount,
}
c.publishItemQueue = append(c.publishItemQueue, queueItem)
log.Infof("Published usage report [start timestamp: %d, end timestamp: %d]", convertTimeToMillis(c.startTime), convertTimeToMillis(c.endTime))
log.Infof("Published usage report [start timestamp: %d, end timestamp: %d]", util.ConvertTimeToMillis(c.startTime), util.ConvertTimeToMillis(c.endTime))
}

// func (c *collector) processTransactionMetric(metricName string, metric interface{}) {
Expand Down Expand Up @@ -354,7 +348,7 @@ func (c *collector) publishEvents() {
for _, eventQueueItem := range c.publishItemQueue {
err := c.publisher.publishEvent(eventQueueItem.GetEvent())
if err != nil {
log.Error("Failed to publish usage event : ", err.Error())
log.Errorf("Failed to publish usage event [start timestamp: %d, end timestamp: %d]: %s - current usage report is kept and will be added to the next trigger interval. ", util.ConvertTimeToMillis(c.startTime), util.ConvertTimeToMillis(c.endTime), err.Error())
} else {
c.cleanupCounters(eventQueueItem)
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"hash/fnv"
"net/http"
"net/url"
"time"

"github.com/Axway/agent-sdk/pkg/util/log"
"github.com/sirupsen/logrus"
Expand All @@ -16,7 +17,7 @@ import (
func ComputeHash(data interface{}) (uint64, error) {
dataB, err := json.Marshal(data)
if err != nil {
return 0, fmt.Errorf("Could not marshal data to bytes")
return 0, fmt.Errorf("could not marshal data to bytes")
}

h := fnv.New64a()
Expand Down Expand Up @@ -110,3 +111,8 @@ func RemoveDuplicateValuesFromStringSlice(strSlice []string) []string {
}
return list
}

// ConvertTimeToMillis - convert to milliseconds
func ConvertTimeToMillis(tm time.Time) int64 {
return tm.UnixNano() / 1e6
}

0 comments on commit b6d31fc

Please sign in to comment.