diff --git a/pkg/traceability/traceability.go b/pkg/traceability/traceability.go index be70ce11b..c8e667825 100644 --- a/pkg/traceability/traceability.go +++ b/pkg/traceability/traceability.go @@ -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" @@ -207,7 +206,11 @@ 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) @@ -215,7 +218,11 @@ func (client *Client) Publish(batch publisher.Batch) error { 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 } diff --git a/pkg/transaction/metric/metricscollector.go b/pkg/transaction/metric/metricscollector.go index 1f41ab1ad..8f1dc1091 100644 --- a/pkg/transaction/metric/metricscollector.go +++ b/pkg/transaction/metric/metricscollector.go @@ -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" @@ -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 @@ -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() }() @@ -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() @@ -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)) } } @@ -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{}) { @@ -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) } diff --git a/pkg/util/util.go b/pkg/util/util.go index f7031f8d1..11f9a24e2 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -6,6 +6,7 @@ import ( "hash/fnv" "net/http" "net/url" + "time" "github.com/Axway/agent-sdk/pkg/util/log" "github.com/sirupsen/logrus" @@ -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() @@ -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 +}