-
Notifications
You must be signed in to change notification settings - Fork 13
/
usagereportingconfig.go
272 lines (234 loc) · 9.73 KB
/
usagereportingconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package config
import (
"net/url"
"os"
"strconv"
"time"
"github.com/Axway/agent-sdk/pkg/cmd/properties"
"github.com/Axway/agent-sdk/pkg/util/exception"
"github.com/Axway/agent-sdk/pkg/util/log"
"github.com/gorhill/cronexpr"
)
const (
// DEPRECATE remove old and new env vars as well as checks below
oldUsageReportingURLEnvVar = "CENTRAL_LIGHTHOUSEURL"
oldUsageReportingPublishEnvVar = "CENTRAL_PUBLISHUSAGE"
oldUsageReportingPublishMetricEnvVar = "CENTRAL_PUBLISHMETRIC"
oldUsageReportingIntervalEnvVar = "CENTRAL_EVENTAGGREGATIONINTERVAL"
newUsageReportingURLEnvVar = "CENTRAL_USAGEREPORTING_URL"
newUsageReportingPublishEnvVar = "CENTRAL_USAGEREPORTING_PUBLISH"
newUsageReportingPublishMetricEnvVar = "CENTRAL_USAGEREPORTING_PUBLISHMETRIC"
newUsageReportingIntervalEnvVar = "CENTRAL_USAGEREPORTING_INTERVAL"
// QA EnvVars
qaUsageReportingScheduleEnvVar = "QA_CENTRAL_USAGEREPORTING_OFFLINESCHEDULE"
qaUsageReportingOfflineScheduleEnvVar = "QA_CENTRAL_USAGEREPORTING_OFFLINEREPORTSCHEDULE"
// Config paths
pathUsageReportingURL = "central.usagereporting.url"
pathUsageReportingPublish = "central.usagereporting.publish"
pathUsageReportingPublishMetric = "central.usagereporting.publishMetric"
pathUsageReportingInterval = "central.usagereporting.interval"
pathUsageReportingOffline = "central.usagereporting.offline"
pathUsageReportingSchedule = "central.usagereporting.offlineSchedule"
)
// UsageReportingConfig - Interface to get usage reporting config
type UsageReportingConfig interface {
GetURL() string
CanPublishUsage() bool
CanPublishMetric() bool
GetInterval() time.Duration
IsOfflineMode() bool
GetSchedule() string
GetReportSchedule() string
GetReportGranularity() int
UsingQAVars() bool
Validate()
}
// UsageReportingConfiguration - structure to hold all usage reporting settings
type UsageReportingConfiguration struct {
UsageReportingConfig
URL string `config:"url"`
Publish bool `config:"publish"`
PublishMetric bool `config:"publishMetric"`
Interval time.Duration `config:"interval"`
Offline bool `config:"offline"`
Schedule string `config:"offlineSchedule"`
reportSchedule string
reportGranularity int
qaVars bool
}
// NewUsageReporting - Creates the default usage reporting config
func NewUsageReporting() UsageReportingConfig {
return &UsageReportingConfiguration{
URL: "https://lighthouse.admin.axway.com",
Publish: true,
PublishMetric: true,
Interval: 15 * time.Minute,
Offline: false,
Schedule: "@hourly",
reportSchedule: "@monthly",
reportGranularity: 900000,
qaVars: false,
}
}
func (u *UsageReportingConfiguration) validateURL() {
if val := os.Getenv(newUsageReportingURLEnvVar); val != "" {
return // this env var is set use what has been parsed
}
// check if the old env var had a value
if val := os.Getenv(oldUsageReportingURLEnvVar); val != "" {
log.DeprecationWarningReplace(oldUsageReportingURLEnvVar, newUsageReportingURLEnvVar)
u.URL = val
}
}
func (u *UsageReportingConfiguration) validateInterval() {
if val := os.Getenv(newUsageReportingIntervalEnvVar); val != "" {
return // this env var is set use what has been parsed
}
// check if the old env var had a value
if val := os.Getenv(oldUsageReportingIntervalEnvVar); val != "" {
if value, err := time.ParseDuration(val); err == nil {
log.DeprecationWarningReplace(oldUsageReportingIntervalEnvVar, newUsageReportingIntervalEnvVar)
u.Interval = value
}
}
}
func (u *UsageReportingConfiguration) validatePublish() {
if val := os.Getenv(newUsageReportingPublishEnvVar); val != "" {
return // this env var is set use what has been parsed
}
// check if the old env var had a value
val := os.Getenv(oldUsageReportingPublishEnvVar)
if val != "" {
if value, err := strconv.ParseBool(val); err == nil {
log.DeprecationWarningReplace(oldUsageReportingPublishEnvVar, newUsageReportingPublishEnvVar)
u.Publish = value
}
}
}
func (u *UsageReportingConfiguration) validatePublishMetric() {
if val := os.Getenv(newUsageReportingPublishMetricEnvVar); val != "" {
return // this env var is set use what has been parsed
}
// check if the old env var had a value
if val := os.Getenv(oldUsageReportingPublishMetricEnvVar); val != "" {
if value, err := strconv.ParseBool(val); err == nil {
log.DeprecationWarningReplace(oldUsageReportingPublishMetricEnvVar, newUsageReportingPublishMetricEnvVar)
u.PublishMetric = value
}
}
}
// Validate -
func (u *UsageReportingConfiguration) Validate() {
u.validateURL() // DEPRECATE
if u.URL != "" {
if _, err := url.ParseRequestURI(u.URL); err != nil {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingURL))
}
}
u.validateInterval() // DEPRECATE
eventAgg := u.Interval
if eventAgg < 60*time.Second {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingInterval))
}
u.validatePublish() // DEPRECATE
u.validatePublishMetric() // DEPRECATE
if u.Offline {
u.validateOffline()
}
}
func (u *UsageReportingConfiguration) validateOffline() {
if _, err := cronexpr.Parse(u.Schedule); err != nil {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingSchedule))
}
// reporting is offline, lets read the QA env vars
if val := os.Getenv(qaUsageReportingScheduleEnvVar); val != "" {
if _, err := cronexpr.Parse(val); err != nil {
log.Tracef("Could not use %s (%s) it is not a proper cron schedule", qaUsageReportingScheduleEnvVar, val)
} else {
log.Tracef("Using %s (%s) rather than the default (%s) for non-QA", qaUsageReportingScheduleEnvVar, val, u.Schedule)
u.Schedule = val
u.qaVars = true
}
}
if val := os.Getenv(qaUsageReportingOfflineScheduleEnvVar); val != "" {
if _, err := cronexpr.Parse(val); err != nil {
log.Tracef("Could not use %s (%s) it is not a proper cron schedule", qaUsageReportingOfflineScheduleEnvVar, val)
} else {
log.Tracef("Using %s (%s) rather than the default (%s) for non-QA", qaUsageReportingOfflineScheduleEnvVar, val, u.reportSchedule)
u.reportSchedule = val
u.qaVars = true
}
}
// Check the cron expressions
cron, err := cronexpr.Parse(u.Schedule)
if err != nil {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingSchedule))
}
nextTwoRuns := cron.NextN(time.Now(), 2)
if len(nextTwoRuns) != 2 {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingSchedule))
}
u.reportGranularity = int(nextTwoRuns[1].Sub(nextTwoRuns[0]).Milliseconds())
// if no QA env vars are set then validate the schedule is at least hourly
if nextTwoRuns[1].Sub(nextTwoRuns[0]) < time.Hour && !u.qaVars {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingSchedule))
}
}
// GetURL - Returns the usage reporting URL
func (u *UsageReportingConfiguration) GetURL() string {
return u.URL
}
// CanPublishUsage - Returns the publish boolean
func (u *UsageReportingConfiguration) CanPublishUsage() bool {
return u.Publish
}
// CanPublishMetric - Returns the publish metric boolean
func (u *UsageReportingConfiguration) CanPublishMetric() bool {
return u.PublishMetric
}
// GetInterval - Returns the publish interval
func (u *UsageReportingConfiguration) GetInterval() time.Duration {
return u.Interval
}
// IsOfflineMode - Returns the offline boolean
func (u *UsageReportingConfiguration) IsOfflineMode() bool {
return u.Offline
}
// GetSchedule - Returns the schedule string
func (u *UsageReportingConfiguration) GetSchedule() string {
return u.Schedule
}
// GetReportSchedule - Returns the offline schedule string for creating reports
func (u *UsageReportingConfiguration) GetReportSchedule() string {
return u.reportSchedule
}
// GetReportGranularity - Returns the granularity used in the offline reports
func (u *UsageReportingConfiguration) GetReportGranularity() int {
return u.reportGranularity
}
// UsingQAVars - Returns the offline boolean
func (u *UsageReportingConfiguration) UsingQAVars() bool {
return u.qaVars
}
// AddUsageReportingProperties - Adds the command properties needed for Uage Reporting Settings
func AddUsageReportingProperties(props properties.Properties) {
props.AddStringProperty(pathUsageReportingURL, "https://lighthouse.admin.axway.com", "The URL to publish usage events to in the Amplify platform. Default https://lighthouse.admin.axway.com")
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")
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")
}
// ParseUsageReportingConfig - Parses the Usage Reporting Config values from the command line
func ParseUsageReportingConfig(props properties.Properties) UsageReportingConfig {
// Start with the default config
cfg := NewUsageReporting().(*UsageReportingConfiguration)
// update the config
cfg.URL = props.StringPropertyValue(pathUsageReportingURL)
cfg.Publish = props.BoolPropertyValue(pathUsageReportingPublish)
cfg.PublishMetric = props.BoolPropertyValue(pathUsageReportingPublishMetric)
cfg.Interval = props.DurationPropertyValue(pathUsageReportingInterval)
cfg.Offline = props.BoolPropertyValue(pathUsageReportingOffline)
cfg.Schedule = props.StringPropertyValue(pathUsageReportingSchedule)
return cfg
}