-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usage) Add configurable, regular ping event (#862)
Detail: - If the user consents to share anonymous data with us, the m-apiserver besides the regular ones will also send a `ping`. The `ping` will be a time based regular event, denoting that openebs version `X` is running in a version `Y` k8s cluster with `Z` nodes. - The duration can be configured with some restrictions by setting `OPENEBS_IO_ANALYTICS_PING_INTERVAL` in m-apiserver pod. Other minor refactor: - force update of versions is made possible - enhancement(constants) cs are defined in pkg/usage/const.go - (enhancement)To better manage ENV variables a `GetOrDefault` func has been added - parsing of ping interval is done by time.ParseDuration - (bugfix)helper funcs to set replica-count to current m-apiserver default i.e. `3` improvement: openebs/openebs#2257 Signed-off-by: Harsh Vardhan <[email protected]>
- Loading branch information
Showing
9 changed files
with
198 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Copyright 2018 The OpenEBS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package usage | ||
|
||
const ( | ||
// GAclientID is the unique code of OpenEBS project in Google Analytics | ||
GAclientID string = "UA-127388617-1" | ||
|
||
// supported events categories | ||
|
||
// Install event is sent on pod starts | ||
InstallEvent string = "install" | ||
// Ping event is sent periodically | ||
Ping string = "ping" | ||
// VolumeProvision event is sent when a volume is created | ||
VolumeProvision string = "volume-provision" | ||
//VolumeDeprovision event is sent when a volume is deleted | ||
VolumeDeprovision string = "volume-deprovision" | ||
|
||
AppName string = "OpenEBS" | ||
|
||
// Event labels | ||
RunningStatus string = "running" | ||
EventLabelNode string = "nodes" | ||
EventLabelCapacity string = "capacity" | ||
|
||
// Event action | ||
Replica string = "replica:" | ||
DefaultReplicaCount string = "replica:3" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright 2018 The OpenEBS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package usage | ||
|
||
import ( | ||
"time" | ||
|
||
menv "github.com/openebs/maya/pkg/env/v1alpha1" | ||
) | ||
|
||
var OpenEBSPingPeriod menv.ENVKey = "OPENEBS_IO_ANALYTICS_PING_INTERVAL" | ||
|
||
const ( | ||
// defaultPingPeriod sets the default ping heartbeat interval | ||
defaultPingPeriod time.Duration = 24 * time.Hour | ||
// minimumPingPeriod sets the minimum possible configurable | ||
// heartbeat period, if a value lower than this will be set, the | ||
// defaultPingPeriod will be used | ||
minimumPingPeriod time.Duration = 1 * time.Hour | ||
) | ||
|
||
// PingCheck sends ping events to Google Analytics | ||
func PingCheck() { | ||
// Create a new usage field | ||
u := New() | ||
duration := getPingPeriod() | ||
ticker := time.NewTicker(duration) | ||
for _ = range ticker.C { | ||
u.Build(). | ||
InstallBuilder(true). | ||
SetCategory(Ping). | ||
Send() | ||
} | ||
} | ||
|
||
// getPingPeriod sets the duration of health events, defaults to 24 | ||
func getPingPeriod() time.Duration { | ||
value := menv.GetOrDefault(OpenEBSPingPeriod, string(defaultPingPeriod)) | ||
duration, _ := time.ParseDuration(value) | ||
// Sanitychecks for setting time duration of health events | ||
// This way, we are checking for negative and zero time duration and we | ||
// also have a minimum possible configurable time duration between health events | ||
if duration < minimumPingPeriod { | ||
// Avoid corner case when the ENV value is undesirable | ||
return time.Duration(defaultPingPeriod) | ||
} else { | ||
return time.Duration(duration) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package usage | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestGetPingPeriod(t *testing.T) { | ||
beforeFunc := func(value string) { | ||
if err := os.Setenv(string(OpenEBSPingPeriod), value); err != nil { | ||
t.Logf("Unable to set environment variable") | ||
} | ||
} | ||
afterFunc := func() { | ||
if err := os.Unsetenv(string(OpenEBSPingPeriod)); err != nil { | ||
t.Logf("Unable to unset environment variable") | ||
} | ||
} | ||
testSuite := map[string]struct { | ||
OpenEBSPingPeriodValue string | ||
ExpectedPeriodValue time.Duration | ||
}{ | ||
"24 seconds": {"24s", 86400000000000}, | ||
"24 minutes": {"24m", 86400000000000}, | ||
"24 hours": {"24h", 86400000000000}, | ||
"Negative 24 hours": {"-24h", 86400000000000}, | ||
"Random string input": {"Apache", 86400000000000}, | ||
"Two hours": {"2h", 7200000000000}, | ||
"Three hundred hours": {"300h", 1080000000000000}, | ||
"Fifty two seconds": {"52000000000ns", 86400000000000}, | ||
"Empty env value": {"", 86400000000000}, | ||
} | ||
for testKey, testData := range testSuite { | ||
beforeFunc(testData.OpenEBSPingPeriodValue) | ||
evaluatedValue := getPingPeriod() | ||
if evaluatedValue != testData.ExpectedPeriodValue { | ||
t.Fatalf("Tests failed for %s, expected=%d, got=%d", testKey, testData.ExpectedPeriodValue, evaluatedValue) | ||
} | ||
afterFunc() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters