Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIGOV-26448 - use the config properly #690

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 11 additions & 21 deletions pkg/config/usagereportingconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ const (
qaUsageReportingUsageScheduleEnvVar = "QA_CENTRAL_USAGEREPORTING_USAGESCHEDULE"

// Config paths
pathUsageReportingPublish = "central.usagereporting.publish"
pathUsageReportingPublishMetric = "central.usagereporting.publishMetric"
pathUsageReportingUsageSchedule = "central.usagereporting.usageSchedule"
pathUsageReportingInterval = "central.usagereporting.interval"
pathUsageReportingReportInterval = "central.usagereporting.reportInterval"
pathUsageReportingOffline = "central.usagereporting.offline"
pathUsageReportingSchedule = "central.usagereporting.offlineSchedule"
pathUsageReportingPublish = "central.usagereporting.publish"
pathUsageReportingPublishMetric = "central.usagereporting.publishMetric"
pathUsageReportingUsageSchedule = "central.usagereporting.usageSchedule"
pathUsageReportingInterval = "central.usagereporting.interval"
pathUsageReportingOffline = "central.usagereporting.offline"
pathUsageReportingSchedule = "central.usagereporting.offlineSchedule"
)

// UsageReportingConfig - Interface to get usage reporting config
Expand All @@ -58,7 +57,6 @@ type UsageReportingConfiguration struct {
Publish bool `config:"publish"`
PublishMetric bool `config:"publishMetric"`
Interval time.Duration `config:"interval"`
ReportInterval time.Duration `config:"reportInterval"`
UsageSchedule string `config:"usageSchedule"`
Offline bool `config:"offline"`
Schedule string `config:"offlineSchedule"`
Expand All @@ -75,7 +73,6 @@ func NewUsageReporting(platformURL string) UsageReportingConfig {
Publish: true,
PublishMetric: true,
Interval: 15 * time.Minute,
ReportInterval: 12 * time.Hour,
UsageSchedule: "@daily",
Offline: false,
Schedule: "@hourly",
Expand Down Expand Up @@ -135,8 +132,8 @@ func (u *UsageReportingConfiguration) Validate() {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingInterval))
}

u.validatePublish() // DEPRECATE
u.validatePublishMetric() // DEPRECATE
u.validatePublish() // DEPRECATE
u.validatePublishMetric()

if !u.Offline {
u.validateUsageSchedule()
Expand All @@ -146,7 +143,7 @@ func (u *UsageReportingConfiguration) Validate() {
}

func (u *UsageReportingConfiguration) validateUsageSchedule() {
// check fi the qa env var is set
// check if the qa env var is set
if val := os.Getenv(qaUsageReportingUsageScheduleEnvVar); val != "" {
if _, err := cronexpr.Parse(val); err != nil {
log.Tracef("Could not use %s (%s) it is not a proper cron schedule", qaUsageReportingUsageScheduleEnvVar, val)
Expand All @@ -170,8 +167,8 @@ func (u *UsageReportingConfiguration) validateUsageSchedule() {
}
for i := 1; i < checks-1; i++ {
delta := nextRuns[i].Sub(nextRuns[i-1])
if delta < (time.Minute * 15) {
log.Tracef("%s must be at least 15 minutes apart", pathUsageReportingUsageSchedule)
if delta < time.Hour {
log.Tracef("%s must be at 1 hour apart", pathUsageReportingUsageSchedule)
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingUsageSchedule))
}
}
Expand Down Expand Up @@ -240,11 +237,6 @@ func (u *UsageReportingConfiguration) GetInterval() time.Duration {
return u.Interval
}

// GetReportInterval - Returns the publish report interval
func (u *UsageReportingConfiguration) GetReportInterval() time.Duration {
return u.ReportInterval
}

// IsOfflineMode - Returns the offline boolean
func (u *UsageReportingConfiguration) IsOfflineMode() bool {
return u.Offline
Expand Down Expand Up @@ -283,7 +275,6 @@ func AddUsageReportingProperties(props properties.Properties) {
props.AddBoolProperty(pathUsageReportingPublish, true, "Indicates if the agent can publish usage events to Amplify platform. Default to true")
props.AddBoolProperty(pathUsageReportingPublishMetric, true, "Indicates if the agent can publish metric events to Amplify platform. Default to true")
props.AddDurationProperty(pathUsageReportingInterval, 15*time.Minute, "The time interval at which usage and metric events will be generated", properties.WithLowerLimit(5*time.Minute))
props.AddDurationProperty(pathUsageReportingReportInterval, 12*time.Hour, "The time interval at which usage and metric events will be generated", properties.WithLowerLimit(time.Hour))
props.AddStringProperty(pathUsageReportingUsageSchedule, "@daily", "The schedule at usage events are sent to the platform")
props.AddBoolProperty(pathUsageReportingOffline, false, "Turn this on to save the usage events to disk for manual upload")
props.AddStringProperty(pathUsageReportingSchedule, "@hourly", "The schedule at which usage events are generated, for offline mode only")
Expand All @@ -299,7 +290,6 @@ func ParseUsageReportingConfig(props properties.Properties) UsageReportingConfig
cfg.Publish = props.BoolPropertyValue(pathUsageReportingPublish)
cfg.PublishMetric = props.BoolPropertyValue(pathUsageReportingPublishMetric)
cfg.Interval = props.DurationPropertyValue(pathUsageReportingInterval)
cfg.ReportInterval = props.DurationPropertyValue(pathUsageReportingReportInterval)
cfg.UsageSchedule = props.StringPropertyValue(pathUsageReportingUsageSchedule)
cfg.Offline = props.BoolPropertyValue(pathUsageReportingOffline)
cfg.Schedule = props.StringPropertyValue(pathUsageReportingSchedule)
Expand Down
12 changes: 7 additions & 5 deletions pkg/transaction/metric/metricscollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/golang-jwt/jwt"
"github.com/google/uuid"
"github.com/gorhill/cronexpr"
"github.com/rcrowley/go-metrics"

"github.com/Axway/agent-sdk/pkg/agent"
Expand Down Expand Up @@ -62,6 +63,7 @@ type collector struct {
metricEndTime time.Time
orgGUID string
nextUsageTime time.Time
nextUTCron *cronexpr.Expression
lock *sync.Mutex
batchLock *sync.Mutex
registry metrics.Registry
Expand Down Expand Up @@ -160,6 +162,7 @@ func createMetricCollector() Collector {
usageConfig: agent.GetCentralConfig().GetUsageReportingConfig(),
logger: logger,
nextUsageTime: time.Time{},
nextUTCron: cronexpr.MustParse(agent.GetCentralConfig().GetUsageReportingConfig().GetUsageSchedule()),
}

// Create and initialize the storage cache for usage/metric and offline report cache by loading from disk
Expand Down Expand Up @@ -612,10 +615,8 @@ func (c *collector) skipUsageCreate() bool {
if c.publisher.offline {
return false
}
if time.Now().After(c.nextUsageTime) {
return false
}
return true
// only skip if now is not after the previously set next usage time
return !time.Now().After(c.nextUsageTime)
}

func (c *collector) generateUsageEvent(orgGUID string) {
Expand All @@ -625,7 +626,8 @@ func (c *collector) generateUsageEvent(orgGUID string) {
if c.getOrRegisterCounter(transactionCountMetric).Count() != 0 || c.usageConfig.IsOfflineMode() {
c.generateLighthouseUsageEvent(orgGUID)
}
c.nextUsageTime = time.Now().Add(c.usageConfig.GetReportInterval())
// set the next usage time according to the cron schedule
c.nextUsageTime = c.nextUTCron.Next(time.Now())
}

func (c *collector) generateLighthouseUsageEvent(orgGUID string) {
Expand Down